Exemplo n.º 1
0
def lock(machine, session):
    if machine.session_state != virtualbox.library.SessionState.unlocked:
        msg = ('Acquiring lock on machine failed, state was `{state}\' '
               'instead of `Unlocked\'.'.format(
                   state=str(machine.session_state)))
        raise LockingException(msg)

    machine.lock_machine(session, virtualbox.library.LockType.write)
    yield session.machine

    if machine.session_state != virtualbox.library.SessionState.locked:
        if not waituntil(lambda: machine.session_state == virtualbox.library.
                         SessionState.unlocked):
            msg = (
                'Error before trying to release lock on machine, state was `{state}\' '
                'instead of `Locked\'.'.format(
                    state=str(machine.session_state)))
            raise LockingException(msg)

    session.unlock_machine()

    if not waituntil(lambda: machine.session_state == virtualbox.library.
                     SessionState.unlocked):
        msg = ('Timeout while trying to release lock on machine, '
               'last state was `{state}\''.format(
                   state=str(machine.session_state)))
        raise LockingException(msg)
Exemplo n.º 2
0
 def shutdown(self):
     log.debug(
         'Shutting down vbox machine `{name}\''.format(name=self.name))
     self.session.console.power_down().wait_for_completion(-1)
     if not waituntil(lambda: self.machine.session_state == virtualbox.
                      library.SessionState.unlocked):
         raise LockingException(
             'Timeout while waiting for the machine to become unlocked')
Exemplo n.º 3
0
def run_instance(image, manifest, instance_type, ec2_connection,
                 vpc_connection):

    with create_env(ec2_connection, vpc_connection) as boot_env:

        def waituntil_instance_is(state):
            def instance_has_state():
                instance.update()
                return instance.state == state

            return waituntil(instance_has_state, timeout=600, interval=3)

        instance = None
        try:
            log.debug('Booting ec2 instance')
            reservation = image.ami.run(instance_type=instance_type,
                                        subnet_id=boot_env['subnet_id'])
            [instance] = reservation.instances
            instance.add_tag('Name', 'bootstrap-vz test instance')

            if not waituntil_instance_is('running'):
                raise EC2InstanceStartupException(
                    'Timeout while booting instance')

            if not waituntil(
                    lambda: instance.get_console_output().output is not None,
                    timeout=600,
                    interval=3):
                raise EC2InstanceStartupException(
                    'Timeout while fetching console output')

            from bootstrapvz.common.releases import wheezy
            if manifest.release <= wheezy:
                termination_string = 'INIT: Entering runlevel: 2'
            else:
                termination_string = 'Debian GNU/Linux'

            console_output = instance.get_console_output().output
            if termination_string not in console_output:
                last_lines = '\n'.join(console_output.split('\n')[-50:])
                message = ('The instance did not boot properly.\n'
                           'Last 50 lines of console output:\n{output}'.format(
                               output=last_lines))
                raise EC2InstanceStartupException(message)

            yield instance
        finally:
            if instance is not None:
                log.debug('Terminating ec2 instance')
                instance.terminate()
                if not waituntil_instance_is('terminated'):
                    raise EC2InstanceStartupException(
                        'Timeout while terminating instance')
                # wait a little longer, aws can be a little slow sometimes and think the instance is still running
                import time
                time.sleep(15)
Exemplo n.º 4
0
def lock(machine, session):
    if machine.session_state != virtualbox.library.SessionState.unlocked:
        msg = ('Acquiring lock on machine failed, state was `{state}\' '
               'instead of `Unlocked\'.'.format(state=str(machine.session_state)))
        raise LockingException(msg)

    machine.lock_machine(session, virtualbox.library.LockType.write)
    yield session.machine

    if machine.session_state != virtualbox.library.SessionState.locked:
        if not waituntil(lambda: machine.session_state == virtualbox.library.SessionState.unlocked):
            msg = ('Error before trying to release lock on machine, state was `{state}\' '
                   'instead of `Locked\'.'.format(state=str(machine.session_state)))
            raise LockingException(msg)

    session.unlock_machine()

    if not waituntil(lambda: machine.session_state == virtualbox.library.SessionState.unlocked):
        msg = ('Timeout while trying to release lock on machine, '
               'last state was `{state}\''.format(state=str(machine.session_state)))
        raise LockingException(msg)
Exemplo n.º 5
0
def run_instance(image, manifest, instance_type, ec2_connection, vpc_connection):

	with create_env(ec2_connection, vpc_connection) as boot_env:

		def waituntil_instance_is(state):
			def instance_has_state():
				instance.update()
				return instance.state == state
			return waituntil(instance_has_state, timeout=600, interval=3)

		instance = None
		try:
			log.debug('Booting ec2 instance')
			reservation = image.ami.run(instance_type=instance_type,
			                            subnet_id=boot_env['subnet_id'])
			[instance] = reservation.instances
			instance.add_tag('Name', 'bootstrap-vz test instance')

			if not waituntil_instance_is('running'):
				raise EC2InstanceStartupException('Timeout while booting instance')

			if not waituntil(lambda: instance.get_console_output().output is not None, timeout=600, interval=3):
				raise EC2InstanceStartupException('Timeout while fetching console output')

			from bootstrapvz.common.releases import wheezy
			if manifest.release <= wheezy:
				termination_string = 'INIT: Entering runlevel: 2'
			else:
				termination_string = 'Debian GNU/Linux'

			console_output = instance.get_console_output().output
			if termination_string not in console_output:
				last_lines = '\n'.join(console_output.split('\n')[-50:])
				message = ('The instance did not boot properly.\n'
				           'Last 50 lines of console output:\n{output}'.format(output=last_lines))
				raise EC2InstanceStartupException(message)

			yield instance
		finally:
			if instance is not None:
				log.debug('Terminating ec2 instance')
				instance.terminate()
				if not waituntil_instance_is('terminated'):
					raise EC2InstanceStartupException('Timeout while terminating instance')
				# wait a little longer, aws can be a little slow sometimes and think the instance is still running
				import time
				time.sleep(15)
 def waituntil_instance_is(state):
     def instance_has_state():
         instance.update()
         return instance.state == state
     return waituntil(instance_has_state, timeout=600, interval=3)
Exemplo n.º 7
0
		def waituntil_instance_is(state):
			def instance_has_state():
				instance.update()
				return instance.state == state
			return waituntil(instance_has_state, timeout=600, interval=3)
Exemplo n.º 8
0
 def shutdown(self):
     log.debug('Shutting down vbox machine `{name}\''.format(name=self.name))
     self.session.console.power_down().wait_for_completion(-1)
     if not waituntil(lambda: self.machine.session_state == virtualbox.library.SessionState.unlocked):
         raise LockingException('Timeout while waiting for the machine to become unlocked')