Compare commits

...

18 Commits

Author SHA1 Message Date
pedro 8e4186d9d4 Merge pull request 'contribuciones en estancia con ekoa' (#1) from ekoa into main
Reviewed-on: #1
2024-09-28 02:16:27 +00:00
pedro a47fab6c35 deploy-workbench.sh: clearer persistence size 2024-09-20 19:55:21 -03:00
pedro 8fc62ba8da deploy-workbench.sh: bugfix wrong check 2024-09-20 17:03:50 -03:00
pedro f1de55e0b3 deploy-workbench: increase persistence partition
allow more snapshots on the persistence partition

from 10 to 100 MiB
2024-09-20 17:03:30 -03:00
pedro d53d8eb4d0 .gitignore: ignore snapshots (*.json) 2024-09-20 16:31:42 -03:00
pedro cb7ee61945 workbench-script: improve INFO and ERROR logs 2024-09-20 16:31:12 -03:00
pedro eed87b3b1a deploy-workbench.sh: add FORCE var
to recreate persistence partition
2024-09-20 11:58:58 -03:00
pedro 50e8cb5f6b deploy-workbench.sh: just copy local settings.ini 2024-09-20 11:39:23 -03:00
pedro 9473391ba5 deploy-workbench.sh: use proper sudo here 2024-09-20 11:36:42 -03:00
pedro d0f3f337d2 deploy-workbench.sh: update with new settings.ini 2024-09-20 11:36:24 -03:00
pedro 6d3d4e5c2a make upload snapshot work 2024-09-19 14:04:11 -03:00
pedro 45464c49f0 Makefile: boot_iso: link to workbench_production 2024-09-19 13:08:57 -03:00
pedro 3688088958 workbench deploy: detect ubuntu and guide user 2024-09-19 13:06:25 -03:00
pedro b005a5e158 workbench deploy: bugfix workbench-script copy
create directory and enforce that it is copied in directory
2024-09-19 13:05:52 -03:00
pedro a9af55d109 workbench: use declarative config instead of args
The only arg needed is the location of the config, which is optional
2024-09-19 12:42:03 -03:00
pedro 8627cd8003 bugifx workbench deploy
use tee instead of cat

here ${SUDO} is used, because it is assumed a third party system which
could have its own configuration, so we deal the different situations
fine (with or without sudo, etc.)
2024-09-19 12:40:49 -03:00
pedro d3018abde9 workbench: use sudo everywhere
that means adjusting fine the hostname, which right now is hardcoded
to workbench
2024-09-19 12:40:04 -03:00
pedro 6c781a0503 clearer start-end of script 2024-09-19 11:27:52 -03:00
5 changed files with 99 additions and 58 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
iso
settings.ini
# ignore all possible snapshots in this dir
*.json

View File

@ -22,7 +22,7 @@ install_dependencies:
boot_iso:
sudo qemu-system-x86_64 \
-enable-kvm -m 2G -vga qxl -netdev user,id=wan -device virtio-net,netdev=wan,id=nic1 \
-drive format=raw,file=iso/workbench.iso,cache=none,if=virtio
-drive format=raw,file=iso/workbench_production.iso,cache=none,if=virtio
# src https://www.ubuntubuzz.com/2021/04/how-to-boot-uefi-on-qemu.html
# needs `sudo apt-get install ovmf`

View File

@ -98,7 +98,7 @@ LABEL linux
END
)"
# TIMEOUT 60 means 6 seconds :)
sudo tee "${ISO_PATH}/staging/isolinux/isolinux.cfg" <<EOF
${SUDO} tee "${ISO_PATH}/staging/isolinux/isolinux.cfg" <<EOF
${isolinuxcfg_str}
EOF
${SUDO} cp /usr/lib/ISOLINUX/isolinux.bin "${ISO_PATH}/staging/isolinux/"
@ -188,8 +188,9 @@ create_persistence_partition() {
# persistent partition
rw_img_name="workbench_vfat.img"
rw_img_path="${ISO_PATH}/staging/${rw_img_name}"
if [ ! -f "${rw_img_path}" ] || [ "${DEBUG:-}" ]; then
${SUDO} dd if=/dev/zero of="${rw_img_path}" bs=10M count=1
if [ ! -f "${rw_img_path}" ] || [ "${DEBUG:-}" ] || [ "${FORCE:-}" ]; then
persistent_volume_size_MB=100
${SUDO} dd if=/dev/zero of="${rw_img_path}" bs=1M count=${persistent_volume_size_MB}
${SUDO} mkfs.vfat "${rw_img_path}"
# generate structure on persistent partition
@ -198,17 +199,7 @@ create_persistence_partition() {
mkdir -p "${tmp_rw_mount}"
${SUDO} mount "$(pwd)/${rw_img_path}" "${tmp_rw_mount}"
${SUDO} mkdir -p "${tmp_rw_mount}/settings"
# TODO without SUDO fails
${SUDO} cat > "${tmp_rw_mount}/settings/settings.ini" <<END
[settings]
DH_TOKEN =
DH_URL =
SNAPSHOTS_PATH = /mnt
LOGS_PATH = /mnt
VERSION =
END
${SUDO} cp -v settings.ini "${tmp_rw_mount}/settings/settings.ini"
${SUDO} umount "${tmp_rw_mount}"
uuid="$(blkid "${rw_img_path}" | awk '{ print $3; }')"
@ -249,7 +240,7 @@ END2
###################
# configure hosts
cat > /etc/hosts <<END2
127.0.0.1 localhost \${hostname}
127.0.0.1 localhost workbench
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
@ -261,8 +252,9 @@ prepare_app() {
# prepare app during prepare_chroot_env
# Install hardware_metadata module
workbench_dir="${ISO_PATH}/chroot/opt/workbench"
${SUDO} cp workbench-script.py "${workbench_dir}"
${SUDO} cp requirements.txt "${workbench_dir}"
${SUDO} mkdir -p "${workbench_dir}"
${SUDO} cp workbench-script.py "${workbench_dir}/"
${SUDO} cp requirements.txt "${workbench_dir}/"
# startup script execution
cat > "${ISO_PATH}/chroot/root/.profile" <<END
@ -272,7 +264,7 @@ stty -echo # Do not show what we type in terminal so it does not meddle with our
dmesg -n 1 # Do not report *useless* system messages to the terminal
# clearly specify the right working directory, used in the python script as os.getcwd()
cd /mnt
pipenv run python /opt/workbench/workbench-script.py
pipenv run python /opt/workbench/workbench-script.py --config "/mnt/settings/settings.ini"
stty echo
END
#TODO add some useful commands
@ -286,6 +278,7 @@ echo 'Install requirements'
# Install debian requirements
apt-get install -y --no-install-recommends \
sudo \
python3 python3-dev python3-pip pipenv \
dmidecode smartmontools hwinfo pciutils lshw < /dev/null
# Install python requirements using apt instead of pip
@ -312,7 +305,7 @@ run_chroot() {
set -x
set -e
echo "${hostname}" > /etc/hostname
echo workbench > /etc/hostname
# check what linux images are available on the system
# Figure out which Linux Kernel you want in the live environment.
@ -389,6 +382,11 @@ prepare_chroot_env() {
if [ -z "${VERSION_CODENAME:-}" ]; then
. /etc/os-release
echo "TAKING OS-RELEASE FILE"
if [ ! "${ID}" = "debian" ]; then
echo "ERROR: ubuntu detected, then you are enforced to specify debian variant"
echo " use for example \`VERSION_CODENAME='bookworm'\` or similar"
exit 1
fi
fi
chroot_path="${ISO_PATH}/chroot"

6
settings.ini.example Normal file
View File

@ -0,0 +1,6 @@
[settings]
url = http://127.0.0.1:8000/api/snapshot/
token = '1234'
# path = /path/to/save
# device = your_device_name
# # erase = basic

View File

@ -5,6 +5,7 @@ import json
import uuid
import hashlib
import argparse
import configparser
import ntplib
import requests
@ -228,9 +229,9 @@ def smartctl(all_disks, disk=None):
def get_data(all_disks):
lshw = 'lshw -json'
hwinfo = 'hwinfo --reallyall'
dmidecode = 'dmidecode'
lshw = 'sudo lshw -json'
hwinfo = 'sudo hwinfo --reallyall'
dmidecode = 'sudo dmidecode'
data = {
'lshw': exec_cmd(lshw),
'disks': smartctl(all_disks),
@ -253,17 +254,23 @@ def save_snapshot_in_disk(snapshot, path):
datetime.now().strftime("%Y%m%d-%H_%M_%S"),
snapshot['uuid']
)
print(f"workbench: INFO: Snapshot written in path '{filename}'")
with open(filename, "w") as f:
f.write(json.dumps(snapshot))
# TODO sanitize url, if url is like this, it fails
# url = 'http://127.0.0.1:8000/api/snapshot/'
def send_snapshot_to_devicehub(snapshot, token, url):
headers = {
f"Authorization": "Basic {token}",
"Content-Type": "application/json"
}
return requests.post(url, data=snapshot, header=headers)
try:
requests.post(url, data=json.dumps(snapshot), headers=headers)
print(f"workbench: INFO: Snapshot sent to '{url}'")
except:
print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?")
@logs
def sync_time():
@ -271,48 +278,75 @@ def sync_time():
ntplib.NTPClient()
response = client.request('pool.ntp.org')
def load_config(config_file="settings.ini"):
"""
Tries to load configuration from a config file.
"""
config = configparser.ConfigParser()
if os.path.exists(config_file):
# If config file exists, read from it
print(f"workbench: INFO: Found config file in path: '{config_file}'.")
config.read(config_file)
path = config.get('settings', 'path', fallback=os.getcwd())
# TODO validate that has http:// start
url = config.get('settings', 'url', fallback=None)
token = config.get('settings', 'token', fallback=None)
# TODO validate that the device exists?
device = config.get('settings', 'device', fallback=None)
erase = config.get('settings', 'erase', fallback=None)
else:
print(f"workbench: ERROR: Config file '{config_file}' not found. Using default values.")
path = os.path.join(os.getcwd())
url, token, device, erase = None, None, None, None
return {
'path': path,
'url': url,
'token': token,
'device': device,
'erase': erase
}
def parse_args():
"""
Parse config argument, if available
"""
parser = argparse.ArgumentParser(description="Optional config loader for workbench.")
parser.add_argument(
'--config',
help="Path to the config file. Defaults to 'settings.ini' in the current directory.",
default="settings.ini" # Fallback to 'settings.ini' by default
)
return parser.parse_args()
def main():
print("START")
parser=argparse.ArgumentParser()
parser.add_argument("-p", "--path", required=True)
parser.add_argument("-u", "--url", required=False)
parser.add_argument("-t", "--token", required=False)
parser.add_argument("-d", "--device", required=False)
parser.add_argument(
"-e",
"--erase",
choices=["basic", "baseline", "enhanced"],
required=False
)
args=parser.parse_args()
vline='\n___________\n\n'
print(f"{vline}workbench: START\n")
if args.device and not args.erase:
print("error: argument --erase: expected one argument")
return
if args.token and not args.url:
print("error: argument --url: expected one argument")
return
if args.url and not args.token:
print("error: argument --token: expected one argument")
return
# Parse the command-line arguments
args = parse_args()
# Load the config file, either specified via --config or the default 'settings.ini'
config_file = args.config
config = load_config(config_file)
all_disks = get_disks()
snapshot = gen_snapshot(all_disks)
if args.erase and args.device:
snapshot['erase'] = gen_erase(all_disks, args.erase, user_disk=args.device)
elif args.erase:
snapshot['erase'] = gen_erase(all_disks, args.erase)
if config['erase'] and config['device']:
snapshot['erase'] = gen_erase(all_disks, config['erase'], user_disk=config['device'])
elif config['erase']:
snapshot['erase'] = gen_erase(all_disks, config['erase'])
save_snapshot_in_disk(snapshot, args.path)
if args.url:
send_snapshot_to_devicehub(snapshot, args.token, args.url)
save_snapshot_in_disk(snapshot, config['path'])
print("END")
if config['url']:
send_snapshot_to_devicehub(snapshot, config['token'], config['url'])
print(f"\nworkbench: END{vline}")
if __name__ == '__main__':