4623N/A#!/usr/bin/python2.7
4506N/A
4506N/A# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
4506N/A#
4506N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
4506N/A# not use this file except in compliance with the License. You may obtain
4506N/A# a copy of the License at
4506N/A#
4506N/A# http://www.apache.org/licenses/LICENSE-2.0
4506N/A#
4506N/A# Unless required by applicable law or agreed to in writing, software
4506N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
4506N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
4506N/A# License for the specific language governing permissions and limitations
4506N/A# under the License.
4506N/A
4506N/Aimport os
4506N/Afrom subprocess import Popen, PIPE
4506N/Aimport sys
4506N/A
4506N/Aimport smf_include
4506N/A
4506N/A
4506N/Adef start():
4506N/A ironic_conf = sys.argv[2]
4506N/A
4506N/A # verify config path is valid
4506N/A if not os.path.exists(ironic_conf) or not os.access(ironic_conf, os.R_OK):
4506N/A print >> sys.stderr, '%s does not exist or is not readable' % \
4506N/A ironic_conf
4506N/A return smf_include.SMF_EXIT_ERR_CONFIG
4506N/A
4506N/A # Create/Sync Ironic database
4506N/A cmd = ["/usr/bin/ironic-dbsync", "--config-file", ironic_conf,
4506N/A "create_schema"]
4506N/A proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
4506N/A _out, error = proc.communicate()
4506N/A
4506N/A if proc.returncode != 0:
4506N/A if "DbMigrationError" in error:
4506N/A # Attempted to create already existing database
4506N/A # Attempt to upgrade instead
4506N/A
4506N/A cmd = ["/usr/bin/ironic-dbsync", "--config-file", ironic_conf,
4506N/A "upgrade"]
4506N/A proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
4506N/A _out, error = proc.communicate()
4506N/A if proc.returncode != 0:
4506N/A print >> sys.stderr, \
4506N/A 'Error executing ironic-dbsync upgrade: %s' % error
4506N/A return smf_include.SMF_EXIT_ERR_FATAL
4506N/A else:
4506N/A print >> sys.stderr, \
4506N/A 'Error executing ironic-dbsync create_schema: %s' % error
4506N/A return smf_include.SMF_EXIT_ERR_FATAL
4506N/A
4506N/A return smf_include.SMF_EXIT_OK
4506N/A
4506N/Aif __name__ == "__main__":
4506N/A os.putenv("LC_ALL", "C")
4506N/A smf_include.smf_main()