コード例 #1
0
    def list_states(self, saltenv):
        '''
        Return a list of all available sls modules on the master for a given
        environment
        '''

        limit_traversal = self.opts.get(u'fileserver_limit_traversal', False)
        states = []

        if limit_traversal:
            if saltenv not in self.opts[u'file_roots']:
                log.warning(
                    u'During an attempt to list states for saltenv \'%s\', '
                    u'the environment could not be found in the configured '
                    u'file roots', saltenv
                )
                return states
            for path in self.opts[u'file_roots'][saltenv]:
                for root, dirs, files in os.walk(path, topdown=True):
                    log.debug(
                        u'Searching for states in dirs %s and files %s',
                        dirs, files
                    )
                    if not [filename.endswith(u'.sls') for filename in files]:
                        #  Use shallow copy so we don't disturb the memory used by os.walk. Otherwise this breaks!
                        del dirs[:]
                    else:
                        for found_file in files:
                            stripped_root = os.path.relpath(root, path)
                            if salt.utils.platform.is_windows():
                                stripped_root = stripped_root.replace(u'\\', u'/')
                            stripped_root = stripped_root.replace(u'/', u'.')
                            if found_file.endswith((u'.sls')):
                                if found_file.endswith(u'init.sls'):
                                    if stripped_root.endswith(u'.'):
                                        stripped_root = stripped_root.rstrip(u'.')
                                    states.append(stripped_root)
                                else:
                                    if not stripped_root.endswith(u'.'):
                                        stripped_root += u'.'
                                    if stripped_root.startswith(u'.'):
                                        stripped_root = stripped_root.lstrip(u'.')
                                    states.append(stripped_root + found_file[:-4])
        else:
            for path in self.file_list(saltenv):
                if salt.utils.platform.is_windows():
                    path = path.replace(u'\\', u'/')
                if path.endswith(u'.sls'):
                    # is an sls module!
                    if path.endswith(u'/init.sls'):
                        states.append(path.replace(u'/', u'.')[:-9])
                    else:
                        states.append(path.replace(u'/', u'.')[:-4])
        return states
コード例 #2
0
ファイル: arc_rsync.py プロジェクト: jils2013/scripts
def _get_root(path):
    '''
        remove single top level directory(extracted from an archive)
        based on /usr/lib/python2.7/site-packages/salt/states/rsync.py
        '''
    if path.endswith('/'):
        path = path[:-1]
    for p, d, f in os.walk(path):
        if len(d) != 1 or len(f) != 0:
            log.error('Source directory:%s/' % p)
            return p + '/'
コード例 #3
0
    def cache_dir(self, path, saltenv=u'base', include_empty=False,
                  include_pat=None, exclude_pat=None, cachedir=None):
        '''
        Download all of the files in a subdir of the master
        '''
        ret = []

        path = self._check_proto(sdecode(path))
        # We want to make sure files start with this *directory*, use
        # '/' explicitly because the master (that's generating the
        # list of files) only runs on POSIX
        if not path.endswith(u'/'):
            path = path + u'/'

        log.info(
            u'Caching directory \'%s\' for environment \'%s\'', path, saltenv
        )
        # go through the list of all files finding ones that are in
        # the target directory and caching them
        for fn_ in self.file_list(saltenv):
            fn_ = sdecode(fn_)
            if fn_.strip() and fn_.startswith(path):
                if salt.utils.check_include_exclude(
                        fn_, include_pat, exclude_pat):
                    fn_ = self.cache_file(
                        salt.utils.url.create(fn_), saltenv, cachedir=cachedir)
                    if fn_:
                        ret.append(fn_)

        if include_empty:
            # Break up the path into a list containing the bottom-level
            # directory (the one being recursively copied) and the directories
            # preceding it
            # separated = string.rsplit(path, '/', 1)
            # if len(separated) != 2:
            #     # No slashes in path. (So all files in saltenv will be copied)
            #     prefix = ''
            # else:
            #     prefix = separated[0]
            cachedir = self.get_cachedir(cachedir)

            dest = salt.utils.path.join(cachedir, u'files', saltenv)
            for fn_ in self.file_list_emptydirs(saltenv):
                fn_ = sdecode(fn_)
                if fn_.startswith(path):
                    minion_dir = u'{0}/{1}'.format(dest, fn_)
                    if not os.path.isdir(minion_dir):
                        os.makedirs(minion_dir)
                    ret.append(minion_dir)
        return ret
コード例 #4
0
ファイル: restartcheck.py プロジェクト: lazypete/salt
def _valid_deleted_file(path):
    '''
    Filters file path against unwanted directories and decides whether file is marked as deleted.

    Returns:
        True if file is desired deleted file, else False.

    Args:
        path: A string - path to file
    '''
    ret = False
    if path.endswith(' (deleted)'):
        ret = True
    if re.compile(r"\(path inode=[0-9]+\)$").search(path):
        ret = True

    regex = re.compile("|".join(LIST_DIRS))
    if regex.match(path):
        ret = False
    return ret
コード例 #5
0
ファイル: restartcheck.py プロジェクト: MalloZup/salt-2
def _valid_deleted_file(path):
    '''
    Filters file path against unwanted directories and decides whether file is marked as deleted.

    Returns:
        True if file is desired deleted file, else False.

    Args:
        path: A string - path to file
    '''
    ret = False
    if path.endswith(' (deleted)'):
        ret = True
    if re.compile(r"\(path inode=[0-9]+\)$").search(path):
        ret = True
    # We don't care about log files
    if path.startswith('/var/log/') or path.startswith('/var/local/log/'):
        ret = False
    # Or about files under temporary locations
    if path.startswith('/var/run/') or path.startswith('/var/local/run/'):
        ret = False
    # Or about files under /tmp
    if path.startswith('/tmp/'):
        ret = False
    # Or about files under /dev/shm
    if path.startswith('/dev/shm/'):
        ret = False
    # Or about files under /run
    if path.startswith('/run/'):
        ret = False
    # Or about files under /drm
    if path.startswith('/drm'):
        ret = False
    # Or about files under /var/tmp and /var/local/tmp
    if path.startswith('/var/tmp/') or path.startswith('/var/local/tmp/'):
        ret = False
    # Or /dev/zero
    if path.startswith('/dev/zero'):
        ret = False
    # Or /dev/pts (used by gpm)
    if path.startswith('/dev/pts/'):
        ret = False
    # Or /usr/lib/locale
    if path.startswith('/usr/lib/locale/'):
        ret = False
    # Skip files from the user's home directories
    # many processes hold temporafy files there
    if path.startswith('/home/'):
        ret = False
    # Skip automatically generated files
    if path.endswith('icon-theme.cache'):
        ret = False
    # Skip font files
    if path.startswith('/var/cache/fontconfig/'):
        ret = False
    # Skip Nagios Spool
    if path.startswith('/var/lib/nagios3/spool/'):
        ret = False
    # Skip nagios spool files
    if path.startswith('/var/lib/nagios3/spool/checkresults/'):
        ret = False
    # Skip Postgresql files
    if path.startswith('/var/lib/postgresql/'):
        ret = False
    # Skip VDR lib files
    if path.startswith('/var/lib/vdr/'):
        ret = False
    # Skip Aio files found in MySQL servers
    if path.startswith('/[aio]'):
        ret = False
    # ignore files under /SYSV
    if path.startswith('/SYSV'):
        ret = False
    return ret
コード例 #6
0
ファイル: solr.py プロジェクト: MalloZup/salt-2
def backup(host=None, core_name=None, append_core_to_path=False):
    '''
    Tell solr make a backup.  This method can be mis-leading since it uses the
    backup API.  If an error happens during the backup you are not notified.
    The status: 'OK' in the response simply means that solr received the
    request successfully.

    host : str (None)
        The solr host to query. __opts__['host'] is default.
    core_name : str (None)
        The name of the solr core if using cores. Leave this blank if you are
        not using cores or if you want to check all cores.
    append_core_to_path : boolean (False)
        If True add the name of the core to the backup path. Assumes that
        minion backup path is not None.

    Return : dict<str,obj>::

        {'success':boolean, 'data':dict, 'errors':list, 'warnings':list}

    CLI Example:

    .. code-block:: bash

        salt '*' solr.backup music
    '''
    path = __opts__['solr.backup_path']
    num_backups = __opts__['solr.num_backups']
    if path is not None:
        if not path.endswith(os.path.sep):
            path += os.path.sep

    ret = _get_return_dict()
    if _get_none_or_value(core_name) is None and _check_for_cores():
        success = True
        for name in __opts__['solr.cores']:
            params = []
            if path is not None:
                path = path + name if append_core_to_path else path
                params.append("&location={0}".format(path + name))
            params.append("&numberToKeep={0}".format(num_backups))
            resp = _replication_request('backup',
                                        host=host,
                                        core_name=name,
                                        params=params)
            if not resp['success']:
                success = False
            data = {name: {'data': resp['data']}}
            ret = _update_return_dict(ret, success, data, resp['errors'],
                                      resp['warnings'])
        return ret
    else:
        if core_name is not None and path is not None:
            if append_core_to_path:
                path += core_name
        if path is not None:
            params = ["location={0}".format(path)]
        params.append("&numberToKeep={0}".format(num_backups))
        resp = _replication_request('backup',
                                    host=host,
                                    core_name=core_name,
                                    params=params)
        return resp
