Skip to content

Commit

Permalink
Refactor VMPollerDaemon class to extend Daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Aug 20, 2013
1 parent a4c1b13 commit 3b7083d
Showing 1 changed file with 81 additions and 24 deletions.
105 changes: 81 additions & 24 deletions src/vmpollerd/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,68 +43,125 @@ class VMPollerException(Exception):
"""
pass

class VMPollerDaemon(object):
def __init__(self, config_dir):
"""
VMPollerDaemon object
class VMPollerDaemon(Daemon):
"""
VMPollerDaemon object
Prepares all VMPoller Agents to be ready for polling from the vCenters.
Extends:
Daemon class
Overrides:
run() method
Prepares all VMPoller Agents to be ready for polling from multiple vCenters.
"""
def run(self, config_dir="/etc/vm-poller"):
"""
The main daemon loop.
The 'config_dir' argument should point to a directory containing all *.conf files
for the different vCenters we are connecting our VMPollerAgents to.
Args:
config_dir (str): A directory containing configuration files for the Agents
Raises:
VMPollerException
"""
if not os.path.exists(config_dir) or not os.path.isdir(config_dir):
raise VMPollerException, "%s does not exists or is not a directory"
"""
# Get the configuration files for our vCenters
confFiles = self.load_configs(config_dir)

# Our Agents and ZeroMQ context
self.agents = dict()
self.zcontext = zmq.Context()
path = os.path.join(config_dir, "*.conf")
confFiles = glob.glob(path)


# Load the config for every Agent and vCenter
for eachConf in confFiles:
agent = VMPollerAgent(eachConf, ignore_locks=True, lockdir="/var/run/vm-poller", keep_alive=True)
self.agents[agent.vcenter] = agent

def run(self):
# Time to fire up our poller Agents
self.start_agents()

# TODO: This should be a poller instead of REQ/REP sockets
print "Starting a REP socket on *:9999 ..."

# TODO: This should be set as a attribute
socket = self.zcontext.socket(zmq.REP)
socket.bind("tcp://*:9999")
# Bind to our ZeroMQ proxy as a worker
# TODO: The endpoint we bind should be configurable
# TODO: Exceptions
syslog.syslog("Connecting to the VMPoller Proxy server")
self.worker = self.zcontext.socket(zmq.REP)
self.worker.bind("tcp://localhost:15556")

while True:
msg = socket.recv_json()
msg = self.worker.recv_json()

print "Received: %s" % msg
result = self.process_request(msg)

print "Sending: ", result
socket.send_pyobj(result)
self.worker.send_json(result)

# TODO: Proper shutdown and zmq context termination
self.shutdown_agents()

def load_configs(self, config_dir):
"""
Loads the configuration files for vCenters
The 'config_dir' argument should point to a directory containing all .conf files
for the different vCenters we are connecting our VMPollerAgents to.
Args:
config_dir (str): A directory containing configuration files for the Agents
Returns:
A list of all configuration files found in the config directory
Raises:
VMPollerException
"""
if not os.path.exists(config_dir) or not os.path.isdir(config_dir):
raise VMPollerException, "%s does not exists or is not a directory" % config_dir

# Get all *.conf files for the different vCenters
path = os.path.join(config_dir, "*.conf")
confFiles = glob.glob(path)

if not confFiles:
raise VMPollerException, "No config files found in %s" % config_dir

return confFiles

def start_agents(self):
# TODO: Check for exceptions here
"""
Connects all VMPoller Agents to their vCenters
"""
for eachAgent in self.agents:
try:
self.agents[eachAgent].connect(timeout=3)
except Exception as e:
print 'Cannot connect to %s: %s' % (eachAgent, e)

def shutdown_agents(self):
"""
Disconnects all VMPoller Agents from their vCenters
"""
for eachAgent in self.agents:
self.agents[eachAgent].disconnect()

def process_request(self, msg):
commands = { "status": "no-command-here-yet",
"""
Processes a client request message
Args:
msg (dict): A dictionary containing the client request message
"""
# Valid commands that the VMPoller Agent processes
commands = { "status": "no-command-here-yet", # TODO: Implement a status command
"poll" : self.process_poll_cmd
}

Expand Down

0 comments on commit 3b7083d

Please sign in to comment.