def _spawn(self, instance, vm_ref):
        """Spawn a new instance."""
        LOG.debug(_('Starting VM %s...'), vm_ref)
        self._start(instance, vm_ref)
        instance_name = instance.name
        LOG.info(_('Spawning VM %(instance_name)s created %(vm_ref)s.')
                 % locals())

        ctx = nova_context.get_admin_context()
        agent_build = db.agent_build_get_by_triple(ctx, 'xen',
                              instance.os_type, instance.architecture)
        if agent_build:
            LOG.info(_('Latest agent build for %(hypervisor)s/%(os)s' + \
                       '/%(architecture)s is %(version)s') % agent_build)
        else:
            LOG.info(_('No agent build found for %(hypervisor)s/%(os)s' + \
                       '/%(architecture)s') % {
                        'hypervisor': 'xen',
                        'os': instance.os_type,
                        'architecture': instance.architecture})

        def _check_agent_version():
            LOG.debug(_("Querying agent version"))
            if instance.os_type == 'windows':
                # Windows will generally perform a setup process on first boot
                # that can take a couple of minutes and then reboot. So we
                # need to be more patient than normal as well as watch for
                # domid changes
                version = self.get_agent_version(instance,
                                  timeout=FLAGS.windows_version_timeout)
            else:
                version = self.get_agent_version(instance)
            if not version:
                return

            LOG.info(_('Instance agent version: %s') % version)
            if not agent_build:
                return

            if 0 < cmp_version(version, agent_build['version']):
                LOG.info(_('Updating Agent to %s') % agent_build['version'])
                self.agent_update(instance, agent_build['url'],
                              agent_build['md5hash'])

        def _inject_files():
            injected_files = instance.injected_files
            if injected_files:
                # Check if this is a JSON-encoded string and convert if needed.
                if isinstance(injected_files, basestring):
                    try:
                        injected_files = json.loads(injected_files)
                    except ValueError:
                        LOG.exception(
                            _("Invalid value for injected_files: '%s'")
                                % injected_files)
                        injected_files = []
                # Inject any files, if specified
                for path, contents in instance.injected_files:
                    LOG.debug(_("Injecting file path: '%s'") % path)
                    self.inject_file(instance, path, contents)

        def _set_admin_password():
            admin_password = instance.admin_pass
            if admin_password:
                LOG.debug(_("Setting admin password"))
                self.set_admin_password(instance, admin_password)

        def _reset_network():
            LOG.debug(_("Resetting network"))
            self.reset_network(instance, vm_ref)

        # NOTE(armando): Do we really need to do this in virt?
        # NOTE(tr3buchet): not sure but wherever we do it, we need to call
        #                  reset_network afterwards
        timer = utils.LoopingCall(f=None)

        def _wait_for_boot():
            try:
                state = self.get_info(instance_name)['state']
                if state == power_state.RUNNING:
                    LOG.debug(_('Instance %s: booted'), instance_name)
                    timer.stop()
                    _check_agent_version()
                    _inject_files()
                    _set_admin_password()
                    _reset_network()
                    return True
            except Exception, exc:
                LOG.warn(exc)
                LOG.exception(_('Instance %s: failed to boot'), instance_name)
                timer.stop()
                return False

        timer.f = _wait_for_boot

        return timer.start(interval=0.5, now=True)
dataset/ETHPy150Open nii-cloud/dodai-compute/nova/virt/xenapi/vmops.py/VMOps._spawn/swapped operands of `<`