コード例 #7
0
ファイル: win_shortcut.py プロジェクト: nicholasmhughes/salt
def get(path):
    r"""
    Gets the properties for a shortcut

    Args:
        path (str): The path to the shortcut. Must have a `.lnk` or `.url` file
            extension.

    Returns:
        dict: A dictionary containing all available properties for the specified
            shortcut

    CLI Example:

    .. code-block:: bash

        salt * shortcut.get path="C:\path\to\shortcut.lnk"
    """
    if not os.path.exists(path):
        raise CommandExecutionError("Shortcut not found: {}".format(path))

    if not path.endswith((".lnk", ".url")):
        _, ext = os.path.splitext(path)
        raise CommandExecutionError("Invalid file extension: {}".format(ext))

    # This will load the existing shortcut
    with salt.utils.winapi.Com():
        shell = win32com.client.Dispatch("WScript.Shell")
        shortcut = shell.CreateShortcut(path)

        arguments = ""
        description = ""
        hot_key = ""
        icon_location = ""
        icon_index = 0
        window_style = ""
        working_dir = ""

        path = salt.utils.path.expand(shortcut.FullName)
        # A shortcut can have either a .lnk or a .url extension. We only want to
        # expand the target if it is a .lnk
        if path.endswith(".lnk"):
            target = shortcut.TargetPath
            if target:
                target = salt.utils.path.expand(target)
            else:
                msg = "Not a valid shortcut: {}".format(path)
                log.debug(msg)
                raise CommandExecutionError(msg)
            if shortcut.Arguments:
                arguments = shortcut.Arguments
            if shortcut.Description:
                description = shortcut.Description
            if shortcut.Hotkey:
                hot_key = shortcut.Hotkey
            if shortcut.IconLocation:
                icon_location, icon_index = shortcut.IconLocation.split(",")
                if icon_location:
                    icon_location = salt.utils.path.expand(icon_location)
            if shortcut.WindowStyle:
                window_style = WINDOW_STYLE[shortcut.WindowStyle]
            if shortcut.WorkingDirectory:
                working_dir = salt.utils.path.expand(shortcut.WorkingDirectory)
        else:
            target = shortcut.TargetPath

        return {
            "arguments": arguments,
            "description": description,
            "hot_key": hot_key,
            "icon_index": int(icon_index),
            "icon_location": icon_location,
            "path": path,
            "target": target,
            "window_style": window_style,
            "working_dir": working_dir,
        }
