diff --git a/bin/quads.py b/bin/quads.py index daf7aa874..e7a9be946 100755 --- a/bin/quads.py +++ b/bin/quads.py @@ -12,6 +12,10 @@ from subprocess import call from subprocess import check_call +sys.path.append(os.path.dirname(__file__) + "/../") +#from lib.hardware_services.hardware_service import set_hardware_service + + logger = logging.getLogger('quads') ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) diff --git a/lib/Quads.py b/lib/Quads.py index 90c3b0f84..83e256163 100755 --- a/lib/Quads.py +++ b/lib/Quads.py @@ -63,7 +63,15 @@ def __init__(self, config, statedir, movecommand, datearg, syncstate, initialize self.hardware_service_url = hardwareserviceurl - self.inventory_service.load_data(self, force, initialize) + if initialize: + self.quads_init_data(force) + try: + stream = open(config, 'r') + self.data = yaml.load(stream) + stream.close() + except Exception, ex: + self.logger.error(ex) + exit(1) self.quads = QuadsData(self.data) self._quads_history_init() @@ -126,12 +134,32 @@ def _quads_history_init(self): # we occasionally need to write the data back out def quads_write_data(self, doexit = True): - self.inventory_service.write_data(self, doexit) + try: + stream = open(self.config, 'w') + self.data = {"clouds":self.quads.clouds.data, "hosts":self.quads.hosts.data, "history":self.quads.history.data, "cloud_history":self.quads.cloud_history.data} + stream.write( yaml.dump(self.data, default_flow_style=False)) + if doexit: + exit(0) + except Exception, ex: + self.logger.error("There was a problem with your file %s" % ex) + if doexit: + exit(1) # if passed --init, the config data is wiped. # typically we will not want to continue execution if user asks to initialize def quads_init_data(self, force): - self.inventory_service.init_data(self, force) + if not force: + if os.path.isfile(self.config): + self.logger.warn("Warning: " + self.config + " exists. Use --force to initialize.") + exit(1) + try: + stream = open(self.config, 'w') + data = {"clouds":{}, "hosts":{}, "history":{}, "cloud_history":{}} + stream.write( yaml.dump(data, default_flow_style=False)) + exit(0) + except Exception, ex: + self.logger.error("There was a problem with your file %s" % ex) + exit(1) # helper function called from other methods. Never called from main() def _quads_find_current(self, host, datearg): @@ -192,7 +220,19 @@ def quads_hosts_schedule(self, # sync the statedir db for hosts with schedule def quads_sync_state(self): # sync state - self.inventory_service.sync_state(self) + if self.datearg is not None: + self.logger.error("--sync and --date are mutually exclusive.") + exit(1) + for h in sorted(self.quads.hosts.data.iterkeys()): + default_cloud, current_cloud, current_override = self._quads_find_current(h, self.datearg) + if not os.path.isfile(self.statedir + "/" + h): + try: + stream = open(self.statedir + "/" + h, 'w') + stream.write(current_cloud + '\n') + stream.close() + except Exception, ex: + self.logger.error("There was a problem with your file %s" % ex) + return # list the hosts def quads_list_hosts(self): diff --git a/lib/hardware_services/inventory_drivers/MockInventoryDriver.py b/lib/hardware_services/inventory_drivers/MockInventoryDriver.py index e3545d7e9..423571995 100755 --- a/lib/hardware_services/inventory_drivers/MockInventoryDriver.py +++ b/lib/hardware_services/inventory_drivers/MockInventoryDriver.py @@ -140,7 +140,7 @@ def list_clouds(self,quadsinstance): def list_hosts(self,quadsinstance): quadsinstance.quads.hosts.host_list() - def load_data(self, quadsinstance, force, initialize): + def load_data(self, quadsinstance, force): if initialize: quadsinstance.quads_init_data(force) try: @@ -151,45 +151,14 @@ def load_data(self, quadsinstance, force, initialize): quadsinstance.logger.error(ex) exit(1) - def write_data(self, quadsinstance, doexit): - try: - stream = open(quadsinstance.config, 'w') - quadsinstance.data = {"clouds":quadsinstance.quads.clouds.data, - "hosts":quadsinstance.quads.hosts.data, "history":quadsinstance.quads.history.data, "cloud_history":quadsinstance.quads.cloud_history.data} - stream.write( yaml.dump(quadsinstance.data, default_flow_style=False)) - if doexit: - exit(0) - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - if doexit: - exit(1) + def write_data(self, quadsinstance, doexit = True): + quadsinstance.quads_write_data_(doexit) def sync_state(self, quadsinstance): - # sync state - if quadsinstance.datearg is not None: - quadsinstance.logger.error("--sync and --date are mutually exclusive.") - exit(1) - for h in sorted(quadsinstance.quads.hosts.data.iterkeys()): - default_cloud, current_cloud, current_override = quadsinstance._quads_find_current(h, quadsinstance.datearg) - if not os.path.isfile(quadsinstance.statedir + "/" + h): - try: - stream = open(quadsinstance.statedir + "/" + h, 'w') - stream.write(current_cloud + '\n') - stream.close() - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - return + quadsinstance.quads_sync_state_() def init_data(self, quadsinstance, force): - if not force: - if os.path.isfile(quadsinstance.config): - quadsinstance.logger.warn("Warning: " + quadsinstance.config + " exists. Use --force to initialize.") - exit(1) - try: - stream = open(quadsinstance.config, 'w') - data = {"clouds":{}, "hosts":{}, "history":{}, "cloud_history":{}} - stream.write( yaml.dump(data, default_flow_style=False)) - exit(0) - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - exit(1) + quadsinstance.quads_init_data_(force) + + + diff --git a/lib/hardware_services/inventory_drivers/QuadsNativeInventoryDriver.py b/lib/hardware_services/inventory_drivers/QuadsNativeInventoryDriver.py index e32f53487..1efbbd6c7 100755 --- a/lib/hardware_services/inventory_drivers/QuadsNativeInventoryDriver.py +++ b/lib/hardware_services/inventory_drivers/QuadsNativeInventoryDriver.py @@ -142,7 +142,7 @@ def list_clouds(self,quadsinstance): def list_hosts(self,quadsinstance): quadsinstance.quads.hosts.host_list() - def load_data(self, quadsinstance, force, initialize): + def load_data(self, quadsinstance, force): if initialize: quadsinstance.quads_init_data(force) try: @@ -153,45 +153,12 @@ def load_data(self, quadsinstance, force, initialize): quadsinstance.logger.error(ex) exit(1) - def write_data(self, quadsinstance, doexit): - try: - stream = open(quadsinstance.config, 'w') - quadsinstance.data = {"clouds":quadsinstance.quads.clouds.data, - "hosts":quadsinstance.quads.hosts.data, "history":quadsinstance.quads.history.data, "cloud_history":quadsinstance.quads.cloud_history.data} - stream.write( yaml.dump(quadsinstance.data, default_flow_style=False)) - if doexit: - exit(0) - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - if doexit: - exit(1) + def write_data(self, quadsinstance, doexit = True): + quadsinstance.quads_write_data(doexit) def sync_state(self, quadsinstance): - # sync state - if quadsinstance.datearg is not None: - quadsinstance.logger.error("--sync and --date are mutually exclusive.") - exit(1) - for h in sorted(quadsinstance.quads.hosts.data.iterkeys()): - default_cloud, current_cloud, current_override = quadsinstance._quads_find_current(h, quadsinstance.datearg) - if not os.path.isfile(quadsinstance.statedir + "/" + h): - try: - stream = open(quadsinstance.statedir + "/" + h, 'w') - stream.write(current_cloud + '\n') - stream.close() - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - return + quadsinstance.quads_sync_state() def init_data(self, quadsinstance, force): - if not force: - if os.path.isfile(quadsinstance.config): - quadsinstance.logger.warn("Warning: " + quadsinstance.config + " exists. Use --force to initialize.") - exit(1) - try: - stream = open(quadsinstance.config, 'w') - data = {"clouds":{}, "hosts":{}, "history":{}, "cloud_history":{}} - stream.write( yaml.dump(data, default_flow_style=False)) - exit(0) - except Exception, ex: - quadsinstance.logger.error("There was a problem with your file %s" % ex) - exit(1) + quadsinstance.quads_init_data(force) + diff --git a/lib/hardware_services/inventory_service.py b/lib/hardware_services/inventory_service.py index 78379e740..df293f2a4 100755 --- a/lib/hardware_services/inventory_service.py +++ b/lib/hardware_services/inventory_service.py @@ -70,7 +70,7 @@ def list_hosts(self): """ @abstractmethod - def load_data(self, quads, force, initialize): + def load_data(self, quads, force): """ TODO add documentation """ @@ -85,7 +85,7 @@ def sync_state(self, quads): """ @abstractmethod - def write_data(self, quads, doexit): + def write_data(self, quads, doexit = True): """ TODO add documentation """ diff --git a/lib/hardware_services/network_drivers/hil.yml b/lib/hardware_services/network_drivers/hil.yml deleted file mode 100644 index 155326e82..000000000 --- a/lib/hardware_services/network_drivers/hil.yml +++ /dev/null @@ -1,2 +0,0 @@ -url: http://127.0.0.1:5000 # the address of HIL server - diff --git a/lib/hardware_services/network_drivers/hilapi.py b/lib/hardware_services/network_drivers/hilapi.py deleted file mode 100644 index 238767566..000000000 --- a/lib/hardware_services/network_drivers/hilapi.py +++ /dev/null @@ -1,107 +0,0 @@ -# this is a library to conveniently do REST calls to HIL server - -import requests -import sys -import yaml - -def error_check(response): - if response.status_code < 200 or response.status_code >= 300: - sys.exit(response.text) - else: - print(response.text) - return response.json() - -def make_url(*args): - with open("hil.yml", 'r') as stream: - try: - data = yaml.load(stream) - except yaml.YAMLError: - sys.exit("Can't parse hil.yml file.") - url = data.get('url') - if url is None: - sys.exit("Hil url is not specified in hil.yml.") - for arg in args: - url += '/' + arg - return url - -def do_put(url, data={}): - error_check(requests.put(url, data=json.dumps(data))) - -def do_post(url, data={}): - error_check(requests.post(url, data=json.dumps(data))) - -def do_get(url, params=None): - return error_check(requests.get(url, params=params)) - -def do_delete(url): - error_check(requests.delete(url)) - -def network_create_simple(network, project): - if project is None: - project = network - url = make_url('network', network) - do_put(url, data={'owner': project, - 'access': project, - 'net_id': ""}) - -def network_delete(network): - url = make_url('network', network) - do_delete(url) - -def list_projects(): - url = make_url('projects') - return do_get(url) - -def project_create(project): - url = make_url('project', project) - do_put(url) - -def project_delete(project): - url = make_url('project', project) - do_delete(url) - -def project_connect_node(project, node): - url = make_url('project', project, 'connect_node') - do_post(url, data={'node': node}) - -def project_detach_node(project, node): - url = make_url('project', project, 'detach_node') - do_post(url, data={'node': node}) - -def node_connect_network(node, nic, network, channel='null'): - # use default value nic='nic' - url = make_url('node', node, 'nic', nic, 'connect_network') - do_post(url, data={'network': network, - 'channel': channel}) - -def node_detach_network(node, nic, network): - # use default value nic='nic' - url = make_url('node', node, 'nic', nic, 'detach_network') - do_post(url, data={'network': network}) - -def list_nodes(is_free='all'): - if is_free not in ('all', 'free'): - sys.exit("list_nodes should have 'all' or 'free'") - url = make_url('nodes', is_free) - return do_get(url) - -def list_project_nodes(project): - url = make_url('project', project, 'nodes') - return do_get(url) - -def list_project_networks(project): - url = make_url('project', project, 'networks') - return do_get(url) - -def list_networks(): - url = make_url('networks') - return do_get(url) - -def show_network(network): - url = make_url('network', network) - return do_get(url) - -def show_node(node): - url = make_url('node', node) - return do_get(url) -