diff --git a/3.New_server_deployment/1.Download_envsetup_config/0_setup.py b/3.New_server_deployment/1.Download_envsetup_config/0_setup.py index 379405e2bd167fa7f850d12b5f0b2c0ac5d4dbbe..d01670c01d6a184a801a88480ef12681475b087a 100644 --- a/3.New_server_deployment/1.Download_envsetup_config/0_setup.py +++ b/3.New_server_deployment/1.Download_envsetup_config/0_setup.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os +import subprocess import utils @@ -14,11 +15,12 @@ def setup(interactive=True): with open('/root/.ssh/id_rsa.pub', 'r') as fo: public_key = fo.read() # Get requests module - cmds = [ - 'apt-get update', - 'apt-get install -y python3-requests', - ] - utils.run_commands(cmds) + if subprocess.run(['dpkg', '-s', 'python3-requests'], stdout=subprocess.DEVNULL).returncode != 0: + cmds = [ + 'apt-get update', + 'apt-get install -y python3-requests', + ] + utils.run_commands(cmds) import requests # Check skyreach url verify = utils.get_conf('SKYREACH_SSL_VERIFY') != '0' @@ -29,12 +31,18 @@ def setup(interactive=True): req = requests.head(sk_url, verify=verify, timeout=20) if req.status_code != 301: raise Exception('Unexpected response from "%s": code %s, should have been 301.' % (sk_url, req.status_code)) - # Get conf using an activation key - act_key = utils.get_conf('SKYREACH_ACTIVATION_KEY') - if not act_key: - utils.log('\033[1;33m No activation key is set, skipping configuration download. \033[0m') + # Get conf using API key if already set or using an activation key + req = None + api_key = utils.get_conf('SKYREACH_API_KEY') + if api_key: + req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', params=dict(api_key=api_key), data=dict(public_key=public_key), verify=verify, timeout=20) + else: + act_key = utils.get_conf('SKYREACH_ACTIVATION_KEY') + if act_key: + req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', data=dict(key=act_key, public_key=public_key), verify=verify, timeout=20) + if not req: + utils.log('\033[1;33m No activation key nor API key are set, skipping configuration download. \033[0m') return - req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', data=dict(key=act_key, public_key=public_key), verify=verify, timeout=20) # Write conf if req.status_code != 200: if len(req.text) > 300: @@ -49,10 +57,16 @@ def setup(interactive=True): fo.write(req.text) utils.log('Configuration written.') - utils.log('Removing activation key from conf.sh') - with open('../../conf.sh', 'r') as f: - lines = f.readlines() - with open('../../conf.sh', 'w') as f: - for l in lines: - if not l.startswith('SKYREACH_ACTIVATION_KEY'): - f.write(l) + utils.log('Comment activation key in conf.sh') + path = os.path.join(os.path.dirname(os.path.dirname(utils.get_dir(__file__))), 'conf.sh') + with open(path, 'r') as fo: + content = fo.read() + content = content.replace('SKYREACH_ACTIVATION_KEY', '#SKYREACH_ACTIVATION_KEY').replace('##SKYREACH_ACTIVATION_KEY', '#SKYREACH_ACTIVATION_KEY') + with open(path, 'w') as fo: + fo.write(content) + + utils.log('Autogenerate empty conf.') + cmds = [ + 'bash fill_empty_conf.sh', + ] + utils.run_commands(cmds) diff --git a/3.New_server_deployment/1.Download_envsetup_config/fill_empty_conf.sh b/3.New_server_deployment/1.Download_envsetup_config/fill_empty_conf.sh new file mode 100755 index 0000000000000000000000000000000000000000..8031be0c85e2d81effc362cdc979c873e23da28e --- /dev/null +++ b/3.New_server_deployment/1.Download_envsetup_config/fill_empty_conf.sh @@ -0,0 +1,44 @@ +#!/bin/bash +source ../../global-conf.sh + +if ( dpkg -s pwgen >/dev/null 2>&1 ); then + echo "The pwgen package is already installed." +else + apt-get install -y -o Dpkg::Options::="--force-confold" pwgen +fi + +conf_path="../../auto-generated-conf.sh" + +# Autogenerate missing values +if [ "${MS_ID}" = "" ]; then + MS_ID=$(echo "$(hostname)_msuser") + if ( cat "$conf_path" | grep "MS_ID" >/dev/null ); then + sed -i "s@^MS_ID=.*@MS_ID='${MS_ID}'@" "$conf_path" + else + echo "MS_ID='${MS_ID}'" >> "$conf_path" + fi + echo -e "${YELLOW}The config MS_ID has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" + sleep 3 +fi +if [ "${MS_API_KEY}" = "" ]; then + MS_API_KEY=$(echo "s$(pwgen 4)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)") + # respect API pattern + MS_API_KEY=$(echo $MS_API_KEY | sed "s@[iloILO]@$((${RANDOM} / 10000))@g") + if ( cat "$conf_path" | grep "MS_API_KEY" >/dev/null ); then + sed -i "s@^MS_API_KEY=.*@MS_API_KEY='${MS_API_KEY}'@" "$conf_path" + else + echo "MS_API_KEY='${MS_API_KEY}'" >> "$conf_path" + fi + echo -e "${YELLOW}The config MS_API_KEY has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" + sleep 3 +fi +if [ "${MS_SECRET}" = "secret" ]; then + MS_SECRET=$(echo "$(pwgen 40)") + if ( cat "$conf_path" | grep "MS_SECRET" >/dev/null ); then + sed -i "s@^MS_SECRET=.*@MS_SECRET='${MS_SECRET}'@" "$conf_path" + else + echo "MS_SECRET='${MS_SECRET}'" >> "$conf_path" + fi + echo -e "${YELLOW}The config MS_SECRET has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" + sleep 3 +fi diff --git a/3.New_server_deployment/2.Fill_empty_conf/0_setup.sh b/3.New_server_deployment/2.Fill_empty_conf/0_setup.sh deleted file mode 100755 index e8ac7039d4c178e83845aee3d9d5618f5f26a3a9..0000000000000000000000000000000000000000 --- a/3.New_server_deployment/2.Fill_empty_conf/0_setup.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -source /root/envsetup/global-conf.sh - -apt-get update -apt-get install -y -o Dpkg::Options::="--force-confold" pwgen - -# Autogenerate missing values -if [ "${MS_ID}" = "" ]; then - MS_ID=$(echo "$(hostname)_msuser") - if ( cat "/root/envsetup/conf.sh" | grep "MS_ID" >/dev/null ); then - sed -i "s@^MS_ID=.*@MS_ID='${MS_ID}'@" /root/envsetup/conf.sh - else - echo "MS_ID='${MS_ID}'" >> /root/envsetup/conf.sh - fi - echo "${YELLOW}The config MS_ID has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" - sleep 3 -fi -if [ "${MS_API_KEY}" = "" ]; then - MS_API_KEY=$(echo "s$(pwgen 4)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)") - # respect API pattern - MS_API_KEY=$(echo $MS_API_KEY | sed "s@[iloILO]@$((${RANDOM} / 10000))@g") - if ( cat "/root/envsetup/conf.sh" | grep "MS_API_KEY" >/dev/null ); then - sed -i "s@^MS_API_KEY=.*@MS_API_KEY='${MS_API_KEY}'@" /root/envsetup/conf.sh - else - echo "MS_API_KEY='${MS_API_KEY}'" >> /root/envsetup/conf.sh - fi - echo "${YELLOW}The config MS_API_KEY has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" - sleep 3 -fi -if [ "${MS_SECRET}" = "secret" ]; then - MS_SECRET=$(echo "$(pwgen 40)") - if ( cat "/root/envsetup/conf.sh" | grep "MS_SECRET" >/dev/null ); then - sed -i "s@^MS_SECRET=.*@MS_SECRET='${MS_SECRET}'@" /root/envsetup/conf.sh - else - echo "MS_SECRET='${MS_SECRET}'" >> /root/envsetup/conf.sh - fi - echo "${YELLOW}The config MS_SECRET has been set. If you forgot to fill it before, please change the value in the envsetup configuration.${NC}" - sleep 3 -fi diff --git a/3.New_server_deployment/3.Proxy_settings/0_setup.py b/3.New_server_deployment/2.Proxy_settings/0_setup.py similarity index 100% rename from 3.New_server_deployment/3.Proxy_settings/0_setup.py rename to 3.New_server_deployment/2.Proxy_settings/0_setup.py diff --git a/3.New_server_deployment/4.APT_upgrade/0_setup.sh b/3.New_server_deployment/3.APT_upgrade/0_setup.sh similarity index 100% rename from 3.New_server_deployment/4.APT_upgrade/0_setup.sh rename to 3.New_server_deployment/3.APT_upgrade/0_setup.sh diff --git a/3.New_server_deployment/4.APT_upgrade/sources.list b/3.New_server_deployment/3.APT_upgrade/sources.list similarity index 100% rename from 3.New_server_deployment/4.APT_upgrade/sources.list rename to 3.New_server_deployment/3.APT_upgrade/sources.list diff --git a/3.New_server_deployment/5.Dell_openmanage/0_setup.sh b/3.New_server_deployment/4.Dell_openmanage/0_setup.sh similarity index 100% rename from 3.New_server_deployment/5.Dell_openmanage/0_setup.sh rename to 3.New_server_deployment/4.Dell_openmanage/0_setup.sh diff --git a/launcher.sh b/launcher.sh index 529bc590e041495b747a95e9e96590fa745d94c6..800fe3c9c45515019d4cf2e65ee8c45fdb0b9554 100755 --- a/launcher.sh +++ b/launcher.sh @@ -29,7 +29,6 @@ init() { python3 -u /root/envsetup/envsetup.py 32 python3 -u /root/envsetup/envsetup.py 33 python3 -u /root/envsetup/envsetup.py 34 - python3 -u /root/envsetup/envsetup.py 35 python3 -u /root/envsetup/envsetup.py 11 python3 -u /root/envsetup/envsetup.py 12