コード例 #8
0
ファイル: win_shortcut.py プロジェクト: nicholasmhughes/salt
def create(
    path,
    target,
    arguments="",
    description="",
    hot_key="",
    icon_index=0,
    icon_location="",
    window_style="Normal",
    working_dir="",
    backup=False,
    force=False,
    make_dirs=False,
    user=None,
):
    r"""
    Create a new shortcut. This can be a file shortcut (``.lnk``) or a url
    shortcut (``.url``).

    Args:

        path (str): The full path to the shortcut. Must have a `.lnk` or `.url`
            file extension.

        target (str): The full path to the target

        arguments (str, optional): Any arguments to be passed to the target

        description (str, optional): The description for the shortcut. This is
            shown in the ``Comment`` field of the dialog box. Default is an
            empty string

        hot_key (str, optional): A combination of hot Keys to trigger this
            shortcut. This is something like ``Ctrl+Alt+D``. This is shown in
            the ``Shortcut key`` field in the dialog box. Default is an empty
            string. Available options are:

            - Ctrl
            - Alt
            - Shift
            - Ext

        icon_index (int, optional): The index for the icon to use in files that
            contain multiple icons. Default is 0

        icon_location (str, optional): The full path to a file containing icons.
            This is shown in the ``Change Icon`` dialog box by clicking the
            ``Change Icon`` button. If no file is specified and a binary is
            passed as the target, Windows will attempt to get the icon from the
            binary file. Default is an empty string

        window_style (str, optional): The window style the program should start
            in. This is shown in the ``Run`` field of the dialog box. Default is
            ``Normal``. Valid options are:

            - Normal
            - Minimized
            - Maximized

        working_dir (str, optional): The full path to the working directory for
            the program to run in. This is shown in the ``Start in`` field of
            the dialog box.

        backup (bool, optional): If there is already a shortcut with the same
            name, set this value to ``True`` to backup the existing shortcut and
            continue creating the new shortcut. Default is ``False``

        force (bool, optional): If there is already a shortcut with the same
            name and you aren't backing up the shortcut, set this value to
            ``True`` to remove the existing shortcut and create a new with these
            settings. Default is ``False``

        make_dirs (bool, optional): If the parent directory structure does not
            exist for the new shortcut, create it. Default is ``False``

        user (str, optional): The user to be the owner of any directories
            created by setting ``make_dirs`` to ``True``. If no value is passed
            Salt will use the user account that it is running under. Default is
            an empty string.

    Returns:
        bool: True if successful

    Raises:
        CommandExecutionError: If the path is not a ``.lnk`` or ``.url`` file
            extension.
        CommandExecutionError: If there is an existing shortcut with the same
            name and ``backup`` and ``force`` are both ``False``
        CommandExecutionError: If the parent directory is not created and
            ``make_dirs`` is ``False``
        CommandExecutionError: If there was an error creating the parent
            directories

    CLI Example:

    .. code-block:: bash

        # Create a shortcut and set the ``Shortcut key`` (``hot_key``)
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" hot_key="Ctrl+Alt+N"

        # Create a shortcut and change the icon to the 3rd one in the icon file
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" icon_location="C:\path\to\icon.ico" icon_index=2

        # Create a shortcut and change the startup mode to full screen
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" window_style="Maximized"

        # Create a shortcut and change the icon
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" icon_location="C:\path\to\icon.ico"

        # Create a shortcut and force it to overwrite an existing shortcut
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" force=True

        # Create a shortcut and create any parent directories if they are missing
        salt * shortcut.create "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe" make_dirs=True
    """
    if not path.endswith((".lnk", ".url")):
        _, ext = os.path.splitext(path)
        raise CommandExecutionError("Invalid file extension: {}".format(ext))

    if os.path.exists(path):
        if backup:
            log.debug("Backing up: %s", path)
            file, ext = os.path.splitext(path)
            ext = ext.strip(".")
            backup_path = "{}-{}.{}".format(file, time.time_ns(), ext)
            os.rename(path, backup_path)
        elif force:
            log.debug("Removing: %s", path)
            os.remove(path)
        else:
            log.debug("Shortcut exists: %s", path)
            raise CommandExecutionError("Found existing shortcut")

    if not os.path.isdir(os.path.dirname(path)):
        if make_dirs:

            # Get user from opts if not defined
            if not user:
                user = __opts__["user"]

            # Make sure the user exists in Windows
            # Salt default is 'SYSTEM' for Windows
            if not __salt__["user.info"](user):
                # User not found, use the account salt is running under
                # If username not found, use System
                user = __salt__["user.current"]()
                if not user:
                    user = "******"

            try:
                __salt__["file.makedirs"](path=path, owner=user)
            except CommandExecutionError as exc:
                raise CommandExecutionError(
                    "Error creating parent directory: {}".format(exc.message))
        else:
            raise CommandExecutionError(
                "Parent directory not present: {}".format(
                    os.path.dirname(path)))

    return _set_info(
        path=path,
        arguments=arguments,
        description=description,
        hot_key=hot_key,
        icon_index=icon_index,
        icon_location=icon_location,
        target=target,
        window_style=window_style,
        working_dir=working_dir,
    )
