Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Linux installs while defaulting to an LTS Java 17 version #27

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ apply plugin: 'java'

gocdPlugin {
id = 'com.thoughtworks.gocd.elastic-agent.azure'
pluginVersion = '1.1.0'
goCdVersion = '20.9.0'
pluginVersion = '1.2.0'
goCdVersion = '21.4.0'
name = 'GoCD Elastic Agent Plugin for Azure'
description = 'GoCD Elastic Agent Plugin for Azure allows efficient usage of Azure instances'
vendorName = 'Thoughtworks, Inc.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface Constants {
String REQUEST_SERVER_INFO = REQUEST_SERVER_PREFIX + ".server-info.get";
String REQUEST_ADD_SERVER_HEALTH_MESSAGES = REQUEST_SERVER_PREFIX + ".server-health.add-messages";

String SUPPORTED_GO_SERVER_VERSION= "20.5.0-11820";
String DEFAULT_GO_SERVER_VERSION = "22.3.0-15301";
String DEFAULT_JRE_FEATURE_VERSION = "17";

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.thoughtworks.gocd.elasticagent.azure.utils.HttpUtil;

import static com.thoughtworks.gocd.elasticagent.azure.Constants.SUPPORTED_GO_SERVER_VERSION;
import static com.thoughtworks.gocd.elasticagent.azure.Constants.DEFAULT_GO_SERVER_VERSION;

public class DownloadUrls {
public static final String UNZIP_TAR_DOWNLOAD_URL = "https://oss.oracle.com/el4/unzip/unzip.tar";
Expand All @@ -27,13 +27,13 @@ public class DownloadUrls {

public static String linuxGoAgent(String goAgentVersion) {
String latestGoAgentUrl = String.format(GO_AGENT_LINUX_DOWNLOAD_URL_FORMAT, goAgentVersion, goAgentVersion);
String defaultGoAgentUrl = String.format(GO_AGENT_LINUX_DOWNLOAD_URL_FORMAT, SUPPORTED_GO_SERVER_VERSION, SUPPORTED_GO_SERVER_VERSION);
String defaultGoAgentUrl = String.format(GO_AGENT_LINUX_DOWNLOAD_URL_FORMAT, DEFAULT_GO_SERVER_VERSION, DEFAULT_GO_SERVER_VERSION);
return HttpUtil.isValidUrl(latestGoAgentUrl) ? latestGoAgentUrl : defaultGoAgentUrl;
}

public static String windowsGoAgent(String goAgentVersion) {
String latestGoAgentUrl = String.format(GO_AGENT_WINDOWS_DOWNLOAD_URL_FORMAT, goAgentVersion, goAgentVersion);
String defaultGoAgentUrl = String.format(GO_AGENT_WINDOWS_DOWNLOAD_URL_FORMAT, SUPPORTED_GO_SERVER_VERSION, SUPPORTED_GO_SERVER_VERSION);
String defaultGoAgentUrl = String.format(GO_AGENT_WINDOWS_DOWNLOAD_URL_FORMAT, DEFAULT_GO_SERVER_VERSION, DEFAULT_GO_SERVER_VERSION);
return HttpUtil.isValidUrl(latestGoAgentUrl) ? latestGoAgentUrl : defaultGoAgentUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.thoughtworks.gocd.elasticagent.azure.vm;

import com.thoughtworks.gocd.elasticagent.azure.Constants;
import com.thoughtworks.gocd.elasticagent.azure.DownloadUrls;

import java.util.*;
Expand All @@ -35,13 +36,14 @@ public LinuxCustomScriptExtension(String goAgentVersion,
String pluginId,
String agentId) {
this.goAgentVersion = goAgentVersion;
this.installParams = new HashMap<String, String>() {{
this.installParams = new HashMap<>() {{
put("version", goAgentVersion);
put("go_server_url", goServerUrl);
put("autoregister_key", autoRegisterKey);
put("environment", environment);
put("plugin_id", pluginId);
put("agent_id", agentId);
put("jre_feature_version", Constants.DEFAULT_JRE_FEATURE_VERSION);
}};

}
Expand All @@ -60,7 +62,7 @@ public List<String> getFileUris() {

public String getScript() {
return new CustomScriptBuilder()
.withScript("post_provision_script.template.ftlh", this.installParams)
.withScript("post_provision_script.template.ftl", this.installParams)
.base64Encoded()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import com.microsoft.azure.management.compute.VirtualMachine.DefinitionStages.WithCreate;
import com.microsoft.azure.management.compute.VirtualMachines;
import com.thoughtworks.gocd.elasticagent.azure.AgentConfig;
import com.thoughtworks.gocd.elasticagent.azure.Constants;
import com.thoughtworks.gocd.elasticagent.azure.utils.Util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class LinuxPlatformConfigStrategy implements PlatformConfigStrategy {
public static final String LINUX_START_GO_AGENT_TEMPLATE = "linux_start_go_agent.template.flth";
public static final String LINUX_START_GO_AGENT_TEMPLATE = "linux_start_go_agent.template.ftl";
private CustomScriptBuilder customScriptBuilder;

public LinuxPlatformConfigStrategy(CustomScriptBuilder customScriptBuilder) {
Expand Down Expand Up @@ -82,7 +84,7 @@ public void installGoAgent(VirtualMachines virtualMachines, VmConfig config) {
}

private String startGoAgentScript(AgentConfig agentConfig) {
return customScriptBuilder.withScript(LINUX_START_GO_AGENT_TEMPLATE, Collections.singletonMap("go_server_url", agentConfig.getServerUrl())).build();
return customScriptBuilder.withScript(LINUX_START_GO_AGENT_TEMPLATE, Map.of("go_server_url", agentConfig.getServerUrl(), "jre_feature_version", Constants.DEFAULT_JRE_FEATURE_VERSION)).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.Map;
import java.util.Optional;

import static com.thoughtworks.gocd.elasticagent.azure.Constants.SUPPORTED_GO_SERVER_VERSION;
import static com.thoughtworks.gocd.elasticagent.azure.Constants.DEFAULT_GO_SERVER_VERSION;
import static com.thoughtworks.gocd.elasticagent.azure.models.Platform.LINUX;
import static com.thoughtworks.gocd.elasticagent.azure.utils.Util.uniqueString;
import static com.thoughtworks.gocd.elasticagent.azure.vm.VMTags.*;
Expand Down Expand Up @@ -214,7 +214,7 @@ public Builder setSettingsParams(PluginSettings settings) {

public Builder setServerInfoParams(ServerInfo serverInfo) {
tags.put(GOCD_SERVER_ID_TAG_KEY, serverInfo.getServerId());
this.serverVersion = Optional.ofNullable(serverInfo.getServerVersion()).orElse(SUPPORTED_GO_SERVER_VERSION);
this.serverVersion = Optional.ofNullable(serverInfo.getServerVersion()).orElse(DEFAULT_GO_SERVER_VERSION);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import static com.thoughtworks.gocd.elasticagent.azure.AzurePlugin.LOG;

public class WindowsPlatformConfigStrategy implements PlatformConfigStrategy {
public static final String WINDOWS_START_GO_AGENT_TEMPLATE = "windows_start_go_agent.template.flth";
public static final String WINDOWS_INSTALL_GO_AGENT_TEMPLATE = "windows_install_go_agent.template.flth";
public static final String WINDOWS_START_GO_AGENT_TEMPLATE = "windows_start_go_agent.template.ftl";
public static final String WINDOWS_INSTALL_GO_AGENT_TEMPLATE = "windows_install_go_agent.template.ftl";
private CustomScriptBuilder customScriptBuilder;

public WindowsPlatformConfigStrategy(CustomScriptBuilder scriptBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<#-- @ftlvariable name="go_server_url" type="java.lang.String" -->
<#-- @ftlvariable name="jre_feature_version" type="java.lang.String" -->
#!/usr/bin/env bash

set -e

agent_dir="/var/lib/go-agent"
default_jre_dir="/var/lib/jdk/jdk-12"
default_jre_dir="/var/lib/jdk/${jre_feature_version}-jre"

if [ ! -x "$(command -v java)" ]; then
export GO_JAVA_HOME=$default_jre_dir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<#-- @ftlvariable name="agent_id" type="java.lang.String" -->
<#-- @ftlvariable name="plugin_id" type="java.lang.String" -->
<#-- @ftlvariable name="environment" type="java.lang.String" -->
<#-- @ftlvariable name="autoregister_key" type="java.lang.String" -->
<#-- @ftlvariable name="version" type="java.lang.String" -->
<#-- @ftlvariable name="jre_feature_version" type="java.lang.String" -->
#!/usr/bin/env bash

set -e

java_download_url="https://download.java.net/java/GA/jdk13.0.2/d4173c853231432d94f001e99d882ca7/8/GPL/openjdk-13.0.2_linux-x64_bin.tar.gz"
java_download_url="https://api.adoptium.net/v3/binary/latest/${jre_feature_version}/ga/linux/x64/jre/hotspot/normal/eclipse"

reset_dir () {
rm -rf $1
mkdir $1
mkdir -p $1
}

install_java () {
wget $java_download_url
wget $java_download_url --output-document=jre.tar.gz
reset_dir /var/lib/jdk
tar -xvf openjdk-13.0.2_linux-x64_bin.tar.gz -C /var/lib/jdk/
tar -xvf jre.tar.gz --directory /var/lib/jdk/${jre_feature_version}-jre --strip 1
}

echo "Setting up unzip utility"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class DownloadUrlsTest {

@Test
void testLinuxGoAgentDownloadUrlShouldFetchDefaultURLForVersionNotPublished() {
assertEquals("https://download.gocd.org/binaries/20.5.0-11820/generic/go-agent-20.5.0-11820.zip", DownloadUrls.linuxGoAgent("not valid"));
assertEquals("https://download.gocd.org/binaries/22.3.0-15301/generic/go-agent-22.3.0-15301.zip", DownloadUrls.linuxGoAgent("not valid"));
}

@Test
void testWindowsGoAgentDownloadUrlShouldFetchDefaultURLForVersionNotPublished() {
assertEquals("https://download.gocd.org/binaries/20.5.0-11820/win/go-agent-20.5.0-11820-jre-64bit-setup.exe", DownloadUrls.windowsGoAgent("19.xyz"));
assertEquals("https://download.gocd.org/binaries/22.3.0-15301/win/go-agent-22.3.0-15301-jre-64bit-setup.exe", DownloadUrls.windowsGoAgent("19.xyz"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.Arrays;
import java.util.List;

import static com.thoughtworks.gocd.elasticagent.azure.Constants.SUPPORTED_GO_SERVER_VERSION;
import static com.thoughtworks.gocd.elasticagent.azure.Constants.DEFAULT_GO_SERVER_VERSION;
import static com.thoughtworks.gocd.elasticagent.azure.DownloadUrls.UNZIP_TAR_DOWNLOAD_URL;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -40,9 +40,9 @@ void shouldCreateAzureCustomExtensionScriptToInstallGoAgent() {
);

List<String> expectedFileUris = Arrays.asList(UNZIP_TAR_DOWNLOAD_URL,
String.format(DownloadUrls.GO_AGENT_LINUX_DOWNLOAD_URL_FORMAT, SUPPORTED_GO_SERVER_VERSION, SUPPORTED_GO_SERVER_VERSION));
String.format(DownloadUrls.GO_AGENT_LINUX_DOWNLOAD_URL_FORMAT, DEFAULT_GO_SERVER_VERSION, DEFAULT_GO_SERVER_VERSION));
String base64EncodedScript = new CustomScriptBuilder()
.withScript("post_provision_script.template.ftlh", extension.getInstallParams())
.withScript("post_provision_script.template.ftl", extension.getInstallParams())
.base64Encoded()
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ void testGetExtensionsShouldReturnUserCustomScriptExtension() {
put("environment", "Test");
put("plugin_id", Util.pluginId());
put("agent_id", vmConfig.getName());
put("jre_feature_version", "17");
}};
assertEquals(expectedInstallParams, installParams);
}
Expand Down Expand Up @@ -177,7 +178,7 @@ void testStartAgentShouldInvokeStartAgentScript() throws Exception {

Map<String, String> params = paramCaptor.getValue();
verify(mockVirtualMachines).runShellScript("groupName", "vmName", Collections.singletonList("start agent script"), Collections.emptyList());
assertEquals(Collections.singletonMap("go_server_url", serverURL), params);
assertEquals(Map.of("go_server_url", serverURL, "jre_feature_version", "17"), params);
assertEquals(mockResult, actualResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void testInstallAgentShouldInvokeInstallAgentScriptWithCorrectParams() {
put("agent_id", "agentId");
put("username", "username");
put("password", "password");
put("go_agent_installer_url", "https://download.gocd.org/binaries/20.5.0-11820/win/go-agent-20.5.0-11820-jre-64bit-setup.exe");
put("go_agent_installer_url", "https://download.gocd.org/binaries/22.3.0-15301/win/go-agent-22.3.0-15301-jre-64bit-setup.exe");
}};
assertEquals(expectedParams, actualParams);
verify(mockVirtualMachines).runPowerShellScript("groupName", "vmName", Collections.singletonList("install script"), Collections.emptyList());
Expand Down