12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 # License for the specific language governing permissions and limitations |
14 # License for the specific language governing permissions and limitations |
15 # under the License. |
15 # under the License. |
16 |
16 |
17 import ConfigParser |
|
18 import os |
17 import os |
19 import sys |
18 import sys |
20 |
19 |
21 import smf_include |
20 import smf_include |
22 |
21 |
23 from subprocess import CalledProcessError, check_call, PIPE, Popen |
22 from subprocess import CalledProcessError, check_call |
24 |
|
25 from sqlalchemy import create_engine |
|
26 |
23 |
27 |
24 |
28 def db_sync(): |
25 def start(): |
29 """ function to create the database schema |
26 # sync the database to make sure it's ready |
30 """ |
|
31 |
|
32 cmd = ["/usr/bin/keystone-manage", "db_sync"] |
27 cmd = ["/usr/bin/keystone-manage", "db_sync"] |
33 try: |
28 try: |
34 check_call(cmd) |
29 check_call(cmd) |
35 except CalledProcessError as err: |
30 except CalledProcessError as err: |
36 print "Unable to create database for Keystone: %s" % err |
31 print "Unable to create database for Keystone: %s" % err |
37 sys.exit(smf_include.SMF_EXIT_ERR_CONFIG) |
32 sys.exit(smf_include.SMF_EXIT_ERR_CONFIG) |
38 |
33 |
39 |
|
40 def start(): |
|
41 # read the options from the config file |
|
42 parser = ConfigParser.ConfigParser() |
|
43 parser.read("/etc/keystone/keystone.conf") |
|
44 |
|
45 # get the database type |
|
46 db_engine = create_engine(parser.get("sql", "connection")) |
|
47 db_type = db_engine.name |
|
48 |
|
49 if db_type == "sqlite": |
|
50 # look to see if file exists or if it's zero length |
|
51 abspath = os.path.abspath(db_engine.url.database) |
|
52 if not os.path.exists(abspath) or os.path.getsize(abspath) == 0: |
|
53 db_sync() |
|
54 |
|
55 elif db_type == "mysql": |
|
56 mysql_svc = "svc:/application/database/mysql:version_55" |
|
57 cmd = ["/usr/bin/svcs", "-H", "-o", "state", mysql_svc] |
|
58 |
|
59 try: |
|
60 p = Popen(cmd, stdout=PIPE, stderr=PIPE) |
|
61 output, error = p.communicate() |
|
62 except CalledProcessError: |
|
63 print "mysql service not found. Is it installed?" |
|
64 return smf_include.SMF_EXIT_ERR_CONFIG |
|
65 |
|
66 if output.strip() != "online": |
|
67 # attempt to start mysql |
|
68 cmd = ["/usr/sbin/svcadm", "enable", "-rs", mysql_svc] |
|
69 |
|
70 try: |
|
71 check_call(cmd) |
|
72 except CalledProcessError as err: |
|
73 print "starting mysql service failed: %s" % err |
|
74 return smf_include.SMF_EXIT_ERR_CONFIG |
|
75 |
|
76 # not sure how to check if the database is valid, so just create |
|
77 # the database every time for now |
|
78 db_sync() |
|
79 |
|
80 smf_include.smf_subprocess("/usr/lib/keystone/keystone-all") |
34 smf_include.smf_subprocess("/usr/lib/keystone/keystone-all") |
81 |
35 |
82 if __name__ == "__main__": |
36 if __name__ == "__main__": |
83 os.putenv("LC_ALL", "C") |
37 os.putenv("LC_ALL", "C") |
84 smf_include.smf_main() |
38 smf_include.smf_main() |