Example #1
0
    def __enable_watch(self, parameter, watch_map, module, sid):
        watch_key = None
        watchs = None

        if watch_map.get(module):
            watch_key = (watch_map[module].get("file_key")
                         if watch_map[module].get("file_key")
                         else watch_map[module].get("dir_key"))

        if parameter and type(parameter) is dict and watch_key and parameter.get(watch_key):
            if hasattr(watch_map[module].get("action"), '__call__'):
                watchs = watch_map[module]["action"](self.__config,parameter)
            else:
                watchs = parameter.get(watch_key)
            if type(watchs) is str or type(watchs) is unicode:
                watchs = [watchs]
            if type(watchs) is list:
                utils.log("DEBUG", "Watched state detected",('__enable_watch',self))
                if "watch" in parameter:
                    del parameter["watch"]
                for watch in watchs:
                    if watch_map[module].get("file"):
                        watch = os.path.join(watch,watch_map[module]['file'])
                    utils.log("DEBUG", "Watched file '%s' found"%(watch),('__enable_watch',self))
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=False, tfirst=watch_map[module].get("tfirst",True)):
                        parameter["watch"] = True
                        utils.log("INFO","Watch event triggered, replacing standard action ...",('__enable_watch',self))
        return parameter, watchs
Example #2
0
    def __enable_watch(self, parameter, watch_map, module, sid):
        watch_key = None
        watchs = None

        if watch_map.get(module):
            watch_key = (watch_map[module].get("file_key")
                         if watch_map[module].get("file_key")
                         else watch_map[module].get("dir_key"))

        if parameter and type(parameter) is dict and watch_key and parameter.get(watch_key):
            if hasattr(watch_map[module].get("action"), '__call__'):
                watchs = watch_map[module]["action"](self.__config,parameter)
            else:
                watchs = parameter.get(watch_key)
            if type(watchs) is str or type(watchs) is unicode:
                watchs = [watchs]
            if type(watchs) is list:
                utils.log("DEBUG", "Watched state detected",('__enable_watch',self))
                if "watch" in parameter:
                    del parameter["watch"]
                for watch in watchs:
                    if watch_map[module].get("file"):
                        watch = os.path.join(watch,watch_map[module]['file'])
                    utils.log("DEBUG", "Watched file '%s' found"%(watch),('__enable_watch',self))
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=False, tfirst=watch_map[module].get("tfirst",True)):
                        parameter["watch"] = True
                        utils.log("INFO","Watch event triggered, replacing standard action ...",('__enable_watch',self))
        return parameter, watchs
Example #3
0
def run_service(name, watch_list, state_id):
    comment = ""
    ret = service.running(name,enable=True)
    if not ret.get("result"):
        comment += "Unable to run service %s"%name
        return False,comment
    comment += "Service %s: %s\n"%(name,ret.get("comment","Available"))
    if not watch_list: watch_list = []
    for watch in watch_list:
        cs = Checksum(watch,state_id,WATCH_PATH)
        if cs.update(edit=False,tfirst=True):
            ret = service.mod_watch(name,restart=True)
            if not ret.get("result"):
                comment += "Unable to restart service %s after change triggered on file %s"%(name,watch)
                return False,comment
            comment += "Service %s: %s\n"%(name,ret.get("comment","Restarted"))
            cs.update(edit=True,tfirst=True)
    return True,comment
