commit 0c8e58874594e9055d487e90ffa8cb6fbb8737cb
parent b0dffedffc14fd3abbf7128bcb92c58b36199f4a
Author: Stefan Koch <programming@stefan-koch.name>
Date: Sun, 20 Dec 2020 16:50:08 +0100
Merge branch 'feat-init-script'
Diffstat:
5 files changed, 72 insertions(+), 45 deletions(-)
diff --git a/examples/jitsi_auto_install/README.md b/examples/jitsi_auto_install/README.md
@@ -9,19 +9,7 @@ With the manual installation, we'd have all freedom. But let's stick to the
deb-installation.
This means, we will use a per-host script that is executed on the host
-during startup.
-
-For this, we will create a systemd unit that is execute in case a specific
-folder (`/etc/jitsi`) does not exist. This unit will then perform an
-unattended install of Jitsi.
-
-To achieve this, the systemd unit has to be installed into the QEMU image
-before boot. We achieve this with `guestmount`. All steps to change the image
-are given in `modify-image.sh`.
-
-As of the time of writing this guide, there is no functionality inside
-`aetherscale` to define ones own user script to execute during host boot.
-Since the environment variable `JITSI_HOSTNAME` has to be set differently
-for each VM it also does not work to prepare an image with these steps. Thus,
-at the time of writing this can only serve as an example what could be possible
-with a few modifications.
+during startup. An init script can be passed to the VM using `init-script`
+in the JSON message or `--init-script` on the command line. The example
+Python file reads a jinja2 template and sets a user defined hostname before
+starting the VM.
diff --git a/examples/jitsi_auto_install/jitsi-install.service b/examples/jitsi_auto_install/jitsi-install.service
@@ -1,11 +0,0 @@
-[Unit]
-Description=Install Jitsi
-ConditionPathExists=!/etc/jitsi
-
-[Service]
-Type=oneshot
-ExecStart=/root/jitsi-install.sh
-Environment=JITSI_HOSTNAME=jitsi.example.com
-
-[Install]
-WantedBy=multi-user.target
diff --git a/examples/jitsi_auto_install/jitsi-install.sh b/examples/jitsi_auto_install/jitsi-install.sh
@@ -1,18 +0,0 @@
-#!/usr/bin/env bash
-
-apt update
-apt -y install gnupg2 nginx-full apt-transport-https
-apt-add-repository universe
-apt update
-apt -y install openjdk-8-jdk
-
-hostnamectl set-hostname $JITSI_HOSTNAME
-
-curl https://download.jitsi.org/jitsi-key.gpg.key | sudo sh -c 'gpg --dearmor > /usr/share/keyrings/jitsi-keyring.gpg'
-echo 'deb [signed-by=/usr/share/keyrings/jitsi-keyring.gpg] https://download.jitsi.org stable/' | sudo tee /etc/apt/sources.list.d/jitsi-stable.list > /dev/null
-apt update
-
-echo "jitsi-videobridge jitsi-videobridge/jvb-hostname string $JITSI_HOSTNAME" | debconf-set-selections
-echo "jitsi-meet jitsi-meet/cert-choice select Self-signed certificate will be generated" | debconf-set-selections
-export DEBIAN_FRONTEND=noninteractive
-apt -y install jitsi-meet
diff --git a/examples/jitsi_auto_install/jitsi-install.sh.jinja2 b/examples/jitsi_auto_install/jitsi-install.sh.jinja2
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+
+apt update
+apt -y install gnupg2 nginx-full apt-transport-https
+apt-add-repository universe
+apt update
+apt -y install openjdk-8-jdk
+
+hostnamectl set-hostname {{ hostname }}
+
+curl https://download.jitsi.org/jitsi-key.gpg.key | sudo sh -c 'gpg --dearmor > /usr/share/keyrings/jitsi-keyring.gpg'
+echo 'deb [signed-by=/usr/share/keyrings/jitsi-keyring.gpg] https://download.jitsi.org stable/' | sudo tee /etc/apt/sources.list.d/jitsi-stable.list > /dev/null
+apt update
+
+echo "jitsi-videobridge jitsi-videobridge/jvb-hostname string {{ hostname }}" | debconf-set-selections
+echo "jitsi-meet jitsi-meet/cert-choice select Self-signed certificate will be generated" | debconf-set-selections
+export DEBIAN_FRONTEND=noninteractive
+apt -y install jitsi-meet
diff --git a/examples/jitsi_auto_install/start_vm.py b/examples/jitsi_auto_install/start_vm.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+import jinja2
+from pathlib import Path
+import sys
+import tempfile
+
+from aetherscale.client import ServerCommunication
+
+
+def create_vm(init_script: Path, comm: ServerCommunication) -> str:
+ with open(init_script) as f:
+ script = f.read()
+
+ responses = comm.send_msg({
+ 'command': 'create-vm',
+ 'options': {
+ 'image': 'ubuntu-20.04.1-server-amd64',
+ 'init-script': script,
+ }
+ }, response_expected=True)
+
+ if len(responses) != 1:
+ raise RuntimeError(
+ 'Did not receive exactly one response, something went wrong')
+
+ if responses[0]['execution-info']['status'] != 'success':
+ raise RuntimeError('Execution was not successful')
+
+ return responses[0]['response']['vm-id']
+
+
+def main():
+ env = jinja2.Environment(loader=jinja2.FileSystemLoader('./'))
+ template = env.get_template('jitsi-install.sh.jinja2')
+
+ with tempfile.NamedTemporaryFile('wt') as f:
+ template.stream(hostname='jitsi.example.com').dump(f.name)
+
+ with ServerCommunication() as comm:
+ try:
+ vm_id = create_vm(Path(f.name), comm)
+ print(vm_id)
+ except RuntimeError as e:
+ print(str(e), file=sys.stderr)
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()