Exemple #1
0
    def create(self):
        i = self.index
        clone_path = os.path.expanduser(
            "~/Virtual Machines.localized/test-clone%d.vmwarevm/test-clone%d.vmx" % (i, i))
        self.hostname = "test-clone%d" % i
        self.base_vm.clone(clone_path)

        self.child = VixVm(h, clone_path)
Exemple #2
0
class Clone:
    def __init__(self, base_vm, index):
        self.base_vm = base_vm
        self.index = index

    def create_bootstrap_snapshot(self):
        self.create()
        self.bootstrap()
        self.snapshot()

    def create(self):
        i = self.index
        clone_path = os.path.expanduser(
            "~/Virtual Machines.localized/test-clone%d.vmwarevm/test-clone%d.vmx" % (i, i))
        self.hostname = "test-clone%d" % i
        self.base_vm.clone(clone_path)

        self.child = VixVm(h, clone_path)

    def bootstrap(self):
        self.child.power_on()
        self.child.wait_for_tools()
        guest = self.child.login('root', 'test')
        info = guest.run_command("while ! ip addr | grep 172.16; do sleep 0.1; done")
        print(info)
        RE_IP = re.compile('172.16.[0-9]+\.[0-9]+')
        self.ip = RE_IP.search(info).group(0)
        print("IP is", self.ip)

        self.wait_for_ssh()

        subprocess.check_call(["knife", "bootstrap",
                                "-y",
                                "--node-name", self.hostname,
                                "--run-list", "testbase",
                                self.ip])

    def snapshot(self):
        # Snapshotting a running VM is immediate in the UI, but causes a
        # progress bar to display on the tray icon for the next 45-90s. The
        # API call returns after that time as well. Fusion seems to
        # throttle snapshot I/O to reduce its impact on system performance.
        #
        # Suspending first works around that.
        self.child.suspend()
        snapshot = self.child.snapshot()
        self.child.power_on()
        self.wait_for_power()
        self.child.wait_for_tools()

    def wait_for_power(self):
        while True:
            state = self.child.power_state()
            if "VIX_POWERSTATE_POWERED_ON" in state:
                break
            print("Sleeping for power on")
            time.sleep(0.1)

    def rollback(self):
        self.child.rollback()
        self.child.power_on()
        self.wait_for_power()

    def wait_for_ssh(self):
        while True:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.ip, 22))
                break
            except ConnectionRefusedError as e:
                print("SSH not ready yet on %s: %s" % (self.ip, str(e)))
                time.sleep(0.1)

    def ssh(self, command):
        subprocess.check_call(["unsafe-ssh", "root@%s" % self.ip] + command)

    def run_ls_opt(self):
        self.ssh(['ls', '-lv', '/opt'])

    def mess_up(self):
        self.ssh(['rm -rf /opt/chef'])

    def main(self):

        if '--ci' in sys.argv:
            self.child.rollback()
            self.child.power_on()
            print(guest.run_command("ls -lv /opt/chef"))

        else:
            while True:
                print("Rollback|eXit>" ,end="")
                x = input()
                if x.lower() == 'r':
                    self.child.rollback()
                    self.child.power_on()
                    print("Power state:", self.child.power_state())
                elif x.lower() == 'x':
                    break
                else:
                    print('?')

    def destroy(self):
        try:
            self.child.power_off()
        finally:
            self.child.delete()