nova-conductor revision 2521
2521N/A#!/usr/bin/python2.6
2521N/A
2521N/A# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
2521N/A#
2521N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
2521N/A# not use this file except in compliance with the License. You may obtain
2521N/A# a copy of the License at
2521N/A#
2521N/A# http://www.apache.org/licenses/LICENSE-2.0
2521N/A#
2521N/A# Unless required by applicable law or agreed to in writing, software
2521N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
2521N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
2521N/A# License for the specific language governing permissions and limitations
2521N/A# under the License.
2521N/A
2521N/Aimport ConfigParser
2521N/Aimport os
2521N/Aimport sys
2521N/A
2521N/Aimport smf_include
2521N/A
2521N/Afrom subprocess import CalledProcessError, check_call, PIPE, Popen
2521N/A
2521N/Afrom sqlalchemy import create_engine
2521N/A
2521N/A
2521N/Adef db_sync():
2521N/A """ function to create the database schema
2521N/A """
2521N/A
2521N/A cmd = ["/usr/bin/nova-manage", "db", "sync"]
2521N/A try:
2521N/A check_call(cmd)
2521N/A except CalledProcessError as err:
2521N/A print "Unable to create database for Nova: %s" % err
2521N/A sys.exit(smf_include.SMF_EXIT_ERR_CONFIG)
2521N/A
2521N/A
2521N/Adef start():
2521N/A # read the options from the config file
2521N/A parser = ConfigParser.ConfigParser()
2521N/A parser.read("/etc/nova/nova.conf")
2521N/A
2521N/A # get the database type
2521N/A db_engine = create_engine(parser.get("DEFAULT", "sql_connection"))
2521N/A db_type = db_engine.name
2521N/A
2521N/A if db_type == "sqlite":
2521N/A # look to see if file exists or if it's zero length
2521N/A abspath = os.path.abspath(db_engine.url.database)
2521N/A if not os.path.exists(abspath) or os.path.getsize(abspath) == 0:
2521N/A db_sync()
2521N/A
2521N/A elif db_type == "mysql":
2521N/A mysql_svc = "svc:/application/database/mysql:version_55"
2521N/A cmd = ["/usr/bin/svcs", "-H", "-o", "state", mysql_svc]
2521N/A
2521N/A try:
2521N/A p = Popen(cmd, stdout=PIPE, stderr=PIPE)
2521N/A output, error = p.communicate()
2521N/A except CalledProcessError:
2521N/A print "mysql service not found. Is it installed?"
2521N/A return smf_include.SMF_EXIT_ERR_CONFIG
2521N/A
2521N/A if output.strip() != "online":
2521N/A # attempt to start mysql
2521N/A cmd = ["/usr/sbin/svcadm", "enable", "-rs", mysql_svc]
2521N/A
2521N/A try:
2521N/A check_call(cmd)
2521N/A except CalledProcessError as err:
2521N/A print "starting mysql service failed: %s" % err
2521N/A return smf_include.SMF_EXIT_ERR_CONFIG
2521N/A
2521N/A # not sure how to check if the database is valid, so just create
2521N/A # the database every time for now
2521N/A db_sync()
2521N/A
2521N/A smf_include.smf_subprocess("/usr/lib/nova/nova-conductor")
2521N/A
2521N/Aif __name__ == "__main__":
2521N/A os.putenv("LC_ALL", "C")
2521N/A smf_include.smf_main()