Skip to content

Commit 97d0480

Browse files
committed
[installer-tests] add werft flag to trigger self-hosted preview
1 parent 3a9de75 commit 97d0480

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

.github/workflows/delete-kots-channel.yml

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ jobs:
1616
tar xf replicated.tar.gz replicated && rm replicated.tar.gz
1717
mv replicated /usr/local/bin/replicated
1818
19+
- name: Delete replicated license
20+
run: |
21+
CUSTOMER_ID=$(replicated customer ls | grep ${{ github.event.ref }} | awk '{print $1}')
22+
if [ "${CUSTOMER_ID}" = "" ]; then
23+
echo "No license found"
24+
exit 0
25+
fi
26+
curl --request POST \
27+
--url https://api.replicated.com/vendor/v3/customer/${CUSTOMER_ID}/archive \
28+
--header 'Authorization: ${{ env.REPLICATED_API_TOKEN }}'
29+
1930
- name: Delete Replicated channel
2031
run: |
2132
CHANNEL_ID=$(replicated channel inspect ${{ github.event.ref }} \

.werft/build.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { validateChanges } from "./jobs/build/validate-changes";
99
import { prepare } from "./jobs/build/prepare";
1010
import { deployToPreviewEnvironment } from "./jobs/build/deploy-to-preview-environment";
1111
import { triggerIntegrationTests } from "./jobs/build/trigger-integration-tests";
12-
import { triggerUpgradeTests } from "./jobs/build/self-hosted-upgrade-tests";
12+
import { triggerSelfHostedPreview, triggerUpgradeTests } from "./jobs/build/self-hosted-upgrade-tests";
1313
import { jobConfig } from "./jobs/build/job-config";
1414
import { typecheckWerftJobs } from "./jobs/build/typecheck-werft-jobs";
1515

@@ -60,6 +60,11 @@ async function run(context: any) {
6060
await typecheckWerftJobs(werft);
6161
await buildAndPublish(werft, config);
6262

63+
if (config.withSelfHostedPreview) {
64+
await triggerSelfHostedPreview(werft, config, context.Owner);
65+
return
66+
}
67+
6368
if (!config.withPreview || config.publishRelease) {
6469
werft.phase("deploy", "not deploying");
6570
console.log("running without preview environment or publish-release is set");

.werft/installer-tests.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as https from "https";
33
import { join } from "path";
44
import { exec } from "./util/shell";
55
import { Werft } from "./util/werft";
6+
import { deleteReplicatedLicense } from "./jobs/build/self-hosted-upgrade-tests";
67

78
const context: any = JSON.parse(fs.readFileSync("context.json").toString());
89

@@ -476,6 +477,8 @@ function cleanup() {
476477

477478
werft.done(phase.phase);
478479

480+
deleteReplicatedLicense(werft, process.env["TF_VAR_TEST_ID"]);
481+
479482
return ret;
480483
}
481484

.werft/jobs/build/job-config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface JobConfig {
66
analytics: string;
77
buildConfig: any;
88
cleanSlateDeployment: boolean;
9+
cluster: string;
910
coverageOutput: string;
1011
dontTest: boolean;
1112
fromVersion: string;
@@ -24,6 +25,7 @@ export interface JobConfig {
2425
withContrib: boolean;
2526
withIntegrationTests: boolean;
2627
withUpgradeTests: boolean;
28+
withSelfHostedPreview: boolean;
2729
withObservability: boolean;
2830
withPayment: boolean;
2931
workspaceFeatureFlags: string[];
@@ -83,10 +85,12 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
8385
const withIntegrationTests = "with-integration-tests" in buildConfig && !mainBuild;
8486
const withUpgradeTests = "with-upgrade-tests" in buildConfig && !mainBuild;
8587
const fromVersion = withUpgradeTests ? buildConfig["from-version"] : "";
86-
const replicatedChannel = withUpgradeTests ? buildConfig["channel"] : "";
88+
const replicatedChannel = buildConfig["channel"];
89+
const cluster = buildConfig["cluster"];
90+
const withSelfHostedPreview = "with-sh-preview" in buildConfig;
8791
const publishToNpm = "publish-to-npm" in buildConfig || mainBuild;
8892
const publishToJBMarketplace = "publish-to-jb-marketplace" in buildConfig || mainBuild;
89-
const publishToKots = "publish-to-kots" in buildConfig || mainBuild;
93+
const publishToKots = "publish-to-kots" in buildConfig || withSelfHostedPreview || mainBuild;
9094
const analytics = buildConfig["analytics"];
9195
const localAppVersion = mainBuild || "with-localapp-version" in buildConfig ? version : "unknown";
9296
const retag = "with-retag" in buildConfig ? "" : "--dont-retag";
@@ -122,6 +126,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
122126
analytics,
123127
buildConfig,
124128
cleanSlateDeployment,
129+
cluster,
125130
coverageOutput,
126131
dontTest,
127132
fromVersion,
@@ -145,6 +150,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
145150
withObservability,
146151
withPayment,
147152
withUpgradeTests,
153+
withSelfHostedPreview,
148154
workspaceFeatureFlags,
149155
withLargeVM,
150156
};

.werft/jobs/build/self-hosted-upgrade-tests.ts

+112
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@ import { Werft } from "../../util/werft";
33
import { JobConfig } from "./job-config";
44

55
interface config {
6+
cloud: string,
67
phase: string;
78
description: string;
89
}
910

1011
const phases: { [name: string]: config } = {
1112
gke: {
13+
cloud: "gcp",
1214
phase: "trigger upgrade test in GKE",
1315
description: "Triggers upgrade test on supplied version from Beta channel on GKE cluster",
1416
},
1517
aks: {
18+
cloud: "azure",
1619
phase: "trigger upgrade test in AKS",
1720
description: "Triggers upgrade test on supplied version from Beta channel on AKS cluster",
1821
},
1922
k3s: {
23+
cloud: "k3s",
2024
phase: "trigger upgrade test in K3S",
2125
description: "Triggers upgrade test on supplied version from Beta channel on K3S cluster",
2226
},
2327
eks: {
28+
cloud: "aws",
2429
phase: "trigger upgrade test in EKS",
2530
description: "Triggers upgrade test on supplied version from Beta channel on EKS cluster",
2631
},
@@ -67,3 +72,110 @@ export async function triggerUpgradeTests(werft: Werft, config: JobConfig, usern
6772
}
6873
}
6974
}
75+
76+
export async function triggerSelfHostedPreview(werft: Werft, config: JobConfig, username: string) {
77+
const replicatedChannel = config.replicatedChannel || config.repository.branch;
78+
const cluster = config.cluster || "k3s";
79+
const formattedBranch = config.repository.branch.replace("/", "-").slice(0,10)
80+
const phase = phases[cluster]
81+
const subdomain = `${formattedBranch}x-${phase.cloud}`
82+
83+
const replicatedApp = process.env.REPLICATED_APP
84+
85+
var licenseFlag: string = ""
86+
var annotation: string = ""
87+
88+
89+
if(!["stable", "unstable", "beta"].includes(replicatedChannel.toLowerCase())){
90+
werft.phase("get-replicated-license", `Create and download replicated license for ${replicatedChannel}`);
91+
92+
const customerID = getCustomerID(subdomain)
93+
94+
if(customerID == "") {
95+
exec(`replicated customer create --app ${replicatedApp} --channel ${replicatedChannel} --name ${subdomain}`,
96+
{ slice: "get-replicated-license"})
97+
}
98+
99+
exec(`replicated customer download-license --app ${replicatedApp} --customer ${subdomain} > license.yaml`,
100+
{ slice: "get-replicated-license", dontCheckRc: true})
101+
102+
exec(`install -D license.yaml install/licenses/${replicatedChannel}.yaml`,
103+
{ slice: "get-replicated-license"},
104+
)
105+
werft.done("get-replicated-license");
106+
107+
licenseFlag = `-s install/licenses/${replicatedChannel}.yaml`
108+
}
109+
110+
exec(`git config --global user.name "${username}"`);
111+
112+
annotation = `${annotation} -a channel=${replicatedChannel} -a preview=true -a skipTests=true -a deps=external`;
113+
114+
werft.phase("self-hosted-preview", `Create self-hosted preview in ${cluster}`);
115+
116+
annotation = `${annotation} -a cluster=${cluster} -a updateGitHubStatus=gitpod-io/gitpod -a subdomain=${subdomain}`
117+
118+
const testFile: string = `.werft/${cluster}-installer-tests.yaml`;
119+
120+
try {
121+
exec(
122+
`werft run --remote-job-path ${testFile} ${annotation} github ${licenseFlag}`,
123+
{
124+
slice: "self-hosted-preview"
125+
},
126+
).trim();
127+
128+
werft.done("self-hosted-preview");
129+
} catch (err) {
130+
if (!config.mainBuild) {
131+
werft.fail("self-hosted-preview", err);
132+
}
133+
console.log("Deleting the created license ", subdomain)
134+
deleteReplicatedLicense(werft, subdomain)
135+
exec("exit 0");
136+
}
137+
}
138+
139+
export async function deleteReplicatedLicense(werft: Werft, licenseName: string) {
140+
var customerID: string
141+
142+
if(licenseName == "") {
143+
console.log("No customerID or license name found, skipping replicated license cleanup")
144+
return
145+
}
146+
147+
customerID = getCustomerID(licenseName)
148+
149+
if(customerID == "") {
150+
console.log("Could not find license, skipping replicated license cleanup")
151+
return
152+
}
153+
154+
console.log("trying to cleanup replicated license")
155+
werft.phase("delete-replicated-license", "Deletes the replicated license created")
156+
const ret = exec(`curl --request POST \
157+
--url https://api.replicated.com/vendor/v3/customer/${customerID}/archive \
158+
--header 'Authorization: ${ process.env.REPLICATED_API_TOKEN }'`,
159+
{slice: "delete-replicated-license", dontCheckRc: true})
160+
if(ret.code){
161+
werft.fail("delete-replicated-license", "Could not delete the replciated license")
162+
return
163+
}
164+
165+
werft.done("delete-replicated-license")
166+
}
167+
168+
function getCustomerID(licenseName: string): string {
169+
var customerID: string = ""
170+
const replicatedApp = process.env.REPLICATED_APP
171+
172+
const response = exec(`replicated customer ls --app ${replicatedApp} | grep ${licenseName} | awk '{print $1}'`,
173+
{ slice: "get-replicated-license", dontCheckRc: true})
174+
175+
const customerIDS = response.stdout.split("\n").filter(item => item);
176+
if(customerIDS.length > 0) {
177+
customerID = customerIDS[0].trim()
178+
}
179+
180+
return customerID
181+
}

install/tests/cleanup.sh

+10
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ for i in $(gsutil ls gs://nightly-tests/tf-state); do
3535
export TF_VAR_TEST_ID=$TF_VAR_TEST_ID
3636

3737
make cleanup cloud=$cloud
38+
39+
CUSTOMERID=$(replicated customer ls --app "${REPLICATED_APP}" | grep "$TF_VAR_TEST_ID" | awk '{print $1}')
40+
41+
[ -z "$CUSTOMERID" ] && continue
42+
43+
echo "Trying to archive replicated license"
44+
45+
curl --request POST \
46+
--url https://api.replicated.com/vendor/v3/customer/$CUSTOMERID/archive \
47+
--header "Authorization: ${REPLICATED_API_TOKEN}" || echo "Couldn't delete replicated licese"
3848
done

0 commit comments

Comments
 (0)