From 15f85746cd4e1c61af01744e85b913818a71b042 Mon Sep 17 00:00:00 2001 From: Kload Date: Thu, 24 Oct 2013 16:29:07 +0000 Subject: [PATCH] Add PID file and Yunoception --- yunohost | 31 +++++++++++++++++++++++++++++++ yunohost.tac | 18 +++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/yunohost b/yunohost index e7195ba7..1585012a 100755 --- a/yunohost +++ b/yunohost @@ -64,6 +64,7 @@ def main(): admin_password_provided = False json_print = False + no_ldap = False for key, arg in enumerate(sys.argv): if arg == '--admin-password': @@ -71,6 +72,11 @@ def main(): admin_password = sys.argv[key+1] sys.argv.pop(key) sys.argv.pop(key) + + if arg == '--no-ldap': + no_ldap = True + sys.argv.pop(key) + if arg == '--json': json_print = True sys.argv.pop(key) @@ -90,9 +96,34 @@ def main(): args_dict[sanitized_key] = args_dict[key] del args_dict[key] del args_dict['func'] + try: + with open('/etc/yunohost/passwd') as f: + admin_password = f.read() + admin_password_provided = True + except IOError: pass if admin_password_provided: with YunoHostLDAP(password=admin_password): result = args.func(**args_dict) + elif os.isatty(1) and not no_ldap: + admin_password = getpass.getpass(colorize(_('Admin Password: '), 'yellow')) + with YunoHostLDAP(password=admin_password): + try: + with open('/var/run/yunohost.pid', 'r'): + raise YunoHostError(1, _("A YunoHost command is already running")) + except IOError: + with open('/var/run/yunohost.pid', 'w') as f: + f.write('ldap') + os.system('chmod 400 /var/run/yunohost.pid') + with open('/etc/yunohost/passwd', 'w') as f: + f.write(admin_password) + os.system('chmod 400 /etc/yunohost/passwd') + try: + result = args.func(**args_dict) + except KeyboardInterrupt, EOFError: + raise YunoHostError(125, _("Interrupted")) + finally: + os.remove('/etc/yunohost/passwd') + os.remove('/var/run/yunohost.pid') else: result = args.func(**args_dict) #except TypeError, error: diff --git a/yunohost.tac b/yunohost.tac index 324f6f31..2d752f6e 100755 --- a/yunohost.tac +++ b/yunohost.tac @@ -115,7 +115,23 @@ def http_exec(request, **kwargs): # Execute requested function with YunoHostLDAP(password=request.getPassword()): - result = func(**validated_args) + try: + with open('/var/run/yunohost.pid', 'r'): + raise YunoHostError(1, _("A YunoHost command is already running")) + exceOError: + with open('/var/run/yunohost.pid', 'w') as f: + f.write('ldap') + os.system('chmod 400 /var/run/yunohost.pid') + with open('/etc/yunohost/passwd', 'w') as f: + f.write(admin_password) + os.system('chmod 400 /etc/yunohost/passwd') + try: + result = func(**validated_args) + except KeyboardInterrupt, EOFError: + raise YunoHostError(125, _("Interrupted")) + finally: + os.remove('/etc/yunohost/passwd') + os.remove('/var/run/yunohost.pid') if result is None: result = {} if len(yunohost.win) > 0: