From 0a673e300800b91342499cec9cd482b5d2d9c603 Mon Sep 17 00:00:00 2001 From: binary Date: Tue, 10 Nov 2020 20:46:04 +0100 Subject: Fully working init for alpine vm --- roles/vmm/files/init_vm_alpine.yml | 115 +++++++++++++++++++++++++++++++++++++ roles/vmm/files/init_vm_serial.py | 88 ---------------------------- roles/vmm/tasks/init_vm.yml | 38 ------------ roles/vmm/tasks/init_vm_alpine.yml | 50 ++++++++++++++++ 4 files changed, 165 insertions(+), 126 deletions(-) create mode 100644 roles/vmm/files/init_vm_alpine.yml delete mode 100644 roles/vmm/files/init_vm_serial.py delete mode 100644 roles/vmm/tasks/init_vm.yml create mode 100644 roles/vmm/tasks/init_vm_alpine.yml (limited to 'roles/vmm') diff --git a/roles/vmm/files/init_vm_alpine.yml b/roles/vmm/files/init_vm_alpine.yml new file mode 100644 index 0000000..d9dc0d5 --- /dev/null +++ b/roles/vmm/files/init_vm_alpine.yml @@ -0,0 +1,115 @@ +#!/bin/python3 + +import serial +import subprocess +import sys +import os + +from serial_macro import * + +USAGE = f"USAGE: {sys.argv[0]} vm_guest gate ip mask ssh_key" + +def init_network(): + send_cmds(ser, [ + [1, "setup-interfaces"], + [1, ""], + [1, f"{IP}"], + [1, f"{MASK}"], + [1, f"{GATE}"], + [1, "no"], + [1, "ifdown -a"], + [10, "ifup -a"], + [1, "rc-update add networking"] + ]) + + +def init_dns(): + send_cmds(ser, [ + [1, f"setup-dns"], + [1, ""], + [1, f"{DNS}"], + ]) + + +def init_disk(): + send_cmds(ser, [ + [10, "apk add e2fsprogs sfdisk syslinux"], + [1, "setup-disk"], + [1, ""], + [10, "sys"], + [30, "y"], + ]) + + +def init_ssh(): + send_cmds(ser, [ + [5, "apk add openssh"], + [1, "mkdir /root/.ssh"], + [1, f"echo '{SSHKEY}' > /root/.ssh/authorized_keys"], + [1, f"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config"], + [1, "/etc/init.d/sshd restart"] + ]) + +def init_packages(): + send_cmds(ser, [ + [1, "echo https://mirror.ungleich.ch/mirror/packages/alpine/latest-stable/main/ > /etc/apk/repositories "], + [5, "apk update"] + ]) + + +def main(): + + global ser + global IP + global GATE + global MASK + global DNS + global SSHKEY + + COM = "/dev/" + BAUD = 115200 + TIMEOUT = 1 + + if len(sys.argv) != 7: + sys.stderr.write(USAGE) + sys.exit(1) + + GUEST = "vm-tmp" + HOST = sys.argv[1] + + cmd = f"vmctl show | grep {GUEST} | tr -s ' ' | cut -d ' ' -f7" + _buffer = subprocess.check_output(cmd, shell=True).decode().rstrip() + print(_buffer) + + if _buffer == "": + sys.exit(1) + COM += _buffer + + IP = sys.argv[2] + GATE = sys.argv[3] + MASK = sys.argv[4] + DNS = sys.argv[5] + SSHKEY = sys.argv[6] + + ser = serial.Serial(COM, BAUD, timeout=TIMEOUT) + send_cmd(ser, 1, "root") + # first boot :: live + init_network() + init_dns() + init_packages() + init_disk() + send_cmd(ser, 70, "reboot") + ser.close() + + ser = serial.Serial(COM, BAUD, timeout=TIMEOUT) + send_cmd(ser, 1, "root") + # second boot :: disk + init_network() + init_dns() + init_ssh() + init_packages() + ser.close() + + +if __name__ == "__main__": + main() diff --git a/roles/vmm/files/init_vm_serial.py b/roles/vmm/files/init_vm_serial.py deleted file mode 100644 index 46e34d7..0000000 --- a/roles/vmm/files/init_vm_serial.py +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/python3 - -import serial -import subprocess -import sys -import os - -import time - - -USAGE = f"USAGE: {sys.argv[0]} vm_guest gate ip mask ssh_key" - - -def send_cmd(ser, delay, cmd): - ser.write(f"{cmd}\n".encode("utf-8")) - time.sleep(delay) - -def send_cmds(ser, cmds): - for cmd in cmds: - send_cmd(ser, cmd[0], cmd[1]) - - -def main(): - - COM = "/dev/" - BAUD = 115200 - TIMEOUT = 1 - - if len(sys.argv) != 7: - sys.stderr.write(USAGE) - sys.exit(1) - - GUEST = "vm-tmp" - HOST = sys.argv[1] - - cmd = f"vmctl show | grep {GUEST} | tr -s ' ' | cut -d ' ' -f7" - _buffer = subprocess.check_output(cmd, shell=True).decode().rstrip() - print(_buffer) - - if _buffer == "": - sys.exit(1) - COM += _buffer - - IP = sys.argv[2] - GATE = sys.argv[3] - MASK = sys.argv[4] - DNS = sys.argv[5] - SSHKEY = sys.argv[6] - - ser = serial.Serial(COM, BAUD, timeout=TIMEOUT) - - send_cmd(ser, 1, "root") - - # virtual interface - send_cmds(ser, [ - [1, "setup-interfaces"], - [1, ""], - [1, f"{IP}"], - [1, f"{MASK}"], - [1, f"{GATE}"], - [1, "no"], - [1, "ifdown -a"], - [10, "ifup -a"] - ]) - - # dns - send_cmds(ser, [ - [1, "setup-dns"], - [1, f"{HOST}"], - [1, f"{DNS}"] - ]) - - # ssh - send_cmds(ser, [ - [5, "apk add openssh"], - [1, "mkdir /root/.ssh"], - [1, f"echo '{SSHKEY}' > /root/.ssh/authorized_keys"], - [1, f"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config"], - [1, "/etc/init.d/sshd restart"] - ]) - - ser.close() - - print(COM) - - -if __name__ == "__main__": - main() diff --git a/roles/vmm/tasks/init_vm.yml b/roles/vmm/tasks/init_vm.yml deleted file mode 100644 index 6b5cf4d..0000000 --- a/roles/vmm/tasks/init_vm.yml +++ /dev/null @@ -1,38 +0,0 @@ - -# vmm ~~ tasks/init_vm.yml - ---- - -- include_role: - name: serial - -- name: Stop vm if running - shell: vmctl stop "{{ guest }}" ; vmctl stop vm-tmp - ignore_errors: true - -- set_fact: - iso: "{{ vms | selectattr('name', 'equalto', guest) | map(attribute='iso') | first }}" - -- debug: - var: iso - -- include: set_facts.yml - -- name: Start temporary vm - shell: vmctl start -r {{ iso_latest }} -d {{ disk_file }} -n {{ vmm.switch.name }} -m 1G vm-tmp - -- name: Pause 30 seconds for vm boot - pause: - seconds: 30 - -- name: Init vm via script - script: init_vm_serial.py \ - {{ guest }} \ - {{ hostvars[guest].ip.out }} \ - {{ hypervisor.gateway }} \ - {{ hypervisor.mask }} \ - {{ _i.dns[0] }} \ - "{{ lookup('file', inventory_dir + '/files/pubkeys/rgoncalves.pub') }}" - args: - executable: "/usr/local/bin/python3" - diff --git a/roles/vmm/tasks/init_vm_alpine.yml b/roles/vmm/tasks/init_vm_alpine.yml new file mode 100644 index 0000000..0c66a44 --- /dev/null +++ b/roles/vmm/tasks/init_vm_alpine.yml @@ -0,0 +1,50 @@ + +# vmm ~~ tasks/init_vm_alpine.yml + +--- + +- set_fact: + iso: "{{ vms | selectattr('name', 'equalto', guest) | map(attribute='iso') | first }}" + +- include: set_facts.yml + +- name: Check for existing drive + stat: + path: "{{ disk_file }}" + register: st_disk + +- fail: + msg: "No empty disk detected ! You need to generated disks via hypervisor playbook" + when: not st_disk.stat.exists + +- fail: + msg: "Existing installation detected ! Manual action on host required" + when: st_disk.stat.size > 500000 + +- include_role: + name: serial + +- name: Copy vm init script + copy: + src: init_vm_alpine.py + dest: /data/python/init_vm_alpine.py + +- name: Stop vm if running + shell: vmctl stop "{{ guest }}" ; vmctl stop vm-tmp + ignore_errors: true + +- name: Start temporary vm + shell: vmctl start -r {{ iso_latest }} -d {{ disk_file }} -n {{ vmm.switch.name }} -m 1G vm-tmp + +- name: Pause 30 seconds for vm boot + pause: + seconds: 30 + +- name: Init vm via script + command: python3 /data/python/init_vm_alpine.py \ + {{ guest }} \ + {{ hostvars[guest].ip.out }} \ + {{ hypervisor.gateway }} \ + {{ hypervisor.mask }} \ + {{ _i.dns[0] }} \ + "{{ lookup('file', inventory_dir + '/files/pubkeys/rgoncalves.pub') }}" -- cgit v1.2.3