Exemplo n.º 1
0
    def cmd_source_run(self, *argv, **kwargs):
        """
        Syntax: source.run[path,source,branch,envars,facts,username,password,private_key,chown_user,chown_group,rotate,type]
        """
        path = kwargs.get('path', None)
        url = kwargs.get('source', None)
        branch = kwargs.get('branch', None)
        user = kwargs.get('username', None)
        passwd = kwargs.get('password', None)
        private_key = kwargs.get('private_key', None)
        chown_user = kwargs.get('chown_user', None)
        chown_group = kwargs.get('chown_group', None)
        rotate = kwargs.get('rotate', True)
        extract = kwargs.get('extract', True)

        stype = kwargs.get('type', None)
        metadata = kwargs.get('metadata', None)

        if not path or not url or not stype:
            raise ecm.InvalidParameters(self.cmd_source_run.__doc__)
            
        if private_key:
            try:
                private_key = b64decode(private_key)
            except:
                raise ecm.InvalidParameters("Invalid private key format")

        if stype.upper() in ('URL', 'FILE'):
            source = FILE(path, rotate, extract)

        elif stype.upper() == 'GIT':
            source = GIT(path, rotate)

        elif stype.upper() == 'SVN':
            source = SVN(path, rotate)

        else:
            raise ecm.InvalidParameters("Unknown source")

        # Set environment variables before execution
        envars = ecm.metadata_to_env(metadata_b64=metadata)

        # Update metadata
        ecm.write_metadata(metadata_b64=metadata)

        retval = source.clone(url=url,
                              branch=branch,
                              envars=envars,
                              username=user,
                              password=passwd,
                              private_key=private_key)

        # Chown to specified user/group
        if chown_user and chown_group and os.path.isdir(path):
            ecm.chown(path, chown_user, chown_group, recursive=True)
            retval['stdout'] += ecm.output("Owner changed to '%s':'%s'" % (chown_user, chown_group))

        return self._return(retval)
Exemplo n.º 2
0
    def cmd_configfile_run(self, *argv, **kwargs):
        """
        Deploy a file
        Syntax: configfile.run[configfile,file,chown_user,chown_group,chmod,rotate,command,runas]
        """
        code_base64     = kwargs.get('configfile', None)
        filename        = kwargs.get('path', None)
        chown_user      = kwargs.get('chown_user', None)
        chown_group     = kwargs.get('chown_group', None)
        chmod           = kwargs.get('chmod', None)
        rotate          = kwargs.get('rotate', False)

        command         = kwargs.get('command', None)
        runas           = kwargs.get('command_runas', None)

        if not code_base64 or not filename:
            raise ecm.InvalidParameters(self.cmd_configfile_run.__doc__)

        ret = {'out': 0,'stdout': '','stderr': ''}
        try:
            if rotate and os.path.isfile(filename):
                new_file = filename + '_rotated_' + ecm.utime()
                move(filename, new_file)
                ret['stdout'] = ecm.output("Old configfile moved to '%s'" % new_file)

            # Write down file
            ecm.file_write(filename, b64decode(code_base64))
            ret['stdout'] += ecm.output("Configfile created successfully at '%s'" % filename)

        except Exception as e:
            raise Exception("Unable to write configfile: %s" % e)

        try:
            # Chown to specified user/group
            if chown_user and chown_group and os.path.isfile(filename):
                ecm.chown(filename, chown_user, chown_group)
                ret['stdout'] += ecm.output("Owner changed to '%s':'%s'" % (chown_user, chown_group))

            # Chown to specified user/group
            if chmod and os.path.isfile(filename):
                ecm.chmod(filename, chmod)
                ret['stdout'] += ecm.output("Owner changed to '%s':'%s'" % (chown_user, chown_group))

        except Exception as e:
            raise Exception("Unable to change permissions for configfile: %s" % e)

        if command:
            working_dir = os.path.dirname(filename)
            out, stdout, stderr = ecm.run_command(command, runas=runas, workdir=working_dir)
            ret = ecm.format_output(out, stdout, stderr)

        return ret
Exemplo n.º 3
0
    def cmd_script_run(self, *argv, **kwargs):
        """
        run script(b64) extension envars runas executable
        Syntax: script.run[script,extenion,envars,facts,runas,executable]
        """
        script_b64 = kwargs.get('script', None)
        script_extension = kwargs.get('extension', None)
        script_runas = kwargs.get('runas', None)
        script_executable = kwargs.get('executable', None)
        metadata = kwargs.get('metadata', None)

        if not script_extension:
            script_extension = '.cmd'

        if not script_b64:
            raise ecm.InvalidParameters(self.cmd_script_run.__doc__)

        try:
            # Write down
            tmp_dir = mkdtemp()
            tmp_file = tmp_dir + '/script' + script_extension
            ecm.file_write(tmp_file, b64decode(script_b64))
        except:
            raise ecm.InvalidParameters("Unable to decode b64")

        # Set environment variables before execution
        envars = ecm.metadata_to_env(metadata_b64=metadata)

        # Update metadata
        ecm.write_metadata(metadata_b64=metadata)

        # Chown
        if script_runas:
            ecm.chown(tmp_dir,script_runas,recursive=True)

        if script_executable:
            cmd = script_executable + ' ' + tmp_file
            out, stdout, stderr = ecm.run_command(cmd, runas=script_runas, workdir=tmp_dir, envars=envars)
        else:
            out, stdout, stderr = ecm.run_file(tmp_file, runas=script_runas, workdir=tmp_dir, envars=envars)

        rmtree(tmp_dir, ignore_errors=True)
        return ecm.format_output(out, stdout, stderr)