update set_var
This commit is contained in:
parent
e2dbd9183b
commit
78e5ce410b
@ -1,114 +1,59 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys, getpass, socket
|
||||
import sys, getpass, socket, cjson
|
||||
from pyeole.ihm import print_line
|
||||
from pyeole.ansiprint import print_orange, print_red
|
||||
from creole.client import CreoleClient
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
try:
|
||||
from zephir.zephir_conf.zephir_conf import adresse_zephir
|
||||
except:
|
||||
client = CreoleClient()
|
||||
adresse_zephir = client.get_creole('nom_domaine_machine')
|
||||
from zephir.lib_zephir import convert
|
||||
from zephir.lib_zephir import xmlrpclib
|
||||
from zephir.lib_zephir import EoleProxy
|
||||
from zephir.lib_zephir import TransportEole
|
||||
from zephir.lib_zephir import flushed_input
|
||||
|
||||
|
||||
def display_help():
|
||||
print("Modification de la valeur d'une variable d'un serveur")
|
||||
print("{} [numero du serveur] [--var nom_variable --value valeur]".format(sys.argv[0]))
|
||||
print("{} [--var nom_variable --value valeur]".format(sys.argv[0]))
|
||||
|
||||
|
||||
def argparser():
|
||||
# récupère l'ID du groupe
|
||||
arglen = len(sys.argv)
|
||||
if arglen == 1:
|
||||
return None, None, None
|
||||
return None, None
|
||||
if sys.argv[1] in ['-h', '--help']:
|
||||
display_help()
|
||||
sys.exit(0)
|
||||
try:
|
||||
server_id = int(sys.argv[1])
|
||||
except:
|
||||
print_red("\"{}\" n'est pas un ID de serveur valide".format(sys.argv[1]))
|
||||
display_help()
|
||||
sys.exit(1)
|
||||
|
||||
if sys.argv[2] in ['-v', '--var']:
|
||||
varc = sys.argv[3]
|
||||
return server_id, varc, None
|
||||
|
||||
if sys.argv[4] in ['-l', '--value']:
|
||||
valvar = sys.argv[5]
|
||||
return server_id, varc, valvar
|
||||
if sys.argv[1] in ['-v', '--var']:
|
||||
varc = sys.argv[2]
|
||||
return varc, None
|
||||
|
||||
return server_id, None, None
|
||||
if sys.argv[3] in ['-l', '--value']:
|
||||
valvar = sys.argv[4]
|
||||
return varc, valvar
|
||||
|
||||
return None, None
|
||||
|
||||
|
||||
def main():
|
||||
# import des fonctions communes de Zéphir client
|
||||
server_id, varc, valvar = argparser()
|
||||
|
||||
authentified, proxy = get_pwd(adresse_zephir, 7080)
|
||||
if authentified == False:
|
||||
sys.exit(1)
|
||||
|
||||
if server_id is None :
|
||||
server_id = flushed_input("Identifiant du serveur : ")
|
||||
varc, valvar = argparser()
|
||||
|
||||
|
||||
if varc is None :
|
||||
varc = flushed_input("Variable à modifier : ")
|
||||
varc = raw_input("Variable à modifier : ")
|
||||
|
||||
if valvar is None :
|
||||
valvar = flushed_input("Valeur de la variable : ")
|
||||
valvar = raw_input("Valeur de la variable : ")
|
||||
|
||||
rc, config = proxy.serveurs.get_dico(server_id, 'modif_config')
|
||||
values = eval(config[-1])
|
||||
fh = open("/etc/eole/config.eol", 'r')
|
||||
store = cjson.decode(fh.read(), all_unicode=True)
|
||||
fh.close
|
||||
if varc in store:
|
||||
store[varc]['val'] = valvar
|
||||
|
||||
if varc in values:
|
||||
values[varc]['val'] = valvar
|
||||
|
||||
rc, msg = proxy.serveurs.save_conf(server_id, [str(values)], 'modif_config')
|
||||
if rc == 1:
|
||||
print("OK")
|
||||
else:
|
||||
print("Erreur remontée par Zéphir : ", str(msg))
|
||||
|
||||
|
||||
|
||||
def get_pwd(addr, port):
|
||||
"""lecture d'un login/passwd pour l'application zephir
|
||||
"""
|
||||
login_ok = 0
|
||||
user = "toto"
|
||||
while login_ok == 0 and user != "":
|
||||
try:
|
||||
# flush de l'entrée standard au cas où l'utilisateur aurait
|
||||
# tapé <entrée> pendant l'Upgrade
|
||||
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
|
||||
except:
|
||||
pass
|
||||
user = flushed_input("Entrez votre login zephir (rien pour sortir) : ")
|
||||
if user != "":
|
||||
passwd = getpass.getpass("Mot de passe zephir pour %s : " % user)
|
||||
# création du proxy avec zephir
|
||||
proxy = EoleProxy("https://%s:%s@%s:%s" % (user, passwd, addr, port), transport=TransportEole())
|
||||
login_ok = 1
|
||||
try:
|
||||
res = convert(proxy.get_permissions(user))
|
||||
except xmlrpclib.ProtocolError:
|
||||
login_ok = 0
|
||||
print_line("\n Erreur d'authentification \n")
|
||||
else:
|
||||
return False, "! Abandon de la procédure !"
|
||||
return True, proxy
|
||||
fh = open("/etc/eole/config.eol", 'w')
|
||||
fh.write(cjson.encode(store))
|
||||
fh.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user