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