source: installer/vagrant/Vagrantfile-esxi @ 7555a0d

configure-oglivelgromero-new-oglivemainmount-efivarfsubu24tpluse-local-agent-oglivevarios-instalacion
Last change on this file since 7555a0d was 748691b, checked in by Natalia Serrano <natalia.serrano@…>, 10 months ago

refs #491 remove debug statements

  • Property mode set to 100644
File size: 11.0 KB
Line 
1VAGRANTFILE_API_VERSION = "2"
2ENV['VAGRANT_DEFAULT_PROVIDER'] = "vmware_esxi"
3
4BRANCH=ENV['BRANCH'] || "main"
5BRANCH_EXTRA=ENV['BRANCH_EXTRA'] || BRANCH
6
7OGAGENTVERSION="1.1.2"
8
9LANGUAGE = "es_ES"
10ENV['LC_ALL'] = LANGUAGE + ".UTF-8"
11
12NCLIENTS = ENV['NUMBER_OF_CLIENTS']|| 4
13NCLIENTS = NCLIENTS.to_i
14
15SERVERMEM = 2048        # Minimum: 512
16CLIENTMEM = 512         # Minimum: 256
17
18MACPREFIX = ENV['OGN_MAC_PREFIX'] || '00:50:56:aa:aa'
19NETPREFIX = ENV['OGN_NET_PREFIX'] || "192.168.2"
20DHCPNET   = ENV['DHCPNET'] || "192.168.2.0"
21
22SERVERIP = "#{NETPREFIX}.10"
23
24LOCALWEBPORT = 8443
25
26ESXIPASSWORD = ENV['ESXI_PASSWORD'] || 'prompt:'
27
28GITEA_TOKEN = ENV['GITEA_TOKEN']
29GITEA_USER = ENV['GITEA_USER'] || 'unizar'
30SSH_GIT_KEY_PASS = ENV['SSH_GIT_KEY_PASS'] || ''
31
32
33OGSERVERSCRIPT = <<EOT
34echo -n 'hostname: '; hostname -f
35echo env vars beginning with OG:
36env |grep OG
37ip address show eth0
38
39# Fix problem with gitea host
40grep -q '^150\.214\.58\.246' /etc/hosts || echo "150.214.58.246 ognproject.evlt.uma.es" >> /etc/hosts
41
42echo Setting up SSH config and agent
43mkdir -p /root/.ssh/
44cp /vagrant/config/id_rsa /root/.ssh/id_rsa
45chown root:root /root/.ssh/id_rsa
46chmod 0400 /root/.ssh/id_rsa
47cp /vagrant/config/ssh_config /root/.ssh/config
48chown root:root /root/.ssh/config
49chmod 0400 /root/.ssh/config
50cp /vagrant/config/ask_pass.sh /root/ask_pass.sh
51chmod 755 /root/ask_pass.sh
52
53eval `ssh-agent`
54export DISPLAY=:0
55export SSH_ASKPASS=/root/ask_pass.sh
56ssh-add /root/.ssh/id_rsa <<< $SSH_GIT_KEY_PASS
57
58
59echo Setting up language and locales
60export LANG="#{LANGUAGE}.UTF-8"
61echo "LANG=\\\"$LANG\\\"" > /etc/default/locale
62echo "LANG=\\\"$LANG\\\"" >> /etc/environment
63locale-gen --lang #{LANGUAGE}
64sed -i "s/XKBLAYOUT=.*/XKBLAYOUT=\\\"${LANG%_*}\\\"/" /etc/default/keyboard
65dpkg-reconfigure -fnoninteractive console-setup
66
67
68# Exit if OpenGnsys is installed.
69[ -f /opt/opengnsys/doc/VERSION.json ] && echo "Cannot provision, OpenGnsys is already installed." && exit 1
70
71
72echo Creating a repository disk
73if [ -z "$(blkid /dev/mapper/og-images | grep ext4)" ]; then
74    pvcreate /dev/sdb
75    vgcreate og /dev/sdb
76    vgchange -ay
77    lvcreate -ay -n images -l 100%VG og
78    mkfs -t ext4 /dev/mapper/og-images
79    mkdir -p /opt/opengnsys/images
80    echo "/dev/mapper/og-images  /opt/opengnsys/images  ext4  defaults  0  0" >> /etc/fstab
81    mount -a
82fi
83
84
85if [ -f /etc/netplan/01-eth1.yaml ]; then
86    echo "Network configured, skipping"
87else
88    echo "Configuring eth1"
89    sed -i "s/##ADDRESS##/${NETPREFIX}.1/g" /tmp/01-eth1.yaml
90    mv /tmp/01-eth1.yaml /etc/netplan
91    chown root:root /etc/netplan/01-eth1.yaml
92    netplan apply
93    while ! ip address show eth0 |grep -qw inet; do
94        echo eth0 has no IPv4, waiting...
95        sleep 1
96    done
97fi
98
99
100# Download installer
101echo Downloading https://ognproject.evlt.uma.es/gitea/opengnsys/opengnsys/raw/branch/${BRANCH}/installer/opengnsys_installer_devel_esxi.sh
102curl --retry 5 --stderr - --insecure --location --output opengnsys_installer_devel_esxi.sh https://$GITEA_USER:$GITEA_TOKEN@ognproject.evlt.uma.es/gitea/opengnsys/opengnsys/raw/branch/${BRANCH}/installer/opengnsys_installer_devel_esxi.sh
103chmod 755 opengnsys_installer_devel_esxi.sh && ./opengnsys_installer_devel_esxi.sh $BRANCH
104
105[ -e /opt/opengnsys/log/bash.log ] && mv /opt/opengnsys/log/bash.log /opt/opengnsys/log/opengnsys_installer.log
106
107echo y | /opt/opengnsys/bin/setserveraddr $(ip -o link show | tail -1 | cut -d: -f2)
108
109# Insert DHCP data.
110sed -i "/^}$/ i host modelo { hardware ethernet #{MACPREFIX}:FF; fixed-address #{NETPREFIX}.199; }" /etc/dhcp/dhcpd.conf
111for ((i=#{NCLIENTS+10}; i>10; i--)); do
112    sed -i "/^}$/ i host pc${i} { hardware ethernet #{MACPREFIX}:${i}; fixed-address #{NETPREFIX}.${i}; }" /etc/dhcp/dhcpd.conf
113done
114service isc-dhcp-server restart
115
116echo Creating an aula and some clients
117SQL="INSERT INTO aulas (nombreaula, idcentro, urlfoto, grupoid, ubicacion, puestos, modomul, ipmul, pormul, velmul, router, netmask, ntp, dns, proxy, modp2p, timep2p)"\
118"VALUES  ('Aula virtual', 1, 'aula.jpg', 0, 'Despliegue virtual con Vagrant.', 5, 2, '239.194.2.11', 9000, 70, '#{NETPREFIX}.1', '255.255.255.0', '', '', '', 'peer', 30); "\
119"INSERT INTO ordenadores (nombreordenador, ip, mac, idaula, idrepositorio, idperfilhard, idmenu, idproautoexec, grupoid, router, mascara, arranque, netiface, netdriver, fotoord) VALUES "\
120"('modelo', '#{NETPREFIX}.199', REPLACE('#{MACPREFIX}FF',':',''), 1, 1, 0, 0, 0, 0, '#{NETPREFIX}.1', '255.255.255.0', '00unknown', 'eth0', 'generic', 'fotoordenador.gif'),"
121for ((i=11; i<=#{NCLIENTS+10}; i++)); do
122    SQL="$SQL ('pc$i', '#{NETPREFIX}.$i', REPLACE('#{MACPREFIX}$i',':',''), 1, 1, 0, 0, 0, 0, '#{NETPREFIX}.1', '255.255.255.0', '00unknown', 'eth0', 'generic', 'fotoordenador.gif'),"
123done
124mysql -u usuog -ppassusuog -D ogAdmBD -e "${SQL%,}"
125/opt/opengnsys/bin/setclientmode ogLiveAdmin modelo PERM
126/opt/opengnsys/bin/setclientmode ogLiveAdmin pc11 PERM
127for ((i=12; i<=#{NCLIENTS+10}; i++)); do
128    /opt/opengnsys/bin/setclientmode ogLive pc$i PERM
129done
130
131echo "Notes:"
132echo "- OpenGnsys Server URL: https://localhost:${LOCALWEBPORT}/opengnsys/"
133exit 0
134EOT
135
136
137OGDHCPSCRIPT = <<EOT
138echo in ogdhcp script
139echo -n 'hostname: '; hostname -f
140echo env vars beginning with OG:
141env |grep OG
142ip address show eth0
143echo "OGDHCP_BRANCH ($OGDHCP_BRANCH)"
144EOT
145
146
147OGBOOTSCRIPT = <<EOT
148echo in ogboot script
149echo -n 'hostname: '; hostname -f
150echo env vars beginning with OG:
151env |grep OG
152ip address show eth0
153echo "OGBOOT_BRANCH ($OGBOOT_BRANCH)"
154echo "OGSERVER_IP ($OGSERVER_IP)"
155#curl --retry 5 --stderr - --insecure --location --output installer.py https://$GITEA_USER:$GITEA_TOKEN@ognproject.evlt.uma.es/gitea/opengnsys/ogboot/raw/branch/ ${OGBOOT_BRANCH}/installer.py
156#python3 /install.py
157EOT
158
159
160OGREPOSCRIPT = <<EOT
161echo in ogrepo script
162echo -n 'hostname: '; hostname -f
163echo env vars beginning with OG:
164env |grep OG
165ip address show eth0
166echo "OGREPO_BRANCH ($OGREPO_BRANCH)"
167EOT
168
169
170OGLOGSCRIPT = <<EOT
171echo in oglog script
172echo -n 'hostname: '; hostname -f
173echo env vars beginning with OG:
174env |grep OG
175ip address show eth0
176echo "OGLOG_BRANCH ($OGLOG_BRANCH)"
177EOT
178
179
180Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
181    require 'pp'
182
183    def do_debug(msg)
184        if ENV.has_key?("DEBUG_VAGRANT") and ENV["DEBUG_VAGRANT"] == "true" then
185            if msg.class == Array then
186                #PP.pp(msg, STDERR)        ## doesn't actually pretty print to the jenkins log
187                STDERR.puts msg.pretty_inspect
188            else
189                STDERR.puts msg
190            end
191        end
192    end
193
194    provisioners_env = {
195        "NETPREFIX"                 => NETPREFIX,
196        "BRANCH"                    => BRANCH,
197        "OGDHCP_BRANCH"             => ENV['OGDHCP_BRANCH'],
198        "OGBOOT_BRANCH"             => ENV['OGBOOT_BRANCH'],
199        "OGREPO_BRANCH"             => ENV['OGREPO_BRANCH'],
200        "OGLOG_BRANCH"              => ENV['OGLOG_BRANCH'],
201        "GITEA_TOKEN"               => GITEA_TOKEN,
202        "GITEA_USER"                => GITEA_USER,
203        "DHCPNET"                   => DHCPNET,
204        "SSH_GIT_KEY_PASS"          => SSH_GIT_KEY_PASS,
205        "LOCALWEBPORT"              => LOCALWEBPORT,
206        "DEFAULT_UDS_REST_URL"      => ENV['DEFAULT_UDS_REST_URL'],
207        "DEFAULT_UDS_AUTHENTICATOR" => ENV['DEFAULT_UDS_AUTHENTICATOR'],
208        "DEFAULT_UDS_USER"          => ENV['DEFAULT_UDS_USER'],
209        "DEFAULT_UDS_PASS"          => ENV['DEFAULT_UDS_PASS'],
210        "OGLOG_IP"                  => ENV['OGLOG_IP'],
211        "OGSERVER_IP"               => ENV['OGSERVER_IP'],
212    }
213    app_servers = {
214        'vm1' => {
215            :name               => "og-#{BRANCH_EXTRA}-server",
216            :memsize            => SERVERMEM,
217            :clone_from         => 'Ubuntu-18-template-image',
218            :networks           => [ 'vLan_742', BRANCH_EXTRA ],
219            :macs               => [ "#{MACPREFIX}:00", "#{MACPREFIX}:01" ],
220            :file_provisioner   => {
221                :src => "config/01-eth1.yaml",
222                :dst => "/tmp/",
223            },
224            :shell_provisioners => [ OGSERVERSCRIPT ],
225        },
226    }
227
228    vm1_components = vm2_components = []
229
230    if ENV.has_key?("VM1_COMPONENTS") then
231        vm1_components = ENV["VM1_COMPONENTS"].split(" ")
232    end
233
234    if ENV.has_key?("VM2_COMPONENTS") then
235        vm2_components = ENV["VM2_COMPONENTS"].split(" ")
236        if vm2_components.size > 0 then
237            app_servers['vm2'] = {
238                :name               => "og-#{BRANCH_EXTRA}",
239                :memsize            => SERVERMEM,
240                :clone_from         => 'Ubuntu-18-template-image',
241                :networks           => [ 'vLan_742' ],
242                :macs               => [ "#{MACPREFIX}:02" ],
243                :shell_provisioners => [],
244            }
245        end
246    end
247    do_debug ("VM1_COMPONENTS: " + vm1_components.to_s)
248    do_debug ("VM2_COMPONENTS: " + vm2_components.to_s)
249
250    vms_loop = [
251        { "components" => vm1_components, "key" => "vm1" },
252        { "components" => vm2_components, "key" => "vm2" },
253    ]
254    all_components = [
255        { "name" => "oglog",  "hostname_suffix" => "-log",  "script" => OGLOGSCRIPT  },
256        { "name" => "ogdhcp", "hostname_suffix" => "-dhcp", "script" => OGDHCPSCRIPT },
257        { "name" => "ogboot", "hostname_suffix" => "-boot", "script" => OGBOOTSCRIPT },
258        { "name" => "ogrepo", "hostname_suffix" => "-repo", "script" => OGREPOSCRIPT },
259    ]
260
261    ## foreach VM, foreach OG component: edit hostname and add provisioning script
262    vms_loop.each do |vm|
263        components = vm['components']
264        key = vm['key']
265        all_components.each do |c|
266            if components.include?(c['name']) then
267                app_servers[key][:name] += c['hostname_suffix']
268                app_servers[key][:shell_provisioners].push (c['script'])
269            end
270        end
271    end
272
273    do_debug (app_servers)
274
275    app_servers.each do |machine,data|
276        boxname = data[:name]
277        config.vm.define boxname do |og|
278            og.vm.provider :vmware_esxi do |esxi|
279                esxi.guest_name = boxname
280                esxi.guest_memsize = data[:memsize]
281                esxi.esxi_username = 'root'
282                esxi.esxi_password = ESXIPASSWORD
283                esxi.esxi_hostname = 'esxi-jenkins.evlt.uma.es'
284                esxi.clone_from_vm = data[:clone_from]
285                esxi.esxi_resource_pool = "/"
286                esxi.local_allow_overwrite = 'True'
287                esxi.guest_custom_vmx_settings = [['monitor.allowLegacyCPU', 'TRUE']]
288                esxi.guest_storage = [ 10 ]
289                esxi.esxi_virtual_network = data[:networks]
290                esxi.guest_mac_address = data[:macs]
291                esxi.guest_nic_type = 'e1000'
292            end
293            og.vm.box = "esxi_clone/dummy"
294            og.vm.hostname = boxname
295            if data.key?(:file_provisioner)
296                og.vm.provision "file", source: data[:file_provisioner][:src], destination: data[:file_provisioner][:dst]
297            end
298            data[:shell_provisioners].each do |script|
299                og.vm.provision "shell", inline: script, env: provisioners_env
300            end
301        end
302    end
303end
Note: See TracBrowser for help on using the repository browser.