Ejemplo n.º 1
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")
Ejemplo n.º 2
0
Archivo: vps_ops.py Proyecto: 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")
Ejemplo n.º 3
0
Archivo: vps_ops.py Proyecto: 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")
Ejemplo n.º 4
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")
Ejemplo n.º 5
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")
Ejemplo n.º 6
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")