Ejemplo n.º 1
0
 def retry_operation(self, message=None, retry_after=None):
     if self._return_value is not _NOTHING:
         self._return_value = ILLEGAL_CTX_OPERATION_ERROR
         raise self._return_value
     self.operation.retry(message=message, retry_after=retry_after)
     self._return_value = ScriptException(message, retry=True)
     return self._return_value
Ejemplo n.º 2
0
 def abort_operation(message=None):
     if actual_ctx._return_value is not None:
         actual_ctx._return_value = ILLEGAL_CTX_OPERATION_ERROR
         raise actual_ctx._return_value
     if actual_ctx.is_script_exception_defined:
         actual_ctx._return_value = ScriptException(message)
     else:
         actual_ctx._return_value = UNSUPPORTED_SCRIPT_FEATURE_ERROR
         raise actual_ctx
     return actual_ctx._return_value
Ejemplo n.º 3
0
 def retry_operation(message=None, retry_after=None):
     if ctx._return_value is not None:
         ctx._return_value = ILLEGAL_CTX_OPERATION_ERROR
         raise ctx._return_value
     if ctx.is_script_exception_defined:
         ctx._return_value = ScriptException(message, retry=True)
         ctx.operation.retry(message=message, retry_after=retry_after)
     else:
         ctx._return_value = UNSUPPORTED_SCRIPT_FEATURE_ERROR
         raise ctx._return_value
     return ctx._return_value
Ejemplo n.º 4
0
def run_script(ctx, script_path, client, proxy, env=None, use_sudo=False,
               stdin=None, **kwargs):
    base_dir = '/tmp/cloudify-ctx'
    work_dir = os.path.join(base_dir, 'work')

    proxy_client_path = proxy_client.__file__
    if proxy_client_path.endswith('.pyc'):
        proxy_client_path = proxy_client_path[:-1]
    local_ctx_py_path = os.path.join(
        os.path.dirname(cloudify.ctx_wrappers.__file__), 'ctx-py.py')
    script_path = get_script(ctx.download_resource, script_path)
    remote_ctx_path = os.path.join(base_dir, 'ctx')
    try:
        _run_command(client, 'test -e {0}'.format(remote_ctx_path))
    except RemoteProcessError:
        _run_command(client, 'mkdir -p {0}'.format(work_dir))
        with client.open_sftp() as sftp:
            sftp.put(proxy_client_path, remote_ctx_path)
            sftp.put(local_ctx_py_path, os.path.join(base_dir, 'cloudify.py'))
        _run_command(client, 'chmod +x {0}'.format(remote_ctx_path))

    out, _err = _run_command(client, 'mktemp -d --tmpdir={0}'.format(work_dir))
    remote_script_dir = out.strip()
    remote_script_path = os.path.join(remote_script_dir, 'script')
    with client.open_sftp() as sftp:
        sftp.put(script_path, remote_script_path)
    _run_command(client, 'chmod +x {0}'.format(remote_script_path))

    wrapped_ctx = _CtxWrapper(ctx, client, remote_script_dir)

    ctx_id = uuid.uuid4().hex
    proxy.contexts[ctx_id] = wrapped_ctx

    cmd = '{0}{1}'.format('sudo ' if use_sudo else '', remote_script_path)
    if env is None:
        env = {}
    env.update({
        'CTX_SOCKET_URL': '{0}?id={1}'.format(proxy.proxy_url, ctx_id),
        'PATH': '{0}:/sbin:$PATH'.format(base_dir),
        'PYTHONPATH': '{0}:$PYTHONPATH'.format(base_dir)
    })
    try:
        stdout, stderr = _run_command(client, cmd, stdin=stdin, env=env)
    except RemoteProcessError as e:
        raise ScriptException(e.stderr)
    finally:
        _run_command(client, 'rm -fr {0}'.format(remote_script_dir))
        proxy.contexts.pop(ctx_id)

    sys.stdout.write(stdout)
    sys.stderr.write(stderr)

    return handle_script_result(wrapped_ctx)
Ejemplo n.º 5
0
 def abort_operation(self, message=None):
     if self._return_value is not _NOTHING:
         self._return_value = ILLEGAL_CTX_OPERATION_ERROR
         raise self._return_value
     self._return_value = ScriptException(message)
     return self._return_value