Ejemplo n.º 1
0
    def cmd_saltstack_apply(self, *argv, **kwargs):
        """
        Apply a saltstack manifest
        Syntax: saltstack.apply[recipe_code,pillar_code,envars,facts]
        """
        recipe_base64   = kwargs.get('recipe_code', None)
        pillar_base64   = kwargs.get('pillar_code', None)
        recipe_envars   = kwargs.get('envars', None)
        recipe_facts    = kwargs.get('facts', None)

        if not recipe_base64:
            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.envars_decode(recipe_envars)
        facts = ecm.envars_decode(recipe_facts)

        # Update envars and facts file
        ecm.write_envars_facts(envars, facts)

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

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

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

        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)
Ejemplo n.º 2
0
    def _is_available(self):
        """ which puppet
        """
        if ecm.is_windows():
            return ecm.which('puppet.exe')

        return ecm.which('puppet')
Ejemplo n.º 3
0
    def _is_available(self):
        """ checks if git is on path
        """
        if ecm.is_windows():
            return ecm.which('git.exe')

        return ecm.which('git')
Ejemplo n.º 4
0
    def cmd_saltstack_install(self, *argv, **kwargs):
        """ Installs saltstack using bootstrap scripts
        """
        if self._is_available(): return True

        bootstrap = BOOTSTRAP
        bootstrap_file = 'bootstrap.sh'

        if ecm.is_windows():
            bootstrap = BOOTSTRAP_WINDOWS
            bootstrap_file = 'bootstrap.ps1'

        if not self._install(bootstrap,bootstrap_file):
            # Try alternative bootstrap
            bootstrap = BOOTSTRAP_ALT
            if ecm.is_windows():
                bootstrap = BOOTSTRAP_WINDOWS_ALT

            if not self._install(bootstrap,bootstrap_file):
                raise Exception("Unable to install saltstack")

        return True
Ejemplo n.º 5
0
    def cmd_puppet_apply_file(self, *argv, **kwargs):
        """
        Syntax: puppet.apply_file[recipe_url,envars,facts]
        """
        recipe_url = kwargs.get('recipe_url', None)
        envars = kwargs.get('envars', None)
        facts = kwargs.get('facts', None)

        if not recipe_url:
            raise ecm.InvalidParameters(self.cmd_puppet_apply.__doc__)

        recipe_file = None
        recipe_path = None
        module_path = MODULES_PATH
        if ecm.is_windows(): module_path = MODULES_PATH_WINDOWS
        module_path = kwargs.get('module_path', module_path)

        # Set environment variables before execution
        envars = ecm.envars_decode(envars)
        facts = ecm.envars_decode(facts)

        # Update envars and facts file
        ecm.write_envars_facts(envars, facts)

        try:
            # Download recipe url
            recipe_path = mkdtemp()
            tmp_file = recipe_path + '/recipe.tar.gz'

            if ecm.download_file(recipe_url, tmp_file):
                if tarfile.is_tarfile(tmp_file):
                    tar = tarfile.open(tmp_file)
                    tar.extractall(path=recipe_path)

                    for file_name in tar.getnames():
                        if file_name.endswith('.catalog.pson'):
                            recipe_file = file_name
                    tar.close()

                    # Apply puppet catalog
                    return self._run_catalog(recipe_file, recipe_path, module_path=module_path, envars=envars)
                else:
                    raise Exception("Invalid recipe tgz file")
            else:
                raise Exception("Unable to download file")

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

        finally:
            rmtree(recipe_path, ignore_errors=True)
Ejemplo n.º 6
0
    def cmd_puppet_apply(self, *argv, **kwargs):
        """
        Syntax: puppet.appy[recipe_code,evars,facts]
        """
        recipe_base64 = kwargs.get('recipe_code', None)
        envars = kwargs.get('envars', None)
        facts = kwargs.get('facts', None)

        if not recipe_base64:
            raise ecm.InvalidParameters(self.cmd_puppet_apply.__doc__)

        # Set module path
        module_path = kwargs.get('module_path', None)
        if module_path is None:
            module_path = MODULES_PATH
            if ecm.is_windows():
                module_path = MODULES_PATH_WINDOWS

        # Set environment variables before execution
        envars = ecm.envars_decode(envars)
        facts = ecm.envars_decode(facts)

        # Set environment variables before execution
        ecm.write_envars_facts(envars, facts)

        try:
            catalog = b64decode(recipe_base64)
        except:
            raise ecm.InvalidParameters("Unable to decode recipe")

        try:
            command = ['puppet',
                       'apply',
                       '--modulepath',
                       module_path,
                       '--detailed-exitcodes',
                       '--debug']

            out, stdout, stderr = ecm.run_command(command, stdin=catalog, envars=envars)
            ret = ecm.format_output(out, stdout, stderr)

            # exit code of '2' means there were changes
            if ret['out'] == 2: ret['out'] = 0
            if "\nError: " in ret['stderr']: ret['out'] = 4

            return ret

        except Exception as e:
            raise Exception("Error running puppet apply: %s" % e)
Ejemplo n.º 7
0
 def _is_available(self):
     """ it's salt-call on path?
     """
     if ecm.is_windows():
         return ecm.which('salt-call.exe')
     return ecm.which('salt-call')
Ejemplo n.º 8
0
 def _is_available(self):
     """ is svn on path
     """
     if ecm.is_windows():
         return ecm.which('svn.cmd')
     return ecm.which('svn')
Ejemplo n.º 9
0
    def _boot_time(self):
        """ Returns server boot time """
        if ecm.is_windows():
            return self._boot_time_windows()

        return self._boot_time_linux()