addressing some comments and some rework

This commit is contained in:
Ralph Schmieder
2025-05-16 12:31:04 +02:00
parent 676fd934ed
commit a5ca0f6496
2 changed files with 38 additions and 29 deletions

View File

@@ -23,14 +23,15 @@ $ sudo bash ./autostart.sh
installing defaults ✅
installing script ✅
installing service unit ✅
******************************************************************************
* IMPORTANT! *
* you need to ensure that you change the username and password for a user of *
* the system that can start the labs in /etc/default/cml-autostart *
* *
* Add the string "(autostart)" to the description of each lab that you wish *
* to start automatically. *
******************************************************************************
*****************************************************
* ⚠️ IMPORTANT! ⚠️ *
* you need to ensure that you change the username *
* and password for a user of the system that can *
* start the labs in /etc/default/cml-autostart *
* *
* Add the string "(autostart)" to the description *
* of each lab that you wish to start automatically. *
*****************************************************
$
```

View File

@@ -1,5 +1,7 @@
#!/bin/bash
set -e
ensure_dir() {
local install_dir="$1"
if ! test -d "$install_dir"; then
@@ -11,24 +13,28 @@ ensure_dir() {
install_default() {
local install_dir="/etc/default"
ensure_dir $install_dir
cat >"$install_dir/$(basename cml-autostart)" <<EOF
cat >"$install_dir/cml-autostart" <<EOF
USERNAME="admin"
PASSWORD="password-that-needs-to-be-changed"
TITLE_REGEX="\(autostart\)"
EOF
chown root:root "$install_dir/$(basename cml-autostart)"
chmod 0600 "$install_dir/$(basename cml-autostart)"
chown root:root "$install_dir/cml-autostart"
chmod 0600 "$install_dir/cml-autostart"
}
install_script() {
local install_dir="/usr/local/bin"
ensure_dir $install_dir
cat >"$install_dir/$(basename cml-autostart.sh)" <<EOF
cat >"$install_dir/cml-autostart.sh" <<EOF
#!/bin/bash
# set variables
source /etc/default/cml-autostart
HOST="ip6-localhost"
# the /etc/default/cml-autostart needs to define and these need to be present
# in the environment!
# - USERNAME
# - PASSWORD
# - TITLE_REGEX
# set -x
@@ -50,7 +56,7 @@ TOKEN=\$(curl -skX POST "\${API}/authenticate" \\
LABS=\$(curl -skX GET "\${API}/labs?show_all=true" \\
-H "Authorization: Bearer \${TOKEN}")
while read lab; do
while read -r lab; do
labinfo=\$(curl -skX GET "\${API}/labs/\${lab}" \\
-H "Authorization: Bearer \${TOKEN}")
description=\$(echo \$labinfo | jq -r '.lab_description')
@@ -63,30 +69,31 @@ while read lab; do
done <<<\$(echo \$LABS | jq -r '. | join("\n")')
exit
EOF
chown root:root "$install_dir/$(basename cml-autostart.sh)"
chmod a+x "$install_dir/$(basename cml-autostart.sh)"
chown root:root "$install_dir/cml-autostart.sh"
chmod a+x "$install_dir/cml-autostart.sh"
}
install_service_unit() {
local install_dir="/etc/systemd/system"
ensure_dir $install_dir
cat >"$install_dir/$(basename cml-autostart.service)" <<EOF
cat >"$install_dir/cml-autostart.service" <<EOF
[Unit]
Description=Start CML Lab
Description=Autostart CML Labs based on description text
After=network.target
After=virl2.target
[Service]
Type=oneshot
EnvironmentFile=/etc/default/cml-autostart
ExecStart=/usr/local/bin/cml-autostart.sh
User=root
User=virl2
[Install]
WantedBy=multi-user.target
EOF
chown root:root "$install_dir/$(basename cml-autostart.service)"
chown root:root "$install_dir/cml-autostart.service"
systemctl daemon-reload
systemctl enable cml-autostart.service
systemctl &>/dev/null enable cml-autostart.service
}
if [[ $EUID -ne 0 ]]; then
@@ -105,12 +112,13 @@ install_service_unit
echo -e "\t✅"
cat <<EOF
******************************************************************************
* ⚠️ IMPORTANT! ⚠️ *
* you need to ensure that you change the username and password for a user of *
* the system that can start the labs in /etc/default/cml-autostart *
* *
* Add the string "(autostart)" to the description of each lab that you wish *
* to start automatically. *
******************************************************************************
*****************************************************
* ⚠️ IMPORTANT! ⚠️ *
* you need to ensure that you change the username *
* and password for a user of the system that can *
* start the labs in /etc/default/cml-autostart *
* *
* Add the string "(autostart)" to the description *
* of each lab that you wish to start automatically. *
*****************************************************
EOF