Example #1
0
    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")
Example #2
0
File: vps_ops.py Project: 42qu/vps
    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")
Example #3
0
File: migrate.py Project: 42qu/vps
 def sync_partition(self, dev, partition_name=None, speed=None, log_file=None):
     """ when you sync a snapshot lv to remote, you'll need to specify partition_name
     """
     arr = dev.split("/")
     if arr[0] == "" and arr[1] == 'dev' and len(arr) == 4:
         mount_point, size_g, _partition_name = self._load_lvm(dev)
     else:
         mount_point, size_g, _partition_name = self._load_image(dev)
     if not partition_name:
         partition_name = _partition_name
     try:
         fs_type = vps_common.get_fs_type(dev)
         self.connect(timeout=size_g / 2 + 12)
         remote_mount_point = self.rpc.call(
             "alloc_partition", partition_name, size_g, fs_type)
         self.logger.info("remote(%s) mounted" % (remote_mount_point))
         ret, err = self.rsync(mount_point, remote_mount_point, speed=speed, log_file=log_file)
         if ret == 0:
             print "rsync ok"
             self.logger.info("rsync %s to %s ok" % (dev, self.server_ip))
         else:
             print "rsync failed", err
             self.logger.info("rsync %s to %s error, ret=%s, err=%s" %
                              (dev, self.server_ip, ret, err))
             raise Exception('rsync failed')
         time.sleep(3)
         self.rpc.call("umount", remote_mount_point, _retry=1) # possible after a long time socket is closed by server
         print "remote umounted %s" % (partition_name)
         self.logger.info("remote(%s) umounted" % (remote_mount_point))
     finally:
         # probably not work when keyboard cancel
         vps_common.umount_tmp(mount_point)
         self.close()
Example #4
0
 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")
Example #5
0
    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")
Example #6
0
 def umount(self, mount_point):
     mount_point_path = os.path.join(conf.MOUNT_POINT_DIR, mount_point)
     try:
         vps_common.umount_tmp(mount_point_path)
         self.logger.info("%s umounted" % (mount_point_path))
         self._stop_rsync(mount_point)
     except Exception, e:
         self.logger.exception(e)
         raise
Example #7
0
File: migrate.py Project: 42qu/vps
 def umount(self, mount_point):
     mount_point_path = os.path.join(conf.MOUNT_POINT_DIR, mount_point)
     try:
         vps_common.umount_tmp(mount_point_path)
         self.logger.info("%s umounted" % (mount_point_path))
         self._stop_rsync(mount_point)
     except Exception, e:
         self.logger.exception(e)
         raise
Example #8
0
File: vps_ops.py Project: 42qu/vps
    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")
Example #9
0
 def reset_pw(self, xv):
     if not xv.root_pw:
         raise Exception("orz, root passwd is empty")
     if xv.stop():
         self.loginfo(xv, "stopped")
     else:
         xv.destroy()
         self.loginfo(xv, "force destroy")
     vps_mountpoint = xv.root_store.mount_tmp()
     try:
         os_init.set_root_passwd_2(xv, vps_mountpoint)
     finally:
         vps_common.umount_tmp(vps_mountpoint)
     self._boot_and_test(xv, is_new=True)
     self.loginfo(xv, "done vps reset passwd")
Example #10
0
File: vps_ops.py Project: 42qu/vps
 def reset_pw(self, xv):
     if not xv.root_pw:
         raise Exception("orz, root passwd is empty")
     if xv.stop():
         self.loginfo(xv, "stopped")
     else:
         xv.destroy()
         self.loginfo(xv, "force destroy")
     vps_mountpoint = xv.root_store.mount_tmp()
     try:
         os_init.set_root_passwd_2(xv, vps_mountpoint)
     finally:
         vps_common.umount_tmp(vps_mountpoint)
     self._boot_and_test(xv, is_new=True)
     self.loginfo(xv, "done vps reset passwd")
Example #11
0
 def sync_partition(self,
                    dev,
                    partition_name=None,
                    speed=None,
                    log_file=None):
     """ when you sync a snapshot lv to remote, you'll need to specify partition_name
     """
     arr = dev.split("/")
     if arr[0] == "" and arr[1] == 'dev' and len(arr) == 4:
         mount_point, size_g, _partition_name = self._load_lvm(dev)
     else:
         mount_point, size_g, _partition_name = self._load_image(dev)
     if not partition_name:
         partition_name = _partition_name
     try:
         fs_type = vps_common.get_fs_type(dev)
         self.connect(timeout=size_g / 2 + 12)
         remote_mount_point = self.rpc.call("alloc_partition",
                                            partition_name, size_g, fs_type)
         self.logger.info("remote(%s) mounted" % (remote_mount_point))
         ret, err = self.rsync(mount_point,
                               remote_mount_point,
                               speed=speed,
                               log_file=log_file)
         if ret == 0:
             print "rsync ok"
             self.logger.info("rsync %s to %s ok" % (dev, self.server_ip))
         else:
             print "rsync failed", err
             self.logger.info("rsync %s to %s error, ret=%s, err=%s" %
                              (dev, self.server_ip, ret, err))
             raise Exception('rsync failed')
         time.sleep(3)
         self.rpc.call(
             "umount", remote_mount_point, _retry=1
         )  # possible after a long time socket is closed by server
         print "remote umounted %s" % (partition_name)
         self.logger.info("remote(%s) umounted" % (remote_mount_point))
     finally:
         # probably not work when keyboard cancel
         vps_common.umount_tmp(mount_point)
         self.close()
Example #12
0
def pack_vps_fs_tarball(img_path, tarball_dir_or_path, is_image=False):
    """ if tarball_dir_or_path is a directory, will generate filename like XXX_fs_FSTYPE.tar.gz  """
    tarball_dir = None
    tarball_path = None
    if os.path.isdir(tarball_dir_or_path):
        tarball_dir = tarball_dir_or_path
    else:
        if os.path.exists(tarball_dir_or_path):
            raise Exception("file %s exists" % (tarball_dir_or_path))
        tarball_path = tarball_dir_or_path
        tarball_dir = os.path.dirname(tarball_path)
        if not os.path.isdir(tarball_dir):
            raise Exception("directory %s not exists" % (tarball_dir))

    if not tarball_path and tarball_dir:
        fs_type = vps_common.get_fs_type(img_path)
        tarball_name = "%s_fs_%s.tar.gz" % (
            os.path.basename(img_path), fs_type)
        tarball_path = os.path.join(tarball_dir, tarball_name)
        if os.path.exists(tarball_path):
            raise Exception("file %s already exists" % (tarball_path))

    if img_path.find("/dev") == 0:
        mount_point = vps_common.mount_partition_tmp(
            img_path, readonly=not is_image)
    else:
        mount_point = vps_common.mount_loop_tmp(
            img_path, readonly=not is_image)

    if is_image:
        clean_up_img(mount_point)

    cwd = os.getcwd()
    os.chdir(mount_point)
    try:
        call_cmd("tar zcf %s ." % (tarball_path))
    finally:
        os.chdir(cwd)
        vps_common.umount_tmp(mount_point)
    return tarball_path
Example #13
0
File: vps_ops.py Project: 42qu/vps
 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")
Example #14
0
    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)
Example #15
0
File: vps_ops.py Project: 42qu/vps
    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")
Example #16
0
File: vps_ops.py Project: 42qu/vps
    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)
Example #17
0
    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")
Example #18
0
    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")
Example #19
0
    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")