Skip to content

Instantly share code, notes, and snippets.

@houming818
Created July 17, 2017 06:18
Show Gist options
  • Save houming818/fd2a31d50ebebba5f1e7c72c8001fe81 to your computer and use it in GitHub Desktop.
Save houming818/fd2a31d50ebebba5f1e7c72c8001fe81 to your computer and use it in GitHub Desktop.
write a salt sls file in pure python.
#!py
import salt.utils
import sys
def _sub_tail(host, port, filename):
import subprocess
import select
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (host, port)
sock.connect(server_address)
p = select.poll()
fp = subprocess.Popen(['tail','-F',filename],\
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p.register(fp.stdout)
server_id = __grains__['id']
sock.sendall(server_id+'\n')
sock.sendall(filename+'\n')
while True:
if p.poll(1):
tmp = fp.stdout.readline()
try:
sock.sendall(tmp)
except:
print 'I dead'
fp.kill()
sys.exit(0)
def tail(host, port, filename):
from multiprocessing import Process
p = Process(target=_sub_tail, args=(host, port, filename))
p.start()
return {'retcode': 0, 'stdout': 'ok', 'stderr': ''}
def run():
host = __pillar__['tail']['host']
port = __pillar__['tail']['port']
filename = __pillar__['tail']['filename']
ret = tail(host, port, filename)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment