diff options
Diffstat (limited to 'roles/vmm')
-rw-r--r-- | roles/vmm/files/init_vm_alpine.yml (renamed from roles/vmm/files/init_vm_serial.py) | 105 | ||||
-rw-r--r-- | roles/vmm/tasks/init_vm_alpine.yml (renamed from roles/vmm/tasks/init_vm.yml) | 38 |
2 files changed, 91 insertions, 52 deletions
diff --git a/roles/vmm/files/init_vm_serial.py b/roles/vmm/files/init_vm_alpine.yml index 46e34d7..d9dc0d5 100644 --- a/roles/vmm/files/init_vm_serial.py +++ b/roles/vmm/files/init_vm_alpine.yml @@ -5,23 +5,67 @@ import subprocess import sys import os -import time - +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 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 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 @@ -48,40 +92,23 @@ def main(): 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"] - ]) - + # first boot :: live + init_network() + init_dns() + init_packages() + init_disk() + send_cmd(ser, 70, "reboot") ser.close() - print(COM) + 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__": diff --git a/roles/vmm/tasks/init_vm.yml b/roles/vmm/tasks/init_vm_alpine.yml index 6b5cf4d..0c66a44 100644 --- a/roles/vmm/tasks/init_vm.yml +++ b/roles/vmm/tasks/init_vm_alpine.yml @@ -1,23 +1,38 @@ -# vmm ~~ tasks/init_vm.yml +# 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 -- 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 @@ -26,13 +41,10 @@ seconds: 30 - name: Init vm via script - script: init_vm_serial.py \ + 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') }}" - args: - executable: "/usr/local/bin/python3" - |