@@ -3,24 +3,29 @@ import { Werft } from "../../util/werft";
33import { JobConfig } from "./job-config" ;
44
55interface config {
6+ cloud : string ,
67 phase : string ;
78 description : string ;
89}
910
1011const 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+ }
0 commit comments