Skip to content

Instantly share code, notes, and snippets.

@crazed
Created January 3, 2014 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crazed/8231763 to your computer and use it in GitHub Desktop.
Save crazed/8231763 to your computer and use it in GitHub Desktop.
example doing parallel junos pyez execution
import time
from multiprocessing import Pool
from pprint import pprint
from optopus import Client, OPTOPUS_ENDPOINT
from jnpr.junos import Device
# work in progress
class MetaStore(object):
"""
Subclass this to create a custom metadata store
"""
device_cache_time = 600
def __init__(self, device_options={}):
"""
:param device_options:
This will be passed along when creating new Device objects
"""
self.device_options = device_options
def search(*args, **kwargs):
pass
# Optopus is my custom metadata store with facts about everything in the environment
class OptopusMetaStore(MetaStore):
def __init__(self, endpoint=OPTOPUS_ENDPOINT, **kwargs):
self._client = Client(endpoint=OPTOPUS_ENDPOINT)
self._devices = {}
self.last_devices_refresh = 0
super(OptopusMetaStore, self).__init__(**kwargs)
@property
def devices(self):
do_refresh = (time.time() - self.last_devices_refresh) > device_cache_time
if not self._devices or do_refresh:
self._populate_devices()
return self._devices
def search(self, query_string):
"""
:param query_string:
a valid query string for Optopus, very similar to lucene syntax
returns a list of Device objects
"""
results = self._client.search(query_string, types=['network_node'])
devices = []
for node in results:
devices.append(node['hostname'])
return devices
def _populate_devices(self):
devices = {}
data = self._client.network_nodes
for device_data in data:
node = device_data['network_node']
hostname = node.pop('hostname')
devices[hostname] = node
self._devices = devices
store = OptopusMetaStore(device_options={'port':22})
# Device object cannot be pickled, so we have to return a list of hostnames here
# and then initialize a new Device object in our Pool.map function
devices = store.search('ex2200 active:true location:nyc02')
def get_facts(host):
print "Working on %s" % host
device = Device(host, **store.device_options)
return device.facts
# Spawn 10 processes, and work through our array of device hostnames
pool = Pool(10)
results = pool.map(get_facts, devices)
pprint(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment