From e15d9acbd07c1ecb6e6c6b24f547b60f7d92ebaf Mon Sep 17 00:00:00 2001 From: binary Date: Sun, 8 Nov 2020 20:56:18 +0100 Subject: Add working script for ssh init on alpine vm --- roles/vmm/files/init_vm_serial.py | 88 +++++++++++++++++++++++++++++++++++++ roles/vmm/tasks/boot_alpine.yml | 43 ------------------ roles/vmm/tasks/generate_vmconf.yml | 21 +++++++-- roles/vmm/tasks/init_hypervisor.yml | 1 + roles/vmm/tasks/init_vm.yml | 26 +++++++++++ roles/vmm/templates/vm.conf.j2 | 21 ++++++--- roles/vmm/vars/main.yml | 8 +++- 7 files changed, 154 insertions(+), 54 deletions(-) create mode 100644 roles/vmm/files/init_vm_serial.py delete mode 100644 roles/vmm/tasks/boot_alpine.yml create mode 100644 roles/vmm/tasks/init_vm.yml (limited to 'roles/vmm') diff --git a/roles/vmm/files/init_vm_serial.py b/roles/vmm/files/init_vm_serial.py new file mode 100644 index 0000000..46e34d7 --- /dev/null +++ b/roles/vmm/files/init_vm_serial.py @@ -0,0 +1,88 @@ +#!/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/boot_alpine.yml b/roles/vmm/tasks/boot_alpine.yml deleted file mode 100644 index da4e91e..0000000 --- a/roles/vmm/tasks/boot_alpine.yml +++ /dev/null @@ -1,43 +0,0 @@ - -# vmm ~~ tasks/boot_alpine.yml -# Boot an alpine image and init its installation set. -# Required : -# - guest : vm to be be bootup and initialized - ---- - -- name: Check arguments - fail: - msg: "arguments : guest" - when: guest is not defined - -- set_fact: - iso: "{{ hostvars[guest].iso }}" - disk_file : "{{ vmm.disk.dir }}/{{ guest }}.{{ vmm.disk.format }}" - -- include: set_facts.yml - -- name: Start vm - shell: | - vmctl stop dummy - sleep 2 - vmctl start -d {{ iso_latest | quote }} \ - -d {{ disk_file | quote }} \ - -n {{ vmm.config.switch.name | quote }} \ - -m {{ hostvars[guest].memory | quote }} dummy - - sleep 2 - tty=$(vmctl show | grep dummy | tr -s " " " " | cut -d " " -f 7) - sleep 20 - - cat << EOF > /dev/${tty} - echo "mkdir /root/.ssh" - echo "echo $(cat /root/.ssh/authorized_keys) > /root/.ssh/authorized_keys" > /dev/ttyp1 - echo "apk add openssh ; rc-update add sshd ; /etc/init.d/sshd start" /dev/${tty} - EOF - - exit 0 - - args: - executable: /bin/sh - diff --git a/roles/vmm/tasks/generate_vmconf.yml b/roles/vmm/tasks/generate_vmconf.yml index c4e80d8..e8f8245 100644 --- a/roles/vmm/tasks/generate_vmconf.yml +++ b/roles/vmm/tasks/generate_vmconf.yml @@ -4,13 +4,26 @@ --- +- name: Start ip forwarding + shell: sysctl net.inet.ip{{ item }}.forwarding=1 + loop: + - "" + - "6" + +- name: Enable ip forwarding + lineinfile: + path: /etc/sysctl.conf + regexp: "^net.inet.ip{{ item }}.forwarding=" + line: "net.inet.ip{{ item }}.forwarding=1" + loop: + - "" + - "6" + - name: Create network switch - template: - src: templates/hostname.j2 - dest: "/etc/hostname.{{ vmm.switch.interface }}" + shell: echo "add {{ hypervisor.interface }}" > /etc/hostname.{{ vmm.switch.iface }} - name: Start network switch - shell: "sh /etc/netstart {{ vmm.switch.interface }}" + shell: "sh /etc/netstart {{ vmm.switch.iface }}" - name: Generate vmm configuration template: diff --git a/roles/vmm/tasks/init_hypervisor.yml b/roles/vmm/tasks/init_hypervisor.yml index c963bbb..b206279 100644 --- a/roles/vmm/tasks/init_hypervisor.yml +++ b/roles/vmm/tasks/init_hypervisor.yml @@ -10,3 +10,4 @@ - include: generate_vmconf.yml guest="{{ item }}" loop: "{{ hostvars[ansible_host] }}.vm.hosts" + diff --git a/roles/vmm/tasks/init_vm.yml b/roles/vmm/tasks/init_vm.yml new file mode 100644 index 0000000..4ded177 --- /dev/null +++ b/roles/vmm/tasks/init_vm.yml @@ -0,0 +1,26 @@ + +# vmm ~~ tasks/init_vm.yml + +--- + +- include_role: + name: serial + +- set_fact: + + +- 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" + register: rg + +- name: Installation result + debug: + var: rg.stdout_lines diff --git a/roles/vmm/templates/vm.conf.j2 b/roles/vmm/templates/vm.conf.j2 index 2665c16..f9fa6f5 100644 --- a/roles/vmm/templates/vm.conf.j2 +++ b/roles/vmm/templates/vm.conf.j2 @@ -1,8 +1,22 @@ #jinja2: trim_blocks: True, lstrip_blocks: True +# vm configuration ~~ /etc/vm.conf +# managed by Ansible + +# ====================== # +# virtual network switch +# ====================== # + +switch "{{ vmm.switch.name }}" { + interface {{ vmm.switch.iface }} +} + +# ============================ # +# virtual machines declaration +# ============================ # + {% for vm in vms if hostvars[vm.name] is defined %} {% set guest = hostvars[vm.name] %} - vm "{{ guest.ansible_host }}" { {% if vm.enabled and vm.enabled is defined %} enable @@ -15,8 +29,5 @@ vm "{{ guest.ansible_host }}" { switch "{{ vmm.switch.name }}" } } -{% endfor %} -switch "{{ vmm.switch.name }}" { - interface {{ vmm.switch.interface }} -} +{% endfor %} diff --git a/roles/vmm/vars/main.yml b/roles/vmm/vars/main.yml index 8119c88..8ad57a0 100644 --- a/roles/vmm/vars/main.yml +++ b/roles/vmm/vars/main.yml @@ -13,11 +13,15 @@ vmm: disk_format: "qcow2" config_file: "/etc/vm.conf" + + viface: + iface: "vether0" + ip: "192.168.10.1" + mask: "255.255.255.0" switch: + iface: "bridge0" name: "uplink" - interface: "bridge0" - interface_host: "bnx0" iso: -- cgit v1.2.3