Skip to content

Instantly share code, notes, and snippets.

@insom
Created January 28, 2011 23:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/801265 to your computer and use it in GitHub Desktop.
Save insom/801265 to your computer and use it in GitHub Desktop.
A Python language clone of MCollective's ext/perl/mc-find-hosts.pl
#!/usr/bin/python
import sys
import syck as yaml
import time
from hashlib import sha1
from M2Crypto.RSA import load_pub_key, load_key
from stompy.simple import Client
# Change these.
PREFIX = 'mcollective'
CERTNAME = 'certificate-name'
s = Client('stomp.server')
s.connect('stompusername','stomppassword')
rr = load_key('path-to-unencrypted-private-key.pem')
target = '/topic/%s.discovery.command' % PREFIX
target_reply = '/topic/%s.discovery.reply' % PREFIX
s.subscribe(target_reply)
rid = sha1(str(time.time())).hexdigest()
# Put together message.
r = {}
r[':msgtime'] = int(time.time())
r[':filter'] = {
'identity': [],
'fact': [],
'agent': [],
'cf_class': [],
}
r[":requestid"] = rid
r[":callerid"] = 'cert=%s' % CERTNAME
r[":senderid"] = 'pythontest'
r[":msgtarget"] = target
r[':body'] = yaml.dump('ping')
h = rr.sign(sha1(r[':body']).digest(), 'sha1')
r[':hash'] = h.encode('base64').replace("\n", "").strip()
data = yaml.dump(r)
s.put(data, target)
time.sleep(2)
results = []
while True:
x = None
try:
x = s.get_nowait()
print x
except:
break
if not x:
break
y = yaml.load(x.body)
print y
if y[':requestid'] == rid:
results.append(y)
print [q[':senderid'] for q in results]
@insom
Copy link
Author

insom commented Jan 28, 2011

A Python language clone of MCollective's ext/perl/mc-find-hosts.pl.

This has a tall set of requirements:

  • pysyck (and therefore syck) - because PyYaml doesn't speak quite the same YAML as MCollective.
  • M2Crypto - seems to be the most comprehensive OpenSSL binding, though I needed to read the source.
  • stompy - make Python speak STOMP.

And it also requires you to have particular MCollective setup:

securityprovider = ssl
plugin.ssl_serializer = yaml

@insom
Copy link
Author

insom commented Feb 6, 2011

This gist has been turned into a simple library, maintained here: https://github.com/insom/mcollective-python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment