首页 > 代码库 > trove upgrade 源码阅读
trove upgrade 源码阅读
trove upgrade 用于将数据库从一个版本更新到另一个版本,如:将mysql5.5更换到mysql5.6
trove/taskmanager/manager.py 368行
def upgrade(self, context, instance_id, datastore_version_id): instance_tasks = models.BuiltInstanceTasks.load(context, instance_id) datastore_version = DatastoreVersion.load_by_uuid(datastore_version_id) with EndNotification(context): instance_tasks.upgrade(datastore_version)
instance_tasks.upgrade(datastore_version)调用
trove/taskmanager/models.py 1501行
def upgrade(self, datastore_version): ... try: upgrade_info = self.guest.pre_upgrade() ... self.server.rebuild(datastore_version.image_id, files=injected_files) ... if volume: upgrade_info[‘device‘] = volume_device self.guest.post_upgrade(upgrade_info) self.reset_task_status() ...
upgrade instance 主要是以下几步完成:
self.guest.pre_upgrade:更新前的一些操作,主要是将mysql的配置文件保存在cinder volume中
self.server.rebuild:用新的数据库基础镜像重建trove的nova实例
self.guest.post_upgrade:重建nova instance後的一些操作,主要操作是从cinder volume中恢复mysql配置文件,启动mysql
self.guest.pre_upgrade,self.guest.post_upgrade 通过rcp方式调用运行在nova instance上的trove-guestagent的中对应方法
trove/guestagent/datastore/mysql/_common/manager.py 245行
def pre_upgrade(self, context): app = self.mysql_app(self.mysql_app_status.get()) data_dir = app.get_data_dir() mount_point, _data = os.path.split(data_dir) save_dir = "%s/etc_mysql" % mount_point save_etc_dir = "%s/etc" % mount_point home_save = "%s/trove_user" % mount_point app.status.begin_restart() app.stop_db() if operating_system.exists("/etc/my.cnf", as_root=True): operating_system.create_directory(save_etc_dir, as_root=True) operating_system.copy("/etc/my.cnf", save_etc_dir, preserve=True, as_root=True) operating_system.copy("/etc/mysql/.", save_dir, preserve=True, as_root=True) operating_system.copy("%s/." % os.path.expanduser(‘~‘), home_save, preserve=True, as_root=True) self.unmount_volume(context, mount_point=data_dir) return { ‘mount_point‘: mount_point, ‘save_dir‘: save_dir, ‘save_etc_dir‘: save_etc_dir, ‘home_save‘: home_save }
pre_upgrade备份数据库的配置文件、guestagent位于trove home 目录下的配置信息,返回包含备份信息的一个字典
trove/guestagent/datastore/mysql/_common/manager.py 275行
def post_upgrade(self, context, upgrade_info): app = self.mysql_app(self.mysql_app_status.get()) app.stop_db() if ‘device‘ in upgrade_info: self.mount_volume(context, mount_point=upgrade_info[‘mount_point‘], device_path=upgrade_info[‘device‘], write_to_fstab=True) if operating_system.exists(upgrade_info[‘save_etc_dir‘], is_directory=True, as_root=True): operating_system.copy("%s/." % upgrade_info[‘save_etc_dir‘], "/etc", preserve=True, as_root=True) operating_system.copy("%s/." % upgrade_info[‘save_dir‘], "/etc/mysql", preserve=True, as_root=True) operating_system.copy("%s/." % upgrade_info[‘home_save‘], os.path.expanduser(‘~‘), preserve=True, as_root=True) self.configuration_manager.refresh_cache() app.start_mysql()
post_upgrade恢复备份中的配置文件,不对老数据库的任何文件作修改,直接使用老数据库的配置文件与数据文件启动数据库
本文出自 “武陵荒草” 博客,请务必保留此出处http://penguintux.blog.51cto.com/3021117/1912357
trove upgrade 源码阅读