def _install_managed_plugin(plugin, args):
    dst_dir = target_plugin_prefix(
        name=plugin.package_name,
        tenant_name=ctx.tenant_name,
        version=plugin.package_version
    )
    with _lock(dst_dir):
        if is_already_installed(dst_dir, plugin.id):
            ctx.logger.info(
                'Using existing installation of managed plugin: %s [%s]',
                plugin.id, _get_plugin_description(plugin))
            return

        ctx.logger.info(
            'Installing managed plugin: %s [%s]',
            plugin.id, _get_plugin_description(plugin))
        _make_virtualenv(dst_dir)
        try:
            _wagon_install(plugin, venv=dst_dir, args=args)
            with open(os.path.join(dst_dir, 'plugin.id'), 'w') as f:
                f.write(plugin.id)
        except Exception as e:
            shutil.rmtree(dst_dir, ignore_errors=True)
            tpe, value, tb = sys.exc_info()
            exc = NonRecoverableError(
                'Failed installing managed plugin: {0} [{1}][{2}]'
                .format(plugin.id, plugin, e))
            reraise(NonRecoverableError, exc, tb)
示例#2
0
 def _ensure_connection(self):
     if self._connection is None:
         self._connection = Connection(**self.env)
         try:
             self._connection.open()
         except Exception as e:
             _, _, tb = sys.exc_info()
             reraise(FabricCommandExecutionError,
                     FabricCommandExecutionError(str(e)), tb)
示例#3
0
def install_plugins(plugins, **_):
    installer = PluginInstaller(logger=ctx.logger)
    for plugin in plugins:
        ctx.logger.info('Installing plugin: {0}'.format(plugin['name']))
        try:
            installer.install(plugin=plugin,
                              deployment_id=ctx.deployment.id,
                              blueprint_id=ctx.blueprint.id)
        except exceptions.PluginInstallationError as e:
            # preserve traceback
            tpe, value, tb = sys.exc_info()
            reraise(NonRecoverableError, NonRecoverableError(str(e)), tb)
示例#4
0
def install_plugins(plugins, **_):
    for plugin in plugins:
        name = plugin.get('package_name') or plugin['name']
        ctx.logger.info('Installing plugin: %s', name)
        try:
            plugin_installer.install(plugin=plugin,
                                     deployment_id=ctx.deployment.id,
                                     blueprint_id=ctx.blueprint.id)
        except exceptions.PluginInstallationError as e:
            # preserve traceback
            tpe, value, tb = sys.exc_info()
            reraise(NonRecoverableError, NonRecoverableError(str(e)), tb)
示例#5
0
def _init_script_agent_setup(container_id, install_agent_script):
    container_install_agent_script_path = '/root/agent_install_script.sh'
    with tempfile.NamedTemporaryFile() as f:
        f.write(install_agent_script)
        f.flush()
        try:
            _docker(
                'cp',
                '{0} {1}:{2}'.format(f.name, container_id,
                                     container_install_agent_script_path))
            _docker_exec(
                container_id,
                'chmod +x {0}'.format(container_install_agent_script_path))
            _docker_exec(container_id, container_install_agent_script_path)
        except BaseException as e:
            tpe, value, tb = sys.exc_info()
            reraise(NonRecoverableError, NonRecoverableError(str(e)), tb)
示例#6
0
    def execute(self,
                workflow,
                parameters=None,
                allow_custom_parameters=False,
                task_retries=-1,
                task_retry_interval=30,
                subgraph_retries=0,
                task_thread_pool_size=DEFAULT_LOCAL_TASK_THREAD_POOL_SIZE):
        workflows = self.plan['workflows']
        workflow_name = workflow
        if workflow_name not in workflows:
            raise ValueError("'{0}' workflow does not exist. "
                             "existing workflows are: [{1}]".format(
                                 workflow_name, ', '.join(workflows)))

        workflow = workflows[workflow_name]
        execution_id = str(uuid.uuid4())
        ctx = {
            'type': 'workflow',
            'local': True,
            'deployment_id': self.name,
            'blueprint_id': self.name,
            'execution_id': execution_id,
            'workflow_id': workflow_name,
            'storage': self.storage,
            'task_retries': task_retries,
            'task_retry_interval': task_retry_interval,
            'subgraph_retries': subgraph_retries,
            'local_task_thread_pool_size': task_thread_pool_size,
            'task_name': workflow['operation']
        }

        merged_parameters = _merge_and_validate_execution_parameters(
            workflow, workflow_name, parameters, allow_custom_parameters)
        self.storage.store_execution(execution_id, ctx, merged_parameters)
        try:
            rv = dispatch.dispatch(__cloudify_context=ctx, **merged_parameters)
            self.storage.execution_ended(execution_id)
            return rv
        except Exception as e:
            self.storage.execution_ended(execution_id, e)
            reraise(e.__class__, e, sys.exc_info()[2])