コード例 #9
0
ファイル: win_shortcut.py プロジェクト: nicholasmhughes/salt
def modify(
    path,
    target="",
    arguments="",
    description="",
    hot_key="",
    icon_index=0,
    icon_location="",
    window_style="Normal",
    working_dir="",
):
    r"""
    Modify an existing shortcut. This can be a file shortcut (``.lnk``) or a
    url shortcut (``.url``).

    Args:

        path (str): The full path to the shortcut. Must have a `.lnk` or `.url`
            file extension.

        target (str, optional): The full path to the target

        arguments (str, optional): Any arguments to be passed to the target

        description (str, optional): The description for the shortcut. This is
            shown in the ``Comment`` field of the dialog box. Default is an
            empty string

        hot_key (str, optional): A combination of hot Keys to trigger this
            shortcut. This is something like ``Ctrl+Alt+D``. This is shown in
            the ``Shortcut key`` field in the dialog box. Default is an empty
            string. Available options are:

            - Ctrl
            - Alt
            - Shift
            - Ext

        icon_index (int, optional): The index for the icon to use in files that
            contain multiple icons. Default is 0

        icon_location (str, optional): The full path to a file containing icons.
            This is shown in the ``Change Icon`` dialog box by clicking the
            ``Change Icon`` button. If no file is specified and a binary is
            passed as the target, Windows will attempt to get the icon from the
            binary file. Default is an empty string

        window_style (str, optional): The window style the program should start
            in. This is shown in the ``Run`` field of the dialog box. Default is
            ``Normal``. Valid options are:

            - Normal
            - Minimized
            - Maximized

        working_dir (str, optional): The full path to the working directory for
            the program to run in. This is shown in the ``Start in`` field of
            the dialog box.

    Returns:
        bool: True if successful

    CLI Example:

    .. code-block:: bash

        # Modify an existing shortcut. Set it to target notepad.exe
        salt * shortcut.modify "C:\path\to\shortcut.lnk" "C:\Windows\notepad.exe"
    """
    if not os.path.exists(path):
        raise CommandExecutionError("Shortcut not found: {}".format(path))

    if not path.endswith((".lnk", ".url")):
        _, ext = os.path.splitext(path)
        raise CommandExecutionError("Invalid file extension: {}".format(ext))

    return _set_info(
        path=path,
        arguments=arguments,
        description=description,
        hot_key=hot_key,
        icon_index=icon_index,
        icon_location=icon_location,
        target=target,
        window_style=window_style,
        working_dir=working_dir,
    )
コード例 #10
0
ファイル: win_shortcut.py プロジェクト: nicholasmhughes/salt
def _set_info(
    path,
    target="",
    arguments="",
    description="",
    hot_key="",
    icon_index=0,
    icon_location="",
    window_style="Normal",
    working_dir="",
):
    r"""
    The main worker function for creating and modifying shortcuts. the `create`
    and `modify` functions are wrappers around this function.

    Args:

        path (str): The full path to the shortcut

        target (str): The full path to the target

        arguments (str, optional): Any arguments to be passed to the target

        description (str, optional): The description for the shortcut. This is
            shown in the ``Comment`` field of the dialog box. Default is an
            empty string

        hot_key (str, optional): A combination of hot Keys to trigger this
            shortcut. This is something like ``Ctrl+Alt+D``. This is shown in
            the ``Shortcut key`` field in the dialog box. Default is an empty
            string. Available options are:

            - Ctrl
            - Alt
            - Shift
            - Ext

        icon_index (int, optional): The index for the icon to use in files that
            contain multiple icons. Default is 0

        icon_location (str, optional): The full path to a file containing icons.
            This is shown in the ``Change Icon`` dialog box by clicking the
            ``Change Icon`` button. If no file is specified and a binary is
            passed as the target, Windows will attempt to get the icon from the
            binary file. Default is an empty string

        window_style (str, optional): The window style the program should start
            in. This is shown in the ``Run`` field of the dialog box. Default is
            ``Normal``. Valid options are:

            - Normal
            - Minimized
            - Maximized

        working_dir (str, optional): The full path to the working directory for
            the program to run in. This is shown in the ``Start in`` field of
            the dialog box.

    Returns:
        bool: True if successful
    """
    path = salt.utils.path.expand(path)

    # This will load the existing shortcut if it already exists
    # If it is a new shortcut, it won't be created until it is saved
    with salt.utils.winapi.Com():
        shell = win32com.client.Dispatch("WScript.Shell")
        shortcut = shell.CreateShortcut(path)

        # A shortcut can have either a .lnk or a .url extension. We only want to
        # expand the target if it is a .lnk
        if path.endswith(".lnk"):
            if target:
                target = salt.utils.path.expand(target)

            # These settings only apply to lnk shortcuts
            if arguments:
                shortcut.Arguments = arguments
            if description:
                shortcut.Description = description
            if hot_key:
                shortcut.Hotkey = hot_key
            if icon_location:
                shortcut.IconLocation = ",".join(
                    [icon_location, str(icon_index)])
            if window_style:
                shortcut.WindowStyle = WINDOW_STYLE[window_style]
            if working_dir:
                shortcut.WorkingDirectory = working_dir

        shortcut.TargetPath = target

        shortcut.Save()
    return True