Skip to content

Latest commit

 

History

History
216 lines (179 loc) · 7.84 KB

07-2.Mod-Aggregate-State-and-File-State-Backups.md

File metadata and controls

216 lines (179 loc) · 7.84 KB

Mod Aggregate State Runtime Modifications - Mod聚合状态运行时修改

New in version 2014.7.0.

Salt 2014.7.0版本中添加了mod_aggregate系统,并允许在运行时修改执行状态数据。 简而言之,它允许Salt状态系统使用的数据在运行时即时更改,就像配置管理JIT编译器或运行时导入系统一样。 总而言之,它使Salt更具活力。

How it Works

最好的例子是pkg状态。 长期以来,呼吁给Salt的增加的功能之一就是同时安装所有定义的软件包的功能。 mod_aggregate系统使这成为现实。 在执行Salt的状态系统时,当遇到处理pkg状态时,将调用状态模块中的mod_aggregate函数。 对于pkg,此功能将扫描所有将要运行的其他状态,并选择对namepkgs的引用,然后将它们添加到第一个状态的pkgs中。 结果是作为第一个软件包安装的一部分,形成对yum,apt-get,pacman等的单个调用。

How to Use it

注意:由于此选项更改了状态运行时的基本行为,因此启用该选项后,应先使用test=True执行状态,以确保行为结果的正确性。

In config files - 使用配置文件进行设置

启用聚合功能的第一种方法是使用主配置文件或minion配置文件中的配置选项。 Salt第一次遇到具有聚合支持的状态模块时,它将调用mod_aggregate

如果在主配置中设置了此选项,它将应用于所有minions的所有状态运行,如果在minion配置中设置,则将仅应用于所涉及的minion。

对所有的状态启用时:

state_aggregate: True

只针对特定的状态启用时:

state_aggregate:
  - pkg

In states - 在states定义中设置

启用聚合的第二种方法是使用状态级别的聚合关键字。 在此配置中,Salt首次遇到此关键字时将调用mod_aggregate函数。 该关键字的任何其他出现都将被忽略,因为已经进行了聚合。

以下示例将在处理lamp_stack状态时触发mod_aggregate,从而产生对基础包管理器的单次调用。

lamp_stack:
  pkg.installed:
    - pkgs:
      - php
      - mysql-client
    - aggregate: True

memcached:
  pkg.installed:
    - name: memcached

在一个状态模块中增加mod_aggregate功能

向现有状态模块添加mod_aggregate功能支持时仅需要向状态模块添加一个称为mod_aggregate的附加功能函数。

mod_aggregate函数只需要接受三个参数并返回low data即可使用。 由于mod_aggregate在状态运行时级别上工作,因此确实需要处理少量数据。

这三个参数分别为low, chunks, 和 running。 low选项是状态执行将要调用的low data。 chunks是运行时正在执行的所有low data字典的列表,而running字典是已执行的所有状态执行的返回数据。

此示例从pkg状态简化得到,显示了如何创建mod_aggregate函数:

def mod_aggregate(low, chunks, running):
    '''
    The mod_aggregate function which looks up all packages in the available
    low chunks and merges them into a single pkgs ref in the present low data
    '''
    pkgs = []
    # What functions should we aggregate?
    agg_enabled = [
            'installed',
            'latest',
            'removed',
            'purged',
            ]
    # The `low` data is just a dict with the state, function (fun) and
    # arguments passed in from the sls
    if low.get('fun') not in agg_enabled:
        return low
    # Now look into what other things are set to execute
    for chunk in chunks:
        # The state runtime uses "tags" to track completed jobs, it may
        # look familiar with the _|-
        tag = __utils__['state.gen_tag'](chunk)
        if tag in running:
            # Already ran the pkg state, skip aggregation
            continue
        if chunk.get('state') == 'pkg':
            if '__agg__' in chunk:
                continue
            # Check for the same function
            if chunk.get('fun') != low.get('fun'):
                continue
            # Pull out the pkg names!
            if 'pkgs' in chunk:
                pkgs.extend(chunk['pkgs'])
                chunk['__agg__'] = True
            elif 'name' in chunk:
                pkgs.append(chunk['name'])
                chunk['__agg__'] = True
    if pkgs:
        if 'pkgs' in low:
            low['pkgs'].extend(pkgs)
        else:
            low['pkgs'] = pkgs
    # The low has been modified and needs to be returned to the state
    # runtime for execution
    return low

File State Backups - 文件状态的备份管理

在0.10.2中,添加了一项新功能,用于备份由file.managed和file.recurse状态替换的文件。 新功能称为备份模式。 设置备份模式很容易,可以在许多地方进行设置。

可以在minion配置文件中设置backup_mode:

backup_mode: minion

或者是在每个文件中指定:

/etc/ssh/sshd_config:
  file.managed:
    - source: salt://ssh/sshd_config
    - backup: minion

Backed-up Files

这些文件将保存在名为file_backup的minion cachedir中。 文件位于相对于它们在根文件系统下的位置,并附加时间戳,以使它们易于被浏览。

Interacting with Backups

从 0.17.0 版本开始, 可以对备份过的文件执行查看、恢复或删除的操作。

Listing

使用 file.list_backups 查看指定文件的备份数据:

# salt foo.bar.com file.list_backups /tmp/foo.txt
foo.bar.com:
    ----------
    0:
        ----------
        Backup Time:
            Sat Jul 27 2013 17:48:41.738027
        Location:
            /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:41_738027_2013
        Size:
            13
    1:
        ----------
        Backup Time:
            Sat Jul 27 2013 17:48:28.369804
        Location:
            /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:28_369804_2013
        Size:
            35

Restoring

使用 file.restore_backup 可以将文件恢复为指定的备份版本, 只需将文件路径和通过 file.list_backups 得到的备份文件id数字作为参数传递给恢复命令:

# salt foo.bar.com file.restore_backup /tmp/foo.txt 1
foo.bar.com:
    ----------
    comment:
        Successfully restored /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:28_369804_2013 to /tmp/foo.txt
    result:
        True

而当前存在的文件也将被备份处理, 并可以通过 file.list_backups 查看:

# salt foo.bar.com file.list_backups /tmp/foo.txt
foo.bar.com:
    ----------
    0:
        ----------
        Backup Time:
            Sat Jul 27 2013 18:00:19.822550
        Location:
            /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_18:00:19_822550_2013
        Size:
            53
    1:
        ----------
        Backup Time:
            Sat Jul 27 2013 17:48:41.738027
        Location:
            /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:41_738027_2013
        Size:
            13
    2:
        ----------
        Backup Time:
            Sat Jul 27 2013 17:48:28.369804
        Location:
            /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:28_369804_2013
        Size:
            35

注意:由于未运行任何状态,因此还原文件不会触发对该文件的任何监视动作。 因此,如果要还原服务的配置文件,则可能仍需要运行service.restart以使变更生效。

Deleting

使用 file.delete_backup 删除文件的备份数据:

# salt foo.bar.com file.delete_backup /tmp/foo.txt 0
foo.bar.com:
    ----------
    comment:
        Successfully removed /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_18:00:19_822550_2013
    result:
        True