chg: [config update] refractor, auto update, create config if file doesnt exist

pull/342/head
Terrtia 2019-04-23 15:04:45 +02:00
parent 4ecff21194
commit 35c188829f
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
4 changed files with 78 additions and 115 deletions

1
.gitignore vendored
View File

@ -34,6 +34,7 @@ var/www/submitted
bin/packages/config.cfg bin/packages/config.cfg
bin/packages/config.cfg.backup bin/packages/config.cfg.backup
configs/keys configs/keys
configs/update.cfg
update/current_version update/current_version
files files

View File

@ -125,12 +125,7 @@ function launching_queues {
function checking_configuration { function checking_configuration {
bin_dir=${AIL_HOME}/bin bin_dir=${AIL_HOME}/bin
echo -e "\t* Checking configuration" echo -e "\t* Checking configuration"
if [ "$1" == "automatic" ]; then bash -c "${ENV_PY} $bin_dir/Update-conf.py"
bash -c "${ENV_PY} $bin_dir/Update-conf.py True"
else
bash -c "${ENV_PY} $bin_dir/Update-conf.py False"
fi
exitStatus=$? exitStatus=$?
if [ $exitStatus -ge 1 ]; then if [ $exitStatus -ge 1 ]; then
echo -e $RED"\t* Configuration not up-to-date"$DEFAULT echo -e $RED"\t* Configuration not up-to-date"$DEFAULT
@ -140,7 +135,7 @@ function checking_configuration {
} }
function launching_scripts { function launching_scripts {
checking_configuration $1; checking_configuration;
screen -dmS "Script_AIL" screen -dmS "Script_AIL"
sleep 0.1 sleep 0.1
@ -359,14 +354,14 @@ function launch_scripts {
if [[ ! $isscripted ]]; then if [[ ! $isscripted ]]; then
sleep 1 sleep 1
if checking_ardb && checking_redis; then if checking_ardb && checking_redis; then
launching_scripts $1; launching_scripts;
else else
no_script_launched=true no_script_launched=true
while $no_script_launched; do while $no_script_launched; do
echo -e $YELLOW"\tScript not started, waiting 5 more secondes"$DEFAULT echo -e $YELLOW"\tScript not started, waiting 5 more secondes"$DEFAULT
sleep 5 sleep 5
if checking_redis && checking_ardb; then if checking_redis && checking_ardb; then
launching_scripts $1; launching_scripts;
no_script_launched=false no_script_launched=false
else else
echo -e $RED"\tScript not started"$DEFAULT echo -e $RED"\tScript not started"$DEFAULT
@ -457,7 +452,7 @@ function launch_all {
launch_ardb; launch_ardb;
launch_logs; launch_logs;
launch_queues; launch_queues;
launch_scripts $1; launch_scripts;
launch_flask; launch_flask;
} }
@ -521,7 +516,7 @@ function launch_all {
update; update;
;; ;;
Update-config) Update-config)
checking_configuration "manual"; checking_configuration;
;; ;;
Update-thirdparty) Update-thirdparty)
update_thirdparty; update_thirdparty;

View File

@ -1,123 +1,90 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*-coding:UTF-8 -* # -*-coding:UTF-8 -*
import configparser
from configparser import ConfigParser as cfgP
import os import os
from collections import OrderedDict
import sys import sys
import shutil import argparse
import configparser
def print_message(message_to_print, verbose):
if verbose:
print(message_to_print)
def update_config(config_file, config_file_sample, config_file_backup=False):
verbose = True
# Check if confile file exist
if not os.path.isfile(config_file):
# create config file
with open(config_file, 'w') as configfile:
with open(config_file_sample, 'r') as config_file_sample:
configfile.write(config_file_sample.read())
print_message('Config File Created', verbose)
else:
config_server = configparser.ConfigParser()
config_server.read(config_file)
config_sections = config_server.sections()
config_sample = configparser.ConfigParser()
config_sample.read(config_file_sample)
sample_sections = config_sample.sections()
mew_content_added = False
for section in sample_sections:
new_key_added = False
if section not in config_sections:
# add new section
config_server.add_section(section)
mew_content_added = True
for key in config_sample[section]:
if key not in config_server[section]:
# add new section key
config_server.set(section, key, config_sample[section][key])
if not new_key_added:
print_message('[{}]'.format(section), verbose)
new_key_added = True
mew_content_added = True
print_message(' {} = {}'.format(key, config_sample[section][key]), verbose)
# new keys have been added to config file
if mew_content_added:
# backup config file
if config_file_backup:
with open(config_file_backup, 'w') as configfile:
with open(config_file, 'r') as configfile_origin:
configfile.write(configfile_origin.read())
print_message('New Backup Created', verbose)
# create new config file
with open(config_file, 'w') as configfile:
config_server.write(configfile)
print_message('Config file updated', verbose)
else:
print_message('Nothing to update', verbose)
#return true if the configuration is up-to-date #return true if the configuration is up-to-date
def main(): def main():
if len(sys.argv) != 2:
print('usage:', 'Update-conf.py', 'Automatic (boolean)')
exit(1)
else:
automatic = sys.argv[1]
if automatic == 'True':
automatic = True
else:
automatic = False
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg') #------------------------------------------------------------------------------------#
configfileBackup = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg') + '.backup'
if not os.path.exists(configfile): config_file_default = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
config_file_default_sample = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg.sample')
config_file_default_backup = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg.backup')
config_file_update = os.path.join(os.environ['AIL_HOME'], 'configs/update.cfg')
config_file_update_sample = os.path.join(os.environ['AIL_HOME'], 'configs/update.cfg.sample')
if not os.path.exists(config_file_default_sample):
raise Exception('Unable to find the configuration file. \ raise Exception('Unable to find the configuration file. \
Did you set environment variables? \ Did you set environment variables? \
Or activate the virtualenv.') Or activate the virtualenv.')
configfileSample = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg.sample')
cfg = configparser.ConfigParser()
cfg.read(configfile)
cfgSample = configparser.ConfigParser()
cfgSample.read(configfileSample)
sections = cfgP.sections(cfg) update_config(config_file_default, config_file_default_sample, config_file_backup=config_file_default_backup)
sectionsSample = cfgP.sections(cfgSample) update_config(config_file_update, config_file_update_sample)
missingSection = []
dicoMissingSection = {}
missingItem = []
dicoMissingItem = {}
for sec in sectionsSample:
if sec not in sections:
missingSection += [sec]
dicoMissingSection[sec] = cfgP.items(cfgSample, sec)
else:
setSample = set(cfgP.options(cfgSample, sec))
setNormal = set(cfgP.options(cfg, sec))
if setSample != setNormal:
missing_items = list(setSample.difference(setNormal))
missingItem += [sec]
list_items = []
for i in missing_items:
list_items.append( (i, cfgSample.get(sec, i)) )
dicoMissingItem[sec] = list_items
if len(missingSection) == 0 and len(missingItem) == 0:
#print("Configuration up-to-date")
return True return True
print("/!\\ Configuration not complete. Missing following configuration: /!\\")
print("+--------------------------------------------------------------------+")
for section in missingSection:
print("["+section+"]")
for item in dicoMissingSection[section]:
print(" - "+item[0])
for section in missingItem:
print("["+section+"]")
for item in dicoMissingItem[section]:
print(" - "+item[0])
print("+--------------------------------------------------------------------+")
if automatic:
resp = 'y'
else:
resp = input("Do you want to auto fix it? [y/n] ")
if resp != 'y':
return False
else:
if automatic:
resp2 = 'y'
else:
resp2 = input("Do you want to keep a backup of the old configuration file? [y/n] ")
if resp2 == 'y':
shutil.move(configfile, configfileBackup)
#Do not keep item ordering in section. New items appened
for section in missingItem:
for item, value in dicoMissingItem[section]:
cfg.set(section, item, value)
#Keep sections ordering while updating the config file
new_dico = add_items_to_correct_position(cfgSample._sections, cfg._sections, missingSection, dicoMissingSection)
cfg._sections = new_dico
with open(configfile, 'w') as f:
cfg.write(f)
return True
''' Return a new dico with the section ordered as the old configuration with the updated one added '''
def add_items_to_correct_position(sample_dico, old_dico, missingSection, dicoMissingSection):
new_dico = OrderedDict()
positions = {}
for pos_i, sec in enumerate(sample_dico):
if sec in missingSection:
positions[pos_i] = sec
for pos_i, sec in enumerate(old_dico):
if pos_i in positions:
missSection = positions[pos_i]
new_dico[missSection] = sample_dico[missSection]
new_dico[sec] = old_dico[sec]
return new_dico
if __name__ == "__main__": if __name__ == "__main__":