Ejemplo n.º 1
0
    def update_environment(self, appid):
        env = ''

        # Find mysql ip address
        try:
            sid = Service.query.filter_by(application_id=appid,
                                          type='mysql').first().sid
            nodes = callmanager(sid, "list_nodes", False, {})

            params = {'serviceNodeId': nodes['masters'][0]}
            details = callmanager(sid, "get_node_info", False, params)
            env = env + 'echo "env[MYSQL_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details[
                'serviceNode']['ip']
            env = env + 'export MYSQL_IP=\'%s\'\n' % details['serviceNode'][
                'ip']
        except:
            env = env + ''

        # Find xtreemfs ip address
        try:
            sid = Service.query.filter_by(application_id=appid,
                                          type='xtreemfs').first().sid
            nodes = callmanager(sid, "list_nodes", False, {})

            params = {'serviceNodeId': nodes['dir'][0]}
            details = callmanager(sid, "get_node_info", False, params)
            env = env + 'echo "env[XTREEMFS_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details[
                'serviceNode']['ip']
            env = env + 'export XTREEMFS_IP=\'%s\'\n' % details['serviceNode'][
                'ip']
        except:
            env = env + ''

        return env
Ejemplo n.º 2
0
    def update_environment(self, appid):
        env = ''

        # Find mysql ip address
        try:
            sid = Service.query.filter_by(application_id=appid, type='mysql').first().sid
            nodes = callmanager(sid, "list_nodes", False, {})

            params = { 'serviceNodeId': nodes['masters'][0] }
            details = callmanager(sid, "get_node_info", False, params)
            env = env + 'echo "env[MYSQL_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details['serviceNode']['ip']
            env = env + 'export MYSQL_IP=\'%s\'\n' % details['serviceNode']['ip']
        except:
            env = env + ''

        # Find xtreemfs ip address
        try:
            sid = Service.query.filter_by(application_id=appid, type='xtreemfs').first().sid
            nodes = callmanager(sid, "list_nodes", False, {})

            params = { 'serviceNodeId': nodes['dir'][0] }
            details = callmanager(sid, "get_node_info", False, params)
            env = env + 'echo "env[XTREEMFS_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details['serviceNode']['ip']
            env = env + 'export XTREEMFS_IP=\'%s\'\n' % details['serviceNode']['ip']
        except:
            env = env + ''

        return env
Ejemplo n.º 3
0
    def get_archive(self, service_id):
        res = callmanager(service_id, 'list_code_versions', False, {})
        if 'error' in res:
            return ''

        version = ''
        filename = ''
        for row in res['codeVersions']:
            if 'current' in row:
                version = row['codeVersionId']
                filename = row['filename']
                break

        if version == '' or filename == '':
            return ''

        params = { 'codeVersionId': version }

        res = callmanager(service_id, "download_code_version", False, params)
        if 'error' in res:
            return ''

        _, temp_path = mkstemp(suffix=filename, dir=get_userdata_dir())
        open(temp_path, 'w').write(res)

        return '%s/download_data/%s' % (get_director_url(), basename(temp_path))
Ejemplo n.º 4
0
    def get_archive(self, service_id):
        res = callmanager(service_id, 'list_code_versions', False, {})
        if 'error' in res:
            return ''

        version = ''
        filename = ''
        for row in res['codeVersions']:
            if 'current' in row:
                version = row['codeVersionId']
                filename = row['filename']
                break

        if version == '' or filename == '':
            return ''

        params = {'codeVersionId': version}

        res = callmanager(service_id, "download_code_version", False, params)
        if 'error' in res:
            return ''

        _, temp_path = mkstemp(suffix=filename, dir=get_userdata_dir())
        open(temp_path, 'w').write(res)

        return '%s/download_data/%s' % (get_director_url(),
                                        basename(temp_path))
Ejemplo n.º 5
0
    def shutdown(self, service_id):
        res = callmanager(service_id, "get_service_info", False, {})

        if res['state'] == "RUNNING":
            res = callmanager(service_id, "shutdown", True, {})
        else:
            log("Service is in '%(state)s' state. We can not stop it." % res)

        return res
