Beispiel #1
0
def _get_app(app_id, user):
    agave = user.agave_oauth.client
    data = {'definition': agave.apps.get(appId=app_id)}

    # GET EXECUTION SYSTEM INFO FOR USER APPS
    exec_sys = ExecutionSystem(agave, data['definition']['executionSystem'])
    data['exec_sys'] = exec_sys.to_dict()

    # set maxNodes from system queue for app
    if (data['definition']['parallelism']
            == 'PARALLEL') and ('defaultQueue' in data['definition']):
        for queue in exec_sys.queues.all():
            if queue.name == data['definition']['defaultQueue']:
                data['definition']['maxNodes'] = queue.maxNodes
                break

    lic_type = _app_license_type(app_id)
    data['license'] = {'type': lic_type}
    if lic_type is not None:
        _, license_models = get_license_info()
        license_model = list(
            filter(lambda x: x.license_type == lic_type, license_models))[0]
        lic = license_model.objects.filter(user=user).first()
        data['license']['enabled'] = lic is not None

    # Update any App Tray entries upon app retrieval, if their revision numbers have changed
    matching = AppTrayEntry.objects.all().filter(
        name=data['definition']['name'])
    if len(matching) > 0:
        first_match = matching[0]
        if first_match.lastRetrieved and first_match.lastRetrieved != data[
                'definition']['id']:
            data['lastRetrieved'] = first_match.lastRetrieved

    return data
Beispiel #2
0
 def test_execution_sys_serializer(self):
     """Test :class:`BaseAgaveSystemSerializer`"""
     sys = ExecutionSystem.from_dict(self.magave, self.execution_sys)
     serial_sys = json.dumps(sys, cls=BaseAgaveSystemSerializer)
     _sys = ExecutionSystem.from_dict(self.magave, json.loads(serial_sys))
     self.assertEqual(self.execution_sys['id'], _sys.id)
     self.assertEqual(self.execution_sys['type'], _sys.type)
     self.assertEqual(self.execution_sys['uuid'], _sys.uuid)
     self.assertEqual(self.execution_sys['storage']['host'],
                      _sys.storage.host)
     self.assertEqual(self.execution_sys['storage']['rootDir'],
                      _sys.storage.root_dir)
     self.assertEqual(self.execution_sys['storage']['homeDir'],
                      _sys.storage.home_dir)
     self.assertEqual(self.execution_sys['workDir'], _sys.work_dir)
     self.assertEqual(self.execution_sys['scratchDir'], _sys.scratch_dir)
     self.assertEqual(len(self.execution_sys['queues']),
                      len(_sys.queues.queues))
Beispiel #3
0
 def test_execution_system(self):
     """Test Storage System initialization"""
     self.magave.reset_mock()
     self.magave.systems.get.reset_mock()
     self.magave.systems.get = Mock(return_value=self.agave_execution)
     execution = ExecutionSystem(self.magave, id=self.agave_execution['id'])
     self.assertEqual(execution.uuid, self.agave_execution['uuid'])
     self.assertEqual(execution.id, self.agave_execution['id'])
     self.assertEqual(execution.name, self.agave_execution['name'])
Beispiel #4
0
    def get_exec_system(self, systemId, *args, **kwargs):
        """Gets an execution system

        :param systemId: Agave Execution system id

        :returns: ExecutionSystem instance
        :rtype: class ExecutionSystem
        """

        exec_sys = ExecutionSystem(self.client, systemId, ignore_error=None)
        return exec_sys
Beispiel #5
0
    def get_or_create_app(self, appId, allocation):
        """Gets or creates application for user.

        If application selected is owned by user, return the app,
        else clone the app to the same exec system with the
        specified allocation.

        ..note: Entry point.

        :param str appId: Agave id of application selected to run
        :param str allocation: Project alloction for app to run on

        :returns: Application instance
        :rtype: class Application
        """

        host_app = self.get_application(appId)

        # if app is owned by user, no need to clone
        if host_app.owner == self.user.username:
            logger.info(
                'User is app owner, no need to clone. Returning original app.')
            app = host_app
            exec_sys = ExecutionSystem(self.client,
                                       app.execution_system,
                                       ignore_error=None)
        else:
            exec_sys = self.get_or_create_cloned_app_exec_system(
                host_app.execution_system, allocation)
            app = self.get_or_create_cloned_app(host_app, allocation, exec_sys)

        # Check if app's execution system needs keys reset and pushed
        if not app.exec_sys:
            sys_ok, res = exec_sys.test()
            if not sys_ok and (exec_sys.owner == self.user.username):
                logger.debug(res)
                logger.info('System {} needs new keys.'.format(exec_sys.id))
                app.exec_sys = exec_sys

        return app
