components/openstack/nova/files/nova-upgrade
changeset 4287 aba3ed31b37a
parent 4181 3ac4ce913bec
child 5405 66fd59fecd68
--- a/components/openstack/nova/files/nova-upgrade	Wed May 13 10:49:08 2015 -0700
+++ b/components/openstack/nova/files/nova-upgrade	Wed May 13 14:39:42 2015 -0600
@@ -14,21 +14,19 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-from ConfigParser import NoOptionError
-from datetime import datetime
-import errno
 import glob
 import os
-import shutil
 from subprocess import check_call, Popen, PIPE
 import sys
-import time
 import traceback
 
 import iniparse
 import smf_include
 import sqlalchemy
 
+from openstack_common import alter_mysql_tables, create_backups, modify_conf, \
+    move_conf
+
 
 NOVA_CONF_MAPPINGS = {
     # Deprecated group/name
@@ -50,7 +48,6 @@
     ('DEFAULT', 'cinder_cross_az_attach'): ('cinder', 'cross_az_attach'),
     ('DEFAULT', 'db_backend'): ('database', 'backend'),
     ('DEFAULT', 'sql_connection'): ('database', 'connection'),
-    ('DEFAULT', 'sql_connection'): ('database', 'connection'),
     ('sql', 'connection'): ('database', 'connection'),
     ('DEFAULT', 'sql_idle_timeout'): ('database', 'idle_timeout'),
     ('DATABASE', 'sql_idle_timeout'): ('database', 'idle_timeout'),
@@ -98,169 +95,38 @@
         ('neutron', 'ca_certificates_file'),
     ('DEFAULT', 'spicehtml5proxy_host'): ('spice', 'html5proxy_host'),
     ('DEFAULT', 'spicehtml5proxy_port'): ('spice', 'html5proxy_port'),
-    # No longer referenced by the service
-    ('DEFAULT', 'sql_connection'): (None, None),
 }
 
-
-def update_mapping(section, key, mapping):
-    """ look for deprecated variables and, if found, convert it to the new
-    section/key.
-    """
-
-    if (section, key) in mapping:
-        print "Deprecated value found: [%s] %s" % (section, key)
-        section, key = mapping[(section, key)]
-        if section is None and key is None:
-            print "Removing from configuration"
-        else:
-            print "Updating to: [%s] %s" % (section, key)
-    return section, key
-
-
-def alter_mysql_tables(engine):
-    """ Convert MySQL tables to use utf8
-    """
-
-    import MySQLdb
-
-    for _none in range(5):
-        try:
-            db = MySQLdb.connect(host=engine.url.host,
-                                 user=engine.url.username,
-                                 passwd=engine.url.password,
-                                 db=engine.url.database)
-            break
-        except MySQLdb.OperationalError as err:
-            # mysql is not ready. sleep for 2 more seconds
-            time.sleep(2)
-    else:
-        print "Unable to connect to MySQL:  %s" % err
-        print ("Please verify MySQL is properly configured and online "
-               "before using svcadm(1M) to clear this service.")
-        sys.exit(smf_include.SMF_EXIT_ERR_FATAL)
-
-    cursor = db.cursor()
-    cursor.execute("ALTER DATABASE %s CHARACTER SET = 'utf8'" %
-                   engine.url.database)
-    cursor.execute("ALTER DATABASE %s COLLATE = 'utf8_general_ci'" %
-                   engine.url.database)
-    cursor.execute("SHOW tables")
-    res = cursor.fetchall()
-    if res:
-        cursor.execute("SET foreign_key_checks = 0")
-        for item in res:
-            cursor.execute("ALTER TABLE %s.%s CONVERT TO "
-                           "CHARACTER SET 'utf8', COLLATE 'utf8_general_ci'"
-                           % (engine.url.database, item[0]))
-        cursor.execute("SET foreign_key_checks = 1")
-        db.commit()
-        db.close()
-
-
-def modify_conf(old_file, mapping=None):
-    """ Copy over all uncommented options from the old configuration file.  In
-    addition, look for deprecated section/keys and convert them to the new
-    section/key.
-    """
-
-    new_file = old_file + '.new'
-
-    # open the previous version
-    old = iniparse.ConfigParser()
-    old.readfp(open(old_file))
+NOVA_CONF_EXCEPTIONS = [
+    ('DEFAULT', 'ec2_workers'),
+    ('DEFAULT', 'osapi_compute_workers'),
+    ('DEFAULT', 'metadata_workers'),
+    ('DEFAULT', 'lock_path'),
+    ('DEFAULT', 'novncproxy_base_url'),
+    ('conductor', 'workers'),
+    ('database', 'connection'),
+    ('keystone_authtoken', 'auth_uri'),
+    ('keystone_authtoken', 'signing_dir'),
+    ('keystone_authtoken', 'identity_uri'),
+    ('keystone_authtoken', 'admin_user'),
+    ('keystone_authtoken', 'admin_password'),
+    ('keystone_authtoken', 'admin_tenant_name'),
+    ('neutron', 'service_metadata_proxy'),
+]
 
-    # open the new version
-    new = iniparse.ConfigParser()
-    try:
-        new.readfp(open(new_file))
-    except IOError as err:
-        if err.errno == errno.ENOENT:
-            # The upgrade did not deliver a .new file so, return
-            print "%s not found - continuing with %s" % (new_file, old_file)
-            return
-        else:
-            raise
-    print "\nupdating %s" % old_file
-
-    # It's possible that nova.conf has database.connection commented out (to
-    # use the default value).  If it is, and none of other deprecated values
-    # are set, manually set database.connection in the new conf file.
-    if 'nova.conf' in old_file:
-        options = [
-            ('database', 'sql_connection'),
-            ('sql', 'connection'),
-            ('database', 'connection'),
-            ('DEFAULT', 'sql_connection')
-        ]
-        test = lambda x: old.has_section(x[0]) and old.has_option(x[0], x[1])
-        if not any(map(test, options)):
-            if old.has_option('DEFAULT', 'state_path'):
-                state_path = old.get('DEFAULT', 'state_path')
-            else:
-                state_path = '/var/lib/nova'
-
-            if old.has_option('DEFAULT', 'sqlite_db'):
-                sqlite_db = old.get('DEFAULT', 'sqlite_db')
-            else:
-                sqlite_db = 'nova.sqlite'
-
-            new.set('database', 'connection',
-                    'sqlite:///%s/%s' % (state_path, sqlite_db))
-
-    # walk every single section for uncommented options
-    default_items = set(old.items('DEFAULT'))
-    for section in old.sections() + ['DEFAULT']:
-
-        # DEFAULT items show up in every section so remove them
-        if section != 'DEFAULT':
-            section_items = set(old.items(section)) - default_items
-        else:
-            section_items = default_items
-
-        for key, value in section_items:
-            # keep a copy of the old value
-            oldvalue = value
-            oldsection = section
-
-            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)
-            section = oldsection
-
-    # 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)
+NOVA_MOVE_CONFIG = {
+    ('filter:authtoken', 'auth_uri'): ('keystone_authtoken', 'auth_uri'),
+    ('filter:authtoken', 'identity_uri'):
+        ('keystone_authtoken', 'identity_uri'),
+    ('filter:authtoken', 'admin_tenant_name'):
+        ('keystone_authtoken', 'admin_tenant_name'),
+    ('filter:authtoken', 'admin_user'): ('keystone_authtoken', 'admin_user'),
+    ('filter:authtoken', 'admin_password'):
+        ('keystone_authtoken', 'admin_password'),
+    ('filter:authtoken', 'signing_dir'): ('keystone_authtoken', 'signing_dir'),
+    ('filter:authtoken', 'auth_version'):
+        ('keystone_authtoken', 'auth_version'),
+}
 
 
 def start():
@@ -285,9 +151,60 @@
     if glob.glob('/etc/nova/*.new'):
         # the versions are different, so perform an upgrade
         # modify the configuration files
+
+        # backup all the old configuration files
+        create_backups('/etc/nova')
+
         modify_conf('/etc/nova/api-paste.ini')
         modify_conf('/etc/nova/logging.conf')
-        modify_conf('/etc/nova/nova.conf', NOVA_CONF_MAPPINGS)
+
+        # It's possible that nova.conf has database.connection commented out
+        # (to use the default value).  If it is, and none of other deprecated
+        # values are set, manually set database.connection in the new conf
+        # file.
+        if os.path.exists('/etc/nova/nova.conf.new'):
+
+            # open the previous version
+            old = iniparse.ConfigParser()
+            old.readfp(open('/etc/nova/nova.conf'))
+
+            # open the new version
+            new = iniparse.ConfigParser()
+            new.readfp(open('/etc/nova/nova.conf.new'))
+
+            options = [
+                ('database', 'sql_connection'),
+                ('sql', 'connection'),
+                ('database', 'connection'),
+                ('DEFAULT', 'sql_connection')
+            ]
+            test = lambda x: old.has_section(x[0]) and \
+                    old.has_option(x[0], x[1])
+
+            if not any(map(test, options)):
+                if old.has_option('DEFAULT', 'state_path'):
+                    state_path = old.get('DEFAULT', 'state_path')
+                else:
+                    state_path = '/var/lib/nova'
+
+                if old.has_option('DEFAULT', 'sqlite_db'):
+                    sqlite_db = old.get('DEFAULT', 'sqlite_db')
+                else:
+                    sqlite_db = 'nova.sqlite'
+
+                new.set('database', 'connection',
+                        'sqlite:///%s/%s' % (state_path, sqlite_db))
+
+                with open('/etc/nova/nova.conf.new', 'w+') as fh:
+                    new.write(fh)
+
+        # before modifying nova.conf, move the [filter:authtoken] entries from
+        # the updated api-paste.ini to the old nova.conf
+        move_conf('/etc/nova/api-paste.ini', '/etc/nova/nova.conf',
+                  NOVA_MOVE_CONFIG)
+
+        modify_conf('/etc/nova/nova.conf', NOVA_CONF_MAPPINGS,
+                    NOVA_CONF_EXCEPTIONS)
 
     config = iniparse.RawConfigParser()
     config.read('/etc/nova/nova.conf')
@@ -314,6 +231,8 @@
     os.putenv('LC_ALL', 'C')
     try:
         smf_include.smf_main()
+    except RuntimeError:
+        sys.exit(smf_include.SMF_EXIT_ERR_FATAL)
     except Exception as err:
         print 'Unknown error:  %s' % err
         print