Skip to content

Commit 310a911

Browse files
author
Eduardo N. S. R
committed
Adicionado VagrantFile do projeto
1 parent 1b9e71d commit 310a911

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

src/Vagrantfile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Para executar no LibVirt sem precisar de senha de root:
5+
# - sudo usermod -a -G libvirt $(whoami)
6+
# - sudo usermod -a -G kvm $(whoami)
7+
# - sudo usermod -a -G libvirt-qemu $(whoami)
8+
9+
ENV["VAGRANT_DEFAULT_PROVIDER"] = "libvirt"
10+
11+
# Definição dos nodes com seus IPs e recursos
12+
nodes = {
13+
"bal01" => { "ip" => "172.28.0.11", "memory" => 512, "cpus" => 1 },
14+
"app01" => { "ip" => "172.28.0.12", "memory" => 512, "cpus" => 2 },
15+
"app02" => { "ip" => "172.28.0.12", "memory" => 512, "cpus" => 2 },
16+
"sip01" => { "ip" => "172.28.0.12", "memory" => 512, "cpus" => 1 },
17+
"mysql01" => { "ip" => "172.28.0.13", "memory" => 512, "cpus" => 2 },
18+
"repo01" => { "ip" => "172.28.0.14", "memory" => 512, "cpus" => 1 },
19+
20+
"solr01" => { "ip" => "172.28.0.14", "memory" => 512, "cpus" => 2 },
21+
"jod01" => { "ip" => "172.28.0.14", "memory" => 512, "cpus" => 1 },
22+
"memcached01" => { "ip" => "172.28.0.15", "memory" => 512, "cpus" => 1 }
23+
}
24+
25+
Vagrant.configure("2") do |config|
26+
# Gera a chave SSH se não existir
27+
unless File.exist?('id_ed25519') && File.exist?('id_ed25519.pub')
28+
system('ssh-keygen -t ed25519 -f id_ed25519 -N "" >/dev/null 2>&1')
29+
puts "Nova chave SSH gerada."
30+
end
31+
32+
# Remove as chaves após destruir todas as VMs
33+
config.trigger.after :destroy do |trigger|
34+
trigger.ruby do |env, machine|
35+
if File.exist?('id_ed25519')
36+
File.delete('id_ed25519')
37+
File.delete('id_ed25519.pub')
38+
puts "Chave SSH removida."
39+
end
40+
end
41+
end
42+
43+
# Imagem a ser utilizada
44+
config.vm.box = "almalinux/9"
45+
config.vm.box_version = "9.5.20241203"
46+
config.vm.post_up_message = ""
47+
config.ssh.insert_key = false
48+
config.vm.synced_folder "./", "/vagrant", type: "virtiofs"
49+
50+
# Configuração comum para todas as VMs (LibVirt)
51+
config.vm.provider :libvirt do |libvirt|
52+
libvirt.driver = "kvm"
53+
libvirt.memorybacking :source, :type => "memfd"
54+
libvirt.memorybacking :access, :mode => "shared"
55+
56+
libvirt.cpu_mode = "host-model"
57+
libvirt.nested = true
58+
59+
libvirt.nic_model_type = "virtio"
60+
end
61+
62+
nodes.each do |node_name, specs|
63+
config.vm.define node_name, autostart: ENV['MON'] ? true : specs.fetch("as", true) do |node|
64+
node.vm.hostname = node_name
65+
node.vm.network "private_network", ip: specs["ip"]
66+
67+
# Sobrescreve as configurações de memória e CPU para cada VM
68+
node.vm.provider :libvirt do |libvirt|
69+
libvirt.memory = specs["memory"]
70+
libvirt.cpus = specs["cpus"]
71+
end
72+
73+
# Adiciona a chave pública se ela não existir
74+
node.vm.provision "shell" do |s|
75+
s.inline = <<-SHELL
76+
PUBKEY=$(cat /vagrant/id_ed25519.pub)
77+
if ! grep -q "$PUBKEY" /home/vagrant/.ssh/authorized_keys; then
78+
echo "$PUBKEY" >> /home/vagrant/.ssh/authorized_keys
79+
echo "Chave SSH adicionada."
80+
else
81+
echo "A chave SSH já existe no arquivo authorized_keys."
82+
fi
83+
SHELL
84+
end
85+
end
86+
end
87+
end

0 commit comments

Comments
 (0)