components/openstack/nova/files/nova-conductor
branchs11-update
changeset 3028 5e73a3a3f66a
child 1820 f3a6bd7bd4a6
equal deleted inserted replaced
3027:3bcf7d43558b 3028:5e73a3a3f66a
       
     1 #!/usr/bin/python2.6
       
     2 
       
     3 # Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
       
     4 #
       
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
       
     6 #    not use this file except in compliance with the License. You may obtain
       
     7 #    a copy of the License at
       
     8 #
       
     9 #         http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 #    Unless required by applicable law or agreed to in writing, software
       
    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
       
    14 #    License for the specific language governing permissions and limitations
       
    15 #    under the License.
       
    16 
       
    17 import ConfigParser
       
    18 import os
       
    19 import sys
       
    20 
       
    21 import smf_include
       
    22 
       
    23 from subprocess import CalledProcessError, check_call, PIPE, Popen
       
    24 
       
    25 from sqlalchemy import create_engine
       
    26 
       
    27 
       
    28 def db_sync():
       
    29     """ function to create the database schema
       
    30     """
       
    31 
       
    32     cmd = ["/usr/bin/nova-manage", "db", "sync"]
       
    33     try:
       
    34         check_call(cmd)
       
    35     except CalledProcessError as err:
       
    36         print "Unable to create database for Nova:  %s" % err
       
    37         sys.exit(smf_include.SMF_EXIT_ERR_CONFIG)
       
    38 
       
    39 
       
    40 def start():
       
    41     # read the options from the config file
       
    42     parser = ConfigParser.ConfigParser()
       
    43     parser.read("/etc/nova/nova.conf")
       
    44 
       
    45     # get the database type
       
    46     db_engine = create_engine(parser.get("DEFAULT", "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/nova/nova-conductor")
       
    81 
       
    82 if __name__ == "__main__":
       
    83     os.putenv("LC_ALL", "C")
       
    84     smf_include.smf_main()