Adds SQLite DataBase
[pharos-tools.git] / laas-fog / source / resetDataBase.py
1 #!/usr/bin/python
2 """
3 #############################################################################
4 #Copyright 2017 Parker Berberian and others                                 #
5 #                                                                           #
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                                    #
9 #                                                                           #
10 #    http://www.apache.org/licenses/LICENSE-2.0                             #
11 #                                                                           #
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 #############################################################################
18 """
19
20 import sys
21 import os
22 import yaml
23 from api.fog import FOG_Handler
24 from database import HostDataBase
25 from database import BookingDataBase
26
27 """
28 This file just resets the host database with
29 all the hosts in fog, with all of them
30 showing as available
31
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")
40
41 """
42 config = None
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]):
46         try:
47             config = yaml.safe_load(open(sys.argv[i+1]))
48         except Exception:
49             print "failed to read config file. exiting"
50             sys.exit(1)
51     else:
52         print "config file not found. exiting"
53         sys.exit(1)
54 else:
55     print "no config file given. Specify file with '--config <FILE_PATH>'"
56     sys.exit(1)
57
58 host = False
59 if "--host" in sys.argv or "--both" in sys.argv:
60     host = True
61
62 booking = False
63 if "--booking" in sys.argv or "--both" in sys.argv:
64     booking = True
65
66
67 if host:
68
69     fog = FOG_Handler(
70             config['fog']['server']
71             )
72     if os.path.isfile(config['fog']['api_key']):
73         fog.getFogKeyFromFile(config['fog']['api_key'])
74     else:
75         fog.setFogKey(config['fog']['api_key'])
76
77     if os.path.isfile(config['fog']['user_key']):
78         fog.getUserKeyFromFile(config['fog']['user_key'])
79     else:
80         fog.setUserKey(config['fog']['user_key'])
81     hosts = fog.getHostsinGroup("vm")
82     host_names = []
83     for host in hosts:
84         host_names.append(host['name'])
85
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):
89         os.makedirs(dbDir)
90
91     db = HostDataBase(config['database'])
92
93     # check if the table already exists or not
94     try:
95         db.cursor.execute("SELECT * FROM hosts")
96     except Exception as err:
97         if "no such table" in str(err):
98             db.createTable()
99
100     db.resetHosts(host_names)
101
102 if booking:
103     db = BookingDataBase(config['database'])
104     db.createTable()
105     db.close()
106
107 else:
108     print "you must specify the '--host', '--booking', or '--both' option"
109     print "depending on which database you wish to reset"
110     sys.exit(0)