def main(): ank_version = pkg_resources.get_distribution("AutoNetkit").version log.info("AutoNetkit %s" % ank_version) import optparse opt = optparse.OptionParser() opt.add_option('--file', '-f', default= None, help="Load topology from FILE") opt.add_option('--monitor', '-m', action="store_true", default= False, help="Monitor input file for changes") opt.add_option('--debug', action="store_true", default= False, help="Debug mode") opt.add_option('--compile', action="store_true", default= False, help="Compile") opt.add_option('--deploy', action="store_true", default= False, help="Deploy") opt.add_option('--measure', action="store_true", default= False, help="Measure") options, arguments = opt.parse_args() input_filename = options.file if not options.file: input_filename = "ank.graphml" if options.debug: #TODO: fix this import logging logger = logging.getLogger("ANK") logger.setLevel(logging.DEBUG) if options.compile: anm = build_network(input_filename) anm.save() nidb = compile_network(anm) nidb.save() render.remove_dirs(["rendered/nectar1/nklab/"]) render.render(nidb) else: anm = AbstractNetworkModel() anm.restore_latest() nidb = NIDB() nidb.restore_latest() if options.deploy: deploy_network(nidb) if options.measure: measure_network(nidb) if options.monitor: try: log.info("Monitoring for updates...") while True: time.sleep(0.2) if change_monitor.check_for_change(input_filename, anm): try: log.info("Input graph updated, recompiling network") if options.compile: nidb = compile_network(anm) render.remove_dirs(["rendered/nectar1/nklab/"]) render.render(nidb) if options.deploy: deploy_network(nidb) if options.measure: measure_network(nidb) log.info("Monitoring for updates...") except: # TODO: remove this, add proper warning log.warn("Unable to build network") pass except KeyboardInterrupt: log.info("Exiting")
def manage_network(input_graph_string, timestamp, build_options, reload_build=False): #import build_network_simple as build_network import build_network if reload_build: # remap? build_network = reload(build_network) settings = config.settings rabbitmq_server = settings['Rabbitmq']['server'] messaging = ank_messaging.AnkMessaging(rabbitmq_server) if build_options['build']: anm = build_network.build(input_graph_string, timestamp) if not build_options['compile']: # publish without nidb import autonetkit.ank_json body = autonetkit.ank_json.dumps(anm) messaging.publish_compressed("www", "client", body) if build_options['compile']: if build_options['archive']: anm.save() nidb = compile_network(anm) import autonetkit.ank_json body = autonetkit.ank_json.dumps(anm, nidb) messaging.publish_compressed("www", "client", body) log.debug("Sent ANM to web server") if build_options['archive']: nidb.save() #render.remove_dirs(["rendered"]) if build_options['render']: render.render(nidb) if not(build_options['build'] or build_options['compile']): # Load from last run import autonetkit.anm anm = autonetkit.anm.AbstractNetworkModel() anm.restore_latest() nidb = NIDB() nidb.restore_latest() body = autonetkit.ank_json.dumps(anm, nidb) messaging.publish_compressed("www", "client", body) if build_options['diff']: import autonetkit.diff nidb_diff = autonetkit.diff.nidb_diff() import ank_json import json data = json.dumps(nidb_diff, cls=ank_json.AnkEncoder, indent = 4) log.info("Wrote diff to diff.json") with open("diff.json", "w") as fh: #TODO: make file specified in config fh.write(data) # Note: this clobbers command line options #build_options.update(settings['General']) # update in case build has updated, eg for deploy #build_options.update(settings['General']) # update in case build has updated, eg for deploy if build_options['deploy']: deploy_network(nidb, input_graph_string) if build_options['measure']: measure_network(nidb)