Ejemplo n.º 6
0
    def shutdown(self, service_id):
        res = callmanager(service_id, "get_service_info", False, {})

        if res['state'] == "RUNNING":
            res = callmanager(service_id, "shutdown", True, {})
        else:
            log("Service is in '%(state)s' state. We can not stop it." % res)

        return res
Ejemplo n.º 7
0
    def set_persistent(self, service_id):
        res = callmanager(service_id, 'get_service_info', False, {})

        if res['persistent']:
            log('Service %s is already persistent' % service_id)
        else:
            res = callmanager(service_id, 'toggle_persistent', True, {})
            log('Service %s is now persistent' % service_id)

        return res['persistent']
Ejemplo n.º 8
0
    def set_persistent(self, service_id):
        res = callmanager(service_id, 'get_service_info', False, {})

        if res['persistent']:
            log('Service %s is already persistent' % service_id)
        else:
            res = callmanager(service_id, 'toggle_persistent', True, {})
            log('Service %s is now persistent' % service_id)

        return res['persistent']
Ejemplo n.º 9
0
def deleteapp(user_id, app_id):
    app = get_app_by_id(user_id, app_id)
    if not app:
        return False

    # If an application with id 'app_id' exists and user is the owner
    for service in Service.query.filter_by(application_id=app_id):
        callmanager(service.sid, "shutdown", True, {})
        stop(service.sid)

    db.session.delete(app)
    db.session.commit()

    return True
Ejemplo n.º 10
0
def deleteapp(user_id, app_id):
    app = get_app_by_id(user_id, app_id)
    if not app:
        return False

    # If an application with id 'app_id' exists and user is the owner
    for service in Service.query.filter_by(application_id=app_id):
        callmanager(service.sid, "shutdown", True, {})
        stop(service.sid)

    db.session.delete(app)
    db.session.commit()

    return True
Ejemplo n.º 11
0
    def startup(self, service_id, cloud='default'):
        data = {'cloud': cloud}

        if self.resuming:
            data['resuming'] = True

        return callmanager(service_id, "startup", True, data)
Ejemplo n.º 12
0
    def startup(self, service_id, cloud='default'):
        data = {'cloud': cloud }

        if self.resuming:
            data['resuming'] = True

        return callmanager(service_id, "startup", True, data)
Ejemplo n.º 13
0
    def get_service_manifest(self, service):
        tmp = MGeneral.get_service_manifest(self, service)

        self.set_persistent(service.sid)
        
        log('Calling get_service_snapshot')
        snapshot = callmanager(service.sid, 'get_service_snapshot', True, {})

        if 'StartupInstances' not in tmp:
            tmp['StartupInstances'] = {}

        tmp['StartupInstances']['resume'] = {}
        tmp['StartupInstances']['resume']['nodes'] = []

        for node in snapshot['nodes']:
            node_filename = self.__get_node_archive_filename(node)
            data = base64.b64decode(node.pop('data'))
            open(node_filename, 'wb').write(data)
            log('%s created' % node_filename)

            node['archive'] = '%s/download_data/%s' % (get_director_url(),
                    basename(node_filename))

            tmp['StartupInstances']['resume']['nodes'].append(node)

        tmp['StartupInstances']['resume']['manager'] = snapshot['manager']

        return tmp
Ejemplo n.º 14
0
    def enable_code(self, service_id, code_version):
        params = {'codeVersionId': code_version}

        res = callmanager(service_id, "update_java_configuration", True,
                          params)

        return res
Ejemplo n.º 15
0
    def get_service_manifest(self, service):
        tmp = MGeneral.get_service_manifest(self, service)

        self.set_persistent(service.sid)

        log('Calling get_service_snapshot')
        snapshot = callmanager(service.sid, 'get_service_snapshot', True, {})

        if 'StartupInstances' not in tmp:
            tmp['StartupInstances'] = {}

        tmp['StartupInstances']['resume'] = []

        for node in snapshot:
            node_filename = self.__get_node_archive_filename(node)
            data = base64.b64decode(node.pop('data'))
            open(node_filename, 'wb').write(data)
            log('%s created' % node_filename)

            node['archive'] = '%s/download_data/%s' % (get_director_url(),
                                                       basename(node_filename))

            tmp['StartupInstances']['resume'].append(node)

        return tmp
Ejemplo n.º 16
0
    def add_nodes(self, appid, service_id, params):
        params['cloud'] = 'default'
        params['service_id'] = service_id

        res = callmanager(appid, 0, 'add_nodes', True, params)

        return res
Ejemplo n.º 17
0
    def start(self, json, appid):
        try:
            to_resume = { 'nodes': json['StartupInstances']['resume']['nodes'], 
                          'manager' : json['StartupInstances']['resume']['manager'] }
        except KeyError:
            to_resume = {}
            
        # Set the resuming flag if necessary
        self.resuming = to_resume != {}

        sid = MGeneral.start(self, json, appid)

        if type(sid) != int:
            # Error!
            return sid

        if json.get('VolumeStartup'):
            name = json.get('VolumeStartup').get('volumeName')
            owner = json.get('VolumeStartup').get('owner')

            # Wait few seconds so that the new node is up.
            time.sleep(20)

            if name != "" and owner != "":
                res = self.createvolume(sid, name, owner)
                if 'error' in res:
                    return res['error']

        if json.get('StartupInstances'):
            params = {
                    'osd': 1
            }

            if json.get('StartupInstances').get('osd'):
                params['osd'] = int(json.get('StartupInstances').get('osd'))

            # We have started the service already, so one OSD node is there
            # for sure.
            params['osd'] -= 1
            
            params['resuming'] = to_resume

            res = self.add_nodes(sid, params)
            if 'error' in res:
                return res['error']

            if to_resume:
                log('Resuming the following xtreemfs nodes: %s' % to_resume)
                self.wait_for_state(sid, 'RUNNING')
                res = callmanager(sid, "set_service_snapshot", True,
                        to_resume)

                if 'error' in res:
                    return res['error']
            else:
                log('No xtreemfs node to be resumed')

        return 'ok'
Ejemplo n.º 18
0
    def load_dump(self, sid, url):
        contents = urllib2.urlopen(url).read()
        filename = url.split('/')[-1]

        files = [('mysqldump_file', filename, contents)]

        res = callmanager(sid, "/", True, {'method': 'load_dump'}, files)

        return res
Ejemplo n.º 19
0
    def upload_code(self, app_id, service_id, url):
        contents = read_from_url(url)
        filename = url.split('/')[-1]

        files = [ ( 'code', filename, contents ) ]

        res = callmanager(app_id, service_id, "/", True, { 'method': "upload_code_version",  }, files)

        return res
Ejemplo n.º 20
0
    def createvolume(self, service_id, name, owner):
        params = {
                'volumeName': name,
                'owner' : owner
        }

        res = callmanager(service_id, 'createVolume', True, params)

        return res
Ejemplo n.º 21
0
    def load_dump(self, aid, sid, url):
        contents = read_from_url(url)
        filename = url.split('/')[-1]

        files = [ ( 'mysqldump_file', filename, contents ) ]

        res = callmanager(aid, sid, "/", True, { 'method' : 'load_dump' }, files)

        return res
Ejemplo n.º 22
0
    def load_dump(self, sid, url):
        contents = urllib2.urlopen(url).read()
        filename = url.split('/')[-1]

        files = [ ( 'mysqldump_file', filename, contents ) ]

        res = callmanager(sid, "/", True, { 'method' : 'load_dump' }, files)

        return res
Ejemplo n.º 23
0
    def upload_code(self, service_id, url):
        contents = urllib2.urlopen(url).read()
        filename = url.split('/')[-1]

        files = [ ( 'code', filename, contents ) ]

        res = callmanager(service_id, "/", True, { 'method': "upload_code_version",  }, files)

        return res
Ejemplo n.º 24
0
    def wait_for_state(self, sid, state):
        """Poll the state of service 'sid' till it matches 'state'."""
        res = {'state': None}

        while res['state'] != state:
            try:
                res = callmanager(sid, "get_service_info", False, {})
            except (socket.error, urllib2.URLError):
                time.sleep(2)
Ejemplo n.º 25
0
    def wait_for_state(self, sid, state):
        """Poll the state of service 'sid' till it matches 'state'."""
        res = { 'state': None }

        while res['state'] != state:
            try:
                res = callmanager(sid, "get_service_info", False, {})
            except (socket.error, urllib2.URLError):
                time.sleep(2)
Ejemplo n.º 26
0
def get_list_nodes(sid):
    nodes = callmanager(sid, "list_nodes", False, {})
    if 'error' in nodes:
        return ''

    tmp = {}
    for node in nodes:
        tmp[node] = len(nodes[node])

    return tmp
Ejemplo n.º 27
0
def get_list_nodes(sid):
    nodes = callmanager(sid, "list_nodes", False, {})
    if 'error' in nodes:
        return ''

    tmp = {}
    for node in nodes:
        tmp[node] = len(nodes[node])

    return tmp
Ejemplo n.º 28
0
    def save_dump(self, service_id):
        res = callmanager(service_id, 'sqldump', False, {})
        if type(res) is dict and 'error' in res:
            log('Error getting SQL dump: %s' % res['error'])
            return ''

        _, temp_path = mkstemp(dir=get_userdata_dir())
        open(temp_path, 'w').write(res)

        return '%s/download_data/%s' % (get_director_url(), basename(temp_path))
Ejemplo n.º 29
0
    def save_dump(self, service_id):
        res = callmanager(service_id, 'sqldump', False, {})
        if type(res) is dict and 'error' in res:
            log('Error getting SQL dump: %s' % res['error'])
            return ''

        _, temp_path = mkstemp(dir=get_userdata_dir())
        open(temp_path, 'w').write(res)

        return '%s/download_data/%s' % (get_director_url(),
                                        basename(temp_path))
Ejemplo n.º 30
0
    def start(self, json, appid):
        try:
            to_resume = {'nodes': json['StartupInstances']['resume']}
        except KeyError:
            to_resume = {}

        # Set the resuming flag if necessary
        self.resuming = to_resume != {}

        sid = MGeneral.start(self, json, appid)

        if type(sid) != int:
            # Error!
            return sid

        if json.get('VolumeStartup'):
            name = json.get('VolumeStartup').get('volumeName')
            owner = json.get('VolumeStartup').get('owner')

            # Wait few seconds so that the new node is up.
            time.sleep(20)

            if name != "" and owner != "":
                res = self.createvolume(sid, name, owner)
                if 'error' in res:
                    return res['error']

        if json.get('StartupInstances'):
            params = {'osd': 1}

            if json.get('StartupInstances').get('osd'):
                params['osd'] = int(json.get('StartupInstances').get('osd'))

            # We have started the service already, so one OSD node is there
            # for sure.
            params['osd'] -= 1

            params['resuming'] = to_resume

            res = self.add_nodes(sid, params)
            if 'error' in res:
                return res['error']

            if to_resume:
                log('Resuming the following xtreemfs nodes: %s' % to_resume)
                self.wait_for_state(sid, 'RUNNING')
                res = callmanager(sid, "set_service_snapshot", True, to_resume)

                if 'error' in res:
                    return res['error']
            else:
                log('No xtreemfs node to be resumed')

        return 'ok'
Ejemplo n.º 31
0
    def upload_code(self, service_id, url):
        contents = urllib2.urlopen(url).read()
        filename = url.split('/')[-1]

        files = [('code', filename, contents)]

        res = callmanager(service_id, "/", True, {
            'method': "upload_code_version",
        }, files)

        return res
Ejemplo n.º 32
0
    def get_service_manifest(self, service):
        tmp = MGeneral.get_service_manifest(self, service)

        ret = self.save_dump(service.sid)
        if ret != '':
            tmp['Dump'] = ret

        password = callmanager(service.sid, "get_password", False, {})
        if password:
            tmp['Password'] = password

        return tmp
Ejemplo n.º 33
0
    def get_service_manifest(self, service):
        tmp = MGeneral.get_service_manifest(self, service)

        ret = self.save_dump(service.sid)
        if ret != '':
            tmp['Dump'] = ret

        password = callmanager(service.sid, "get_password", False, {})
        if password:
            tmp['Password'] = password

        return tmp
Ejemplo n.º 34
0
    def update_environment(self, appid):
        env = ''

        # Find mysql ip address
        try:
            sid = Service.query.filter_by(application_id=appid, type='mysql').first().sid
            nodes = callmanager(appid, sid, "list_nodes", False, {})

            if nodes['glb']:
                params = { 'serviceNodeId': nodes['glb'][0] }
            else:
                params = { 'serviceNodeId': nodes['mysql'][0] }
            details = callmanager(appid, sid, "get_node_info", False, params)
            env = env + 'echo "env[MYSQL_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details['serviceNode']['ip']
            env = env + 'export MYSQL_IP=\'%s\'\n' % details['serviceNode']['ip']
        except:
            env = env + ''

        # Find xtreemfs ip address and generate certificate
        try:
            sid = Service.query.filter_by(application_id=appid, type='xtreemfs').first().sid
            nodes = callmanager(appid, sid, "list_nodes", False, {})

            params = { 'serviceNodeId': nodes['dir'][0] }
            details = callmanager(appid, sid, "get_node_info", False, params)
            env = env + 'echo "env[XTREEMFS_IP]=\'%s\'" >> /root/ConPaaS/src/conpaas/services/webservers/etc/fpm.tmpl\n' % details['serviceNode']['ip']
            env = env + 'export XTREEMFS_IP=\'%s\'\n' % details['serviceNode']['ip']

            passphrase = 'contrail123';
            env = env + 'export XTREEMFS_PASSPHRASE=\'%s\'\n' % passphrase

            params = { 'passphrase': passphrase, 'adminflag': False }
            res = callmanager(appid, sid, "get_client_cert", True, params)
            env = env + 'export XTREEMFS_CERT=\'%s\'\n' % res['cert']
        except:
            env = env + ''

        return env
Ejemplo n.º 35
0
    def upload_startup_script(self, service_id, url, environment=''):
        contents = environment
        filename = 'env.sh'

        if url != '':
            contents = environment + urllib2.urlopen(url).read()
            filename = url.split('/')[-1]

        files = [ ( 'script', filename, contents ) ]

        res = callmanager(service_id, "/", True,
            { 'method': 'upload_startup_script', }, files)

        return res
Ejemplo n.º 36
0
def delete(appid):
    """eg: POST /delete/3

    POSTed values must contain username and password.

    Returns a boolean value. True in case of successful authentication and
    proper service termination. False otherwise.
    """
    log('User %s attempting to delete application %s' % (g.user.uid, appid))

    app = get_app_by_id(g.user.uid, appid)
    if not app:
        return build_response(simplejson.dumps(False))

    # If an application with id 'appid' exists and user is the owner
    for service in Service.query.filter_by(application_id=appid):
        callmanager(service.sid, "shutdown", True, {})
        stop(service.sid)

    db.session.delete(app)
    db.session.commit()

    return build_response(simplejson.dumps(True))
Ejemplo n.º 37
0
    def upload_startup_script(self, app_id, service_id, url, environment=''):
        contents = environment
        filename = 'env.sh'

        if url != '':
            contents = environment + read_from_url(url)
            filename = url.split('/')[-1]

        files = [ ( 'script', filename, contents ) ]

        res = callmanager(app_id, 0, "/", True,
            { 'method': 'upload_startup_script', 'sid': service_id }, files)

        return res
Ejemplo n.º 38
0
def delete(appid):
    """eg: POST /delete/3

    POSTed values must contain username and password.

    Returns a boolean value. True in case of successful authentication and
    proper service termination. False otherwise.
    """
    log('User %s attempting to delete application %s' % (g.user.uid, appid))

    app = get_app_by_id(g.user.uid, appid)
    if not app:
        return build_response(simplejson.dumps(False))

    # If an application with id 'appid' exists and user is the owner
    for service in Service.query.filter_by(application_id=appid):
        callmanager(service.sid, "shutdown", True, {})
        stop(service.sid)

    db.session.delete(app)
    db.session.commit()

    return build_response(simplejson.dumps(True))
Ejemplo n.º 39
0
def create_startup_script(sid):
    script = callmanager(sid, "get_startup_script", False, {})
    if 'error' in script:
        return ''

    # We need to get rid of environment variables, they will get overwritten
    # with new values anyways.
    script_start = False
    new_script = []
    for line in script.split('\n'):
        if script_start or '/bin/bash' in line:
            new_script.append(line)
            script_start = True

    return '\n'.join(new_script)
Ejemplo n.º 40
0
def create_startup_script(sid):
    script = callmanager(sid, "get_startup_script", False, {})
    if 'error' in script:
        return ''

    # We need to get rid of environment variables, they will get overwritten
    # with new values anyways.
    script_start = False
    new_script = []
    for line in script.split('\n'):
        if script_start or '/bin/bash' in line:
            new_script.append(line)
            script_start = True

    return '\n'.join(new_script)
Ejemplo n.º 41
0
    def upload_startup_script(self, service_id, url, environment=''):
        contents = environment
        filename = 'env.sh'

        if url != '':
            contents = environment + urllib2.urlopen(url).read()
            filename = url.split('/')[-1]

        files = [('script', filename, contents)]

        res = callmanager(service_id, "/", True, {
            'method': 'upload_startup_script',
        }, files)

        return res
Ejemplo n.º 42
0
    def start(self, json, appid, cloud='default', need_env=False):
        """Start the given service. Return service id upon successful
        termination."""
        servicetype = json.get('Type')

        if json.get('Cloud'):
            cloud = json.get('Cloud')

        res = add_service(servicetype, appid)
        if 'error' in res:
            return res['error']

        sid = res['service']['sid']

        # self.wait_for_state(sid, 'INIT')

        if json.get('ServiceName'):
            res = rename_service(appid, sid, json.get('ServiceName'))
            if 'error' in res:
                return res['error']

        env = ''
        if need_env:
            env = self.update_environment(appid)

        url = ''

        if json.get('StartupScript'):
            url = json.get('StartupScript')

        if url or env:
            # Only upload startup script if necessary
            res = self.upload_startup_script(appid, sid, url, env)
            if 'error' in res:
                return res['error']

        # if not json.get('Start') or json.get('Start') == 0:
        #     return sid

        data = { 'cloud': cloud, 'service_id': sid }
        res = callmanager(appid, 0, "start_service", True, data)
        # # Start == 1
        # res = self.startup(sid)
        if 'error' in res:
            return res['error']

        # self.wait_for_state(sid, 'RUNNING')
        return sid
Ejemplo n.º 43
0
    def add_nodes(self, service_id, params):
        params['cloud'] = 'default'

        res = callmanager(service_id, 'add_nodes', True, params)

        return res
Ejemplo n.º 44
0
def get_service_state(sid):
    res = callmanager(sid, "get_service_info", False, {})
    return res['state']
Ejemplo n.º 45
0
    def startup(self, service_id, cloud = 'default'):
        data = {'cloud': cloud}

        res = callmanager(service_id, "startup", True, data)

        return res
Ejemplo n.º 46
0
    def set_password(self, sid, password):
        data = {'user': '******', 'password': password}
        res = callmanager(sid, "set_password", True, data)

        return res
Ejemplo n.º 47
0
    def startup(self, service_id, cloud='default'):
        data = {'cloud': cloud}

        res = callmanager(service_id, "startup", True, data)

        return res
Ejemplo n.º 48
0
    def enable_code(self, service_id, code_version):
        params = { 'codeVersionId': code_version }

        res = callmanager(service_id, "update_php_configuration", True, params)

        return res
Ejemplo n.º 49
0
    def createvolume(self, service_id, name, owner):
        params = {'volumeName': name, 'owner': owner}

        res = callmanager(service_id, 'createVolume', True, params)

        return res
Ejemplo n.º 50
0
    def set_password(self, sid, password):
        data = { 'user': '******', 'password': password }
        res = callmanager(sid, "set_password", True, data)

        return res
Ejemplo n.º 51
0
    def enable_code(self, app_id, service_id, code_version):
        params = { 'codeVersionId': code_version }

        res = callmanager(app_id, service_id, "enable_code", True, params)

        return res
Ejemplo n.º 52
0
def get_service_state(sid):
    res = callmanager(sid, "get_service_info", False, {})
    return res['state']
Ejemplo n.º 53
0
    def add_nodes(self, service_id, params):
        params['cloud'] = 'default'

        res = callmanager(service_id, 'add_nodes', True, params)

        return res