python-mcollective icon indicating copy to clipboard operation
python-mcollective copied to clipboard

How to use mcollective agents

Open jantman opened this issue 11 years ago • 6 comments

With #25 I can now get the simple_action.py example to do a SimpleRPC ping correctly. How do I use other mcollective agents, like "puppet", "service", "package", etc.? Is there any example code for this?

jantman avatar Oct 14 '14 17:10 jantman

@jantman there is not MCollective agents support yet, I was starting it at #9 before stop working on this project... You should be able to implement any agent using SimpleRPC, thought it will be too "verbose". Now that I'm back I would like to implement some example agents and probably some abstraction in order to make them less verbose.

rafaduran avatar Jan 12 '15 20:01 rafaduran

Ok. I should have updated this, but I do have a proof of concept for using SimpleRPC to replicate the Puppet Agent's status command:

#!/usr/bin/env python
# coding: utf-8
import logging

from pymco import config
from pymco import rpc
from pymco import message
import time
import pprint

# logging
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(name)s.%(funcName)s() ] %(message)s"
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
logger = logging.getLogger(__name__)

# read the config as a Config object
logger.debug("reading config...")
config = config.Config.from_configfile('client.cfg')

# construct a dict/hash to send as the message body
msg_data = {':action': 'status', ':data': {':process_results': True}, ':ssl_ttl': 60, ':ssl_msgtime': int(time.time())}
logger.debug("msg_data:\n{m}".format(m=msg_data))

##############################
# serialize the message body
##############################
# config
serializer = config.get_serializer('plugin.ssl_serializer')
msg_body = serializer.serialize(msg_data)
logger.debug("msg_body: \n{m}".format(m=msg_body))

# construct the Message itself
msg = message.Message(body=msg_body, agent='puppet', config=config)
logger.debug("calling rpc.SimpleAction")
action = rpc.SimpleAction(config=config, msg=msg, agent='puppet')
logger.debug("SimpleAction.call()")
results = action.call()
for r in results:
    if 'body' in r:
        r['body'] = serializer.deserialize(r['body'])
    elif ':body' in r:
        r[':body'] = serializer.deserialize(r[':body'])
    pprint.pprint(r)

jantman avatar Jan 12 '15 23:01 jantman

@jantman nice! I will probably start something based on that.

rafaduran avatar Jan 13 '15 20:01 rafaduran

Thanks for the example There is one problem using that serializer. To run puppet in noop mode, I use this message body:

msg_data = {':action': 'runonce', ':data': {':noop': True, ':process_results': True}, ':ssl_ttl': 60, ':ssl_msgtime': int(time.time())}

This is the serialized data with the above serializer:

:action: runonce
:data: {':noop': true, ':process_results': true}
:ssl_msgtime: 1421845915
:ssl_ttl: 60

The Puppet agent on the other end can't find the variables in the data section. It works using the yaml dump with default_flow_style=False:

msg_body = yaml.safe_dump(msg_data, default_flow_style=False)
:action: runonce
:data:
  :noop: true
  :process_results: true
:ssl_msgtime: 1421845915
:ssl_ttl: 60

It works when setting default_flow_style=False in the serializer, but I am not sure about the consequences, so I just use it on that message.

saliceti avatar Jan 21 '15 13:01 saliceti

@saliceti I've just pushed a change so YAML serializer uses default_flow_style option, could you please confirm that change solves your issue?

rafaduran avatar Jan 21 '15 22:01 rafaduran

@rafaduran it's working now, thanks

saliceti avatar Jan 22 '15 10:01 saliceti