def create_from_migrate(self, xv): """ server side """ xv.check_resource_avail(ignore_trash=True) if xv.swap_store.size_g > 0 and not xv.swap_store.exists(): xv.swap_store.create() self.loginfo(xv, "swap image %s created" % (str(xv.swap_store))) for disk in xv.data_disks.values(): disk.create_limit() xv.check_storage_integrity() self._clear_nonexisting_trash(xv) vps_mountpoint = xv.root_store.mount_tmp() self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store))) try: _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id) self.loginfo(xv, "begin to init os") os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=False, to_init_passwd=False, to_init_fstab=True) self.loginfo(xv, "done init os") finally: vps_common.umount_tmp(vps_mountpoint) self.create_xen_config(xv) self._boot_and_test(xv, is_new=False) self.loginfo(xv, "done vps creation")
def change_ip(self, _xv): xv = self.load_vps_meta(_xv.vps_id) self._update_vif_setting(xv, _xv) _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id) if xv.stop(): self.loginfo(xv, "vps stopped") else: xv.destroy() self.loginfo(xv, "vps cannot shutdown, destroyed it") time.sleep(3) vps_mountpoint = xv.root_store.mount_tmp() self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store))) try: self.loginfo(xv, "begin to init os") os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=False, to_init_passwd=False, to_init_fstab=False) self.loginfo(xv, "done init os") finally: vps_common.umount_tmp(vps_mountpoint) self.create_xen_config(xv) self._boot_and_test(xv, is_new=False) self.loginfo(xv, "done vps change ip")
def create_vps(self, xv, vps_image=None, is_new=True): """ check resources, create vps, wait for ip reachable, check ssh loging and check swap of vps. on error raise Exception, the caller should log exception """ assert isinstance(xv, XenVPS) assert xv.has_all_attr _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id) if not vps_image: vps_image = _vps_image if not vps_image: raise Exception( "no template image configured for os_type=%s, os_id=%s" % (os_type, xv.os_id)) if not os.path.exists(vps_image): raise Exception("image %s not exists" % (vps_image)) xv.check_resource_avail() # fs_type is tied to the image fs_type = vps_common.get_fs_from_tarball_name(vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE self.loginfo(xv, "begin to create image") if xv.swap_store.size_g > 0: xv.swap_store.create() xv.root_store.fs_type = fs_type for disk in xv.data_disks.values(): disk.create() self.loginfo(xv, "%s created" % (str(disk))) vps_mountpoint = xv.root_store.mount_tmp() self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store))) try: xv.root_store.destroy_limit() if re.match(r'.*\.img$', vps_image): vps_common.sync_img(vps_mountpoint, vps_image) else: vps_common.unpack_tarball(vps_mountpoint, vps_image) self.loginfo(xv, "synced vps os to %s" % (str(xv.root_store))) self.loginfo(xv, "begin to init os") os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=is_new, to_init_passwd=is_new, to_init_fstab=True) self.loginfo(xv, "done init os") xv.root_store.create_limit() finally: vps_common.umount_tmp(vps_mountpoint) self.create_xen_config(xv) self._boot_and_test(xv, is_new=is_new) self.loginfo(xv, "done vps creation")
def reinstall_os(self, vps_id, _xv=None, os_id=None, vps_image=None): meta_path = self._meta_path(vps_id, is_trash=False) xv = None if os.path.exists(meta_path): xv = self._load_vps_meta(meta_path) if os_id: xv.os_id = os_id elif _xv: xv.os_id = _xv.os_id self._update_vif_setting(xv, _xv) else: raise Exception("missing os_id") elif _xv: xv = _xv if os_id: xv.os_id = os_id raise Exception("missing vps metadata") _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id) if not vps_image: vps_image = _vps_image if not vps_image: raise Exception( "no template image configured for os_type=%s, os_id=%s" % (os_type, xv.os_id)) if not os.path.exists(vps_image): raise Exception("image %s not exists" % (vps_image)) # fs_type is tied to the image fs_type = vps_common.get_fs_from_tarball_name(vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE assert xv.has_all_attr if xv.stop(): self.loginfo(xv, "stopped") else: xv.destroy() self.loginfo(xv, "force destroy") time.sleep(3) root_store_trash, root_store = xv.renew_storage( xv.root_store.xen_dev, 5) xv.root_store.create(fs_type) self.loginfo(xv, "create new root") # we have to know fs_type for fstab generation for xen_dev, disk in xv.data_disks.iteritems(): if xen_dev == xv.root_store.xen_dev: continue if disk.exists(): pass else: disk.create(fs_type) vps_mountpoint_bak = root_store_trash.mount_trash_temp() try: vps_mountpoint = xv.root_store.mount_tmp() self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store))) try: xv.root_store.destroy_limit() if re.match(r'.*\.img$', vps_image): vps_common.sync_img(vps_mountpoint, vps_image) else: vps_common.unpack_tarball(vps_mountpoint, vps_image) self.loginfo(xv, "synced vps os to %s" % (str(xv.root_store))) for sync_dir in ['home', 'root']: dir_org = os.path.join(vps_mountpoint_bak, sync_dir) dir_now = os.path.join(vps_mountpoint, sync_dir) if os.path.exists(dir_org): call_cmd("rsync -a --exclude='.bash*' '%s/' '%s/'" % (dir_org, dir_now)) self.loginfo(xv, "sync dir /%s to new os" % (sync_dir)) self.loginfo(xv, "begin to init os") if _xv: xv.root_pw = _xv.root_pw os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=True, to_init_passwd=True, to_init_fstab=True) else: # if no user data provided from backend os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=True, to_init_passwd=False, to_init_fstab=True,) os_init.migrate_users( xv, vps_mountpoint, vps_mountpoint_bak) self.loginfo(xv, "done init os") xv.root_store.create_limit() finally: vps_common.umount_tmp(vps_mountpoint) finally: vps_common.umount_tmp(vps_mountpoint_bak) self.create_xen_config(xv) self._boot_and_test(xv, is_new=False) self.loginfo(xv, "done vps reinstall")
def _upgrade(self, xv_new, xv_old): # TODO: check hard disk downgrade size _vps_image, os_type, os_version = os_image.find_os_image(xv_new.os_id) fs_type = vps_common.get_fs_from_tarball_name(_vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE for xen_dev, new_disk in xv_new.data_disks.iteritems(): old_disk = xv_old.data_disks.get(xen_dev) if not new_disk.exists(): new_disk.create(fs_type) self.loginfo(xv_new, "create %s" % (str(new_disk))) else: assert new_disk.size_g if old_disk: old_size = old_disk.get_size() new_size = new_disk.size_g if old_size == new_size: pass elif old_size == new_size: pass elif old_size < new_size and new_disk.can_resize(): new_disk.destroy_limit() new_disk.resize(new_disk.size_g) new_disk.create_limit() self.loginfo(xv_new, "resized %s from %s to %s" % (str(new_disk), old_size, new_size)) else: old_disk, new_disk = xv_new.renew_storage( xen_dev, new_size=new_disk.size_g) vps_mountpoint_bak = old_disk.mount_trash_temp() self.loginfo(xv_new, "mounted %s" % (old_disk.trash_str())) try: fs_type = vps_common.get_mounted_fs_type( mount_point=vps_mountpoint_bak) new_disk.create(fs_type) new_disk.destroy_limit() self.loginfo(xv_new, "create new %s" % (str(new_disk))) vps_mountpoint = new_disk.mount_tmp() self.loginfo(xv_new, "mounted %s" % (str(new_disk))) try: call_cmd("rsync -a '%s/' '%s/'" % (vps_mountpoint_bak, vps_mountpoint)) self.loginfo( xv_new, "synced old %s to new one" % (str(new_disk))) finally: vps_common.umount_tmp(vps_mountpoint) new_disk.create_limit() finally: vps_common.umount_tmp(vps_mountpoint_bak) for xen_dev, old_disk in xv_old.data_disks.iteritems(): if not xv_new.data_disks.has_key(xen_dev): xv_new.data_disks[xen_dev] = old_disk xv_new.dump_storage_to_trash(old_disk) self.loginfo(xv_new, "%s dump to trash" % (str(old_disk))) self.create_xen_config(xv_new) self.loginfo(xv_new, "begin to init os") vps_mountpoint = xv_new.root_store.mount_tmp() try: os_init.os_init(xv_new, vps_mountpoint, os_type, os_version, is_new=False, to_init_passwd=False, to_init_fstab=True) self.loginfo(xv_new, "done init os") finally: vps_common.umount_tmp(vps_mountpoint)
def reinstall_os (self, vps_id, _vps=None, os_id=None, vps_image=None): meta_path = self._meta_path (vps_id, is_trash=False) if os.path.exists (meta_path): vps = self._load_vps_meta (meta_path) if _vps: vps.os_id = _vps.os_id elif os_id: vps.os_id = os_id else: raise Exception ("missing os_id") elif _vps: vps = _vps else: raise Exception ("missing vps metadata") _vps_image, os_type, os_version = os_image.find_os_image (vps.os_id) if not vps_image: vps_image = _vps_image if not vps_image: raise Exception ("no template image configured for os_type=%s, os_id=%s" % (os_type, vps.os_id)) if not os.path.exists (vps_image): raise Exception ("image %s not exists" % (vps_image)) #fs_type is tied to the image fs_type = vps_common.get_fs_from_tarball_name (vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE assert vps.has_all_attr if vps.stop (): self.loginfo (vps, "stopped") else: vps.destroy () self.loginfo (vps, "force destroy") root_store_trash = vps.root_store vps.renew_root_storage (5) vps.root_store.create (fs_type) self.loginfo (vps, "create new root") vps_mountpoint_bak = root_store_trash.mount_trash_temp () try: vps_mountpoint = vps.root_store.mount_tmp () self.loginfo (vps, "mounted vps image %s" % (str(vps.root_store))) try: if re.match (r'.*\.img$', vps_image): vps_common.sync_img (vps_mountpoint, vps_image) else: vps_common.unpack_tarball (vps_mountpoint, vps_image) self.loginfo (vps, "synced vps os to %s" % (str(vps.root_store))) for sync_dir in ['home', 'root']: dir_org = os.path.join (vps_mountpoint_bak, sync_dir) dir_now = os.path.join (vps_mountpoint, sync_dir) if os.path.exists (dir_org): call_cmd ("rsync -a --exclude='.bash*' '%s/' '%s/'" % (dir_org, dir_now)) self.loginfo (vps, "sync dir /%s to new os" % (sync_dir)) self.loginfo (vps, "begin to init os") os_init.os_init (vps, vps_mountpoint, os_type, os_version, to_init_passwd=False) os_init.migrate_users (vps, vps_mountpoint, vps_mountpoint_bak) self.loginfo (vps, "done init os") finally: vps_common.umount_tmp (vps_mountpoint) finally: vps_common.umount_tmp (vps_mountpoint_bak) self.save_vps_meta (vps) self._boot_and_test (vps, is_new=False) self.loginfo (vps, "done vps reinstall")
def test_find_image (self): image_path, os_type, version = os_image.find_os_image (50001) self.assert_ (os.path.exists (image_path))
def reinstall_os(self, vps_id, _xv=None, os_id=None, vps_image=None): meta_path = self._meta_path(vps_id, is_trash=False) xv = None if os.path.exists(meta_path): xv = self._load_vps_meta(meta_path) if os_id: xv.os_id = os_id elif _xv: xv.os_id = _xv.os_id self._update_vif_setting(xv, _xv) else: raise Exception("missing os_id") elif _xv: xv = _xv if os_id: xv.os_id = os_id raise Exception("missing vps metadata") _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id) if not vps_image: vps_image = _vps_image if not vps_image: raise Exception( "no template image configured for os_type=%s, os_id=%s" % (os_type, xv.os_id)) if not os.path.exists(vps_image): raise Exception("image %s not exists" % (vps_image)) # fs_type is tied to the image fs_type = vps_common.get_fs_from_tarball_name(vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE assert xv.has_all_attr if xv.stop(): self.loginfo(xv, "stopped") else: xv.destroy() self.loginfo(xv, "force destroy") time.sleep(3) root_store_trash, root_store = xv.renew_storage( xv.root_store.xen_dev, 5) xv.root_store.create(fs_type) self.loginfo(xv, "create new root") # we have to know fs_type for fstab generation for xen_dev, disk in xv.data_disks.iteritems(): if xen_dev == xv.root_store.xen_dev: continue if disk.exists(): pass else: disk.create(fs_type) vps_mountpoint_bak = root_store_trash.mount_trash_temp() try: vps_mountpoint = xv.root_store.mount_tmp() self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store))) try: xv.root_store.destroy_limit() if re.match(r'.*\.img$', vps_image): vps_common.sync_img(vps_mountpoint, vps_image) else: vps_common.unpack_tarball(vps_mountpoint, vps_image) self.loginfo(xv, "synced vps os to %s" % (str(xv.root_store))) for sync_dir in ['home', 'root']: dir_org = os.path.join(vps_mountpoint_bak, sync_dir) dir_now = os.path.join(vps_mountpoint, sync_dir) if os.path.exists(dir_org): call_cmd("rsync -a --exclude='.bash*' '%s/' '%s/'" % (dir_org, dir_now)) self.loginfo(xv, "sync dir /%s to new os" % (sync_dir)) self.loginfo(xv, "begin to init os") if _xv: xv.root_pw = _xv.root_pw os_init.os_init(xv, vps_mountpoint, os_type, os_version, is_new=True, to_init_passwd=True, to_init_fstab=True) else: # if no user data provided from backend os_init.os_init( xv, vps_mountpoint, os_type, os_version, is_new=True, to_init_passwd=False, to_init_fstab=True, ) os_init.migrate_users(xv, vps_mountpoint, vps_mountpoint_bak) self.loginfo(xv, "done init os") xv.root_store.create_limit() finally: vps_common.umount_tmp(vps_mountpoint) finally: vps_common.umount_tmp(vps_mountpoint_bak) self.create_xen_config(xv) self._boot_and_test(xv, is_new=False) self.loginfo(xv, "done vps reinstall")
def _upgrade(self, xv_new, xv_old): # TODO: check hard disk downgrade size _vps_image, os_type, os_version = os_image.find_os_image(xv_new.os_id) fs_type = vps_common.get_fs_from_tarball_name(_vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE for xen_dev, new_disk in xv_new.data_disks.iteritems(): old_disk = xv_old.data_disks.get(xen_dev) if not new_disk.exists(): new_disk.create(fs_type) self.loginfo(xv_new, "create %s" % (str(new_disk))) else: assert new_disk.size_g if old_disk: old_size = old_disk.get_size() new_size = new_disk.size_g if old_size == new_size: pass elif old_size == new_size: pass elif old_size < new_size and new_disk.can_resize(): new_disk.destroy_limit() new_disk.resize(new_disk.size_g) new_disk.create_limit() self.loginfo( xv_new, "resized %s from %s to %s" % (str(new_disk), old_size, new_size)) else: old_disk, new_disk = xv_new.renew_storage( xen_dev, new_size=new_disk.size_g) vps_mountpoint_bak = old_disk.mount_trash_temp() self.loginfo(xv_new, "mounted %s" % (old_disk.trash_str())) try: fs_type = vps_common.get_mounted_fs_type( mount_point=vps_mountpoint_bak) new_disk.create(fs_type) new_disk.destroy_limit() self.loginfo(xv_new, "create new %s" % (str(new_disk))) vps_mountpoint = new_disk.mount_tmp() self.loginfo(xv_new, "mounted %s" % (str(new_disk))) try: call_cmd("rsync -a '%s/' '%s/'" % (vps_mountpoint_bak, vps_mountpoint)) self.loginfo( xv_new, "synced old %s to new one" % (str(new_disk))) finally: vps_common.umount_tmp(vps_mountpoint) new_disk.create_limit() finally: vps_common.umount_tmp(vps_mountpoint_bak) for xen_dev, old_disk in xv_old.data_disks.iteritems(): if not xv_new.data_disks.has_key(xen_dev): xv_new.data_disks[xen_dev] = old_disk xv_new.dump_storage_to_trash(old_disk) self.loginfo(xv_new, "%s dump to trash" % (str(old_disk))) self.create_xen_config(xv_new) self.loginfo(xv_new, "begin to init os") vps_mountpoint = xv_new.root_store.mount_tmp() try: os_init.os_init(xv_new, vps_mountpoint, os_type, os_version, is_new=False, to_init_passwd=False, to_init_fstab=True) self.loginfo(xv_new, "done init os") finally: vps_common.umount_tmp(vps_mountpoint)
def reinstall_os(self, vps_id, _vps=None, os_id=None, vps_image=None): meta_path = self._meta_path(vps_id, is_trash=False) if os.path.exists(meta_path): vps = self._load_vps_meta(meta_path) if _vps: vps.os_id = _vps.os_id elif os_id: vps.os_id = os_id else: raise Exception("missing os_id") elif _vps: vps = _vps else: raise Exception("missing vps metadata") _vps_image, os_type, os_version = os_image.find_os_image(vps.os_id) if not vps_image: vps_image = _vps_image if not vps_image: raise Exception( "no template image configured for os_type=%s, os_id=%s" % (os_type, vps.os_id)) if not os.path.exists(vps_image): raise Exception("image %s not exists" % (vps_image)) #fs_type is tied to the image fs_type = vps_common.get_fs_from_tarball_name(vps_image) if not fs_type: fs_type = conf.DEFAULT_FS_TYPE assert vps.has_all_attr if vps.stop(): self.loginfo(vps, "stopped") else: vps.destroy() self.loginfo(vps, "force destroy") root_store_trash = vps.root_store vps.renew_root_storage(5) vps.root_store.create(fs_type) self.loginfo(vps, "create new root") vps_mountpoint_bak = root_store_trash.mount_trash_temp() try: vps_mountpoint = vps.root_store.mount_tmp() self.loginfo(vps, "mounted vps image %s" % (str(vps.root_store))) try: if re.match(r'.*\.img$', vps_image): vps_common.sync_img(vps_mountpoint, vps_image) else: vps_common.unpack_tarball(vps_mountpoint, vps_image) self.loginfo(vps, "synced vps os to %s" % (str(vps.root_store))) for sync_dir in ['home', 'root']: dir_org = os.path.join(vps_mountpoint_bak, sync_dir) dir_now = os.path.join(vps_mountpoint, sync_dir) if os.path.exists(dir_org): call_cmd("rsync -a --exclude='.bash*' '%s/' '%s/'" % (dir_org, dir_now)) self.loginfo(vps, "sync dir /%s to new os" % (sync_dir)) self.loginfo(vps, "begin to init os") os_init.os_init(vps, vps_mountpoint, os_type, os_version, to_init_passwd=False) os_init.migrate_users(vps, vps_mountpoint, vps_mountpoint_bak) self.loginfo(vps, "done init os") finally: vps_common.umount_tmp(vps_mountpoint) finally: vps_common.umount_tmp(vps_mountpoint_bak) self.save_vps_meta(vps) self._boot_and_test(vps, is_new=False) self.loginfo(vps, "done vps reinstall")