def configure(filebeat_config_file='', filebeat_config='', **kwargs):
    """Generating configuration file from your own desire destination
    or from filebeat_plugin filebeat.conf file.

    Rendering your inputs/outputs definitions.
    """
    ctx.logger.info('Configuring filebeat...')
    dest_file = os.path.join(tempfile.gettempdir(), 'filebeat.yml')
    if filebeat_config_file:
        try:
            ctx.download_resource_and_render(filebeat_config_file,
                                             dest_file,
                                             filebeat_config)
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")
    else:
        filebeat_config_file = pkg_resources.resource_string(
            filebeat_plugin.__name__, 'resources/filebeat.yml')
        configuration = jinja2.Template(filebeat_config_file)
        try:
            with open(dest_file, 'w') as f:
                f.write(configuration.render(filebeat_config))
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")
    _run('sudo mv {0} {1}'.format(dest_file, FILEBEAT_CONFIG_FILE_DEFAULT))
    try:
        _run('filebeat -c {0} -configtest'.format(
             FILEBEAT_CONFIG_FILE_DEFAULT))
    except:
        raise ValueError(
            "wrong inputs prodided! configuration file is unvalid")
    ctx.logger.info('filebeat was configured...')
예제 #2
0
def configure(telegraf_config_file='', telgraf_config='', **kwargs):
    """Generating configuration file from your own desire destination
    or from telegraf_plugin telegraf.conf file.

    Rendering your inputs/outputs definitions.
    """
    ctx.logger.info('Configuring Telegraf...')
    dest_file = os.path.join(tempfile.gettempdir(), 'telegraf.conf')
    if telegraf_config_file:
        try:
            ctx.download_resource_and_render(telegraf_config_file, dest_file,
                                             telgraf_config)
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")
    else:
        telegraf_config_file = pkg_resources.resource_string(
            telegraf_plugin.__name__, 'resources/telegraf.conf')
        configuration = jinja2.Template(telegraf_config_file)
        try:
            with open(dest_file, 'w') as f:
                f.write(configuration.render(telgraf_config))
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")

    _run('sudo mv {0} {1}'.format(dest_file, TELEGRAF_CONFIG_FILE_DEFAULT))

    try:
        _run('telegraf -config {0} -test'.format(TELEGRAF_CONFIG_FILE_DEFAULT))
    except:
        raise ValueError(
            "wrong inputs prodided! configuration file is unvalid")
    ctx.logger.info('telegraf.conf was configured...')
예제 #3
0
def configure(filebeat_config_file='', filebeat_config='', **kwargs):
    """Generating configuration file from your own desire destination
    or from filebeat_plugin filebeat.conf file.

    Rendering your inputs/outputs definitions.
    """
    ctx.logger.info('Configuring filebeat...')
    dest_file = os.path.join(tempfile.gettempdir(), 'filebeat.yml')
    if filebeat_config_file:
        try:
            ctx.download_resource_and_render(filebeat_config_file, dest_file,
                                             filebeat_config)
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")
    else:
        filebeat_config_file = pkg_resources.resource_string(
            filebeat_plugin.__name__, 'resources/filebeat.yml')
        configuration = jinja2.Template(filebeat_config_file)
        try:
            with open(dest_file, 'w') as f:
                f.write(configuration.render(filebeat_config))
        except:
            raise ValueError(
                "wrong inputs provided! can't redner configuration file")
    _run('sudo mv {0} {1}'.format(dest_file, FILEBEAT_CONFIG_FILE_DEFAULT))
    try:
        _run(
            'filebeat -c {0} -configtest'.format(FILEBEAT_CONFIG_FILE_DEFAULT))
    except:
        raise ValueError(
            "wrong inputs prodided! configuration file is unvalid")
    ctx.logger.info('filebeat was configured...')
예제 #4
0
파일: pip.py 프로젝트: mxmrlv/clue
def configure_virtualenv(virtualenv_location, constraints,
                         postactivate_resource_path,
                         git_retag_cloudify_resource_path,
                         register_python_argcomplete, **_):

    # constraints.txt
    constraints = constraints or []
    constraints_path = os.path.join(
        ctx.instance.runtime_properties['virtualenv_location'],
        'constraints.txt')
    with open(constraints_path, 'w') as f:
        f.write('\n'.join(constraints))

    virtualenv_bin = path(virtualenv_location) / 'bin'

    # clue symlink
    clue_source_path = path(sys.executable).dirname() / 'clue'
    clue_target_path = virtualenv_bin / 'clue'
    if not clue_target_path.exists():
        os.symlink(clue_source_path, clue_target_path)

    # postactivate
    variables = dict(register_python_argcomplete=register_python_argcomplete)
    ctx.download_resource_and_render(postactivate_resource_path,
                                     target_path=virtualenv_bin /
                                     'postactivate',
                                     template_variables=variables)

    # git-retag-cloudify
    retag_cloudify_target_path = virtualenv_bin / 'git-retag-cloudify'
    ctx.download_resource_and_render(
        git_retag_cloudify_resource_path,
        template_variables={'sys_executable': sys.executable},
        target_path=retag_cloudify_target_path)
    os.chmod(retag_cloudify_target_path, 0755)
예제 #5
0
def create_app():

    ctx.instance.runtime_properties['dns_domain'] = 'cluster.local'
    ctx.instance.runtime_properties['dns_replicas'] = 1
    ctx.instance.runtime_properties['enable_cluster_dns'] = True

    rc_file = ctx.download_resource_and_render(
        os.path.join(BASE_DIR, 'rc.yaml'))
    svc_file = ctx.download_resource_and_render(
        os.path.join(BASE_DIR, 'svc.yaml'))

    response = get('kube-system/replicationcontrollers/kube-dns-v11')

    if response.status_code != WORKING_CODE:
        ctx.logger.debug('CODE: {0} RESPONSE: {1}'.format(
            response.status_code, response.json()))
        with open(rc_file, 'r') as f:
            rc_yaml = yaml.load(f)
        created = create('replicationcontrollers', rc_yaml)
        if created != SUCCESS_CODE and created != ALREADY_EXISTS:
            raise NonRecoverableError(
                'Failed to create replication controller.')

    timeout = time.time() + 60
    while True:
        response = get('kube-system/replicationcontrollers/kube-dns-v11')
        if response.status_code == WORKING_CODE:
            ctx.logger.info('DNS Replication Controller is working.')
            ctx.logger.debug(
                'replicationcontroller get response.text: {0}'.format(
                    response.text))
            break
        if time.time() > timeout:
            raise NonRecoverableError(
                'Timed out waiting for ReplicationController. '
                'More info: {0}'.format(response.text))

    response = get('kube-system/services/kube-dns')

    if response.status_code != WORKING_CODE:
        ctx.logger.debug('CODE: {0} RESP: {1}'.format(response.status_code,
                                                      response.json()))
        with open(svc_file, 'r') as f:
            svc_yaml = yaml.load(f)
        created = create('services', svc_yaml)
        if created != SUCCESS_CODE and created != ALREADY_EXISTS:
            raise NonRecoverableError('Failed to create service.')
    timeout = time.time() + 30
    while True:
        response = get('kube-system/services/kube-dns')
        if response.status_code == WORKING_CODE:
            ctx.logger.info('DNS Service is working.')
            ctx.logger.debug('services get response.text: {0}'.format(
                response.text))
            break
        if time.time() > timeout:
            raise NonRecoverableError('Timed out waiting for Service. '
                                      'More info: {0}'.format(response.text))
def _set_nginx_config_file(host_ip):
    project_root_dir = os.path.expanduser('~/djangosample/src')
    config_file = os.path.join(project_root_dir, 'mysite/nginx.conf')
    params = ctx.source.node.properties.copy()
    params.update({'host_ip': host_ip,
                   'project_root_dir': project_root_dir})
    ctx.download_resource_and_render(resource_path='config/nginx/nginx.conf.template',
                                     target_path=config_file,
                                     template_variables=params)
예제 #7
0
def get_package_dir_from_dir(resource_dir, template_variables={}):
    # Case, when user defines path to a directory, where files, which need to
    # be downloaded and rendered, reside.

    ctx.logger.debug('only resource_dir is not empty.')

    # Deal with ZIP files
    filename, extension = os.path.splitext(resource_dir)
    if extension == '.zip':
        archive_path = os.path.join(get_deployment_directory(), resource_dir)
        target_directory = os.path.join(get_deployment_directory(), filename)
        resource_dir = filename
        extract_archive_from_path(archive_path, target_directory)

    merged_list = []

    # This loop goes through a directory defined in resource_dir parameter
    # and prepares a list of paths inside it.
    for resource_path in os.walk(
            os.path.join(get_deployment_directory(), resource_dir)):
        trimmed_resource_path = get_resource_relative_path(
            resource_path, os.path.join(get_deployment_directory()))

        if resource_path[2]:
            for filename in resource_path[2]:
                merged_list.append(
                    os.path.join(trimmed_resource_path, filename))
        elif not resource_path[1] and not resource_path[2]:
            merged_list.append(trimmed_resource_path)

    # This loop goes through the merged_list and downloads the rest of
    # files to our working directory.
    for template_path in merged_list:
        template_dirname = os.path.dirname(template_path)
        download_from_file = template_path
        download_to_directory = os.path.join(get_current_working_directory(),
                                             template_dirname)
        download_to_file = os.path.join(get_current_working_directory(),
                                        download_from_file)

        try:
            os.makedirs(download_to_directory)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        try:
            ctx.download_resource_and_render(download_from_file,
                                             download_to_file,
                                             template_variables.copy())
            if os.path.splitext(download_to_file)[1] == '.py':
                os.chmod(download_to_file, 0755)
        except IOError as e:
            if e.errno != errno.EISDIR:
                raise

    return get_current_working_directory()
def deploy_script(script_name):
    config_file_temp_destination = join(tempfile.gettempdir(), script_name)
    ctx.download_resource_and_render(
        join('components', 'manager-ip-setter', 'scripts', script_name),
        config_file_temp_destination)
    remote_script_path = join(MANAGER_IP_SETTER_DIR, script_name)
    utils.move(config_file_temp_destination, remote_script_path)
    utils.chmod('+x', remote_script_path)
    utils.systemd.configure(MANAGER_IP_SETTER_SERVICE_NAME)
