mtes-scripts/scripts/module/set_var.py

76 lines
1.9 KiB
Python
Raw Normal View History

2021-05-07 11:22:59 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2021-06-30 16:43:06 +02:00
import sys, getpass, socket, cjson, os
2021-05-07 11:22:59 +02:00
from pyeole.ihm import print_line
from pyeole.ansiprint import print_orange, print_red
from creole.client import CreoleClient
from collections import OrderedDict
def display_help():
print("Modification de la valeur d'une variable d'un serveur")
2021-06-30 16:43:06 +02:00
print("{} [--var nom_variable --value valeur --save]".format(sys.argv[0]))
2021-05-07 11:22:59 +02:00
def argparser():
# récupère l'ID du groupe
arglen = len(sys.argv)
2021-06-30 16:43:06 +02:00
save = False
if "--save" in sys.argv:
save = True
2021-05-07 11:22:59 +02:00
if arglen == 1:
2021-06-30 16:43:06 +02:00
return None, None, save
2021-05-07 11:22:59 +02:00
if sys.argv[1] in ['-h', '--help']:
display_help()
sys.exit(0)
2021-05-10 15:40:55 +02:00
if sys.argv[1] in ['-v', '--var']:
varc = sys.argv[2]
2021-06-30 16:43:06 +02:00
if sys.argv[3] in ['-l', '--value']:
valvar = sys.argv[4]
return varc, valvar, save
else:
return varc, None, save
2021-05-07 11:22:59 +02:00
2021-06-30 16:43:06 +02:00
return None, None, save
2021-05-07 11:22:59 +02:00
def main():
# import des fonctions communes de Zéphir client
2021-06-30 16:43:06 +02:00
varc, valvar, save = argparser()
2021-05-07 11:22:59 +02:00
2021-07-05 10:13:40 +02:00
fh = open("/etc/eole/config.eol", 'r')
store = cjson.decode(fh.read(), all_unicode=True)
fh.close
2021-05-07 11:22:59 +02:00
if varc is None :
2021-07-05 10:13:40 +02:00
print
print('Liste des variables modifiables :')
for var in store:
print(' - ' + var)
2021-05-10 15:40:55 +02:00
varc = raw_input("Variable à modifier : ")
2021-05-07 11:22:59 +02:00
if valvar is None :
2021-05-10 15:40:55 +02:00
valvar = raw_input("Valeur de la variable : ")
if varc in store:
2021-06-30 16:43:06 +02:00
if "|" in valvar:
valvar = valvar.split("|")
2021-05-10 15:40:55 +02:00
store[varc]['val'] = valvar
2021-06-30 15:13:29 +02:00
fh = open("/etc/eole/config.eol", 'w')
fh.write(cjson.encode(store))
fh.close()
2021-06-30 16:43:06 +02:00
if save:
cmd="""/usr/share/zephir/scripts/zephir_client save_files"""
output = os.popen(cmd)
output.close()
2021-06-30 15:13:29 +02:00
else:
print_orange('Erreur : variable {} inconnue'.format(varc))
2021-05-07 11:22:59 +02:00
if __name__ == '__main__':
main()