/* * Copyright 2000-2013 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; import java.util.Collection; import java.util.Collections; import java.util.Date; import jetbrains.buildServer.clouds.CloudImage; import jetbrains.buildServer.clouds.CloudInstance; import jetbrains.buildServer.clouds.InstanceStatus; import jetbrains.buildServer.clouds.vmware.settings.VMImageInfo; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author Eugene Petrenko * Created: 07.12.2009 19:24:09 */ public class VMWareImage extends ErrorHolder implements CloudImage { private final VMRemoteHost myRemoteHost; private final VMImageInfo myInfo; private VMWareInstance myInstance = null; public VMWareImage(final VMImageInfo info, final VMRemoteHost remoteHost) { myInfo = info; myRemoteHost = remoteHost; } @NotNull public String getRemoteHostId() { return myRemoteHost.getId(); } public VMImageInfo getInfo() { return myInfo; } @NotNull public String getId() { //We can not use myRemoteHost.getId() because it will drop information on agent compatibility on every client restart return myRemoteHost.getHostName() + "-" + myInfo.getId(); } @NotNull public String getName() { return myRemoteHost.getHostName() + "-" + myInfo.getId(); } @NotNull public String getDescription() { return myInfo.getImagePath(); } @NotNull public synchronized Collection getInstances() { return myInstance == null ? Collections.emptyList() : Collections.singletonList(myInstance); } @Nullable public synchronized CloudInstance findInstanceById(@NotNull final String id) { //For that image there could by only one running instance. No matter what id it has return myInstance; } public synchronized void dispose() { if (myInstance != null) { myInstance.dispose(); myInstance = null; } } public boolean isInstanceRunning() { final VMWareInstance instance = myInstance; return instance != null && instance.getStatus().isCanTerminate(); } public void update(@NotNull final VMImageInfo info) { //NOP } @NotNull public synchronized VMWareInstance createStartingInstance() { myInstance = new VMWareInstance(this, getId() + "-i", getDescription()); myInstance.setStatus(InstanceStatus.SCHEDULED_TO_START); return myInstance; } public synchronized void setRunningInstanceDetected() { if (myInstance != null) { //Support for SCHEDULED_TO_STOP status if (myInstance.getStatus().isCanTerminate()) { myInstance.setStatus(InstanceStatus.RUNNING); } } else { createStartingInstance().setStatus(InstanceStatus.RUNNING); } } public synchronized void setNoRunningInstance() { final VMWareInstance instance = myInstance; if (instance != null) { //Do not let newly started instance gone in less that 5min //Should be handled form client side if (instance.getStatus() == InstanceStatus.SCHEDULED_TO_START && new Date().getTime() - instance.getStartedTime().getTime() < 1000L * 60 * 5) { return; } instance.setStatus(InstanceStatus.STOPPED); instance.dispose(); myInstance = null; } } }