예제 #9
0
def render_to_file(src, dst):
    """Render a file and move it to the desired destination.

    :param src: the current path of the file to be rendered.
    :param dst: the desired path to move the file to.
    """
    tmp = os.path.join(os.path.dirname(__file__), src.split('/')[-1])
    ctx.download_resource_and_render(src, tmp)
    cmd = ['mv', tmp, dst]
    run_command(cmd, su=True)
예제 #10
0
def get_package_dir(resource_list, template_variables):
    """ Download resources and return the path. """

    work_dir = tempfile.mkdtemp()
    for resource_path in resource_list:
        resource_name = os.path.basename(resource_path)
        download_to = os.path.join(work_dir, resource_name)
        ctx.download_resource_and_render(resource_path, download_to,
                                         template_variables)
    return work_dir
예제 #11
0
파일: create.py 프로젝트: ptanX/cloudify
def deploy_utils():
    temp_destination = join(tempfile.gettempdir(), 'utils.py')
    ctx.download_resource_and_render(
        join('components', 'utils.py'),
        temp_destination,
    )
    utils_path = join(MANAGER_IP_SETTER_DIR, 'utils.py')
    utils.move(temp_destination, utils_path)

    utils.chmod('550', utils_path)
    utils.chown('root', utils.CLOUDIFY_GROUP, utils_path)
예제 #12
0
파일: git.py 프로젝트: carriercomm/clue
 def configure(self):
     # configure commit-msg hook
     hook_path = self.repo_location / '.git' / 'hooks' / 'commit-msg'
     ctx.download_resource_and_render(
         'resources/commit-msg',
         template_variables={'sys_executable': sys.executable},
         target_path=hook_path)
     os.chmod(hook_path, 0o755)
     # git config
     for key, value in self.git_config.items():
         self.git.config(key, value).wait()
예제 #13
0
def deploy_utils():
    temp_destination = join(tempfile.gettempdir(), 'utils.py')
    ctx.download_resource_and_render(
        join('components', 'utils.py'),
        temp_destination,
    )
    utils_path = join(MANAGER_IP_SETTER_DIR, 'utils.py')
    utils.move(temp_destination, utils_path)

    utils.chmod('550', utils_path)
    utils.chown('root', utils.CLOUDIFY_GROUP, utils_path)
예제 #14
0
 def configure(self):
     # configure commit-msg hook
     hook_path = self.repo_location / '.git' / 'hooks' / 'commit-msg'
     ctx.download_resource_and_render(
         'resources/commit-msg',
         template_variables={'sys_executable': sys.executable},
         target_path=hook_path)
     os.chmod(hook_path, 0o755)
     # git config
     for key, value in self.git_config.items():
         self.git.config(key, value).wait()
예제 #15
0
 def _download_resource_and_render(self, source, dest, service_name,
                                   load_ctx):
     resource_name = os.path.basename(dest)
     ctx.logger.info('Downloading resource {0} to {1}'.format(
         resource_name, dest))
     if load_ctx:
         params = self._load_node_props(service_name)
         tmp_file = ctx.download_resource_and_render(source, '', params)
     else:
         # rendering will be possible only for runtime properties
         tmp_file = ctx.download_resource_and_render(source, '')
     move(tmp_file, dest)
예제 #16
0
def main():
    '''Entry point'''
    ctx.logger.info('Removing default IIS web application')
    remove_folder_contents(IIS_DEF_DIR)
    ctx.logger.info('Downloading web application to {0}'.format(IIS_DEF_DIR))
    try:
        ctx.download_resource_and_render(
            'webapp/index.html', os.path.join(IIS_DEF_DIR, 'index.html'))
        ctx.download_resource_and_render(
            'webapp/style.css', os.path.join(IIS_DEF_DIR, 'style.css'))
    except HttpException as exc:
        ctx.logger.error('HttpException during web application '
                         'install. "{0}"'.format(str(exc)))
예제 #17
0
파일: git.py 프로젝트: limor-gs/clue
 def status(self, active):
     if active and not self.active_feature.branch:
         return
     for git_prompt_path in self.git_prompt_paths:
         if git_prompt_path.expanduser().exists():
             break
     else:
         git_prompt_path = None
     if git_prompt_path:
         script_path = ctx.download_resource_and_render(
             'resources/git-branch-state.sh',
             template_variables={
                 'git_prompt_path': git_prompt_path,
                 'repo_location': self.repo_location
             })
         try:
             os.chmod(script_path, 0o755)
             script = sh.Command(script_path)
             branch_state = script().stdout.strip()
         finally:
             os.remove(script_path)
     else:
         branch_state = self.current_branch
     ctx.logger.info(branch_state)
     self.git.status(s=True).wait()
