天天看點

salt自定義module - 兩個檔案的diff

[root@saltui openstack-deploy]# cat /etc/salt/master  # 看下salt master的工作目錄
auto_accept: True
worker_threads: 10
file_recv: True
file_roots:
   base:
     - /srv/openstack-deploy/salt/
   dev:
     - /srv/openstack-deploy/salt/dev
jinja_trim_blocks: True
jinja_lstrip_blocks: True

# conf_file子產品放在_modules目錄下,基于difflib庫實作
[root@saltui openstack-deploy]# cat /srv/openstack-deploy/salt/_modules/conf_file.py
# -*- coding: utf-8 -*-
'''
Diff information about files
'''

import contextlib
import difflib
import os
import salt.utils


def _binary_replace(old, new):
    '''
    This function does NOT do any diffing, it just checks the old and new files
    to see if either is binary, and provides an appropriate string noting the
    difference between the two files. If neither file is binary, an empty
    string is returned.

    This function should only be run AFTER it has been determined that the
    files differ.
    '''
    old_isbin = not salt.utils.istextfile(old)
    new_isbin = not salt.utils.istextfile(new)
    if any((old_isbin, new_isbin)):
        if all((old_isbin, new_isbin)):
            return 'Replace binary file'
        elif old_isbin:
            return 'Replace binary file with text file'
        elif new_isbin:
            return 'Replace text file with binary file'
    return ''


def diff(
    src,
    dst,
    onlydiff=True):
    '''
    Return unified diff of file compared to file on master

    CLI Example:

    .. code-block:: bash

        salt '*' conf_file.diff /home/fred/.vimrc /home/users/fred/.vimrc
    '''
    src = os.path.expanduser(src)

    ret = ''

    if not os.path.exists(src):
        ret = 'File {0} does not exist on the minion'.format(src)
        return ret

    if not os.path.exists(dst):
        ret = 'File {0} does not exist on the minion'.format(dst)
        return ret

    with contextlib.nested(salt.utils.fopen(src, 'r'),
                           salt.utils.fopen(dst, 'r')) \
            as (s, d):
        slines = s.readlines()
        dlines = d.readlines()
    if ''.join(slines) != ''.join(dlines):
        bdiff = _binary_replace(src, dst)
        if bdiff:
            ret += bdiff
        else:
            diff = difflib.unified_diff(slines, dlines,
                                        src, dst)
            if onlydiff:
                changes = [l for l in diff if l.startswith('+') or l.startswith('-')
                           or l.startswith('@@')]
            else:
                changes = diff
            ret += ''.join(changes)

    return ret      
# 下發子產品
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' saltutil.sync_modules      
# 預設隻顯示差異的部分
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' conf_file.diff /tmp/nova.conf /etc/nova/nova.conf
node_172_16_214_181:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -1,9 +1,8 @@
    -#
    -verbose = False
    +verbose = True
    @@ -12,7 +11,7 @@
    -quota_cores = 200
    +quota_cores = 20
node_172_16_214_182:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -31,7 +31,7 @@
    -max_io_ops_per_host = 100
    +max_io_ops_per_host = 10
node_172_16_214_183:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -13,7 +13,7 @@
    -quota_floating_ips = 100
    +quota_floating_ips = 10      
# 加上onlydiff=False,也顯示共同部分
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' conf_file.diff /tmp/nova.conf /etc/nova/nova.conf onlydiff=False
node_172_16_214_181:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -1,9 +1,8 @@
     [DEFAULT]
     # Logs / State
    -#
     debug = False
    -verbose = False
    +verbose = True
     fatal_deprecations = False
     log_dir = /var/log/nova
     state_path = /var/lib/nova
    @@ -12,7 +11,7 @@
     novncproxy_host = node_172_16_214_181
     # Quota
    -quota_cores = 200
    +quota_cores = 20
     quota_fixed_ips = -1
     quota_floating_ips = 10
     quota_injected_file_content_bytes = 10240
node_172_16_214_182:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -31,7 +31,7 @@
     cpu_allocation_ratio = 2.0
     disk_allocation_ratio = 1.0
     max_instances_per_host = 50
    -max_io_ops_per_host = 100
    +max_io_ops_per_host = 10
     ram_allocation_ratio = 1.0
     ram_weight_multiplier = 5.0
     reserved_host_disk_mb = 2048
node_172_16_214_183:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -13,7 +13,7 @@
     # Quota
     quota_cores = 20
     quota_fixed_ips = -1
    -quota_floating_ips = 100
    +quota_floating_ips = 10
     quota_injected_file_content_bytes = 10240
     quota_injected_file_path_length = 255
     quota_injected_files = 5      

參考連結

繼續閱讀