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