示例#7
0
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except BaseException as e:
            tpe, value, tb = sys.exc_info()

            if isinstance(e, exceptions.DaemonException):

                # convert api exceptions to click exceptions.
                value = click.ClickException(str(e))

            if isinstance(e, exceptions.DaemonError):

                # convert api errors to cli exceptions
                value = click.ClickException(str(e))

            # set the exit_code accordingly. the exit_code property is later
            # read by the click framework to set the exit code of
            # the process.
            value.exit_code = codes.get(tpe, 1)
            reraise(type(value), value, tb)
示例#8
0
    def _install_managed_plugin(self,
                                deployment_id,
                                managed_plugin,
                                plugin,
                                args,
                                tmp_plugin_dir):
        matching_existing_installation = False
        dst_dir = '{0}-{1}'.format(managed_plugin.package_name,
                                   managed_plugin.package_version)
        dst_dir = self._full_dst_dir(dst_dir, managed_plugin)
        self.logger.debug('Checking if managed plugin installation exists '
                          'in {0}'.format(dst_dir))

        # create_deployment_env should wait for the install_plugin wf to finish
        deadline = time.time() + INSTALLATION_TIMEOUT
        while (self._is_plugin_installing(managed_plugin.id) and
               deployment_id != SYSTEM_DEPLOYMENT):
            if time.time() < deadline:
                time.sleep(PLUGIN_QUERY_INTERVAL)
            else:
                raise exceptions.PluginInstallationError(
                    'Timeout waiting for plugin to be installed. '
                    'Plugin info: [{0}] '.format(managed_plugin))
        if os.path.exists(dst_dir):
            self.logger.debug('Plugin path exists {0}'.format(dst_dir))
            plugin_id_path = os.path.join(dst_dir, 'plugin.id')
            if os.path.exists(plugin_id_path):
                self.logger.debug('Plugin id path exists {0}'.
                                  format(plugin_id_path))
                with open(plugin_id_path) as f:
                    existing_plugin_id = f.read().strip()
                matching_existing_installation = (
                    existing_plugin_id == managed_plugin.id)
                if not matching_existing_installation:
                    raise exceptions.PluginInstallationError(
                        'Managed plugin installation found but its ID '
                        'does not match the ID of the plugin currently '
                        'on the manager. [existing: {0}, new: {1}]'
                        .format(existing_plugin_id,
                                managed_plugin.id))
            else:
                raise exceptions.PluginInstallationError(
                    'Managed plugin installation found but it is '
                    'in a corrupted state. [{0}]'.format(managed_plugin))
        fields = ['package_name',
                  'package_version',
                  'supported_platform',
                  'distribution',
                  'distribution_release']
        description = ', '.join('{0}: {1}'.format(
            field, managed_plugin.get(field))
            for field in fields if managed_plugin.get(field))

        if matching_existing_installation:
            self.logger.info(
                'Using existing installation of managed plugin: {0} [{1}]'
                .format(managed_plugin.id, description))
        elif (deployment_id != SYSTEM_DEPLOYMENT and
              plugin['executor'] == 'central_deployment_agent'):
            raise exceptions.PluginInstallationError(
                'Central deployment agent managed plugins can only be '
                'installed using the REST plugins API. [{0}]'
                .format(managed_plugin))
        else:
            try:
                self.logger.info('Installing managed plugin: {0} [{1}]'
                                 .format(managed_plugin.id, description))
                wait_for_wagon_in_directory(managed_plugin.id)
                # Wait for Syncthing to sync resources for the wagons
                if syncthing_utils:
                    syncthing_utils.wait_for_plugins_sync(
                        sync_dir=syncthing_utils.RESOURCES_DIR)
                self._wagon_install(plugin=managed_plugin, args=args)
                self.logger.debug('Moving plugin from tmp dir `{0}` to `{1}`'.
                                  format(tmp_plugin_dir, dst_dir))
                shutil.move(tmp_plugin_dir, dst_dir)
                with open(os.path.join(dst_dir, 'plugin.id'), 'w') as f:
                    f.write(managed_plugin.id)

                if self._is_plugin_installing(managed_plugin.id):
                    # Wait for Syncthing to sync plugin files on all managers
                    if syncthing_utils:
                        syncthing_utils.wait_for_plugins_sync()
                    self._update_plugin_status(managed_plugin.id)
            except Exception as e:
                tpe, value, tb = sys.exc_info()
                exc = NonRecoverableError('Failed installing managed '
                                          'plugin: {0} [{1}][{2}]'
                                          .format(managed_plugin.id,
                                                  plugin, e))
                reraise(NonRecoverableError, exc, tb)