有兴趣的朋友可以改cp.push的源码,也就那几行。
原文:http://rfyiamcool.blog.51cto.com/1030776/1360468
[root@ruifengyun backup ]$ vim /usr/lib/python2.7/dist-packages/salt/modules/cp.py
我这里为了获取的方便,用basename来获取他的文件名。
def push(path): '''
Push a file from the minion up to the master, the file will be saved to
the salt master in the master's minion files cachedir
(defaults to /var/cache/salt/master/minions/files)
Since this feature allows a minion to push a file up to the master server
it is disabled by default for security purposes. To enable add the option:
file_recv: True
to the master configuration and restart the master
CLI Example::
salt '*' cp.push /etc/fstab
'''
path=os.path.basename(path)
if '../' in path or not os.path.isabs(path):
return False
if not os.path.isfile(path):
return False
auth = _auth()
load = {'cmd': '_file_recv',
'id': __opts__['id'],
'path': path.lstrip(os.sep)}
sreq = salt.payload.SREQ(__opts__['master_uri'])
with salt.utils.fopen(path) as fp_:
while True:
load['loc'] = fp_.tell()
load['data'] = fp_.read(__opts__['file_buffer_size'])
if not load['data']:
return True
ret = sreq.send('aes', auth.crypticle.dumps(load))
if not ret:
return ret
就这样了! 测试的模式用的是cli,大家在运维平台上可以使用api。