예제 #18
0
    def create(self):

        try:
            if isinstance(self.template_variables, dict):
                downloaded_file_path = \
                    ctx.download_resource_and_render(
                        self.resource_path,
                        template_variables=self.template_variables)
            else:
                downloaded_file_path = \
                    ctx.download_resource(self.resource_path)
        except HttpException as e:
            err = '{0}'.format(str(e))
            if self.allow_failure is False:
                raise NonRecoverableError(err)
            ctx.logger.error(err)
            return True

        if self.use_sudo:
            cp_out = execute_command('sudo cp {0} {1}'.format(
                repr(downloaded_file_path), repr(self.file_path)))
            chown_out = execute_command('sudo chown {0} {1}'.format(
                repr(self.owner), self.file_path))
            chmod_out = execute_command('sudo chmod {0} {1}'.format(
                repr(self.mode), self.file_path))
            if cp_out is False or chown_out is False or chmod_out is False:
                raise NonRecoverableError(
                    'Failed, see previous ERROR log message.')
            return True

        if not isinstance(self.owner, basestring):
            raise NonRecoverableError('Property owner must be a string.')

        split_owner = self.owner.split(':')

        if len(split_owner) == 1:
            user_string = split_owner[0]
            group_string = split_owner[0]
        elif len(split_owner) == 2:
            user_string = split_owner[0]
            group_string = split_owner[1]
        else:
            raise NonRecoverableError(
                'Property owner must be one of the following '
                'formats: "user" or "user:group".')

        try:
            uid = pwd.getpwnam(user_string).pw_uid
            gid = grp.getgrnam(group_string).gr_gid
        except KeyError as e:
            raise NonRecoverableError('{0}'.format(str(e)))

        try:
            os.rename(downloaded_file_path, self.file_path)
            os.chown(self.file_path, uid, gid)
            os.chmod(self.file_path, self.mode)
        except OSError as e:
            raise NonRecoverableError('{0}'.format(str(e)))

        return True
def _yaml_from_files(resource_path, target_path=None, template_variables=None):

    template_variables = template_variables or {}

    downloaded_file_path = \
        ctx.download_resource_and_render(
            resource_path,
            target_path,
            template_variables)

    # Validate file size
    if os.path.isfile(downloaded_file_path) \
            and os.path.getsize(downloaded_file_path) == 0:
        raise KuberentesInvalidDefinitionError(
            'Invalid resource file definition.')

    with open(downloaded_file_path) as outfile:
        file_content = outfile.read()

    # Validate file content if it contains at least one dict
    file_content = file_content.strip()
    if len(list(yaml.load_all(file_content))) == 0:
        raise KuberentesInvalidDefinitionError(
            'Invalid resource file definition.')

    return yaml.load_all(file_content)
예제 #20
0
def deploy_blueprint_resource(source, destination):
    """Downloads a resource from the blueprint to a destination.

    This expands `download-resource` as a `sudo mv` is required after
    having downloaded the resource.
    """
    ctx.logger.info('Deploying blueprint resource {0} to {1}'.format(
        source, destination))
    tmp_file = ctx.download_resource_and_render(source)
    move(tmp_file, destination)
예제 #21
0
def get_package_dir_from_list(resource_list, template_variables={}):
    # Case, when user defines a list of files in resource_list,
    # which need to be downloaded and rendered.

    ctx.logger.debug('only resource_list is not empty.')

    # Deal with ZIP files in resource_list
    for template_path in copy.copy(resource_list):

        filename, extension = os.path.splitext(template_path)

        if extension == '.zip':

            resource_list.remove(template_path)

            archive_path = os.path.join(get_deployment_directory(),
                                        template_path)
            target_directory = os.path.join(get_deployment_directory(),
                                            filename)
            extract_archive_from_path(archive_path, target_directory)

            for extracted_template in os.walk(target_directory):
                extracted_template_path = get_resource_relative_path(
                    extracted_template, get_deployment_directory())
                if extracted_template[2]:
                    for filename in extracted_template[2]:
                        resource_list.append(
                            os.path.join(extracted_template_path, filename))
                elif not extracted_template[1] and not extracted_template[2]:
                    resource_list.append(extracted_template_path)

    for template_path in resource_list:
        resource_name = os.path.basename(template_path)
        download_to = os.path.join(get_current_working_directory(),
                                   resource_name)
        try:
            ctx.download_resource_and_render(template_path, download_to,
                                             template_variables.copy())
        except IOError as e:
            if e.errno != errno.EISDIR:
                raise

    return get_current_working_directory()
def deploy_blueprint_resource(source, destination):
    """Downloads a resource from the blueprint to a destination.

    This expands `download-resource` as a `sudo mv` is required after
    having downloaded the resource.
    """
    ctx.logger.info('Deploying blueprint resource {0} to {1}'.format(
        source, destination))
    tmp_file = ctx.download_resource_and_render(source)
    move(tmp_file, destination)
예제 #23
0
def get_rendered_file(_p, _v):

    _, _temporary_file_path = tempfile.mkstemp()

    _rendered_file = \
        ctx.download_resource_and_render(
            _p,
            target_path=_temporary_file_path,
            template_variables=_v or None)

    return _rendered_file
예제 #24
0
 def encode_policy(self, policy):
     self.ctx.logger.info('Encoding policy: {0}'.format(policy))
     if isinstance(policy, basestring):
         return policy
     elif isinstance(policy, dict):
         policy_source = ctx.download_resource_and_render(**policy)
         with open(policy_source, 'r') as infile:
             return base64.b64encode(infile.read())
     raise TypeError(
         'The policy {0} is neither a dict of type '
         'cloudify.types.policies.File, nor a base64 encoded string.')
def copy_start_script():
    try:
        with open('/opt/manager/rest-security.conf') as security_config_file:
            security_config_content = security_config_file.read()
            security_config = json.loads(security_config_content)
        params = {
            'username': security_config['admin_username'],
            'password': security_config['admin_password']
        }
        script_name = 'config_local_cfy.sh'
        script_destination = join(utils.get_exec_tempdir(), script_name)
        ctx.download_resource_and_render(join(CONFIG_PATH, script_name),
                                         script_destination,
                                         params)
        utils.sudo(['mv', script_destination,
                    join(utils.CLOUDIFY_HOME_DIR, script_name)])
        utils.chmod('+x', join(utils.CLOUDIFY_HOME_DIR, script_name))
    except Exception as ex:
        ctx.logger.warn('Failed to deploy local cli config script. '
                        'Error: {0}'.format(ex))
