@Override
  public boolean addUserData(NicProfile nic, VirtualMachineProfile profile) {
    UserVmVO vm = _vmDao.findById(profile.getVirtualMachine().getId());
    _vmDao.loadDetails(vm);

    String serviceOffering =
        _serviceOfferingDao
            .findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId())
            .getDisplayText();
    String zoneName = _dcDao.findById(vm.getDataCenterId()).getName();
    NicVO nvo = _nicDao.findById(nic.getId());
    VmDataCommand cmd =
        new VmDataCommand(
            nvo.getIPv4Address(), vm.getInstanceName(), _ntwkModel.getExecuteInSeqNtwkElmtCmd());
    // if you add new metadata files, also edit
    // systemvm/patches/debian/config/var/www/html/latest/.htaccess
    cmd.addVmData("userdata", "user-data", vm.getUserData());
    cmd.addVmData("metadata", "service-offering", StringUtils.unicodeEscape(serviceOffering));
    cmd.addVmData("metadata", "availability-zone", StringUtils.unicodeEscape(zoneName));
    cmd.addVmData("metadata", "local-ipv4", nic.getIPv4Address());
    cmd.addVmData("metadata", "local-hostname", StringUtils.unicodeEscape(vm.getInstanceName()));
    cmd.addVmData("metadata", "public-ipv4", nic.getIPv4Address());
    cmd.addVmData("metadata", "public-hostname", StringUtils.unicodeEscape(vm.getInstanceName()));
    cmd.addVmData("metadata", "instance-id", String.valueOf(vm.getId()));
    cmd.addVmData("metadata", "vm-id", String.valueOf(vm.getInstanceName()));
    cmd.addVmData("metadata", "public-keys", null);
    String cloudIdentifier = _configDao.getValue("cloud.identifier");
    if (cloudIdentifier == null) {
      cloudIdentifier = "";
    } else {
      cloudIdentifier = "CloudStack-{" + cloudIdentifier + "}";
    }
    cmd.addVmData("metadata", "cloud-identifier", cloudIdentifier);

    List<PhysicalNetworkVO> phys = _phynwDao.listByZone(vm.getDataCenterId());
    if (phys.isEmpty()) {
      throw new CloudRuntimeException(
          String.format("Cannot find physical network in zone %s", vm.getDataCenterId()));
    }
    if (phys.size() > 1) {
      throw new CloudRuntimeException(
          String.format(
              "Baremetal only supports one physical network in zone, but zone %s has %s physical networks",
              vm.getDataCenterId(), phys.size()));
    }
    PhysicalNetworkVO phy = phys.get(0);

    QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
    // TODO: handle both kickstart and PING
    // sc.addAnd(sc.getEntity().getPodId(), Op.EQ, vm.getPodIdToDeployIn());
    sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, phy.getId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
      throw new CloudRuntimeException(
          "No PXE server found in pod: "
              + vm.getPodIdToDeployIn()
              + ", you need to add it before starting VM");
    }

    try {
      Answer ans = _agentMgr.send(pxeVo.getHostId(), cmd);
      if (!ans.getResult()) {
        s_logger.debug(
            String.format(
                "Add userdata to vm:%s failed because %s", vm.getInstanceName(), ans.getDetails()));
        return false;
      } else {
        return true;
      }
    } catch (Exception e) {
      s_logger.debug(String.format("Add userdata to vm:%s failed", vm.getInstanceName()), e);
      return false;
    }
  }
  private void initializeForImplicitPlannerTest(boolean preferred) {
    String plannerMode = new String("Strict");
    if (preferred) {
      plannerMode = new String("Preferred");
    }

    Map<String, String> details = new HashMap<String, String>();
    details.put("ImplicitDedicationMode", plannerMode);
    when(serviceOfferingDetailsDao.findDetails(offeringId)).thenReturn(details);

    // Initialize hosts in clusters
    HostVO host1 = mock(HostVO.class);
    when(host1.getId()).thenReturn(5L);
    HostVO host2 = mock(HostVO.class);
    when(host2.getId()).thenReturn(6L);
    HostVO host3 = mock(HostVO.class);
    when(host3.getId()).thenReturn(7L);
    List<HostVO> hostsInCluster1 = new ArrayList<HostVO>();
    List<HostVO> hostsInCluster2 = new ArrayList<HostVO>();
    List<HostVO> hostsInCluster3 = new ArrayList<HostVO>();
    hostsInCluster1.add(host1);
    hostsInCluster2.add(host2);
    hostsInCluster3.add(host3);
    when(resourceMgr.listAllHostsInCluster(1)).thenReturn(hostsInCluster1);
    when(resourceMgr.listAllHostsInCluster(2)).thenReturn(hostsInCluster2);
    when(resourceMgr.listAllHostsInCluster(3)).thenReturn(hostsInCluster3);

    // Mock vms on each host.
    long offeringIdForVmsOfThisAccount = 15L;
    long offeringIdForVmsOfOtherAccount = 16L;
    UserVmVO vm1 = mock(UserVmVO.class);
    when(vm1.getAccountId()).thenReturn(accountId);
    when(vm1.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
    UserVmVO vm2 = mock(UserVmVO.class);
    when(vm2.getAccountId()).thenReturn(accountId);
    when(vm2.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
    // Vm from different account
    UserVmVO vm3 = mock(UserVmVO.class);
    when(vm3.getAccountId()).thenReturn(201L);
    when(vm3.getServiceOfferingId()).thenReturn(offeringIdForVmsOfOtherAccount);
    List<VMInstanceVO> vmsForHost1 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> vmsForHost2 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> vmsForHost3 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> stoppedVmsForHost = new ArrayList<VMInstanceVO>();
    // Host 2 is empty.
    vmsForHost1.add(vm1);
    vmsForHost1.add(vm2);
    vmsForHost3.add(vm3);
    when(vmInstanceDao.listUpByHostId(5L)).thenReturn(vmsForHost1);
    when(vmInstanceDao.listUpByHostId(6L)).thenReturn(vmsForHost2);
    when(vmInstanceDao.listUpByHostId(7L)).thenReturn(vmsForHost3);
    when(vmInstanceDao.listByLastHostId(5L)).thenReturn(stoppedVmsForHost);
    when(vmInstanceDao.listByLastHostId(6L)).thenReturn(stoppedVmsForHost);
    when(vmInstanceDao.listByLastHostId(7L)).thenReturn(stoppedVmsForHost);

    // Mock the offering with which the vm was created.
    ServiceOfferingVO offeringForVmOfThisAccount = mock(ServiceOfferingVO.class);
    when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfThisAccount))
        .thenReturn(offeringForVmOfThisAccount);
    when(offeringForVmOfThisAccount.getDeploymentPlanner()).thenReturn(planner.getName());

    ServiceOfferingVO offeringForVMOfOtherAccount = mock(ServiceOfferingVO.class);
    when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfOtherAccount))
        .thenReturn(offeringForVMOfOtherAccount);
    when(offeringForVMOfOtherAccount.getDeploymentPlanner()).thenReturn("FirstFitPlanner");
  }