Compare commits
4 commits
0545a2f460
...
a9af55d109
Author | SHA1 | Date | |
---|---|---|---|
a9af55d109 | |||
8627cd8003 | |||
d3018abde9 | |||
6c781a0503 |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
iso
|
iso
|
||||||
|
settings.ini
|
||||||
|
|
|
@ -198,8 +198,7 @@ create_persistence_partition() {
|
||||||
mkdir -p "${tmp_rw_mount}"
|
mkdir -p "${tmp_rw_mount}"
|
||||||
${SUDO} mount "$(pwd)/${rw_img_path}" "${tmp_rw_mount}"
|
${SUDO} mount "$(pwd)/${rw_img_path}" "${tmp_rw_mount}"
|
||||||
${SUDO} mkdir -p "${tmp_rw_mount}/settings"
|
${SUDO} mkdir -p "${tmp_rw_mount}/settings"
|
||||||
# TODO without SUDO fails
|
${SUDO} tee "${tmp_rw_mount}/settings/settings.ini" <<END
|
||||||
${SUDO} cat > "${tmp_rw_mount}/settings/settings.ini" <<END
|
|
||||||
[settings]
|
[settings]
|
||||||
|
|
||||||
DH_TOKEN =
|
DH_TOKEN =
|
||||||
|
@ -249,7 +248,7 @@ END2
|
||||||
###################
|
###################
|
||||||
# configure hosts
|
# configure hosts
|
||||||
cat > /etc/hosts <<END2
|
cat > /etc/hosts <<END2
|
||||||
127.0.0.1 localhost \${hostname}
|
127.0.0.1 localhost workbench
|
||||||
::1 localhost ip6-localhost ip6-loopback
|
::1 localhost ip6-localhost ip6-loopback
|
||||||
ff02::1 ip6-allnodes
|
ff02::1 ip6-allnodes
|
||||||
ff02::2 ip6-allrouters
|
ff02::2 ip6-allrouters
|
||||||
|
@ -272,7 +271,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
|
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()
|
# clearly specify the right working directory, used in the python script as os.getcwd()
|
||||||
cd /mnt
|
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
|
stty echo
|
||||||
END
|
END
|
||||||
#TODO add some useful commands
|
#TODO add some useful commands
|
||||||
|
@ -286,6 +285,7 @@ echo 'Install requirements'
|
||||||
|
|
||||||
# Install debian requirements
|
# Install debian requirements
|
||||||
apt-get install -y --no-install-recommends \
|
apt-get install -y --no-install-recommends \
|
||||||
|
sudo \
|
||||||
python3 python3-dev python3-pip pipenv \
|
python3 python3-dev python3-pip pipenv \
|
||||||
dmidecode smartmontools hwinfo pciutils lshw < /dev/null
|
dmidecode smartmontools hwinfo pciutils lshw < /dev/null
|
||||||
# Install python requirements using apt instead of pip
|
# Install python requirements using apt instead of pip
|
||||||
|
@ -312,7 +312,7 @@ run_chroot() {
|
||||||
set -x
|
set -x
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
echo "${hostname}" > /etc/hostname
|
echo workbench > /etc/hostname
|
||||||
|
|
||||||
# check what linux images are available on the system
|
# check what linux images are available on the system
|
||||||
# Figure out which Linux Kernel you want in the live environment.
|
# Figure out which Linux Kernel you want in the live environment.
|
||||||
|
|
|
@ -5,6 +5,7 @@ import json
|
||||||
import uuid
|
import uuid
|
||||||
import hashlib
|
import hashlib
|
||||||
import argparse
|
import argparse
|
||||||
|
import configparser
|
||||||
|
|
||||||
import ntplib
|
import ntplib
|
||||||
import requests
|
import requests
|
||||||
|
@ -228,9 +229,9 @@ def smartctl(all_disks, disk=None):
|
||||||
|
|
||||||
|
|
||||||
def get_data(all_disks):
|
def get_data(all_disks):
|
||||||
lshw = 'lshw -json'
|
lshw = 'sudo lshw -json'
|
||||||
hwinfo = 'hwinfo --reallyall'
|
hwinfo = 'sudo hwinfo --reallyall'
|
||||||
dmidecode = 'dmidecode'
|
dmidecode = 'sudo dmidecode'
|
||||||
data = {
|
data = {
|
||||||
'lshw': exec_cmd(lshw),
|
'lshw': exec_cmd(lshw),
|
||||||
'disks': smartctl(all_disks),
|
'disks': smartctl(all_disks),
|
||||||
|
@ -253,6 +254,7 @@ def save_snapshot_in_disk(snapshot, path):
|
||||||
datetime.now().strftime("%Y%m%d-%H_%M_%S"),
|
datetime.now().strftime("%Y%m%d-%H_%M_%S"),
|
||||||
snapshot['uuid']
|
snapshot['uuid']
|
||||||
)
|
)
|
||||||
|
print(f"workbench: Snapshot written in path '{filename}'")
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
f.write(json.dumps(snapshot))
|
f.write(json.dumps(snapshot))
|
||||||
|
|
||||||
|
@ -271,48 +273,75 @@ def sync_time():
|
||||||
ntplib.NTPClient()
|
ntplib.NTPClient()
|
||||||
response = client.request('pool.ntp.org')
|
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: 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: 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():
|
def main():
|
||||||
print("START")
|
vline='\n___________\n\n'
|
||||||
parser=argparse.ArgumentParser()
|
print(f"{vline}workbench: START\n")
|
||||||
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()
|
|
||||||
|
|
||||||
if args.device and not args.erase:
|
# Parse the command-line arguments
|
||||||
print("error: argument --erase: expected one argument")
|
args = parse_args()
|
||||||
return
|
|
||||||
|
|
||||||
if args.token and not args.url:
|
# Load the config file, either specified via --config or the default 'settings.ini'
|
||||||
print("error: argument --url: expected one argument")
|
config_file = args.config
|
||||||
return
|
|
||||||
|
|
||||||
if args.url and not args.token:
|
config = load_config(config_file)
|
||||||
print("error: argument --token: expected one argument")
|
|
||||||
return
|
|
||||||
|
|
||||||
all_disks = get_disks()
|
all_disks = get_disks()
|
||||||
snapshot = gen_snapshot(all_disks)
|
snapshot = gen_snapshot(all_disks)
|
||||||
|
|
||||||
if args.erase and args.device:
|
if config['erase'] and config['device']:
|
||||||
snapshot['erase'] = gen_erase(all_disks, args.erase, user_disk=args.device)
|
snapshot['erase'] = gen_erase(all_disks, config['erase'], user_disk=config['device'])
|
||||||
elif args.erase:
|
elif config['erase']:
|
||||||
snapshot['erase'] = gen_erase(all_disks, args.erase)
|
snapshot['erase'] = gen_erase(all_disks, config['erase'])
|
||||||
|
|
||||||
save_snapshot_in_disk(snapshot, args.path)
|
save_snapshot_in_disk(snapshot, config['path'])
|
||||||
|
|
||||||
if args.url:
|
if config['url']:
|
||||||
send_snapshot_to_devicehub(snapshot, args.token, args.url)
|
send_snapshot_to_devicehub(snapshot, config['token'], config['url'])
|
||||||
|
|
||||||
print("END")
|
print(f"\nworkbench: END{vline}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue