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