Exemple #1
0
def content_to_file(content, file_path=None, executable=False):

    """
    Write string to a temporary file.

    :param content:
    :param file_path: absolute path to the desired output file.
    """

    if not file_path:
        tempdir = get_exec_tempdir() if executable else tempfile.gettempdir()
        file_path = tempfile.NamedTemporaryFile(mode='w', delete=False,
                                                dir=tempdir).name
    with open(file_path, 'w') as f:
        f.write(content)
        f.write(os.linesep)
    return file_path
def content_to_file(content, file_path=None, executable=False):

    """
    Write string to a temporary file.

    :param content:
    :param file_path: absolute path to the desired output file.
    """

    if not file_path:
        tempdir = get_exec_tempdir() if executable else tempfile.gettempdir()
        file_path = tempfile.NamedTemporaryFile(mode='w', delete=False,
                                                dir=tempdir).name
    with open(file_path, 'w') as f:
        f.write(content)
        f.write(os.linesep)
    return file_path
Exemple #3
0
def content_to_file(content, file_path=None, executable=False):
    """
    Write string to a temporary file.

    :param content:
    :param file_path: absolute path to the desired output file.
    """

    if not file_path:
        tempdir = get_exec_tempdir() if executable else tempfile.gettempdir()
        file_path = tempfile.NamedTemporaryFile(mode='w',
                                                delete=False,
                                                dir=tempdir).name
    with open(file_path, 'w') as f:
        f.write(content)
        # Py3 enabled universal newlines, which messes this up, then made the
        # wU option disallowed, hence this workaround
        if PY2:
            f.write(os.linesep)
        else:
            f.write('\n')
    return file_path
Exemple #4
0
def _get_process_environment(process, proxy):
    """Get env to be used by the script process.

    This env must at the very least contain the proxy url, and a PATH
    allowing bash scripts to use `ctx`, which is expected to live next to
    the current executable.
    """
    env = os.environ.copy()
    env.setdefault('TMPDIR', get_exec_tempdir())
    process_env = process.get('env', {})
    env.update(process_env)

    env[CTX_SOCKET_URL] = proxy.socket_url

    env_path = env.get('PATH')
    bin_dir = os.path.dirname(sys.executable)
    if env_path:
        if bin_dir not in env_path.split(os.pathsep):
            env['PATH'] = os.pathsep.join([env_path, bin_dir])
    else:
        env['PATH'] = bin_dir

    return env
Exemple #5
0
 def test_executable_override(self):
     self.assertEqual('/fake/temp', get_exec_tempdir())
Exemple #6
0
 def test_executable_no_override(self):
     sys_default_tempdir = tempfile.gettempdir()
     self.assertEqual(sys_default_tempdir, get_exec_tempdir())
Exemple #7
0
    def __init__(self, logger=None, **params):
        """

        ####################################################################
        # When subclassing this, do not implement any logic inside the
        # constructor except for in-memory calculations and settings, as the
        # daemon may be instantiated many times for an existing agent. Also,
        # all daemon attributes must be JSON serializable, as daemons are
        # represented as dictionaries and stored as JSON files on Disk. If
        # you wish to have a non serializable attribute, mark it private by
        # naming it _<name>. Attributes starting with underscore will be
        # omitted when serializing the object.
        ####################################################################

        :param logger: a logger to be used to log various subsequent
        operations.
        :type logger: logging.Logger

        :param params: key-value pairs as stated above.
        :type params dict

        """

        # will be populated later on with runtime properties of the host
        # node instance this agent is dedicated for (if needed)
        self._runtime_properties = None

        # configure logger
        self._logger = logger or setup_logger(
            logger_name='cloudify_agent.api.pm.{0}'.format(
                self.PROCESS_MANAGEMENT))

        # save params
        self._params = params

        # configure command runner
        self._runner = LocalCommandRunner(logger=self._logger)

        # Mandatory parameters
        self.validate_mandatory()
        self.rest_host = params['rest_host']
        self.broker_ip = params['broker_ip']
        self.local_rest_cert_file = params['local_rest_cert_file']
        self.cluster = params.get('cluster', [])

        # Optional parameters - REST client
        self.validate_optional()
        self.rest_port = params.get('rest_port', defaults.INTERNAL_REST_PORT)
        # REST token needs to be prefixed with _ so it's not stored
        # when the daemon is serialized
        self._rest_token = params.get('rest_token')
        self._rest_tenant = params.get('rest_tenant')

        # Optional parameters
        self.name = params.get('name') or self._get_name_from_manager()
        self.user = params.get('user') or getpass.getuser()

        self.broker_user = params.get('broker_user', 'guest')
        self.broker_pass = params.get('broker_pass', 'guest')
        self.broker_vhost = params.get('broker_vhost', '/')
        self.broker_ssl_enabled = params.get('broker_ssl_enabled', False)
        self.broker_ssl_cert_path = params['local_rest_cert_file']
        if self.broker_ssl_enabled:
            self.broker_port = constants.BROKER_PORT_SSL
        else:
            self.broker_port = constants.BROKER_PORT_NO_SSL

        self.host = params.get('host')
        self.deployment_id = params.get('deployment_id')
        self.queue = params.get('queue') or self._get_queue_from_manager()

        # This is not retrieved by param as an option any more as it then
        # introduces ambiguity over which values should be used if the
        # components of this differ from the passed in broker_user, pass, etc
        # These components need to be known for the _delete_amqp_queues
        # function.
        if self.cluster:
            self.broker_url = [
                defaults.BROKER_URL.format(host=node_ip,
                                           port=self.broker_port,
                                           username=self.broker_user,
                                           password=self.broker_pass,
                                           vhost=self.broker_vhost)
                for node_ip in self.cluster
            ]
        else:
            self.broker_url = defaults.BROKER_URL.format(
                host=self.broker_ip,
                port=self.broker_port,
                username=self.broker_user,
                password=self.broker_pass,
                vhost=self.broker_vhost)
        self.min_workers = params.get('min_workers') or defaults.MIN_WORKERS
        self.max_workers = params.get('max_workers') or defaults.MAX_WORKERS
        self.workdir = params.get('workdir') or os.getcwd()
        self.executable_temp_path = params.get('executable_temp_path') or \
            get_exec_tempdir()

        self.extra_env_path = params.get('extra_env_path')
        self.log_level = params.get('log_level') or defaults.LOG_LEVEL
        self.log_file = params.get('log_file') or os.path.join(
            self.workdir, '{0}.log'.format(self.name))
        self.pid_file = params.get('pid_file') or os.path.join(
            self.workdir, '{0}.pid'.format(self.name))

        # create working directory if its missing
        if not os.path.exists(self.workdir):
            self._logger.debug('Creating directory: {0}'.format(self.workdir))
            os.makedirs(self.workdir)

        # save as attributes so that they will be persisted in the json files.
        # we will make use of these values when loading agents by name.
        self.process_management = self.PROCESS_MANAGEMENT
        self.virtualenv = VIRTUALENV
        self.cluster_settings_path = params.get('cluster_settings_path')
        self.network = params.get('network') or 'default'