aboutsummaryrefslogtreecommitdiffstats
path: root/roles/vmm/files
diff options
context:
space:
mode:
authorbinary <me@rgoncalves.se>2020-11-08 20:56:18 +0100
committerbinary <me@rgoncalves.se>2020-11-08 20:56:18 +0100
commite15d9acbd07c1ecb6e6c6b24f547b60f7d92ebaf (patch)
treeef0130c97f594ca7d57676c9e1d027724211fba8 /roles/vmm/files
parent605d9efb0ad2734278d8bdb5f9e0862d8e0a1a7e (diff)
downloadinfrastructure-e15d9acbd07c1ecb6e6c6b24f547b60f7d92ebaf.tar.gz
Add working script for ssh init on alpine vm
Diffstat (limited to 'roles/vmm/files')
-rw-r--r--roles/vmm/files/init_vm_serial.py88
1 files changed, 88 insertions, 0 deletions
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()
remember that computers suck.