Skip to content
Snippets Groups Projects
Commit 1c3e11a9 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Use API key if available to get envsetup configuration | refs #29237

parent cebd350d
No related branches found
No related tags found
No related merge requests found
#!/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)
#!/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
#!/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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment