Ejemplo n.º 1
0
def get_models_info(token, controller):
    result = []
    for mod in get_all_models(controller):
        print(mod, datastore.get_model_access(controller.c_name, mod, token.username))
        if datastore.get_model_access(controller.c_name, mod['name'], token.username) in ['read', 'write', 'admin']:
            result.append(mod)
    return result
Ejemplo n.º 2
0
 def __init__(self, token, controller, model):
     con = datastore.get_controller(controller)
     self.c_endpoint = con['endpoints'][0]
     self.c_cacert = con['ca-cert']
     self.m_name = model
     self.m_access = datastore.get_model_access(controller, self.m_name, token.username)
     self.m_uuid = datastore.get_model(controller, self.m_name)['uuid']
     self.m_connection = Model()
async def set_model_acc(username, password, user, access, controller):
    try:
        logger.info('Starting process to set model access')
        token = JuJu_Token
        token.username = username
        token.password = password
        access_list = ast.literal_eval(access)
        ssh_keys = datastore.get_ssh_keys(user)
        for mod in access_list:
            logger.info('setting Model Access for %s on %s!', user,
                        mod['name'])
            model = juju.Model_Connection(token, controller, mod['name'])
            async with model.connect(token) as mod_con:
                current_access = datastore.get_model_access(
                    controller, mod['name'], user)
                logger.info('Current Access level: %s', current_access)
                if current_access:
                    await mod_con.revoke(user)
                await mod_con.grant(user, acl=mod['access'])
                if mod['access'] in ['admin', 'write']:
                    for key in ssh_keys:
                        try:
                            logger.info('Adding ssh-key: %s', key)
                            mod_con.add_ssh_key(user, key)
                        except (JujuAPIError, JujuError):
                            exc_type, exc_value, exc_traceback = sys.exc_info()
                            lines = traceback.format_exception(
                                exc_type, exc_value, exc_traceback)
                            for l in lines:
                                logger.error(l)
            datastore.set_model_access(controller, mod['name'], user,
                                       mod['access'])
            logger.info('Model Access set for %s on %s!', user, mod['name'])
    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        for l in lines:
            logger.error(l)
async def set_controller_acc(c_name, access, user):
    try:
        token = JuJu_Token()
        con = datastore.get_controller(c_name)
        usr = datastore.get_user(user)
        logger.info('Connecting to controller %s', c_name)
        controller = juju.Controller_Connection(token, c_name)
        async with controller.connect(token) as con_juju:
            logger.info('Connected to controller %s ', c_name)
            await con_juju.grant(user, acl=access)
            datastore.set_controller_access(c_name, user, access)
        logger.info('Controller access set for  %s ', c_name)
        if access == 'superuser':
            for mod in con['models']:
                model = juju.Model_Connection(token, con['name'], mod['name'])
                async with model.connect(token) as mod_con:
                    logger.info('Setting up connection for model: %s',
                                mod['name'])
                    current_access = datastore.get_model_access(
                        c_name, mod['name'], user)
                    logger.info('Current Access level: %s', current_access)
                    if current_access:
                        await mod_con.revoke(user)
                    await mod_con.grant(user, acl='admin')
                    datastore.set_model_access(c_name, mod['name'], user,
                                               'admin')
                    logger.info('Admin Access granted for for %s:%s', c_name,
                                mod['name'])
                for key in usr['ssh-keys']:
                    logger.info('SSh key found... adding SSH key %s', key)
                    await mod_con.add_ssh_key(user, key)
    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        for l in lines:
            logger.error(l)
Ejemplo n.º 5
0
 async def set_model(self, token, controller, modelname):
     await self.m_connection.disconnect()
     self.m_name = modelname
     self.m_uuid = datastore.get_model(controller, self.m_name)['uuid']
     self.m_connection = Model()
     self.m_access = datastore.get_model_access(controller, self.m_name, token.username)
Ejemplo n.º 6
0
def get_model_access(model, controller, username):
    return datastore.get_model_access(controller, model, username) if not None else "None"