diff options
author | Parker Berberian <pberberian@iol.unh.edu> | 2017-08-21 09:31:16 -0400 |
---|---|---|
committer | Parker Berberian <pberberian@iol.unh.edu> | 2017-08-31 13:28:58 -0400 |
commit | 0a783da920b78ddac25bde796e0f9e165c4a7597 (patch) | |
tree | ca864fe7ff661a7265de3bfc5f6cdc3c7417d25e /tools/laas-fog/source/resetDataBase.py | |
parent | 136e34eeb584922f6a3fab4ed9e8f5b35d8b0921 (diff) |
Adds SQLite DataBase
JIRA: N/A
Adds a database handler in source/database.py to store all
hosts and to store any bookings coming from the dashboard.
source/resetDataBase will clean the db and try to repopulate it
with information from the FOG server.
Change-Id: I14537452d8566db17787f116018f45bb1ddd75ba
Signed-off-by: Parker Berberian <pberberian@iol.unh.edu>
Diffstat (limited to 'tools/laas-fog/source/resetDataBase.py')
-rwxr-xr-x | tools/laas-fog/source/resetDataBase.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tools/laas-fog/source/resetDataBase.py b/tools/laas-fog/source/resetDataBase.py new file mode 100755 index 00000000..ff141e58 --- /dev/null +++ b/tools/laas-fog/source/resetDataBase.py @@ -0,0 +1,110 @@ +#!/usr/bin/python +""" +############################################################################# +#Copyright 2017 Parker Berberian and others # +# # +#Licensed under the Apache License, Version 2.0 (the "License"); # +#you may not use this file except in compliance with the License. # +#You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +#Unless required by applicable law or agreed to in writing, software # +#distributed under the License is distributed on an "AS IS" BASIS, # +#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +#See the License for the specific language governing permissions and # +#limitations under the License. # +############################################################################# +""" + +import sys +import os +import yaml +from api.fog import FOG_Handler +from database import HostDataBase +from database import BookingDataBase + +""" +This file just resets the host database with +all the hosts in fog, with all of them +showing as available + +This file is just provided to make populating the host db easier. +If you wanted to do this yourself, you could do the following in +a python command prompt: + from database import HostDataBase + db = HostDataBase("/path/to/file") + db.addHost("host-name") + db.addHost("host-name") + db.addHost("host-name") + +""" +config = None +if "--config" in sys.argv: + i = sys.argv.index("--config") + if len(sys.argv) > i+1 and os.path.isfile(sys.argv[i+1]): + try: + config = yaml.safe_load(open(sys.argv[i+1])) + except Exception: + print "failed to read config file. exiting" + sys.exit(1) + else: + print "config file not found. exiting" + sys.exit(1) +else: + print "no config file given. Specify file with '--config <FILE_PATH>'" + sys.exit(1) + +host = False +if "--host" in sys.argv or "--both" in sys.argv: + host = True + +booking = False +if "--booking" in sys.argv or "--both" in sys.argv: + booking = True + + +if host: + + fog = FOG_Handler( + config['fog']['server'] + ) + if os.path.isfile(config['fog']['api_key']): + fog.getFogKeyFromFile(config['fog']['api_key']) + else: + fog.setFogKey(config['fog']['api_key']) + + if os.path.isfile(config['fog']['user_key']): + fog.getUserKeyFromFile(config['fog']['user_key']) + else: + fog.setUserKey(config['fog']['user_key']) + hosts = fog.getHostsinGroup("vm") + host_names = [] + for host in hosts: + host_names.append(host['name']) + + # creates the directory of the db, if it doesnt yet exist + dbDir = os.path.dirname(config['database']) + if not os.path.isdir(dbDir): + os.makedirs(dbDir) + + db = HostDataBase(config['database']) + + # check if the table already exists or not + try: + db.cursor.execute("SELECT * FROM hosts") + except Exception as err: + if "no such table" in str(err): + db.createTable() + + db.resetHosts(host_names) + +if booking: + db = BookingDataBase(config['database']) + db.createTable() + db.close() + +else: + print "you must specify the '--host', '--booking', or '--both' option" + print "depending on which database you wish to reset" + sys.exit(0) |