Пример #1
0
 def _get_config(self, user):
     cmd = "cat %s/%s.server" % (self.cfg_dir, user)
     res = utils.run_remote_cmd(self.host, self.user, self.key, cmd)
     if res['stderr']:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + res['stderr'],
             'data': None
         }
     text = res['stdout']
     data = {}
     for line in text.split('\n'):
         line.strip()
         if line.startswith('listen'):
             data['port'] = line.split()[1]
             data['proxy'] = 'http://' + self.external + ':' + data['port']
         if line.startswith('proxy_pass'):
             m = re.search(r'http://(.+):(\d+)', line)
             if m:
                 data['ip'] = m.group(1)
                 data['ipython'] = m.group(0)
     if not data:
         return {
             'status': 500,
             'error':
             'Internal Server Error: invalid proxy config for %s' % user,
             'data': None
         }
     data['user'] = user
     data['config'] = text
     return {'status': 200, 'data': data}
Пример #2
0
 def add_server(self, user, ip, port):
     server_cfg = self.template.format(port=port,
                                       ip=ip,
                                       ipy_port=self.pport)
     cmd = 'sudo echo "%s" > %s/%s.server' % (server_cfg, self.cfg_dir,
                                              user)
     res = utils.run_remote_cmd(self.host, self.user, self.key, cmd)
     if res['stderr']:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + res['stderr'],
             'data': None
         }
     res = self._relaod_config()
     if res['status'] != 200:
         return res
     return {
         'status': 200,
         'data': {
             'user': user,
             'ip': ip,
             'port': ip,
             'config': server_cfg
         }
     }
Пример #3
0
 def remove_server(self, user):
     cmd = "sudo rm -f %s/%s.server" % (self.cfg_dir, user)
     res = utils.run_remote_cmd(self.host, self.user, self.key, cmd)
     if res['stderr']:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + res['stderr'],
             'data': None
         }
     return {'status': 200, 'data': 'success'}
Пример #4
0
 def _relaod_config(self):
     cmd = "sudo cat %s/*.server > %s/%s; sudo /etc/init.d/nginx reload" % (
         self.cfg_dir, self.cfg_dir, self.cfg_file)
     res = utils.run_remote_cmd(self.host, self.user, self.key, cmd)
     if res['stderr']:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + res['stderr'],
             'data': None
         }
     return {'status': 200, 'data': 'success'}
Пример #5
0
def api_ipy(vmid):
    # authenticate through db connection
    try_ipydb = get_ipydb(request, True)
    if try_ipydb['status'] != 200:
        return return_json(None, try_ipydb['error'], try_ipydb['status'])
    ipydb = try_ipydb['data']
    ipycfg = dict(cfg.items('ipython'))
    vminfo = ipydb.get('id', vmid)
    if not vminfo:
        return return_json(
            None,
            "Internal Server Error: data not available for VM '%s'" % vmid,
            500)
    vmkey = os.path.join(cfg.get("ipyno", "sshdir"), vminfo['vm_key'] + '.pem')
    if not os.path.isfile(vmkey):
        return return_json(
            None, 'Internal Server Error: missing private key (%s) for VM %s' %
            (vminfo['vm_key'], vmid), 500)
    # run init script for ipython on givien vm
    if request.method == 'POST':
        build = request.args['build'] if 'build' in request.args else 'none'
        if build not in ['none', 'ipython', 'all']:
            return return_json(
                None,
                "Bad Request: 'build' must be one of: none, ipython, all", 400)
        cmd = 'sudo %s/%s -b %s -s %s' % (ipycfg['init_dir'],
                                          ipycfg['init_script'], build,
                                          cfg.get("shock", "url"))
        ipy_status = ['initalized', True]
    # stop then start ipython on givien vm (used for both restart and start)
    elif request.method == 'PUT':
        cmd = 'sudo %s/%s; sleep 3; sudo %s/%s -a %s -s %s' % (
            ipycfg['run_dir'], ipycfg['stop_script'],
            ipycfg['run_dir'], ipycfg['start_script'], cfg.get(
                "shock", "auth"), cfg.get("shock", "url"))
        ipy_status = ['restarted', True]
    # stop ipython on givien vm
    elif request.method == 'DELETE':
        cmd = 'sudo %s/%s' % (ipycfg['run_dir'], ipycfg['stop_script'])
        ipy_status = ['stoped', False]
    else:
        return return_json(
            None,
            'Method Not Allowed (%s): %s' % (request.method, request.url), 405)
    res = utils.run_remote_cmd(vminfo['vm_ip'], ipycfg['user'], vmkey, cmd)
    if res['stderr']:
        return return_json(None, 'Internal Server Error: %s' % res['stderr'],
                           500)
    ipydb.update(vmid, ipy=ipy_status[1])
    ipydb.exit()
    return return_json('%s ipython on %s (%s)' %
                       (ipy_status[0], vminfo['vm_name'], vmid))
