blob: c5c48c21fcbe86e97b35cdcf412653f2e34ed578 [file] [log] [blame]
Joey Armstrong97643b32022-11-14 17:33:27 -05001#!/usr/bin/env groovy
Joey Armstrong379660e2022-12-14 19:21:00 -05002// -----------------------------------------------------------------------
Joey Armstrongaf679da2023-01-31 14:22:41 -05003// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// -----------------------------------------------------------------------
Joey Armstrong379660e2022-12-14 19:21:00 -050017// Install the voltctl command by branch name "voltha-xx"
18// -----------------------------------------------------------------------
Joey Armstrong97643b32022-11-14 17:33:27 -050019
Joey Armstrong379660e2022-12-14 19:21:00 -050020// -----------------------------------------------------------------------
21// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -040022String getIam(String func) {
Joey Armstrong379660e2022-12-14 19:21:00 -050023 // Cannot rely on a stack trace due to jenkins manipulation
24 String src = 'vars/installVoltctl.groovy'
25 String iam = [src, func].join('::')
26 return iam
27}
28
29// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -040030// Intent: Log progress message
31// -----------------------------------------------------------------------
32void enter(String name) {
33 // Announce ourselves for log usability
34 String iam = getIam(name)
35 println("${iam}: ENTER")
36 return
37}
38
39// -----------------------------------------------------------------------
40// Intent: Log progress message
41// -----------------------------------------------------------------------
42void leave(String name) {
43 // Announce ourselves for log usability
44 String iam = getIam(name)
45 println("${iam}: LEAVE")
46 return
47}
48
49// -----------------------------------------------------------------------
Jan Klare1ba67462023-09-28 13:34:55 +020050// Intent: Add host entries to /etc/hosts to allow resolving the virtual hosts
51// used to expose the voltha api (voltha.voltha.local) and etcd
52// (voltha-infra.local) inside of k8s via ingress to localhost
53// -----------------------------------------------------------------------
54void setHostEntries() {
55 enter('setHostEntries')
56
57 sh(
58 label : 'setHostEntries',
59 script: """#!/usr/bin/env bash
60for hostname in voltha.voltha.local voltha-infra.local; do
61 if grep \$hostname /etc/hosts; then
62 echo "host entry for \$hostname already exists"
63 else
64 echo "adding host entry for \$hostname"
65 sed -i "s/127.0.0.1 localhost/& \$hostname/" /etc/hosts
66 fi
67done
68""")
69
70 leave('setHostEntries')
71 return
72}
73
74// -----------------------------------------------------------------------
Joey Armstrong379660e2022-12-14 19:21:00 -050075// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -040076Boolean process(String branch) {
77 Boolean ans = true
78 enter('process')
Joey Armstrong299f1f32022-11-24 08:45:25 -050079
Joey Armstrong97643b32022-11-14 17:33:27 -050080 // This logic seems odd given we branch & tag repositories
81 // for release so hilight non-frozen until we know for sure.
Joey Armstronge0ed0352023-09-05 19:00:50 -040082 def released = [
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040083 // v1.9.1 - https://github.com/opencord/voltctl/releases/tag/untagged-cd611c39178f25b95a87
84 'voltha-2.12' : '1.8.45',
85 'voltha-2.11' : '1.8.45',
86 // https://github.com/opencord/voltctl/releases/tag/v1.7.6
87 'voltha-2.10' : '1.7.6',
88 'voltha-2.9' : '1.7.4',
89 'voltha-2.8' : '1.6.11',
Joey Armstrong97643b32022-11-14 17:33:27 -050090 ]
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070091
Joey Armstrong97643b32022-11-14 17:33:27 -050092 boolean have_released = released.containsKey(branch)
93 boolean is_release = false
94 // TODO: Enable with parameter: RELEASE_VOLTHA=
95 boolean has_binding = binding.hasVariable('WORKSPACE')
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070096
Joey Armstrong97643b32022-11-14 17:33:27 -050097 // WIP: Probe to find out what is available
Joey Armstrong96158a92022-11-25 10:36:06 -050098 print("""
99** have_released: ${have_released}
100** has_binding: ${has_binding}
101""")
Matteo Scandolob47a6fd2021-10-27 17:02:49 -0700102
Joey Armstrong97643b32022-11-14 17:33:27 -0500103 // ---------------------------------------------
104 // Sanity check: released version must be frozen
105 // ---------------------------------------------
106 if (is_release && ! released.containsKey(branch)) {
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400107 // Fingers crossed: jenkins may rewrite the callstack.
Joey Armstrong97af2192023-09-05 17:06:41 -0400108 String myname = this.class.getName()
Joey Armstrong97643b32022-11-14 17:33:27 -0500109
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400110 String url = [
111 'https://docs.voltha.org/master',
112 'howto/release/installVoltctl.html',
113 ].join('/')
Joey Armstrong97643b32022-11-14 17:33:27 -0500114
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400115 String error = [
Joey Armstrong97af2192023-09-05 17:06:41 -0400116 myname, 'ERROR:',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400117 "Detected release version=[$branch]",
Joey Armstrong97af2192023-09-05 17:06:41 -0400118 'but voltctl is not frozen.',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400119 '',
Joey Armstrong97af2192023-09-05 17:06:41 -0400120 'See Also:', url,
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400121 ].join(' ')
Joey Armstrong97af2192023-09-05 17:06:41 -0400122 throw new Exception(error) // groovylint-disable-line ThrowException
Joey Armstrong97643b32022-11-14 17:33:27 -0500123 }
124
125 // --------------------------------
126 // General answer: latest available
127 // --------------------------------
128 if (! have_released) {
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400129 url = 'https://api.github.com/repos/opencord/voltctl/releases/latest'
130 get_version = [
131 "curl -sSL ${url}",
Joey Armstrong97af2192023-09-05 17:06:41 -0400132 'jq -r .tag_name',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400133 "sed -e 's/^v//g'",
134 ].join(' | ')
135
136 print(" ** RUNNING: ${get_version}")
Joey Armstrong97af2192023-09-05 17:06:41 -0400137 released[branch] = sh(
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400138 script: get_version,
139 returnStdout: true
140 ).trim()
Joey Armstrong97643b32022-11-14 17:33:27 -0500141 }
142
143 voltctlVersion = released[branch]
144 println "Installing voltctl version ${voltctlVersion} on branch ${branch}"
145
146 // -----------------------------------------------------------------------
147 // groovy expansion: ${var}
148 // shell expansion: \${var}
149 // -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -0400150 sh(
151 label : 'Install Voltctl',
152 returnStdout: false,
153 script: """#!/bin/bash
Joey Armstrong97643b32022-11-14 17:33:27 -0500154
Joey Armstrong97af2192023-09-05 17:06:41 -0400155 # set -eu -o pipefail
Joey Armstrong97643b32022-11-14 17:33:27 -0500156
157 bin_voltctl="$WORKSPACE/bin/voltctl"
158
159 mkdir -p "$WORKSPACE/bin"
160 cd "$WORKSPACE" || { echo "ERROR: cd $WORKSPACE failed"; exit 1; }
161
162 HOSTOS="\$(uname -s | tr '[:upper:]' '[:lower:]')"
163 HOSTARCH="\$(uname -m | tr '[:upper:]' '[:lower:]')"
164 if [ "\$HOSTARCH" == "x86_64" ]; then
Hardik Windlass9658cd22021-10-25 11:13:25 +0000165 HOSTARCH="amd64"
166 fi
Joey Armstrong97643b32022-11-14 17:33:27 -0500167
168 # Retrieve versioned voltctl binary
169 download_url="https://github.com/opencord/voltctl/releases/download"
170 vol_ver="v${voltctlVersion}"
171 vol_name="voltctl-${voltctlVersion}-\${HOSTOS}-\${HOSTARCH}"
Joey Armstrongb2f89812022-11-24 08:09:40 -0500172 curl -o "\$bin_voltctl" -sSL "\${download_url}/\${vol_ver}/\${vol_name}"
Joey Armstrong97643b32022-11-14 17:33:27 -0500173
Joey Armstronga935e712022-11-24 08:03:15 -0500174 chmod u=rwx,go=rx "\$bin_voltctl"
Joey Armstrong97643b32022-11-14 17:33:27 -0500175
Joey Armstrong379660e2022-12-14 19:21:00 -0500176 # ---------------------------------------------------------
177 # /usr/local/bin/voltctl --version != bin/voltctl --version
178 # Problem when default searchpath has not been modified.
179 # ---------------------------------------------------------
Joey Armstronga935e712022-11-24 08:03:15 -0500180 "\${bin_voltctl}" version --clientonly
Hardik Windlass9658cd22021-10-25 11:13:25 +0000181 voltctl version --clientonly
Joey Armstrong97af2192023-09-05 17:06:41 -0400182""")
Joey Armstrong299f1f32022-11-24 08:45:25 -0500183
Roger Luethiff337d82023-09-20 12:09:44 +0200184 leave('process')
Joey Armstrong97af2192023-09-05 17:06:41 -0400185 return(ans)
Hardik Windlass9658cd22021-10-25 11:13:25 +0000186}
Joey Armstrong97643b32022-11-14 17:33:27 -0500187
Joey Armstrong379660e2022-12-14 19:21:00 -0500188// -----------------------------------------------------------------------
189// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -0400190def call(String branch) {
191 try {
192 enter('main')
Jan Klare1ba67462023-09-28 13:34:55 +0200193 setHostEntries()
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400194 process(branch)
Joey Armstrong379660e2022-12-14 19:21:00 -0500195 }
Joey Armstrong97af2192023-09-05 17:06:41 -0400196 catch (Exception err) { // groovylint-disable-line CatchException
197 String iam = getIam('main')
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400198 println("** ${iam}: EXCEPTION ${err}")
199 throw err
Joey Armstrong379660e2022-12-14 19:21:00 -0500200 }
Joey Armstrong97af2192023-09-05 17:06:41 -0400201 finally {
202 leave('main')
Joey Armstrong379660e2022-12-14 19:21:00 -0500203 }
204 return
205}
206
Joey Armstrong97643b32022-11-14 17:33:27 -0500207// [EOF]