Example #4
0
    def __exec_salt(self, sid, module, parameter, res):
        utils.log("INFO", "Loading state ID '%s' from module '%s' ..."%(sid,module),('__exec_salt',self))

        # init
        cs = None
        result = FAIL

        # Watch prepare
        try:
            watch_map = self.__state_adaptor.watch
            utils.log("DEBUG", "StateAdaptor watch map loaded",('__exec_salt',self))
        except Exception:
            watch_map = WATCH
            utils.log("DEBUG", "Default watch map loaded",('__exec_salt',self))
        rerun = watch_map.get(module,{}).get("rerun",False)

        # dry-run
        if rerun is True:
            try:
                # state convert
                utils.log("INFO", "Dry-run: Begin to convert salt states...", ('__exec_salt', self))
                salt_states = self.__state_adaptor.convert(sid, module, copy.deepcopy(parameter))

                # exec salt states
                utils.log("INFO", "Dry-run: Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "Dry-run: %s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Enable watch
        try:
            parameter, watchs = self.__enable_watch(parameter, watch_map, module, sid)
        except Exception as e:
            err = "Internal error while watch process on watched file: %s"%(e)
            utils.log("ERROR", err,('__exec_salt',self))
            res['result'] = FAIL
            res['comment'] += "Internal error on watched file."
            res['out_log'] += "%s"%err
            return
        watch_valid = parameter.get("watch",False)

        # state exec
        if (not rerun) or (watchs and watch_valid):
            try:
                # state convert
                utils.log("INFO", "Begin to convert salt states...", ('__exec_salt', self))
                if self.__config['module']['mod_tag'] == "v2014-04-15":
                    salt_states = self.__state_adaptor.convert(sid, module, parameter, self.__state_runner.os_type)
                else:
                    salt_states = self.__state_adaptor.convert(sid, module, parameter)

                # exec salt state
                utils.log("INFO", "Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "%s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Persist watch
        if result and watchs:
            for watch in watchs:
                if watch_map[module].get("file"):
                    watch = os.path.join(watch,watch_map[module]['file'])
                try:
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=True,tfirst=watch_map[module].get("tfirst",True)) is not None:
                        utils.log("INFO", "New checksum stored for file %s"%(cs.filepath()),('__exec_salt',self))
                    else:
                        utils.log("INFO", "Checksum for file %s unchanged"%(cs.filepath()),('__exec_salt',self))
                except Exception as e:
                    utils.log("WARNING", "Failed to store new checksum for file %s: %s"%(cs.filepath(),e),('__exec_salt',self))

        # end log
        utils.log("INFO", "State ID '%s' from module '%s' done, result '%s'"%(sid,module,result),('__exec_salt',self))
        utils.log("DEBUG", "State out log='%s'"%(out_log),('__exec_salt',self))
        utils.log("DEBUG", "State comment='%s'"%(comment),('__exec_salt',self))
Example #5
0
    def __exec_salt(self, sid, module, parameter, res):
        utils.log("INFO", "Loading state ID '%s' from module '%s' ..."%(sid,module),('__exec_salt',self))

        # init
        cs = None
        result = FAIL

        # Watch prepare
        try:
            watch_map = self.__state_adaptor.watch
            utils.log("DEBUG", "StateAdaptor watch map loaded",('__exec_salt',self))
        except Exception:
            watch_map = WATCH
            utils.log("DEBUG", "Default watch map loaded",('__exec_salt',self))
        rerun = watch_map.get(module,{}).get("rerun",False)

        # dry-run
        if rerun is True:
            try:
                # state convert
                utils.log("INFO", "Dry-run: Begin to convert salt states...", ('__exec_salt', self))
                salt_states = self.__state_adaptor.convert(sid, module, copy.deepcopy(parameter))

                # exec salt states
                utils.log("INFO", "Dry-run: Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "Dry-run: %s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Enable watch
        try:
            parameter, watchs = self.__enable_watch(parameter, watch_map, module, sid)
        except Exception as e:
            err = "Internal error while watch process on watched file: %s"%(e)
            utils.log("ERROR", err,('__exec_salt',self))
            res['result'] = FAIL
            res['comment'] += "Internal error on watched file."
            res['out_log'] += "%s"%err
            return
        watch_valid = parameter.get("watch",False)

        # state exec
        if (not rerun) or (watchs and watch_valid):
            try:
                # state convert
                utils.log("INFO", "Begin to convert salt states...", ('__exec_salt', self))
                if self.__config['module']['mod_tag'] == "v2014-04-15":
                    salt_states = self.__state_adaptor.convert(sid, module, parameter, self.__state_runner.os_type)
                else:
                    salt_states = self.__state_adaptor.convert(sid, module, parameter)

                # exec salt state
                utils.log("INFO", "Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "%s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Persist watch
        if result and watchs:
            for watch in watchs:
                if watch_map[module].get("file"):
                    watch = os.path.join(watch,watch_map[module]['file'])
                try:
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=True,tfirst=watch_map[module].get("tfirst",True)) is not None:
                        utils.log("INFO", "New checksum stored for file %s"%(cs.filepath()),('__exec_salt',self))
                    else:
                        utils.log("INFO", "Checksum for file %s unchanged"%(cs.filepath()),('__exec_salt',self))
                except Exception as e:
                    utils.log("WARNING", "Failed to store new checksum for file %s: %s"%(cs.filepath(),e),('__exec_salt',self))

        # end log
        utils.log("INFO", "State ID '%s' from module '%s' done, result '%s'"%(sid,module,result),('__exec_salt',self))
        utils.log("DEBUG", "State out log='%s'"%(out_log),('__exec_salt',self))
        utils.log("DEBUG", "State comment='%s'"%(comment),('__exec_salt',self))
Example #6
0
def extracted(name, source, archive_format, tar_options=None, source_hash=None, if_missing=None, if_absent=None):
    """
    State that make sure an archive is extracted in a directory.
    The downloaded archive is erased if succesfully extracted.
    The archive is downloaded only if necessary.

    .. code-block:: yaml

        graylog2-server:
          archive:
            - extracted
            - name: /opt/
            - source: https://github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.6p1.tar.gz
            - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6
            - archive_format: tar
            - tar_options: z
            - if_missing: /opt/graylog2-server-0.9.6p1/

    name
        Directory name where to extract the archive

    source
        Archive source, same syntax as file.managed source argument.

    archive_format
        tar, zip or rar

    if_missing
        Some archive, such as tar, extract themself in a subfolder.
        This directive can be used to validate if the archive had been
        previously extracted.

    if_absent
        Extract the archive only if none of the specified paths exist.

    tar_options
        Only used for tar format, it need to be the tar argument specific to
        this archive, such as 'j' for bzip2, 'z' for gzip, '' for uncompressed
        tar, 'J' for LZMA.
    """
    ret = {"name": name, "result": None, "changes": {}, "comment": ""}
    valid_archives = ("tar", "rar", "zip")

    if archive_format not in valid_archives:
        ret["result"] = False
        ret["comment"] = "{0} is not supported, valids: {1}".format(name, ",".join(valid_archives))
        return ret

    if archive_format == "tar" and tar_options is None:
        ret["result"] = False
        ret["comment"] = "tar archive need argument tar_options"
        return ret

    # if if_missing is None:
    #     if_missing = name
    # if (__salt__['file.directory_exists'](if_missing) or
    #     __salt__['file.file_exists'](if_missing)):
    #     ret['result'] = True
    #     ret['comment'] = '{0} already exists'.format(if_missing)
    #     return ret

    log.debug("Input seem valid so far")
    filename = os.path.join(__opts__["cachedir"], source.split("/")[-1])

    #########################################################################
    # check whether special paths are absent
    if if_absent and isinstance(if_absent, list):
        if any([os.path.isdir(path) for path in if_absent]):
            ret["result"] = True
            ret["comment_"] = "Any specail directory existed"
            return ret

    # get cached checksum
    cs = Checksum(source.split("/")[-1], name, __opts__["watch_dir"])
    current_hash = cs.get()

    # get source hash value
    if source_hash:
        try:
            tmp, source_hash, comment_ = __salt__["file.get_managed"](
                filename, None, source, source_hash, None, None, None, __env__, None, None
            )
            if comment_:
                ret["result"] = False
                ret["comment"] = comment_
                return ret
        except Exception, e:
            ret["result"] = False
            ret["comment"] = "Parse source hash %s failed" % str(source_hash)
            ret["state_stdout"] = str(e)
            return ret

        # fetch source tarball when the target directory isn't existed or no cached checksum
        if os.path.isdir(name) and current_hash:

            # check source hash value
            if source_hash["hash_type"] == "md5" and current_hash == source_hash["hsum"]:
                ret["result"] = True
                ret["comment"] = ("File sum set for file {0} of {1} is unchanged.").format(source, source_hash["hsum"])
                return ret
Example #7
0
def extracted(name,
              source,
              archive_format,
              tar_options=None,
              source_hash=None,
              if_missing=None,
              if_absent=None):
    '''
    State that make sure an archive is extracted in a directory.
    The downloaded archive is erased if succesfully extracted.
    The archive is downloaded only if necessary.

    .. code-block:: yaml

        graylog2-server:
          archive:
            - extracted
            - name: /opt/
            - source: https://github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.6p1.tar.gz
            - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6
            - archive_format: tar
            - tar_options: z
            - if_missing: /opt/graylog2-server-0.9.6p1/

    name
        Directory name where to extract the archive

    source
        Archive source, same syntax as file.managed source argument.

    archive_format
        tar, zip or rar

    if_missing
        Some archive, such as tar, extract themself in a subfolder.
        This directive can be used to validate if the archive had been
        previously extracted.

    if_absent
        Extract the archive only if none of the specified paths exist.

    tar_options
        Only used for tar format, it need to be the tar argument specific to
        this archive, such as 'j' for bzip2, 'z' for gzip, '' for uncompressed
        tar, 'J' for LZMA.
    '''
    ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''}
    valid_archives = ('tar', 'rar', 'zip')

    if archive_format not in valid_archives:
        ret['result'] = False
        ret['comment'] = '{0} is not supported, valids: {1}'.format(
            name, ','.join(valid_archives))
        return ret

    if archive_format == 'tar' and tar_options is None:
        ret['result'] = False
        ret['comment'] = 'tar archive need argument tar_options'
        return ret

    # if if_missing is None:
    #     if_missing = name
    # if (__salt__['file.directory_exists'](if_missing) or
    #     __salt__['file.file_exists'](if_missing)):
    #     ret['result'] = True
    #     ret['comment'] = '{0} already exists'.format(if_missing)
    #     return ret

    log.debug("Input seem valid so far")
    filename = os.path.join(__opts__['cachedir'], source.split('/')[-1])

    #########################################################################
    # check whether special paths are absent
    if if_absent and isinstance(if_absent, list):
        if any([os.path.isdir(path) for path in if_absent]):
            ret['result'] = True
            ret['comment_'] = 'Any specail directory existed'
            return ret

    # get cached checksum
    cs = Checksum(source.split('/')[-1], name, __opts__['watch_dir'])
    current_hash = cs.get()

    # get source hash value
    if source_hash:
        try:
            tmp, source_hash, comment_ = __salt__['file.get_managed'](
                filename, None, source, source_hash, None, None, None, __env__,
                None, None)
            if comment_:
                ret['result'] = False
                ret['comment'] = comment_
                return ret
        except Exception, e:
            ret['result'] = False
            ret['comment'] = 'Parse source hash %s failed' % str(source_hash)
            ret['state_stdout'] = str(e)
            return ret

        # fetch source tarball when the target directory isn't existed or no cached checksum
        if os.path.isdir(name) and current_hash:

            # check source hash value
            if source_hash[
                    'hash_type'] == 'md5' and current_hash == source_hash[
                        'hsum']:
                ret['result'] = True
                ret['comment'] = (
                    'File sum set for file {0} of {1} is unchanged.').format(
                        source, source_hash['hsum'])
                return ret