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)
    def cmd_saltstack_apply(self, *argv, **kwargs):
        """
        Apply a saltstack manifest
        Syntax: saltstack.apply[recipe_code,pillar_code,envars,facts]
        """
        recipe_b64 = kwargs.get('recipe_code', None)
        pillar_b64 = kwargs.get('pillar_code', None)
        metadata = kwargs.get('metadata', None)

        if not recipe_b64:
            raise ecm.InvalidParameters(self.cmd_saltstack_apply.__doc__)

        saltstack_cmd = self._is_available()
        if not saltstack_cmd:
            raise Exception('Saltstack no available')

        # Get default paths
        default_path = DEFAULT_SALT_PATH
        if ecm.is_windows():
            default_path = DEFAULT_SALT_PATH_WINDOWS
        module_path = kwargs.get('module_path', default_path)

        default_pillar_path = DEFAULT_PILLAR_PATH
        if ecm.is_windows():
            default_pillar_path = DEFAULT_PILLAR_PATH_WINDOWS
        pillar_path = kwargs.get('pillar_path', default_pillar_path)

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

        # Update metadata
        ecm.write_metadata(metadata_b64=metadata)

        try:
            # Create top file
            self._create_top_file(module_path)

            recipe_file = module_path + '/ecmanaged.sls'
            ecm.file_write(recipe_file, b64decode(recipe_b64))

            if pillar_b64:
                self._create_top_file(pillar_path)
                pillar_file = pillar_path + '/ecmanaged.sls'
                ecm.file_write(pillar_file, b64decode(pillar_b64))

        except:
            raise Exception("Unable to write recipe")

        try:
            # salt-call state.highstate
            command = [saltstack_cmd, 'state.highstate', '--local', '--no-color', '-l debug']

            out, stdout, stderr = ecm.run_command(command, envars=envars, workdir=module_path)
            return ecm.format_output(out, stdout, stderr)

        except Exception as e:
            raise Exception("Error running saltstack state.highstate: %s" % e)
Beispiel #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)