| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.vcloud.compute.strategy; |
| 20 | |
| 21 | import static org.jclouds.compute.util.ComputeServiceUtils.getCores; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.domain.Template; |
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 33 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; |
| 34 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 35 | import org.jclouds.logging.Logger; |
| 36 | import org.jclouds.vcloud.VCloudClient; |
| 37 | import org.jclouds.vcloud.compute.options.VCloudTemplateOptions; |
| 38 | import org.jclouds.vcloud.domain.GuestCustomizationSection; |
| 39 | import org.jclouds.vcloud.domain.NetworkConnection; |
| 40 | import org.jclouds.vcloud.domain.NetworkConnectionSection; |
| 41 | import org.jclouds.vcloud.domain.Task; |
| 42 | import org.jclouds.vcloud.domain.VApp; |
| 43 | import org.jclouds.vcloud.domain.Vm; |
| 44 | import org.jclouds.vcloud.domain.NetworkConnectionSection.Builder; |
| 45 | import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; |
| 46 | import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; |
| 47 | |
| 48 | import com.google.common.base.Function; |
| 49 | import com.google.common.base.Predicate; |
| 50 | import com.google.common.collect.Iterables; |
| 51 | |
| 52 | /** |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn implements |
| 57 | CreateNodeWithGroupEncodedIntoName { |
| 58 | @Resource |
| 59 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 60 | protected Logger logger = Logger.NULL; |
| 61 | |
| 62 | protected final VCloudClient client; |
| 63 | protected final GetNodeMetadataStrategy getNode; |
| 64 | protected final Predicate<URI> successTester; |
| 65 | |
| 66 | @Inject |
| 67 | protected InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn(Predicate<URI> successTester, |
| 68 | VCloudClient client, GetNodeMetadataStrategy getNode) { |
| 69 | this.client = client; |
| 70 | this.successTester = successTester; |
| 71 | this.getNode = getNode; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public NodeMetadata createNodeWithGroupEncodedIntoName(String tag, String name, Template template) { |
| 76 | InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions(); |
| 77 | |
| 78 | // TODO make disk size specifiable |
| 79 | // disk((long) ((template.getHardware().getVolumes().get(0).getSize()) * |
| 80 | // 1024 * 1024l)); |
| 81 | |
| 82 | String customizationScript = VCloudTemplateOptions.class.cast(template.getOptions()).getCustomizationScript(); |
| 83 | IpAddressAllocationMode ipAddressAllocationMode = VCloudTemplateOptions.class.cast(template.getOptions()) |
| 84 | .getIpAddressAllocationMode(); |
| 85 | |
| 86 | options.description(VCloudTemplateOptions.class.cast(template.getOptions()).getDescription()); |
| 87 | options.customizeOnInstantiate(false); |
| 88 | options.deploy(false); |
| 89 | options.powerOn(false); |
| 90 | |
| 91 | if (!template.getOptions().shouldBlockUntilRunning()) |
| 92 | options.block(false); |
| 93 | |
| 94 | URI VDC = URI.create(template.getLocation().getId()); |
| 95 | URI templateId = URI.create(template.getImage().getId()); |
| 96 | |
| 97 | logger.debug(">> instantiating vApp vDC(%s) template(%s) name(%s) options(%s) ", VDC, templateId, name, options); |
| 98 | |
| 99 | VApp vAppResponse = client.getVAppTemplateClient().createVAppInVDCByInstantiatingTemplate(name, VDC, templateId, |
| 100 | options); |
| 101 | waitForTask(vAppResponse.getTasks().get(0), vAppResponse); |
| 102 | logger.debug("<< instantiated VApp(%s)", vAppResponse.getName()); |
| 103 | |
| 104 | // note customization is a serial concern at the moment |
| 105 | Vm vm = Iterables.get(client.getVAppClient().getVApp(vAppResponse.getHref()).getChildren(), 0); |
| 106 | if (customizationScript != null) { |
| 107 | logger.trace(">> updating customization vm(%s) ", vm.getName()); |
| 108 | waitForTask(updateVmWithCustomizationScript(vm, customizationScript), vAppResponse); |
| 109 | logger.trace("<< updated customization vm(%s) ", vm.getName()); |
| 110 | } |
| 111 | if (ipAddressAllocationMode != null) { |
| 112 | logger.trace(">> updating ipAddressAllocationMode(%s) vm(%s) ", ipAddressAllocationMode, vm.getName()); |
| 113 | waitForTask(updateVmWithIpAddressAllocationMode(vm, ipAddressAllocationMode), vAppResponse); |
| 114 | logger.trace("<< updated ipAddressAllocationMode vm(%s) ", vm.getName()); |
| 115 | } |
| 116 | int cpuCount = new Double(getCores(template.getHardware())).intValue(); |
| 117 | logger.trace(">> updating cpuCount(%d) vm(%s) ", cpuCount, vm.getName()); |
| 118 | waitForTask(updateCPUCountOfVm(vm, cpuCount), vAppResponse); |
| 119 | logger.trace("<< updated cpuCount vm(%s) ", vm.getName()); |
| 120 | int memoryMB = template.getHardware().getRam(); |
| 121 | logger.trace(">> updating memoryMB(%d) vm(%s) ", memoryMB, vm.getName()); |
| 122 | waitForTask(updateMemoryMBOfVm(vm, memoryMB), vAppResponse); |
| 123 | logger.trace("<< updated memoryMB vm(%s) ", vm.getName()); |
| 124 | logger.trace(">> deploying and powering on vApp(%s) ", vAppResponse.getName()); |
| 125 | return blockOnDeployAndPowerOnIfConfigured(options, vAppResponse, client.getVAppClient().deployAndPowerOnVApp( |
| 126 | vAppResponse.getHref())); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | public void waitForTask(Task task, VApp vAppResponse) { |
| 131 | if (!successTester.apply(task.getHref())) { |
| 132 | throw new RuntimeException(String.format("failed to %s %s: %s", task.getName(), vAppResponse.getName(), task)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public Task updateVmWithCustomizationScript(Vm vm, String customizationScript) { |
| 137 | GuestCustomizationSection guestConfiguration = vm.getGuestCustomizationSection(); |
| 138 | // TODO: determine if the server version is beyond 1.0.0, and if so append |
| 139 | // to, but |
| 140 | // not overwrite the customization script. In version 1.0.0, the api |
| 141 | // returns a script that |
| 142 | // loses newlines. |
| 143 | guestConfiguration.setCustomizationScript(customizationScript); |
| 144 | return client.getVmClient().updateGuestCustomizationOfVm(guestConfiguration, vm.getHref()); |
| 145 | } |
| 146 | |
| 147 | public Task updateVmWithIpAddressAllocationMode(Vm vm, final IpAddressAllocationMode ipAddressAllocationMode) { |
| 148 | NetworkConnectionSection net = vm.getNetworkConnectionSection(); |
| 149 | Builder builder = net.toBuilder(); |
| 150 | builder.connections(Iterables.transform(net.getConnections(), |
| 151 | new Function<NetworkConnection, NetworkConnection>() { |
| 152 | |
| 153 | @Override |
| 154 | public NetworkConnection apply(NetworkConnection arg0) { |
| 155 | return arg0.toBuilder().connected(true).ipAddressAllocationMode(ipAddressAllocationMode).build(); |
| 156 | } |
| 157 | |
| 158 | })); |
| 159 | return client.getVmClient().updateNetworkConnectionOfVm(builder.build(), vm.getHref()); |
| 160 | } |
| 161 | |
| 162 | public Task updateCPUCountOfVm(Vm vm, int cpuCount) { |
| 163 | return client.getVmClient().updateCPUCountOfVm(cpuCount, vm.getHref()); |
| 164 | } |
| 165 | |
| 166 | public Task updateMemoryMBOfVm(Vm vm, int memoryInMB) { |
| 167 | return client.getVmClient().updateMemoryMBOfVm(memoryInMB, vm.getHref()); |
| 168 | } |
| 169 | |
| 170 | private NodeMetadata blockOnDeployAndPowerOnIfConfigured(InstantiateVAppTemplateOptions options, VApp vAppResponse, |
| 171 | Task task) { |
| 172 | if (options.shouldBlock()) { |
| 173 | waitForTask(task, vAppResponse); |
| 174 | logger.debug("<< ready vApp(%s)", vAppResponse.getName()); |
| 175 | } |
| 176 | return getNode.getNode(vAppResponse.getHref().toASCIIString()); |
| 177 | } |
| 178 | } |