3 #############################################################################
4 #Copyright 2017 Parker Berberian and others #
6 #Licensed under the Apache License, Version 2.0 (the "License"); #
7 #you may not use this file except in compliance with the License. #
8 #You may obtain a copy of the License at #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
12 #Unless required by applicable law or agreed to in writing, software #
13 #distributed under the License is distributed on an "AS IS" BASIS, #
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 #See the License for the specific language governing permissions and #
16 #limitations under the License. #
17 #############################################################################
23 from api.fog import FOG_Handler
24 from database import HostDataBase
25 from database import BookingDataBase
28 This file just resets the host database with
29 all the hosts in fog, with all of them
32 This file is just provided to make populating the host db easier.
33 If you wanted to do this yourself, you could do the following in
34 a python command prompt:
35 from database import HostDataBase
36 db = HostDataBase("/path/to/file")
37 db.addHost("host-name")
38 db.addHost("host-name")
39 db.addHost("host-name")
43 if "--config" in sys.argv:
44 i = sys.argv.index("--config")
45 if len(sys.argv) > i+1 and os.path.isfile(sys.argv[i+1]):
47 config = yaml.safe_load(open(sys.argv[i+1]))
49 print "failed to read config file. exiting"
52 print "config file not found. exiting"
55 print "no config file given. Specify file with '--config <FILE_PATH>'"
59 if "--host" in sys.argv or "--both" in sys.argv:
63 if "--booking" in sys.argv or "--both" in sys.argv:
70 config['fog']['server']
72 if os.path.isfile(config['fog']['api_key']):
73 fog.getFogKeyFromFile(config['fog']['api_key'])
75 fog.setFogKey(config['fog']['api_key'])
77 if os.path.isfile(config['fog']['user_key']):
78 fog.getUserKeyFromFile(config['fog']['user_key'])
80 fog.setUserKey(config['fog']['user_key'])
81 hosts = fog.getHostsinGroup("vm")
84 host_names.append(host['name'])
86 # creates the directory of the db, if it doesnt yet exist
87 dbDir = os.path.dirname(config['database'])
88 if not os.path.isdir(dbDir):
91 db = HostDataBase(config['database'])
93 # check if the table already exists or not
95 db.cursor.execute("SELECT * FROM hosts")
96 except Exception as err:
97 if "no such table" in str(err):
100 db.resetHosts(host_names)
103 db = BookingDataBase(config['database'])
108 print "you must specify the '--host', '--booking', or '--both' option"
109 print "depending on which database you wish to reset"