Commit 81616cde authored by Alain Takoudjou's avatar Alain Takoudjou Committed by Rafael Monnerat

packer: add vm-bootstrap image build configuration

parent fa8b44d3
vm-bootstrap Image
==================
packer vm-boostrap image contain a vm-bootstrap script (see: scripts/vm-bootstrap.sh) which will run in specific bootstrap script when VM boot for the first time.
How it works
------------
Boostrap script is downloaded from a static URL http://10.0.2.100/vm-bootstrap. The script will be executed until it succeed (exit with 0) or until the maximum execution count is reached. By default, the maximum exucution number is 10. To change this value you can write the new value in /root/bootstrap/bootstrap-max-retry.
#!/bin/bash
echo 1 > /root/bootstrap/bootstrap-max-retry
echo "Starting my custom bootstrap commands..."
....
To disable bootstrap script execution, remove /root/bootstrap/start-bootstrap from the vm:
#!/bin/bash
# temporarily disable bootstrap until XX
rm -f /root/bootstrap/start-bootstrap
...
If no bootstrap script is provided to the vm, an empty script will be generated and will be executed until bootstrap-max-retry=10.
Bootstrap logs are written into file /var/log/vm-bootstrap.log
How boostrap script is downloaded
----------------------------------
Bootstrap script called 'vm-bootstrap' should be placed in and external http server and qemu will be launched with 'guestfwd' option:
qemu -net 'user,guestfwd=tcp:10.0.2.100:80-cmd:netcat server_ip server_port'
for more information, see qemu 'user network' documentation.
......@@ -74,6 +74,34 @@
args:
creates: output-debian8-webrunner-standalone
- file: path=log state=directory mode=0755
- shell: PATH=$PATH:/opt/packer/ packer build debian7-vm-bootstrap.json >> log/debian7-vm-bootstrap.log
args:
creates: output-debian7-vm-bootstrap
- shell: PATH=$PATH:/opt/packer/ packer build debian8-vm-boostrap.json >> log/debian8-vm-boostrap.log
args:
creates: output-debian8-vm-boostrap
- shell: PATH=$PATH:/opt/packer/ packer build ubuntu-14.04-server-amd64-vm-bootstrap.json >> log/ubuntu-14.04-server-amd64-vm-bootstrap.log
args:
creates: output-ubuntu-14.04-server-amd64-vm-bootstrap
- shell: PATH=$PATH:/opt/packer/ packer build ubuntu-15.04-server-amd64-vm-bootstrap.json >> log/ubuntu-15.04-server-amd64-vm-bootstrap.log
args:
creates: output-ubuntu-15.04-server-amd64-vm-bootstrap
- shell: PATH=$PATH:/opt/packer/ packer build centos-6.7-vm-boostrap.json >> log/centos-6.7-vm-boostrap.log
args:
creates: output-centos67-vm-boostrap
- shell: PATH=$PATH:/opt/packer/ packer build centos-7.2-vm-bootstrap.json >> log/centos-7.2-vm-bootstrap.log
args:
creates: output-centos72-vm-bootstrap
- file: path={{ item }} state=directory mode=0755
with_items:
- repository
......@@ -168,6 +196,38 @@
creates: output-debian8-webrunner-standalone/packer-debian8-webrunner-standalone.gz
ignore_errors: True
- shell: gzip output-debian8-vm-boostrap/packer-debian8-slapos
args:
creates: output-debian8-vm-boostrap/packer-debian8-slapos.gz
ignore_errors: True
- shell: gzip output-debian7-vm-bootstrap/packer-debian7-slapos
args:
creates: output-debian7-vm-bootstrap/packer-debian7-slapos.gz
ignore_errors: True
- shell: gzip output-ubuntu-14.04-server-amd64-vm-bootstrap/packer-ubuntu-14-04-server-slapos
args:
creates: output-ubuntu-14.04-server-amd64-vm-bootstrap/packer-ubuntu-14-04-server-slapos.gz
ignore_errors: True
- shell: gzip output-ubuntu-15.04-server-amd64-vm-bootstrap/packer-ubuntu-15-04-server-slapos
args:
creates: output-ubuntu-15.04-server-amd64-vm-bootstrap/packer-ubuntu-15-04-server-slapos.gz
ignore_errors: True
- shell: gzip output-centos67/packer-centos68-slapos
args:
creates: output-centos67/packer-centos68-slapos.gz
ignore_errors: True
- shell: gzip output-centos72-vm-bootstrap/packer-centos72-slapos
args:
creates: output-centos72-vm-bootstrap/packer-centos72-slapos.gz
ignore_errors: True
- shell: mv output-debian8/packer-debian8.gz repository/debian/8/amd64/base-image.qcow2.gz
args:
creates: repository/debian/8/amd64/base-image.qcow2.gz
......@@ -229,6 +289,32 @@
args:
creates: repository/debian/8/amd64/wendelin-image.qcow2.gz
- shell: mv output-debian8-vm-boostrap/packer-debian8-slapos.gz repository/debian/8/amd64/base-slapos-image.qcow2.gz
args:
creates: repository/debian/8/amd64/base-slapos-image.qcow2.gz
- shell: mv output-debian7-vm-bootstrap/packer-debian7-slapos.gz repository/debian/7/amd64/base-slapos-image.qcow2.gz
args:
creates: repository/debian/7/amd64/base-slapos-image.qcow2.gz
- shell: mv output-ubuntu-14.04-server-amd64-vm-bootstrap/packer-ubuntu-14-04-server-slapos.gz repository/ubuntu/14.04/amd64/base-slapos-image.qcow2.gz
args:
creates: repository/ubuntu/14.04/amd64/base-slapos-image.qcow2.gz
- shell: mv output-ubuntu-15.04-server-amd64-vm-bootstrap/packer-ubuntu-15-04-server-slapos.gz repository/ubuntu/15.04/amd64/base-slapos-image.qcow2.gz
args:
creates: repository/ubuntu/15.04/amd64/base-slapos-image.qcow2.gz
- shell: mv output-centos67/packer-centos68-slapos.gz repository/centos/6/x86_64/base-slapos-image.qcow2.gz
args:
creates: repository/centos/6/x86_64/base-slapos-image.qcow2.gz
- shell: mv output-centos72-vm-bootstrap/packer-centos72-slapos.gz repository/centos/7/x86_64/base-slapos-image.qcow2.gz
args:
creates: repository/centos/7/x86_64/base-slapos-image.qcow2.gz
- shell: md5sum * > MD5SUM.txt
args:
chdir: repository/debian/8/amd64/
......
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "200000",
"domain": ""
},
"builders":
[
{
"name": "centos68-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://mirrors.kernel.org/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso",
"iso_checksum": "0ca12fe5f28c2ceed4f4084b41ff8a0b",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "sudo poweroff",
"headless": false,
"boot_wait": "2s",
"boot_command": [
"<esc>",
"<wait>",
"linux ks=http://{{.HTTPIP}}:{{.HTTPPort}}/centos67-anaconda-ks.cfg",
"<enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update-centos.sh",
"scripts/packages-centos.sh",
"scripts/cleanup-centos.sh",
"scripts/centos6-install-base.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "200000",
"domain": ""
},
"builders":
[
{
"name": "centos72-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://mirrors.kernel.org/centos/7.2.1511/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso",
"iso_checksum": "9ed9ffb5d89ab8cca834afce354daa70a21dcb410f58287d6316259ff89758f5",
"iso_checksum_type": "sha256",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "sudo poweroff",
"headless": false,
"boot_wait": "2s",
"boot_command": [
"<esc>",
"<wait>",
"linux ks=http://{{.HTTPIP}}:{{.HTTPPort}}/centos72-anaconda-ks.cfg biosdevname=0 net.ifnames=0",
"<enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update-centos.sh",
"scripts/packages-centos.sh",
"scripts/cleanup-centos.sh",
"scripts/centos7-install-base.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "200000",
"domain": ""
},
"builders":
[
{
"name": "debian7-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://cdimage.debian.org/mirror/cdimage/archive/7.11.0/amd64/iso-cd/debian-7.11.0-amd64-netinst.iso",
"iso_checksum": "096c1c18b44c269808bd815d58c53c8f",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "echo '{{user `password`}}'|sudo -S shutdown -h now",
"headless": false,
"boot_wait": "2s",
"boot_command": [
"<esc><wait><wait>",
"install auto ",
"preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed-debian7.cfg ",
"debian-installer=en_US locale=en_US keymap=fr ",
"netcfg/get_hostname={{ .Name }} ",
"netcfg/get_domain={{ user `domain`}} ",
"fb=false debconf/frontend=noninteractive ",
"passwd/user-fullname={{user `user`}} ",
"passwd/user-password={{user `password`}} ",
"passwd/user-password-again={{user `password`}} ",
"passwd/username={{user `user`}} ",
"<enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update.sh",
"scripts/packages.sh",
"scripts/cleanup.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "200000",
"domain": ""
},
"builders":
[
{
"name": "debian8-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://cdimage.debian.org/debian-cd/8.5.0/amd64/iso-cd/debian-8.5.0-amd64-netinst.iso",
"iso_checksum": "3e1b9029a0cf188730646c379d15073f",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "echo '{{user `password`}}'|sudo -S shutdown -h now",
"headless": false,
"boot_wait": "2s",
"boot_command": [
"<esc><wait><wait>",
"install auto ",
"preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed-debian8.cfg ",
"debian-installer=en_US locale=en_US keymap=fr ",
"netcfg/get_hostname={{ .Name }} ",
"netcfg/get_domain={{ user `domain`}} ",
"fb=false debconf/frontend=noninteractive ",
"passwd/user-fullname={{user `user`}} ",
"passwd/user-password={{user `password`}} ",
"passwd/user-password-again={{user `password`}} ",
"passwd/username={{user `user`}} ",
"<enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update.sh",
"scripts/packages.sh",
"scripts/cleanup.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
......@@ -3,7 +3,7 @@ text
reboot
cdrom
lang en_US.UTF-8
keyboard us
keyboard fr
timezone --utc Etc/UTC
rootpw --plaintext slapos
user --name=slapos --groups=slapos --password=slapos --plaintext
......
......@@ -3,7 +3,7 @@ text
reboot
url --mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
lang en_US.UTF-8
keyboard us
keyboard fr
timezone --utc Etc/UTC
rootpw --plaintext slapos
user --name=slapos --groups=slapos --password=slapos --plaintext
......
d-i debian-installer/locale string en_US
d-i keyboard-configuration/xkb-keymap select fr
d-i clock-setup/ntp boolean true
d-i clock-setup/utc boolean true
d-i finish-install/reboot_in_progress note
......
d-i debian-installer/locale string en_US
d-i keyboard-configuration/xkb-keymap select fr
d-i clock-setup/ntp boolean true
d-i clock-setup/utc boolean true
d-i finish-install/reboot_in_progress note
......
d-i keyboard-configuration/xkb-keymap select fr
choose-mirror-bin mirror/http/proxy string
d-i debian-installer/framebuffer boolean false
d-i debconf/frontend select noninteractive
......
yum install -y wget vim
cat << EOF > /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifcfg-rh,keyfile
[keyfile]
unmanaged-devices=interface-name:eth1
EOF
yum provides ifconfig
yum install -y net-tools wget vim
systemctl stop firewalld
systemctl disable firewalld
cat << EOF > /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifcfg-rh,keyfile
[keyfile]
unmanaged-devices=interface-name:eth1
EOF
......@@ -32,3 +32,6 @@ rm -f /root/.bash_history
# Clean up log files
find /var/log -type f | while read f; do echo -ne '' > $f; done;
# Make sure keyboard language is french
sed -i 's#XKBLAYOUT=.*#XKBLAYOUT="fr"#' /etc/default/keyboard
PACKAGES="
wget
curl
vim
sudo
"
aptitude -y install --without-recommends $PACKAGES
mkdir -p /root/bootstrap
echo 0 > /root/bootstrap/retry-amount
echo 10 > /root/bootstrap/bootstrap-max-retry
touch /root/bootstrap/start-bootstrap
# Put cron task to bootstrap vm
cat << EOF > /etc/cron.d/vm-boostrap
# Bootstrap vm every minutes until it succeed
* * * * * root bash -lc /root/bootstrap/vm-bootstrap >> /var/log/vm-bootstrap.log 2>&1
EOF
# VM bootstrap script
cat << EOF > /root/bootstrap/vm-bootstrap
#!/bin/bash
cd /root/bootstrap
if [[ ! -f "start-bootstrap" ]]
then
exit 1
fi
MAXRETRY=\`cat bootstrap-max-retry\`
RETRY=\`cat retry-amount\`
lf=/tmp/pidLockFile
cat /dev/null >> \$lf
read lastPID < \$lf
# if lastPID is not null and a process with that pid exists , exit
[ ! -z "\$lastPID" -a -d /proc/\$lastPID ] && exit
# save my pid in the lock file
echo \$\$ > \$lf
sleep 3
wget -O bootstrap-script -q http://10.0.2.100/vm-bootstrap
if [[ ! -s "bootstrap-script" ]]
then
echo "exit 1" > bootstrap-script
fi
export TERM=xterm
bash bootstrap-script
RESULT=\$?
test_result=\$((\$MAXRETRY - \$RETRY - 1))
if [[ \$test_result -le 0 ]]
then
rm -f /etc/cron.d/vm-boostrap
echo "Maximun bootstrap retry amount reached..."
fi
echo \$((\$RETRY + 1)) > retry-amount
if [[ \$RESULT != 0 ]]
then
echo "ERROR: bootstrap script exited with return code \$RESULT."
exit \$RESULT
fi
rm -f /etc/cron.d/vm-boostrap
exit 0
EOF
chmod +x /root/bootstrap/vm-bootstrap
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "100000"
},
"builders":
[
{
"name": "ubuntu-14-04-server-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://releases.ubuntu.com/14.04/ubuntu-14.04.4-server-amd64.iso",
"iso_checksum": "2ac1f3e0de626e54d05065d6f549fa3a",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "echo '{{user `password`}}'|sudo -S shutdown -P now",
"boot_wait": "2s",
"boot_command": [
"<esc><esc><enter><wait>",
"/install/vmlinuz url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed-ubuntu.cfg ",
"debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
"hostname={{ .Name }} ",
"keyboard-configuration/modelcode=SKIP ",
"keyboard-configuration/layout=USA ",
"keyboard-configuration/variant=USA ",
"passwd/user-fullname={{user `user`}} ",
"passwd/user-password-again={{user `password`}} ",
"passwd/user-password={{user `password`}} ",
"passwd/username={{user `user`}} ",
"initrd=/install/initrd.gz -- <enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update-ubuntu.sh",
"scripts/packages-ubuntu.sh",
"scripts/network-ubuntu.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
{
"variables": {
"user": "slapos",
"password": "slapos",
"disk_size": "100000"
},
"builders":
[
{
"name": "ubuntu-15-04-server-slapos",
"type": "qemu",
"format": "qcow2",
"accelerator": "kvm",
"disk_size": "{{ user `disk_size`}}",
"iso_url": "http://releases.ubuntu.com/15.04/ubuntu-15.04-server-amd64.iso",
"iso_checksum": "487f4a81f22f8597503db3d51a1b502e",
"iso_checksum_type": "md5",
"http_directory": "http",
"ssh_username": "{{user `user`}}",
"ssh_password": "{{user `password`}}",
"ssh_wait_timeout": "1800s",
"shutdown_command": "echo '{{user `password`}}'|sudo -S shutdown -P now",
"boot_wait": "2s",
"boot_command": [
"<esc><esc><enter><wait>",
"/install/vmlinuz url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed-ubuntu.cfg ",
"debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
"hostname={{ .Name }} ",
"keyboard-configuration/modelcode=SKIP ",
"keyboard-configuration/layout=USA ",
"keyboard-configuration/variant=USA ",
"passwd/user-fullname={{user `user`}} ",
"passwd/user-password-again={{user `password`}} ",
"passwd/user-password={{user `password`}} ",
"passwd/username={{user `user`}} ",
"initrd=/install/initrd.gz -- <enter>"
]
}
],
"provisioners": [
{
"type": "shell",
"execute_command": "echo '{{user `password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/update-ubuntu.sh",
"scripts/packages-ubuntu.sh",
"scripts/network-ubuntu.sh",
"scripts/vm-bootstrap.sh"
]
}
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment