Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockInventoryDriver.py QuadsNativeInventoryDriver.py functions #53

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions bin/quads.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
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)
Expand Down Expand Up @@ -56,14 +52,14 @@ def main(argv):

sys.path.append(quads_config["install_dir"] + "/lib")
sys.path.append(os.path.dirname(__file__) + "/../lib")
sys.path.append(os.path.dirname(__file__) + "/../lib/hardware_services/hardware_drivers/")

import libquads

defaultconfig = quads_config["data_dir"] + "/schedule.yaml"
defaultstatedir = quads_config["data_dir"] + "/state"
defaultmovecommand = "/bin/echo"

# EC528 addition - sets hardware service
# EC528: addition - sets hardware service
defaulthardwareservice = quads_config["hardware_service"]


Expand Down
47 changes: 39 additions & 8 deletions lib/hardware_services/inventory_drivers/MockInventoryDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def update_cloud(self, quadsinstance, **kwargs):

quadsinstance.quads.clouds.data[cloudresource] = { "description": description, "networks": {}, "owner": cloudowner, "ccusers": ccusers, "ticket": cloudticket, "qinq": qinq }
quadsinstance.quads_write_data()

return

def update_host(self, quadsinstance, **kwargs):
Expand Down Expand Up @@ -140,7 +139,7 @@ def list_clouds(self,quadsinstance):
def list_hosts(self,quadsinstance):
quadsinstance.quads.hosts.host_list()

def load_data(self, quadsinstance, force):
def load_data(self, quadsinstance, force, initialize):
if initialize:
quadsinstance.quads_init_data(force)
try:
Expand All @@ -151,14 +150,46 @@ def load_data(self, quadsinstance, force):
quadsinstance.logger.error(ex)
exit(1)

def write_data(self, quadsinstance, doexit = True):
quadsinstance.quads_write_data_(doexit)
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 sync_state(self, quadsinstance):
quadsinstance.quads_sync_state_()
# 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

def init_data(self, quadsinstance, force):
quadsinstance.quads_init_data_(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)

Original file line number Diff line number Diff line change
Expand Up @@ -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):
def load_data(self, quadsinstance, force, initialize):
if initialize:
quadsinstance.quads_init_data(force)
try:
Expand All @@ -153,12 +153,46 @@ def load_data(self, quadsinstance, force):
quadsinstance.logger.error(ex)
exit(1)

def write_data(self, quadsinstance, doexit = True):
quadsinstance.quads_write_data(doexit)
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 sync_state(self, quadsinstance):
quadsinstance.quads_sync_state()
# 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

def init_data(self, quadsinstance, force):
quadsinstance.quads_init_data(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)

2 changes: 2 additions & 0 deletions lib/hardware_services/inventory_drivers/hil.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
url: http://127.0.0.1:5000 # the address of HIL server
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemp see #54
as we discussed prior, I added this as a parameter to conf/quads.yml

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We discussed that. And I agreed with you. But I don't see it added.

So can we merge this first and then we can move this line anywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


107 changes: 107 additions & 0 deletions lib/hardware_services/inventory_drivers/hilapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemp see #54

2 changes: 1 addition & 1 deletion lib/hardware_services/inventory_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def list_hosts(self):
"""

@abstractmethod
def load_data(self, quads, force):
def load_data(self, quads, force, initialize):
""" TODO add documentation
"""

Expand Down
2 changes: 2 additions & 0 deletions lib/hardware_services/network_drivers/hil.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
url: http://127.0.0.1:5000 # the address of HIL server

107 changes: 107 additions & 0 deletions lib/hardware_services/network_drivers/hilapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemp see #54

Loading