예제 #26
0
def drain_and_remove():
    """Mark the node as unschedulable, evict all pods, and remove it.

    Runs `kubectl drain` and `kubectl delete nodes` on the kubernetes
    master in order to drain and afterwards remove the specified node
    from the cluster.

    """
    if ctx.node.properties['master']:  # FIXME Is this necessary?
        return

    # Get master instance.
    master = ctx.instance.relationships[0]._target.instance

    # Render script.
    script = os.path.join(os.path.dirname(__file__), 'drain-node.sh')
    ctx.download_resource_and_render(
        os.path.join('scripts', 'drain-node.sh'), script,
        template_variables={
            'server_ip': master.runtime_properties.get('server_ip',''),
            'auth_user': master.runtime_properties['auth_user'],
            'auth_pass': master.runtime_properties['auth_pass'],
            'hostname': ctx.instance.runtime_properties.get('machine_name', '').lower()
        },
    )

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=master.runtime_properties['cloud_id'],
        machine_id=master.runtime_properties['machine_id'],
    )

    ctx.logger.info('Running "kubectl drain && kubectl delete" on %s', machine)

    _add_run_remove_script(
        cloud_id=machine.cloud.id,
        machine_id=machine.id,
        script_path=os.path.abspath(script),
        script_name='kubectl_drain_%s' % random_string(length=4)
    )
def prepare_cloud_init():
    """Render the cloud-init script.

    This method is executed if the cloud provider is included in the
    CLOUD_INIT_PROVIDERS in order to prepare the cloud-init that is
    used to install kubernetes on each of the provisioned VMs at boot
    time.

    This method, based on each node's type, is meant to invoke:

        get_master_init_args()
        get_worker_init_args()

    in order to get the arguments required by the kubernetes installation
    script.

    The cloud-init.yml is just a wrapper around the mega-deploy.sh, which
    is provided as a parameter at VM provision time. In return, we avoid
    the extra step of uploading an extra script and executing it over SSH.

    """
    if ctx.node.properties['master']:
        arguments = get_master_init_args()
    else:
        arguments = get_worker_init_args()

    ctx.logger.debug('Will run mega-deploy.sh with: %s', arguments)
    ctx.instance.runtime_properties['cloud_init_arguments'] = arguments

    ctx.logger.debug('Current runtime: %s', ctx.instance.runtime_properties)

    ctx.logger.info('Rendering cloud-init.yml')

    cloud_init = os.path.join(os.path.dirname(__file__), 'cloud_init.yml')
    ctx.download_resource_and_render(
        os.path.join('cloud-init', 'cloud-init.yml'), cloud_init
    )
    with open(os.path.abspath(cloud_init)) as fobj:
        ctx.instance.runtime_properties['cloud_init'] = fobj.read()
예제 #28
0
파일: pip.py 프로젝트: carriercomm/clue
def configure_virtualenv(
        virtualenv_location,
        constraints,
        postactivate_resource_path,
        git_retag_cloudify_resource_path,
        register_python_argcomplete,
        **_):

    # constraints.txt
    constraints = constraints or []
    constraints_path = os.path.join(
        ctx.instance.runtime_properties['virtualenv_location'],
        'constraints.txt')
    with open(constraints_path, 'w') as f:
        f.write('\n'.join(constraints))

    virtualenv_bin = path(virtualenv_location) / 'bin'

    # clue symlink
    clue_source_path = path(sys.executable).dirname() / 'clue'
    clue_target_path = virtualenv_bin / 'clue'
    if not clue_target_path.exists():
        os.symlink(clue_source_path, clue_target_path)

    # postactivate
    variables = dict(register_python_argcomplete=register_python_argcomplete)
    ctx.download_resource_and_render(
        postactivate_resource_path,
        target_path=virtualenv_bin / 'postactivate',
        template_variables=variables)

    # git-retag-cloudify
    retag_cloudify_target_path = virtualenv_bin / 'git-retag-cloudify'
    ctx.download_resource_and_render(
        git_retag_cloudify_resource_path,
        template_variables={'sys_executable': sys.executable},
        target_path=retag_cloudify_target_path)
    os.chmod(retag_cloudify_target_path, 0755)
예제 #29
0
def _yaml_from_file(resource_path, target_path=None, template_variables=None):

    template_variables = template_variables or {}

    downloaded_file_path = \
        ctx.download_resource_and_render(
            resource_path,
            target_path,
            template_variables)

    with open(downloaded_file_path) as outfile:
        file_content = outfile.read()

    return yaml.load(file_content)
예제 #30
0
def _yaml_from_files(resource_path, target_path=None, template_variables=None):

    template_variables = template_variables or {}

    downloaded_file_path = \
        ctx.download_resource_and_render(
            resource_path,
            target_path,
            template_variables)

    with open(downloaded_file_path) as outfile:
        file_content = outfile.read()

    return [
        yaml.load(content)
        for content in file_content.replace("\r\n", "\n").split("\n---\n")
    ]
예제 #31
0
파일: git.py 프로젝트: max-orlov/clue
 def status(self, active):
     if active and not self.active_branch_set:
         return
     for git_prompt_path in self.git_prompt_paths:
         if git_prompt_path.expanduser().exists():
             break
     else:
         git_prompt_path = None
     if git_prompt_path:
         script_path = ctx.download_resource_and_render(
             'resources/git-branch-state.sh', template_variables={
                 'git_prompt_path': git_prompt_path,
                 'repo_location': self.repo_location})
         try:
             os.chmod(script_path, 0o755)
             script = sh.Command(script_path)
             branch_state = script().stdout.strip()
         finally:
             os.remove(script_path)
     else:
         branch_state = self.current_branch
     ctx.logger.info(branch_state)
     self.git.status(s=True).wait()
예제 #32
0
def create_service(service_name):
    service_path = '/etc/systemd/system/{}.service'.format(service_name)
    if not os.path.isfile(service_path):
        try:
            _tv = {'home_dir': os.path.expanduser('~')}
            cfy_service = \
                ctx.download_resource_and_render(
                    'resources/{}.service'.format(service_name),
                    template_variables=_tv)
        except HttpException:
            raise NonRecoverableError(
                '{}.service not in resources.'.format(service_name))

        execute_command(['sudo', 'cp', cfy_service, service_path])
        execute_command([
            'sudo', 'cp', service_path,
            '/etc/systemd/system/multi-user.target.wants/'
        ])

        execute_command(['sudo', 'systemctl', 'daemon-reload'])
        execute_command(
            ['sudo', 'systemctl', 'enable', '{}.service'.format(service_name)])
        execute_command(
            ['sudo', 'systemctl', 'start', '{}.service'.format(service_name)])
예제 #33
0
# coding=UTF-8

from cloudify import ctx
from cloudify.exceptions import NonRecoverableError
from subprocess import check_output, CalledProcessError


def do(*args, **kwargs):
    try:
        return check_output(args), 0
    except CalledProcessError as e:
        if kwargs.get('fail_on_nonzero'):
            raise kwargs.get('exception', NonRecoverableError)(
                '[{}] {}'.format(e.cmd, e.message))
        else:
            return e.output, e.returncode


def sudo(*args, **kwargs):
    do('sudo', *args, **kwargs)


ctx.logger.info("[fix_fqdn] FQDN")
ctx.download_resource_and_render('templates/hosts', '/tmp/hosts')

sudo('mv', '/tmp/hosts', '/etc/hosts')
예제 #34
0
    def create(self):

        try:
            if isinstance(self.template_variables, dict):
                downloaded_file_path = \
                    ctx.download_resource_and_render(
                        self.resource_path,
                        template_variables=self.template_variables)
            else:
                downloaded_file_path = \
                    ctx.download_resource(self.resource_path)
        except HttpException as e:
            err = '{0}'.format(str(e))
            if self.allow_failure is False:
                raise NonRecoverableError(err)
            ctx.logger.error(err)
            return True

        if self.use_sudo:
            add_shell = {'shell': True}
            cp_out = execute_command(
                ['sudo', 'cp',
                 str(downloaded_file_path),
                 str(self.file_path)],
                extra_args=add_shell)
            chown_out = execute_command(
                ['sudo', 'chown',
                 str(self.owner),
                 str(self.file_path)],
                extra_args=add_shell)
            chmod_out = execute_command(
                ['sudo', 'chmod',
                 str(self.mode),
                 str(self.file_path)],
                extra_args=add_shell)
            if cp_out is False or chown_out is False or chmod_out is False:
                raise NonRecoverableError(
                    'Sudo command failed. '
                    'Most likely this is related to {0} user permissions. '
                    'See previous ERROR log message.'.format(getuser()))
            return True

        if not isinstance(self.owner, text_type):
            raise NonRecoverableError('Property owner must be a string.')

        split_owner = self.owner.split(':')

        if len(split_owner) == 1:
            user_string = split_owner[0]
            group_string = split_owner[0]
        elif len(split_owner) == 2:
            user_string = split_owner[0]
            group_string = split_owner[1]
        else:
            raise NonRecoverableError(
                'Property owner must be one of the following '
                'formats: "user" or "user:group".')

        try:
            uid = pwd.getpwnam(user_string).pw_uid
            gid = grp.getgrnam(group_string).gr_gid
        except KeyError as e:
            raise NonRecoverableError('{0}'.format(str(e)))

        try:
            shutil.move(downloaded_file_path, self.file_path)
            os.chown(self.file_path, uid, gid)
            os.chmod(self.file_path, int(str(self.mode), 8))
        except OSError as e:
            raise NonRecoverableError('{0}'.format(str(e)))

        return True
예제 #35
0
파일: idea.py 프로젝트: mxmrlv/clue
def create_idea_project(virtualenv_name, **_):
    project_dir, repo_paths, package_paths, resource_dirs = _extract_dirs()
    if not project_dir:
        ctx.logger.info('No project dir configured.')
        return
    project_dir = path(project_dir)
    idea_dir = project_dir / '.idea'
    name_file = idea_dir / '.name'
    project_iml = idea_dir / '{}.iml'.format(project_dir.basename())
    misc_xml = idea_dir / 'misc.xml'
    modules_xml = idea_dir / 'modules.xml'
    vcs_xml = idea_dir / 'vcs.xml'
    module_paths = []
    for resource_dir in resource_dirs:
        if resource_dir.endswith('/'):
            resource_dir = resource_dir[:-1]
        resource_dir = path(resource_dir)
        iml_path = resource_dir / '{}.iml'.format(resource_dir.basename())
        module_paths.append(iml_path)
        if not iml_path.exists():
            ctx.logger.info('Adding resource module: {}'.format(iml_path))
            ctx.download_resource_and_render(
                resource_path='resources/idea/module.xml',
                target_path=iml_path,
                template_variables={'module_type': 'WEB_MODULE'})
    for package_path in package_paths:
        if package_path.endswith('/'):
            package_path = package_path[:-1]
        package_path = path(package_path)
        iml_path = package_path / '{}.iml'.format(package_path.basename())
        module_paths.append(iml_path)
        if not iml_path.exists():
            ctx.logger.info('Adding python module: {}'.format(iml_path))
            ctx.download_resource_and_render(
                resource_path='resources/idea/module.xml',
                target_path=iml_path,
                template_variables={'module_type': 'PYTHON_MODULE'})
    if not idea_dir.exists():
        idea_dir.mkdir_p()
    if not name_file.exists():
        name_file.write_text(virtualenv_name)
    if not project_iml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/module.xml',
            target_path=project_iml,
            template_variables={'module_type': 'JAVA_MODULE'})
    if not misc_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/misc.xml',
            target_path=misc_xml,
            template_variables={'virtualenv_name': virtualenv_name})
    if not modules_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/modules.xml',
            target_path=modules_xml,
            template_variables={'module_paths': module_paths})
    if not vcs_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/vcs.xml',
            target_path=vcs_xml,
            template_variables={'repo_paths': repo_paths})
# coding=UTF-8

from cloudify import ctx
from cloudify.exceptions import NonRecoverableError
from subprocess import check_output, CalledProcessError
from socket import gethostname


def do(*args, **kwargs):
    try:
        return check_output(args), 0
    except CalledProcessError as e:
        if kwargs.get('fail_on_nonzero'):
            raise kwargs.get('exception', NonRecoverableError)(
                '[{}] {}'.format(e.cmd, e.message))
        else:
            return e.output, e.returncode


def sudo(*args, **kwargs):
    do('sudo', *args, **kwargs)


ctx.logger.info("[fix_fqdn] FQDN")
ctx.download_resource_and_render('templates/hosts', '/tmp/hosts', {'hostname': gethostname()})

sudo('mv', '/tmp/hosts', '/etc/hosts')
예제 #37
0
def get_package_dir_from_dir_and_list(resource_dir,
                                      resource_list,
                                      template_variables={}):
    # Case, when user defines a directory with files, which need to be
    # downloaded, but doesn't want to render all of them - only these
    # defined in resource_list.

    ctx.logger.debug('resource_dir and resource_list params are not empty.')

    # Deal with ZIP files
    filename, extension = os.path.splitext(resource_dir)
    if extension == '.zip':
        archive_path = os.path.join(get_deployment_directory(), resource_dir)
        target_directory = os.path.join(get_deployment_directory(), filename)
        resource_dir = filename
        extract_archive_from_path(archive_path, target_directory)

    # Deal with ZIP files in resource_list
    for template_path in copy.copy(resource_list):

        filename, extension = os.path.splitext(template_path)

        if extension == '.zip':
            resource_list.remove(template_path)
            archive_path = os.path.join(get_deployment_directory(),
                                        resource_dir, template_path)
            target_directory = os.path.join(get_deployment_directory(),
                                            resource_dir, filename)
            extract_archive_from_path(archive_path, target_directory)

            for extracted_template in os.walk(target_directory):
                extracted_template_path = get_resource_relative_path(
                    extracted_template,
                    os.path.join(get_deployment_directory(), resource_dir))

                if extracted_template[2]:
                    for filename in extracted_template[2]:
                        resource_list.append(
                            os.path.join(extracted_template_path, filename))
                elif not extracted_template[1] and not extracted_template[2]:
                    resource_list.append(extracted_template_path)

    merged_list = []

    # This loop goes through a directory defined in resource_dir parameter
    # and prepares a list of paths inside it.
    for resource_path in os.walk(
            os.path.join(get_deployment_directory(), resource_dir)):
        trimmed_resource_path = get_resource_relative_path(
            resource_path, os.path.join(get_deployment_directory()))

        if resource_path[2]:
            for filename in resource_path[2]:
                merged_list.append(
                    os.path.join(trimmed_resource_path, filename))
        elif not resource_path[1] and not resource_path[2]:
            merged_list.append(trimmed_resource_path)

    # This loop goes through a templates list defined in resource_list
    # parameter. For each template it renders it (resolves all of variables
    # defined in template_variables parameter) and downloads it to working
    # directory. Finally, it removes a path to this file from the merged_list,
    # because it should be ommitted at the next step, which is copying the rest
    # of the files, which are not templates.
    for template_path in resource_list:
        template_dirname = os.path.join(resource_dir,
                                        os.path.dirname(template_path))
        download_from_file = os.path.join(resource_dir, template_path)
        download_to_directory = os.path.join(get_current_working_directory(),
                                             template_dirname)
        download_to_file = os.path.join(get_current_working_directory(),
                                        download_from_file)

        try:
            os.makedirs(download_to_directory)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        try:
            ctx.download_resource_and_render(download_from_file,
                                             download_to_file,
                                             template_variables.copy())
            if os.path.splitext(download_to_file)[1] == '.py':
                os.chmod(download_to_file, 0755)
        except IOError as e:
            if e.errno != errno.EISDIR:
                raise

        if download_from_file in merged_list:
            merged_list.remove(download_from_file)

    # This loop goes through the merged_list and downloads the rest of
    # files to our working directory.
    for resource_path in merged_list:
        resource_dirname = os.path.dirname(resource_path)
        download_to_directory = os.path.join(get_current_working_directory(),
                                             resource_dirname)
        download_to_file = os.path.join(get_current_working_directory(),
                                        resource_path)

        try:
            os.makedirs(download_to_directory)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        try:
            ctx.download_resource(resource_path, download_to_file)
            if os.path.splitext(download_to_file)[1] == '.py':
                os.chmod(download_to_file, 0755)
        except IOError as e:
            if e.errno != errno.EISDIR:
                raise

    return get_current_working_directory()
import os


def do(*args, **kwargs):
    try:
        return check_output(args), 0
    except CalledProcessError as e:
        if kwargs.get('fail_on_nonzero'):
            raise kwargs.get('exception', NonRecoverableError)(
                '[{}] {}'.format(e.cmd, e.message))
        else:
            return e.output, e.returncode


def sudo(*args, **kwargs):
    do('sudo', *args, **kwargs)


ORYX_BIN = os.path.expanduser('~/oryx')
cc = ORYX_BIN + '/compute-classpath.sh'
conf = ORYX_BIN + '/oryx.conf'
template_vars = ctx.instance.runtime_properties.copy()

do('rm', '-f', cc)
do('rm', '-f', conf)

ctx.download_resource('resources/compute-classpath.sh', cc)
ctx.download_resource_and_render('templates/oryx.conf', conf, template_vars)

do('chmod', '+x', cc)
예제 #39
0
# coding=UTF-8

from cloudify import ctx
from cloudify.exceptions import NonRecoverableError
from subprocess import check_output, CalledProcessError


def do(*args, **kwargs):
    try:
        return check_output(args), 0
    except CalledProcessError as e:
        if kwargs.get('fail_on_nonzero'):
            raise kwargs.get('exception',
                             NonRecoverableError)('[{}] {}'.format(
                                 e.cmd, e.message))
        else:
            return e.output, e.returncode


def sudo(*args, **kwargs):
    do('sudo', *args, **kwargs)


ctx.logger.info("[fix_fqdn] FQDN")
ctx.download_resource_and_render('templates/hosts', '/tmp/hosts')

sudo('mv', '/tmp/hosts', '/etc/hosts')
예제 #40
0
파일: idea.py 프로젝트: dankilman/clue
def create_idea_project(virtualenv_name, **_):
    project_dir, repo_paths, package_paths, resource_dirs = _extract_dirs()
    if not project_dir:
        ctx.logger.info('No project dir configured.')
        return
    project_dir = path(project_dir)
    idea_dir = project_dir / '.idea'
    name_file = idea_dir / '.name'
    project_iml = idea_dir / '{}.iml'.format(project_dir.basename())
    misc_xml = idea_dir / 'misc.xml'
    modules_xml = idea_dir / 'modules.xml'
    vcs_xml = idea_dir / 'vcs.xml'
    module_paths = []
    for resource_dir in resource_dirs:
        if resource_dir.endswith('/'):
            resource_dir = resource_dir[:-1]
        resource_dir = path(resource_dir)
        iml_path = resource_dir / '{}.iml'.format(resource_dir.basename())
        module_paths.append(iml_path)
        if not iml_path.exists():
            ctx.logger.info('Adding resource module: {}'.format(iml_path))
            ctx.download_resource_and_render(
                resource_path='resources/idea/module.xml',
                target_path=iml_path,
                template_variables={'module_type': 'WEB_MODULE'})
    for package_path in package_paths:
        if package_path.endswith('/'):
            package_path = package_path[:-1]
        package_path = path(package_path)
        iml_path = package_path / '{}.iml'.format(package_path.basename())
        module_paths.append(iml_path)
        if not iml_path.exists():
            ctx.logger.info('Adding python module: {}'.format(iml_path))
            ctx.download_resource_and_render(
                resource_path='resources/idea/module.xml',
                target_path=iml_path,
                template_variables={'module_type': 'PYTHON_MODULE'})
    if not idea_dir.exists():
        idea_dir.mkdir_p()
    if not name_file.exists():
        name_file.write_text(virtualenv_name)
    if not project_iml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/module.xml',
            target_path=project_iml,
            template_variables={'module_type': 'JAVA_MODULE'})
    if not misc_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/misc.xml',
            target_path=misc_xml,
            template_variables={'virtualenv_name': virtualenv_name})
    if not modules_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/modules.xml',
            target_path=modules_xml,
            template_variables={'module_paths': module_paths})
    if not vcs_xml.exists():
        ctx.download_resource_and_render(
            resource_path='resources/idea/vcs.xml',
            target_path=vcs_xml,
            template_variables={'repo_paths': repo_paths})