Beispiel #6
0
def reset_system_keys(username, system_id):
    """Reset system's Keys

    Creates a new set of keys, saves the set of key to the DB
    and updates the Agave System.

    :param str username: Username

    .. note::
        If :param:`system_id` is a home system then the Home Manager
        class will be used to reset the keys.
        This because there might be some specific actions to do
        when managing home directories
    """
    user = check_user(username)

    sys_dict = user.agave_oauth.client.systems.get(systemId=system_id)
    if sys_dict['type'] == StorageSystem.TYPES.STORAGE:
        sys = StorageSystem.from_dict(user.agave_oauth.client, sys_dict)
    elif sys_dict['type'] == ExecutionSystem.TYPES.EXECUTION:
        sys = ExecutionSystem.from_dict(user.agave_oauth.client, sys_dict)

    private_key = EncryptionUtil.create_private_key()
    priv_key_str = EncryptionUtil.export_key(private_key, 'PEM')
    public_key = EncryptionUtil.create_public_key(private_key)
    publ_key_str = EncryptionUtil.export_key(public_key, 'OpenSSH')

    sys.set_storage_keys(username,
                         priv_key_str,
                         publ_key_str,
                         update=(sys.type == StorageSystem.TYPES.STORAGE))
    SSHKeys.objects.update_keys(user,
                                system_id=system_id,
                                priv_key=priv_key_str,
                                pub_key=publ_key_str)

    # Update keys for hostname too
    SSHKeys.objects.update_hostname_keys(user,
                                         hostname=sys.storage.host,
                                         priv_key=priv_key_str,
                                         pub_key=publ_key_str)
    if sys.type == ExecutionSystem.TYPES.EXECUTION:
        sys.set_login_keys(username, priv_key_str, publ_key_str)

        SSHKeys.objects.update_hostname_keys(user,
                                             hostname=sys.login.host,
                                             priv_key=priv_key_str,
                                             pub_key=publ_key_str)

    return publ_key_str
Beispiel #7
0
def execution_systems(user, offset=0, limit=100):
    """Return all execution systems for a user.

    :param user: Django user's instance
    :param int offset: Offset.
    :param int limit: Limit.
    """
    systems = []
    res = ExecutionSystem.list(user.agave_oauth.client,
                               type=ExecutionSystem.TYPES.EXECUTION,
                               offset=offset,
                               limit=limit)
    systems = list(res)
    return systems
Beispiel #8
0
def get_system(user, system_id):
    """Returns system

    :param user: Django's user object
    :param str system_id: System id
    :returns: System object
    :rtype: :class:`StorageSystem` or :class:`ExecutionSystem`
    """
    system = user.agave_oauth.client.systems.get(systemId=system_id)
    if system.type == StorageSystem.TYPES.STORAGE:
        sys = StorageSystem.from_dict(user.agave_oauth.client, system)
    elif system.type == ExecutionSystem.TYPES.EXECUTION:
        sys = ExecutionSystem.from_dict(user.agave_oauth.client, system)

    sys.test()
    return sys
Beispiel #9
0
 def get_or_create_cloned_app_exec_system(self, host_exec_id, allocation):
     host_exec = ExecutionSystem(self.client, host_exec_id)
     host_exec_user_role = host_exec.roles.for_user(
         username=self.user.username)
     if host_exec_user_role and host_exec_user_role.role == 'OWNER':
         cloned_exec_sys = host_exec
         logger.debug('Using current execution system {}'.format(
             cloned_exec_sys.id))
     else:
         cloned_exec_id = '{username}.{allocation}.exec.{resource}.{execType}.{revision}'.format(
             username=self.user.username.replace('_', '-'),
             allocation=allocation,
             resource=host_exec.login.host.replace('.tacc.utexas.edu', ''),
             execType=host_exec.execution_type,
             revision=host_exec.revision)
         logger.debug(
             'Getting cloned execution system: {}'.format(cloned_exec_id))
         cloned_exec_sys = self.get_or_create_exec_system(
             cloned_exec_id, host_exec.id, allocation)
     return cloned_exec_sys