Пример #6
0
 def _list_users(self):
     cmd = "ls %s/*.server" % (self.cfg_dir)
     res = utils.run_remote_cmd(self.host, self.user, self.key, cmd)
     if res['stderr']:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + res['stderr'],
             'data': None
         }
     try:
         files = res['stdout'].strip().split('\n')
         users = map(lambda x: basename(x).split('.')[0], files)
         return {'status': 200, 'data': users}
     except:
         return {
             'status': 500,
             'error': 'Internal Server Error: ' + sys.exc_info()[0].__doc__,
             'data': None
         }
Пример #7
0
def api_ipy(vmid):
    # authenticate through db connection
    try_ipydb = get_ipydb(request, True)
    if try_ipydb['status'] != 200:
        return return_json(None, try_ipydb['error'], try_ipydb['status'])
    ipydb  = try_ipydb['data']
    ipycfg = dict(cfg.items('ipython'))
    vminfo = ipydb.get('id', vmid)
    if not vminfo:
        return return_json(None, "Internal Server Error: data not available for VM '%s'"%vmid, 500)
    vmkey = os.path.join(cfg.get("ipyno", "sshdir"), vminfo['vm_key']+'.pem')
    if not os.path.isfile(vmkey):
        return return_json(None, 'Internal Server Error: missing private key (%s) for VM %s'%(vminfo['vm_key'], vmid), 500)
    # run init script for ipython on givien vm
    if request.method == 'POST':
        build = request.args['build'] if 'build' in request.args else 'none'
        if build not in ['none', 'ipython', 'all']:
            return return_json(None, "Bad Request: 'build' must be one of: none, ipython, all", 400)
        cmd = 'sudo %s/%s -b %s -s %s'%(ipycfg['init_dir'], ipycfg['init_script'], build, cfg.get("shock", "url"))
        ipy_status = ['initalized', True]
    # stop then start ipython on givien vm (used for both restart and start)
    elif request.method == 'PUT':
        cmd = 'sudo %s/%s; sleep 3; sudo %s/%s -a %s -s %s'%(ipycfg['run_dir'], ipycfg['stop_script'], ipycfg['run_dir'], ipycfg['start_script'], cfg.get("shock", "auth"), cfg.get("shock", "url"))
        ipy_status = ['restarted', True]
    # stop ipython on givien vm 
    elif request.method == 'DELETE':
        cmd = 'sudo %s/%s'%(ipycfg['run_dir'], ipycfg['stop_script'])
        ipy_status = ['stoped', False]
    else:
        return return_json(None, 'Method Not Allowed (%s): %s'%(request.method, request.url), 405)
    res = utils.run_remote_cmd(vminfo['vm_ip'], ipycfg['user'], vmkey, cmd)
    if res['stderr']:
        return return_json(None, 'Internal Server Error: %s'%res['stderr'], 500)
    ipydb.update(vmid, ipy=ipy_status[1])
    ipydb.exit()
    return return_json('%s ipython on %s (%s)'%(ipy_status[0], vminfo['vm_name'], vmid))