heat-upgrade revision 4070
4070N/A#!/usr/bin/python2.6
4070N/A
4070N/A# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4070N/A#
4070N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
4070N/A# not use this file except in compliance with the License. You may obtain
4070N/A# a copy of the License at
4070N/A#
4070N/A# http://www.apache.org/licenses/LICENSE-2.0
4070N/A#
4070N/A# Unless required by applicable law or agreed to in writing, software
4070N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
4070N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
4070N/A# License for the specific language governing permissions and limitations
4070N/A# under the License.
4070N/A
4070N/Afrom ConfigParser import NoOptionError
4070N/Afrom datetime import datetime
4070N/Aimport errno
4070N/Aimport glob
4070N/Aimport os
4070N/Aimport shutil
4070N/Afrom subprocess import check_call, Popen, PIPE
4070N/Aimport sys
4070N/Aimport time
4070N/Aimport traceback
4070N/A
4070N/Aimport iniparse
4070N/Aimport smf_include
4070N/Aimport sqlalchemy
4070N/A
4070N/A
4070N/AHEAT_CONF_MAPPINGS = {
4070N/A # Deprecated group/name
4070N/A ('DEFAULT', 'stack_user_domain'): ('DEFAULT', 'stack_user_domain_id'),
4070N/A ('DEFAULT', 'rabbit_durable_queues'): ('DEFAULT', 'amqp_durable_queues'),
4070N/A ('rpc_notifier2', 'topics'): ('DEFAULT', 'notification_topics'),
4070N/A ('DEFAULT', 'log_config'): ('DEFAULT', 'log_config_append'),
4070N/A ('DEFAULT', 'logfile'): ('DEFAULT', 'log_file'),
4070N/A ('DEFAULT', 'logdir'): ('DEFAULT', 'log_dir'),
4070N/A ('DEFAULT', 'db_backend'): ('database', 'backend'),
4070N/A ('DEFAULT', 'sql_connection'): ('database', 'connection'),
4070N/A ('DATABASE', 'sql_connection'): ('database', 'connection'),
4070N/A ('sql', 'connection'): ('database', 'connection'),
4070N/A ('DEFAULT', 'sql_idle_timeout'): ('database', 'idle_timeout'),
4070N/A ('DATABASE', 'sql_idle_timeout'): ('database', 'idle_timeout'),
4070N/A ('sql', 'idle_timeout'): ('database', 'idle_timeout'),
4070N/A ('DEFAULT', 'sql_min_pool_size'): ('database', 'min_pool_size'),
4070N/A ('DATABASE', 'sql_min_pool_size'): ('database', 'min_pool_size'),
4070N/A ('DEFAULT', 'sql_max_pool_size'): ('database', 'max_pool_size'),
4070N/A ('DATABASE', 'sql_max_pool_size'): ('database', 'max_pool_size'),
4070N/A ('DEFAULT', 'sql_max_retries'): ('database', 'max_retries'),
4070N/A ('DATABASE', 'sql_max_retries'): ('database', 'max_retries'),
4070N/A ('DEFAULT', 'sql_retry_interval'): ('database', 'retry_interval'),
4070N/A ('DATABASE', 'reconnect_interval'): ('database', 'retry_interval'),
4070N/A ('DEFAULT', 'sql_max_overflow'): ('database', 'max_overflow'),
4070N/A ('DATABASE', 'sqlalchemy_max_overflow'): ('database', 'max_overflow'),
4070N/A ('DEFAULT', 'sql_connection_debug'): ('database', 'connection_debug'),
4070N/A ('DEFAULT', 'sql_connection_trace'): ('database', 'connection_trace'),
4070N/A ('DATABASE', 'sqlalchemy_pool_timeout'): ('database', 'pool_timeout'),
4070N/A ('DEFAULT', 'memcache_servers'):
4070N/A ('keystone_authtoken', 'memcached_servers'),
4070N/A ('DEFAULT', 'matchmaker_ringfile'): ('matchmaker_ring', 'ringfile'),
4070N/A # No longer referenced by the service or causes a DeprecationWarning
4070N/A ('DEFAULT', 'instance_user'): (None, None),
4070N/A ('DEFAULT', 'onready'): (None, None),
4070N/A ('DEFAULT', 'list_notifier_drivers'): (None, None),
4070N/A}
4070N/A
4070N/A
4070N/Adef update_mapping(section, key, mapping):
4070N/A """ look for deprecated variables and, if found, convert it to the new
4070N/A section/key.
4070N/A """
4070N/A
4070N/A if (section, key) in mapping:
4070N/A print "Deprecated value found: [%s] %s" % (section, key)
4070N/A section, key = mapping[(section, key)]
4070N/A if section is None and key is None:
4070N/A print "Removing from configuration"
4070N/A else:
4070N/A print "Updating to: [%s] %s" % (section, key)
4070N/A return section, key
4070N/A
4070N/A
4070N/Adef alter_mysql_tables(engine):
4070N/A """ Convert MySQL tables to use utf8
4070N/A """
4070N/A
4070N/A import MySQLdb
4070N/A
4070N/A for _none in range(5):
4070N/A try:
4070N/A db = MySQLdb.connect(host=engine.url.host,
4070N/A user=engine.url.username,
4070N/A passwd=engine.url.password,
4070N/A db=engine.url.database)
4070N/A break
4070N/A except MySQLdb.OperationalError as err:
4070N/A # mysql is not ready. sleep for 2 more seconds
4070N/A time.sleep(2)
4070N/A else:
4070N/A print "Unable to connect to MySQL: %s" % err
4070N/A print ("Please verify MySQL is properly configured and online "
4070N/A "before using svcadm(1M) to clear this service.")
4070N/A sys.exit(smf_include.SMF_EXIT_ERR_FATAL)
4070N/A
4070N/A cursor = db.cursor()
4070N/A cursor.execute("ALTER DATABASE %s CHARACTER SET = 'utf8'" %
4070N/A engine.url.database)
4070N/A cursor.execute("ALTER DATABASE %s COLLATE = 'utf8_general_ci'" %
4070N/A engine.url.database)
4070N/A cursor.execute("SHOW tables")
4070N/A res = cursor.fetchall()
4070N/A if res:
4070N/A cursor.execute("SET foreign_key_checks = 0")
4070N/A for item in res:
4070N/A cursor.execute("ALTER TABLE %s.%s CONVERT TO "
4070N/A "CHARACTER SET 'utf8', COLLATE 'utf8_general_ci'"
4070N/A % (engine.url.database, item[0]))
4070N/A cursor.execute("SET foreign_key_checks = 1")
4070N/A db.commit()
4070N/A db.close()
4070N/A
4070N/A
4070N/Adef modify_conf(old_file, mapping=None):
4070N/A """ Copy over all uncommented options from the old configuration file. In
4070N/A addition, look for deprecated section/keys and convert them to the new
4070N/A section/key.
4070N/A """
4070N/A
4070N/A new_file = old_file + '.new'
4070N/A
4070N/A # open the previous version
4070N/A old = iniparse.ConfigParser()
4070N/A old.readfp(open(old_file))
4070N/A
4070N/A # open the new version
4070N/A new = iniparse.ConfigParser()
4070N/A try:
4070N/A new.readfp(open(new_file))
4070N/A except IOError as err:
4070N/A if err.errno == errno.ENOENT:
4070N/A # The upgrade did not deliver a .new file so, return
4070N/A print "%s not found - continuing with %s" % (new_file, old_file)
4070N/A return
4070N/A else:
4070N/A raise
4070N/A print "\nupdating %s" % old_file
4070N/A
4070N/A # walk every single section for uncommented options
4070N/A default_items = set(old.items('DEFAULT'))
4070N/A for section in old.sections() + ['DEFAULT']:
4070N/A
4070N/A # DEFAULT items show up in every section so remove them
4070N/A if section != 'DEFAULT':
4070N/A section_items = set(old.items(section)) - default_items
4070N/A else:
4070N/A section_items = default_items
4070N/A
4070N/A for key, value in section_items:
4070N/A # keep a copy of the old value
4070N/A oldvalue = value
4070N/A
4070N/A if mapping is not None:
4070N/A section, key = update_mapping(section, key, mapping)
4070N/A
4070N/A if section is None and key is None:
4070N/A # option is deprecated so continue
4070N/A continue
4070N/A
4070N/A if not new.has_section(section):
4070N/A if section != 'DEFAULT':
4070N/A new.add_section(section)
4070N/A
4070N/A # print to the log when a value for the same section.key is
4070N/A # changing to a new value
4070N/A try:
4070N/A new_value = new.get(section, key)
4070N/A if new_value != value and '%SERVICE' not in new_value:
4070N/A print "Changing [%s] %s:\n- %s\n+ %s" % \
4070N/A (section, key, oldvalue, new_value)
4070N/A print
4070N/A except NoOptionError:
4070N/A # the new configuration file does not have this option set so
4070N/A # just continue
4070N/A pass
4070N/A
4070N/A # Only copy the old value to the new conf file if the entry doesn't
4070N/A # exist or if it contains '%SERVICE'
4070N/A if not new.has_option(section, key) or \
4070N/A '%SERVICE' in new.get(section, key):
4070N/A new.set(section, key, value)
4070N/A
4070N/A # copy the old conf file to a backup
4070N/A today = datetime.now().strftime("%Y%m%d%H%M%S")
4070N/A shutil.copy2(old_file, old_file + '.' + today)
4070N/A
4070N/A # copy the new conf file in place
4070N/A with open(old_file, 'wb+') as fh:
4070N/A new.write(fh)
4070N/A
4070N/A
4070N/Adef start():
4070N/A # pull out the current version of config/upgrade-id
4070N/A p = Popen(['/usr/bin/svcprop', '-p', 'config/upgrade-id',
4070N/A os.environ['SMF_FMRI']], stdout=PIPE, stderr=PIPE)
4070N/A curr_ver, _err = p.communicate()
4070N/A curr_ver = curr_ver.strip()
4070N/A
4070N/A # extract the openstack-upgrade-id from the pkg
4070N/A p = Popen(['/usr/bin/pkg', 'contents', '-H', '-t', 'set', '-o', 'value',
4070N/A '-a', 'name=openstack.upgrade-id',
4070N/A 'pkg:/cloud/openstack/heat'], stdout=PIPE, stderr=PIPE)
4070N/A pkg_ver, _err = p.communicate()
4070N/A pkg_ver = pkg_ver.strip()
4070N/A
4070N/A if curr_ver == pkg_ver:
4070N/A # No need to upgrade
4070N/A sys.exit(smf_include.SMF_EXIT_OK)
4070N/A
4070N/A # look for any .new files
4070N/A if glob.glob('/etc/heat/*.new'):
4070N/A # the versions are different, so perform an upgrade
4070N/A # modify the configuration files
4070N/A modify_conf('/etc/heat/api-paste.ini')
4070N/A modify_conf('/etc/heat/heat.conf', HEAT_CONF_MAPPINGS)
4070N/A
4070N/A config = iniparse.RawConfigParser()
4070N/A config.read('/etc/heat/heat.conf')
4070N/A # In certain cases the database section does not exist and the
4070N/A # default database chosen is sqlite.
4070N/A if config.has_section('database'):
4070N/A db_connection = config.get('database', 'connection')
4070N/A
4070N/A if db_connection.startswith('mysql'):
4070N/A engine = sqlalchemy.create_engine(db_connection)
4070N/A if engine.url.username != '%SERVICE_USER%':
4070N/A alter_mysql_tables(engine)
4070N/A print "altered character set to utf8 in heat tables"
4070N/A
4070N/A # update the current version
4070N/A check_call(['/usr/sbin/svccfg', '-s', os.environ['SMF_FMRI'], 'setprop',
4070N/A 'config/upgrade-id', '=', pkg_ver])
4070N/A check_call(['/usr/sbin/svccfg', '-s', os.environ['SMF_FMRI'], 'refresh'])
4070N/A
4070N/A sys.exit(smf_include.SMF_EXIT_OK)
4070N/A
4070N/A
4070N/Aif __name__ == '__main__':
4070N/A os.putenv('LC_ALL', 'C')
4070N/A try:
4070N/A smf_include.smf_main()
4070N/A except Exception as err:
4070N/A print 'Unknown error: %s' % err
4070N/A print
4070N/A traceback.print_exc(file=sys.stdout)
4070N/A sys.exit(smf_include.SMF_EXIT_ERR_FATAL)