/* * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.clouds.vmware.client; import com.intellij.openapi.diagnostic.Logger; import java.net.InetAddress; import java.net.SocketException; import java.util.Collection; import java.util.Vector; import jetbrains.buildServer.NetworkUtil; import jetbrains.buildServer.clouds.CloudException; import jetbrains.buildServer.clouds.CloudInstanceUserData; import jetbrains.buildServer.clouds.vmware.settings.VMImageInfo; import jetbrains.buildServer.clouds.vmware.vmrun.VMRun; import jetbrains.buildServer.clouds.vmware.vmrun.VMRunParameters; import jetbrains.buildServer.clouds.vmware.vmrun.local.LocalVMRun; import jetbrains.buildServer.util.StringUtil; import org.jetbrains.annotations.NotNull; /** * @author Eugene Petrenko * Created: 09.12.2009 20:12:08 */ public class DownloadingVMRun implements VMRun { private static final Logger LOG = Logger.getInstance(DownloadingVMRun.class.getName()); private final String myId = "wm-" + StringUtil.generateUniqueHash(); private final LocalVMRun myHost; private final ImagesStore myImagesStore; private String myHostName; public DownloadingVMRun(final LocalVMRun host, final ImagesStore imagesStore) { myHost = host; myImagesStore = imagesStore; } @NotNull public Collection getRunningImages() { return new Vector(myHost.getRunningImages()); } @NotNull public Collection getLocalImages() { return new Vector(myImagesStore.getLocalImages()); } public int getAllowedMachinesToRun() { return myHost.getAllowedMachinesToRun(); } public int getVersion(final int serverVersion) { return VMRun.VERSION; } @NotNull public String getLocationId() { return myId; } @NotNull public String getHostName() { return myHostName != null ? myHostName : (myHostName = computeHostName()); } private static String computeHostName() { try { final InetAddress[] addresses = NetworkUtil.getSelfAddresses(); final String addr = addresses[addresses.length - 1].getHostName(); int idx = addr.indexOf('.'); if (idx >= 0 && idx + 1 < addr.length()) return addr.substring(0, idx); return addr; } catch (SocketException e) { LOG.warn("Failed to get host network interfaces"); return "Unknown"; } } public void startMachine(@NotNull final VMRunParameters instance, @NotNull final CloudInstanceUserData data) { final VMImageInfo local = myImagesStore.mapImage(instance.getImageInfo()); if (local == null) { LOG.warn("Failed to start the instance. Image is not downloaded. " + instance); throw new CloudException("Failed to start the instance. Image is not downloaded."); } myHost.startMachine(instance.updateImageInfo(local), data); } public void terminateMachine(@NotNull final VMImageInfo image) { final VMImageInfo img = myImagesStore.mapImage(image); if (img == null) { LOG.warn("Failed to stop instance. No image was downloaded: " + image); return; } myHost.terminateMachine(img); } }