Esempio n. 1
0
File: vm.py Progetto: koder-ua/vm_ut
    def __init__(self,
                 name,
                 fs_file,
                 ip="192.168.122.2",
                 hwaddr='4A:59:43:49:79:BF',
                 destroy_on_exit=True,
                 conn=None,
                 lxc_name=None):
        """
        configures lxc based vm
        name -- name of your vm
        you may change ip or hwaddr if you need
        fs_file may be changed to your fs(btrfs) file
        """

        self.name = name
        self.ip = ip
        self.hwaddr = hwaddr
        self.img_file = fs_file

        self.fs = RestorableFS(image_file=fs_file)

        self.destroy_on_exit = destroy_on_exit
        self.use_libvirt = (conn != None)
        self.conn = conn
        self.lxc_name = lxc_name if lxc_name is not None else self.name
Esempio n. 2
0
File: vm.py Progetto: koder-ua/vm_ut
class LXC(IVM):
    """Class for working with LXC based virtual machines"""
    type_ = 'lxc'
    def __init__(self,
                 name,
                 fs_file,
                 ip="192.168.122.2",
                 hwaddr='4A:59:43:49:79:BF',
                 destroy_on_exit=True,
                 conn=None,
                 lxc_name=None):
        """
        configures lxc based vm
        name -- name of your vm
        you may change ip or hwaddr if you need
        fs_file may be changed to your fs(btrfs) file
        """

        self.name = name
        self.ip = ip
        self.hwaddr = hwaddr
        self.img_file = fs_file

        self.fs = RestorableFS(image_file=fs_file)

        self.destroy_on_exit = destroy_on_exit
        self.use_libvirt = (conn != None)
        self.conn = conn
        self.lxc_name = lxc_name if lxc_name is not None else self.name

    def start(self, fs_mpoint_folder="/tmp/vms"):
        """
        starts lxc based vm
        fs_mpoint_folder is a folder where your vms file system
        will be mounted in some folder. It should exist
        """
        self.started = False
        
        if  not os.path.exists(fs_mpoint_folder):
            shell_exec('mkdir -p "{0}"'.format(fs_mpoint_folder))
        
        mpoint = os.path.join(fs_mpoint_folder, str(uuid.uuid1()))

        shell_exec('mkdir -p "{0}"'.format(mpoint))
        self.fs.mount(mpoint)
        
        try:
            if self.use_libvirt:
                self.vm = LXCDomain.construct(self.conn,
                                          True,
                                          self.name,
                                          1024 * 1024,
                                          2,
                                          FileSystemDevice(mpoint),
                                          ETHNetworkDevice(self.hwaddr,
                                                           "vnet7",
                                                           ip=self.ip))
            else:
                shell_exec('lxc-start -d -n "{0}"'.format(self.lxc_name))
        
        except:
            self.stop()
            raise
     
        logger.info("Domain started ok with ip {0!r} wait ssh ready".format(self.ip))
        
        for i in range(100):
            if is_ssh_ready(self.ip):
                self.started = True
                return self
        
        logger.critical("ssh failed to start on ip {0!r}".format(self.ip))
        self.stop()
        raise Exception("VM refuses to start")

    def stop(self):
        """Stops vm. Destroys changes on it """
        if self.destroy_on_exit:
            if self.started:
                if self.use_libvirt:                
                    self.vm.destroy()
                else:
                    shell_exec('lxc-stop -n "{0}"'.format(self.lxc_name))

            self.fs.umount()
            shell_exec('rm -rf "{0}"'.format(self.fs.mpoint))