Skip to content

Commit

Permalink
Merge pull request #20 from macstadium/is/update-to-3.0
Browse files Browse the repository at this point in the history
Update to Orka 3.0
  • Loading branch information
ispasov authored Nov 7, 2023
2 parents 6d58933 + f1c7ac0 commit d11ca62
Show file tree
Hide file tree
Showing 32 changed files with 718 additions and 789 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.macstadium.orka.client.DeletionResponse;
import com.macstadium.orka.client.DeploymentResponse;
import com.macstadium.orka.client.OrkaClient;
import com.macstadium.orka.client.VMInstance;
import com.macstadium.orka.client.VMResponse;

import java.io.IOException;
Expand All @@ -16,7 +15,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -92,14 +90,12 @@ private Map<String, String> getNodeMappings(String mappingsData) {

private void initializeOrkaClient(CloudClientParameters params) {
String endpoint = params.getParameter(OrkaConstants.ORKA_ENDPOINT);
String user = params.getParameter(OrkaConstants.ORKA_USER);
String password = params.getParameter(OrkaConstants.ORKA_PASSWORD);

LOG.debug(String.format("OrkaCloudClient with endpoint: %s, user: %s, agentDirectory: %s", endpoint, user,
this.agentDirectory));
String token = params.getParameter(OrkaConstants.TOKEN);

LOG.debug(
String.format("OrkaCloudClient with endpoint: %s, agentDirectory: %s", endpoint, this.agentDirectory));
try {
this.orkaClient = new OrkaClient(endpoint, user, password);
this.orkaClient = new OrkaClient(endpoint, token);
} catch (IOException e) {
this.errorInfo = new CloudErrorInfo("Cannot initialize Orka client", e.toString(), e);
}
Expand All @@ -114,6 +110,7 @@ private void initializeBackgroundTasks() {
}

private OrkaCloudImage createImage(CloudClientParameters params) {
String namespace = params.getParameter(OrkaConstants.NAMESPACE);
String vm = params.getParameter(OrkaConstants.VM_NAME);
String vmUser = params.getParameter(OrkaConstants.VM_USER);
String vmPassword = params.getParameter(OrkaConstants.VM_PASSWORD);
Expand All @@ -125,7 +122,7 @@ private OrkaCloudImage createImage(CloudClientParameters params) {
LOG.debug(String.format("OrkaCloudClient createImage with vm: %s, user: %s, poolId: %s, instanceLimit: %s", vm,
vmUser, agentPoolId, instanceLimit));

return new OrkaCloudImage(vm, vmUser, vmPassword, agentPoolId, limit);
return new OrkaCloudImage(vm, namespace, vmUser, vmPassword, agentPoolId, limit);
}

public boolean isInitialized() {
Expand Down Expand Up @@ -168,20 +165,16 @@ private OrkaCloudInstance createInstanceFromExistingAgent(OrkaCloudImage image,
try {
LOG.debug(String.format("createInstanceFromExistingAgent searching for vm: %s.", image.getName()));

VMResponse vmResponse = this.getVM(image.getName());
if (vmResponse != null) {
VMResponse vmResponse = this.getVM(instanceId, image.getNamespace());
if (vmResponse != null && vmResponse.isSuccessful()) {
LOG.debug(String.format("createInstanceFromExistingAgent vm found %s.", vmResponse));

Optional<VMInstance> instance = Arrays.stream(vmResponse.getInstances())
.filter(i -> i.getId().equalsIgnoreCase(instanceId)).findFirst();
if (instance.isPresent()) {
LOG.debug(String.format("createInstanceFromExistingAgent instance found %s.", instance.get()));
OrkaCloudInstance cloudInstance = image.startNewInstance(instanceId);
cloudInstance.setStatus(InstanceStatus.RUNNING);
cloudInstance.setHost(this.getRealHost(instance.get().getHost()));
cloudInstance.setPort(Integer.parseInt(instance.get().getSSHPort()));
return cloudInstance;
}
LOG.debug(String.format("createInstanceFromExistingAgent instance found %s.", vmResponse));
OrkaCloudInstance cloudInstance = image.startNewInstance(instanceId);
cloudInstance.setStatus(InstanceStatus.RUNNING);
cloudInstance.setHost(this.getRealHost(vmResponse.getIP()));
cloudInstance.setPort(vmResponse.getSSH());
return cloudInstance;
}

} catch (IOException | NumberFormatException e) {
Expand Down Expand Up @@ -225,17 +218,18 @@ public CloudInstance startNewInstance(@NotNull final CloudImage image, @NotNull

private void setUpVM(OrkaCloudImage image, OrkaCloudInstance instance) {
try {
LOG.debug(String.format("setUpVM deploying vm: %s", image.getName()));
DeploymentResponse response = this.deployVM(image.getName());
if (response.hasErrors()) {
LOG.debug(String.format("setUpVM deployment errors: %s", Arrays.toString(response.getErrors())));
LOG.debug(
String.format("setUpVM deploying vm: %s, in namespace: %s", image.getName(), image.getNamespace()));
DeploymentResponse response = this.deployVM(image.getName(), image.getNamespace());
if (!response.isSuccessful()) {
LOG.debug(String.format("setUpVM deployment errors: %s", response.getMessage()));
image.terminateInstance(instance.getInstanceId());
return;
}

String instanceId = response.getId();
String host = this.getRealHost(response.getHost());
int sshPort = response.getSSHPort();
String instanceId = response.getName();
String host = this.getRealHost(response.getIP());
int sshPort = response.getSSH();

LOG.debug(String.format("setUpVM instanceId: %s, host: %s, port: %s", instanceId, host, sshPort));

Expand Down Expand Up @@ -269,16 +263,16 @@ private void terminateNonInitilizedInstance(@NotNull final OrkaCloudInstance ins
}
}

private DeploymentResponse deployVM(String vmName) throws IOException {
return this.orkaClient.deployVM(vmName);
private DeploymentResponse deployVM(String vmName, String namespace) throws IOException {
return this.orkaClient.deployVM(vmName, namespace);
}

DeletionResponse deleteVM(String vmId) throws IOException {
return this.orkaClient.deleteVM(vmId);
DeletionResponse deleteVM(String vmId, String namespace) throws IOException {
return this.orkaClient.deleteVM(vmId, namespace);
}

VMResponse getVM(String vmName) throws IOException {
return this.orkaClient.getVM(vmName);
VMResponse getVM(String vmName, String namespace) throws IOException {
return this.orkaClient.getVM(vmName, namespace);
}

private void waitForVM(String host, int sshPort) throws InterruptedException, IOException {
Expand All @@ -305,13 +299,13 @@ public void terminateInstance(@NotNull final CloudInstance instance) {
image.getUser(), image.getPassword(), this.agentDirectory);

LOG.debug("terminateInstance deleting vm");
DeletionResponse response = this.deleteVM(instance.getInstanceId());
if (!response.hasErrors()) {
DeletionResponse response = this.deleteVM(instance.getInstanceId(), orkaInstance.getNamespace());
if (response.isSuccessful()) {
orkaInstance.setStatus(InstanceStatus.STOPPED);
image.terminateInstance(instance.getInstanceId());
} else {
this.setInstanceForDeletion(orkaInstance,
new CloudErrorInfo("Error deleting VM", Arrays.toString(response.getErrors())));
new CloudErrorInfo("Error deleting VM", response.getMessage()));
}
} catch (IOException e) {
LOG.debug("terminateInstance error", e);
Expand All @@ -335,10 +329,6 @@ public void dispose() {
image.dispose();
}
this.images.clear();

if (this.orkaClient != null) {
this.orkaClient.close();
}
}

@Nullable
Expand All @@ -357,4 +347,4 @@ private String getRealHost(String host) {
return this.nodeMappings.keySet().stream().filter(k -> k.equalsIgnoreCase(host)).findFirst()
.map(k -> this.nodeMappings.get(k)).orElse(host);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ public class OrkaCloudImage implements CloudImage {
private final String user;
@NotNull
private final String password;
@NotNull
private final String namespace;
private final int agentPoolId;
private final int instanceLimit;
@NotNull
private final Map<String, OrkaCloudInstance> instances = new ConcurrentHashMap<String, OrkaCloudInstance>();

public OrkaCloudImage(@NotNull final String imageId, @NotNull final String user, @NotNull final String password,
public OrkaCloudImage(@NotNull final String imageId, @NotNull final String namespace, @NotNull final String user,
@NotNull final String password,
@NotNull final String agentPoolId, int instanceLimit) {
this.id = imageId;
this.namespace = namespace;
this.user = user;
this.password = password;
this.agentPoolId = Integer.parseInt(agentPoolId);
Expand All @@ -58,6 +62,11 @@ public String getPassword() {
return this.password;
}

@NotNull
public String getNamespace() {
return this.namespace;
}

@NotNull
public Collection<? extends CloudInstance> getInstances() {
return Collections.unmodifiableCollection(this.instances.values());
Expand Down Expand Up @@ -98,7 +107,7 @@ public synchronized OrkaCloudInstance startNewInstance(@NotNull final String ins
}

protected OrkaCloudInstance createInstance(String instanceId) {
return new OrkaCloudInstance(this, instanceId);
return new OrkaCloudInstance(this, instanceId, this.getNamespace());
}

public void terminateInstance(String instanceId) {
Expand All @@ -117,4 +126,4 @@ void removeInstance(String instanceId) {
void dispose() {
this.instances.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class OrkaCloudInstance implements CloudInstance {
@NotNull
private String id;
@NotNull
private String namespace;
@NotNull
private final OrkaCloudImage image;
@NotNull
private final Date startDate;
Expand All @@ -32,8 +34,10 @@ public class OrkaCloudInstance implements CloudInstance {
private volatile CloudErrorInfo errorInfo;
private boolean markedForTermination;

public OrkaCloudInstance(@NotNull final OrkaCloudImage image, @NotNull final String instanceId) {
public OrkaCloudInstance(@NotNull final OrkaCloudImage image, @NotNull final String instanceId,
@NotNull final String namespace) {
this.image = image;
this.namespace = namespace;
this.status = InstanceStatus.SCHEDULED_TO_START;
this.id = instanceId;
this.startDate = new Date();
Expand Down Expand Up @@ -96,6 +100,11 @@ public String getHost() {
return this.host;
}

@NotNull
public String getNamespace() {
return this.namespace;
}

public void setHost(String host) {
this.host = host;
}
Expand Down Expand Up @@ -126,4 +135,4 @@ public boolean containsAgent(@NotNull final AgentDescription agentDescription) {

return this.id.equals(instanceId) && getImageId().equals(imageId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class OrkaConstants {
@NotNull
public static final String ORKA_ENDPOINT = "cloud.orka.endpoint";
@NotNull
public static final String ORKA_USER = "cloud.orka.user";
public static final String VM_NAME = "cloud.orka.vm.name";
@NotNull
public static final String ORKA_PASSWORD = Constants.SECURE_PROPERTY_PREFIX + "cloud.orka.password";
public static final String NAMESPACE = "cloud.orka.namespace";
@NotNull
public static final String VM_NAME = "cloud.orka.vm.name";
public static final String TOKEN = Constants.SECURE_PROPERTY_PREFIX + "cloud.orka.token";
@NotNull
public static final String VM_USER = "cloud.orka.vm.user";
@NotNull
Expand All @@ -39,14 +39,6 @@ public String getOrkaEndpoint() {
return ORKA_ENDPOINT;
}

public String getOrkaUser() {
return ORKA_USER;
}

public String getOrkaPassword() {
return ORKA_PASSWORD;
}

public String getVmName() {
return VM_NAME;
}
Expand Down Expand Up @@ -74,4 +66,12 @@ public String getInstanceLimit() {
public String getNodeMappings() {
return NODE_MAPPINGS;
}
}

public String getNamespace() {
return NAMESPACE;
}

public String getToken() {
return TOKEN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.macstadium.orka.client.VMResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import jetbrains.buildServer.clouds.CloudImage;
Expand All @@ -34,40 +32,34 @@ private void terminateFailedInstances(CloudImage image) {
.collect(Collectors.toList());

if (instancesToTerminate.size() > 0) {
try {
LOG.debug(String.format("Failed instances found for image %s", image.getName()));
LOG.debug(String.format("Failed instances found for image %s", image.getName()));

Set<String> runningInstances = this.getRunningInstances(image.getName());
instancesToTerminate.forEach(instance -> {
LOG.debug(String.format("Removing instance with id: %s", instance.getInstanceId()));
instancesToTerminate.forEach(instance -> {
LOG.debug(String.format("Removing instance with id: %s", instance.getInstanceId()));
VMResponse vmResponse;
try {
vmResponse = this.client.getVM(instance.getInstanceId(), instance.getNamespace());

if (runningInstances.contains(instance.getInstanceId())) {
if (vmResponse.isSuccessful()) {
this.tryDeleteVM(instance);
} else {
this.terminateInstance(instance);
}
});

} catch (IOException e) {
LOG.info(String.format("Failed to execute terminate failed instances for image: %s", image.getName()),
e);
}
} catch (IOException e) {
LOG.info(String.format("Failed to get VM: %s", instance.getInstanceId()), e);
}
});
}
}

private Set<String> getRunningInstances(String imageName) throws IOException {
VMResponse vmResponse = this.client.getVM(imageName);
return Arrays.stream(vmResponse.getInstances()).map(instance -> instance.getId()).collect(Collectors.toSet());
}

private void tryDeleteVM(OrkaCloudInstance instance) {
try {
DeletionResponse response = this.client.deleteVM(instance.getInstanceId());
if (!response.hasErrors()) {
DeletionResponse response = this.client.deleteVM(instance.getInstanceId(), instance.getNamespace());
if (response.isSuccessful()) {
this.terminateInstance(instance);
} else {
LOG.info(String.format("Failed to terminate VM: %s and message: %s", instance.getInstanceId(),
Arrays.toString(response.getErrors())));
response.getHttpResponse()));
}
} catch (IOException e) {
LOG.info(String.format("Failed to terminate VM: %s", instance.getInstanceId()), e);
Expand All @@ -77,4 +69,4 @@ private void tryDeleteVM(OrkaCloudInstance instance) {
private void terminateInstance(OrkaCloudInstance failedInstance) {
failedInstance.getImage().terminateInstance(failedInstance.getInstanceId());
}
}
}

This file was deleted.

Loading

0 comments on commit d11ca62

Please sign in to comment.