moulinette/yunohost

135 lines
4.3 KiB
Text
Raw Normal View History

2012-10-06 17:57:08 +02:00
#!/usr/bin/env python
2012-10-06 16:22:15 +02:00
# -*- coding: utf-8 -*-
2012-10-08 18:16:43 +02:00
import os
2012-10-06 16:22:15 +02:00
import sys
import argparse
2012-10-07 16:20:20 +02:00
import gettext
2012-11-08 19:20:13 +01:00
import getpass
2012-10-28 15:55:35 +01:00
try:
import yaml
except ImportError:
sys.stderr.write('Error: Yunohost CLI Require yaml lib\n')
sys.stderr.write('apt-get install python-yaml\n')
sys.exit(1)
2012-10-08 18:16:43 +02:00
import json
if not __debug__:
import traceback
2012-10-07 16:20:20 +02:00
gettext.install('YunoHost')
2012-10-07 13:16:19 +02:00
2012-10-10 16:01:54 +02:00
try:
2013-09-23 19:13:24 +02:00
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate, win, parse_dict
2012-10-10 16:01:54 +02:00
except ImportError:
2012-10-16 14:56:54 +02:00
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
2012-10-10 16:01:54 +02:00
sys.exit(1)
2012-10-08 20:46:52 +02:00
2012-10-06 16:22:15 +02:00
2012-10-22 20:52:15 +02:00
def main():
2012-10-28 15:55:35 +01:00
"""
2013-03-03 01:18:28 +01:00
Main instructions
2012-10-28 15:55:35 +01:00
2013-03-03 01:18:28 +01:00
Parse the action_dict and execute the action-specific function,
then print json or pretty result if executed in a tty :)
2012-10-14 17:40:13 +02:00
2013-03-03 01:18:28 +01:00
Returns:
int -- 0 or error code
2012-10-28 15:55:35 +01:00
2013-03-03 01:18:28 +01:00
"""
2013-09-23 18:35:17 +02:00
2013-02-10 20:31:29 +01:00
if len(sys.argv) < 2:
sys.argv.append('-h')
2012-10-22 20:52:15 +02:00
with open('action_map.yml') as f:
action_map = yaml.load(f)
2013-01-26 21:34:02 +01:00
admin_password_provided = False
2013-03-13 13:22:27 +01:00
json_print = False
2013-10-24 18:49:40 +02:00
write_ldap = True
2013-10-26 23:37:01 +02:00
postinstall = False
2013-01-26 21:34:02 +01:00
for key, arg in enumerate(sys.argv):
if arg == '--admin-password':
admin_password_provided = True
2013-02-10 20:31:29 +01:00
admin_password = sys.argv[key+1]
2013-01-26 21:34:02 +01:00
sys.argv.pop(key)
sys.argv.pop(key)
2013-10-24 18:29:07 +02:00
if arg == '--no-ldap':
2013-10-24 18:49:40 +02:00
write_ldap = False
2013-10-24 18:29:07 +02:00
sys.argv.pop(key)
2013-03-13 13:21:23 +01:00
if arg == '--json':
json_print = True
sys.argv.pop(key)
2013-01-26 21:34:02 +01:00
2012-10-08 18:16:43 +02:00
try:
2013-06-13 14:20:22 +02:00
try:
with open('/etc/yunohost/installed') as f: pass
except IOError:
2013-10-26 22:56:26 +02:00
postinstall = True
2013-06-13 14:27:34 +02:00
if len(sys.argv) < 3 or sys.argv[1] != 'tools' or sys.argv[2] != 'postinstall':
2013-06-13 14:23:39 +02:00
raise YunoHostError(17, _("YunoHost is not correctly installed, please execute 'yunohost tools postinstall'"))
2013-06-13 14:20:22 +02:00
2012-11-08 19:20:13 +01:00
args = parse_dict(action_map)
2012-12-01 15:33:56 +01:00
args_dict = vars(args).copy()
for key in args_dict.keys():
sanitized_key = key.replace('-', '_')
if sanitized_key is not key:
args_dict[sanitized_key] = args_dict[key]
del args_dict[key]
del args_dict['func']
2013-10-24 18:29:07 +02:00
try:
with open('/etc/yunohost/passwd') as f:
admin_password = f.read()
admin_password_provided = True
except IOError: pass
2013-10-26 22:56:26 +02:00
if postinstall:
result = args.func(**args_dict)
elif admin_password_provided:
2013-01-26 21:34:02 +01:00
with YunoHostLDAP(password=admin_password):
result = args.func(**args_dict)
2013-10-24 18:49:40 +02:00
elif os.isatty(1) and write_ldap:
2013-10-24 18:29:07 +02:00
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')
2013-01-26 21:34:02 +01:00
else:
2013-10-24 18:49:40 +02:00
with YunoHostLDAP(anonymous=True):
result = args.func(**args_dict)
2013-02-12 13:52:11 +01:00
#except TypeError, error:
#if not __debug__ :
#traceback.print_exc()
#print(_("Not (yet) implemented function"))
#return 1
2012-10-08 18:16:43 +02:00
except YunoHostError, error:
2013-03-15 16:59:00 +01:00
display_error(error, json_print)
2012-10-08 18:16:43 +02:00
return error.code
2012-12-01 15:33:56 +01:00
else:
2013-10-29 18:32:49 +01:00
if json_print or not os.isatty(1) and result is not None:
2013-03-15 16:59:00 +01:00
if len(win) > 0:
2013-09-23 18:35:17 +02:00
result['success'] = win
2013-03-15 16:59:00 +01:00
print(json.dumps(result))
elif result is not None:
2012-10-10 14:19:17 +02:00
pretty_print_dict(result)
2012-10-08 18:16:43 +02:00
else:
2013-03-15 16:59:00 +01:00
pass
2012-10-14 17:40:13 +02:00
2012-10-28 15:55:35 +01:00
return 0
2012-10-07 13:16:19 +02:00
2012-10-06 16:22:15 +02:00
if __name__ == '__main__':
2012-10-22 20:52:15 +02:00
sys.exit(main())