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