blob: 10e3aa76d00d23cac4e62fe9501cdbba906b2153 [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// -----------------------------------------------------------------------
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040050// Intent: Assign perms to fix access problems
51// . /bin/sh: 1: /home/jenkins/.volt/config: Permission denied
52// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -040053void fixPerms() {
54 enter('fixPerms')
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040055
Joey Armstrong97af2192023-09-05 17:06:41 -040056 sh(label : 'fixperms',
57 script : """
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040058
59 umask 022
60
Joey Armstrong97af2192023-09-05 17:06:41 -040061 declare volt_dir='~/.volt'
62 declare volt_cfg='~/.volt/config'
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040063
64 echo
Joey Armstrong97af2192023-09-05 17:06:41 -040065 echo "** Fixing perms: \$volt_cfg"
66 mkdir -p "\$volt_dir"
67 chmod -R u+w,go-rwx "\$volt_dir"
68 chmod u=rwx "\$volt_dir"
69 touch "\$volt_conf"
70 /bin/ls -l "\$volt_dir"
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040071""")
Joey Armstrong97af2192023-09-05 17:06:41 -040072 leave('fixPerms')
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040073 return
74}
75
76// -----------------------------------------------------------------------
Joey Armstrong379660e2022-12-14 19:21:00 -050077// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -040078Boolean process(String branch) {
79 Boolean ans = true
80 enter('process')
Joey Armstrong299f1f32022-11-24 08:45:25 -050081
Joey Armstrong97643b32022-11-14 17:33:27 -050082 // This logic seems odd given we branch & tag repositories
83 // for release so hilight non-frozen until we know for sure.
Joey Armstrong97af2192023-09-05 17:06:41 -040084 def released = [
Joey Armstrong9d35c0f2023-08-25 21:35:54 -040085 // v1.9.1 - https://github.com/opencord/voltctl/releases/tag/untagged-cd611c39178f25b95a87
86 'voltha-2.12' : '1.8.45',
87 'voltha-2.11' : '1.8.45',
88 // https://github.com/opencord/voltctl/releases/tag/v1.7.6
89 'voltha-2.10' : '1.7.6',
90 'voltha-2.9' : '1.7.4',
91 'voltha-2.8' : '1.6.11',
Joey Armstrong97643b32022-11-14 17:33:27 -050092 ]
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070093
Joey Armstrong97643b32022-11-14 17:33:27 -050094 boolean have_released = released.containsKey(branch)
95 boolean is_release = false
96 // TODO: Enable with parameter: RELEASE_VOLTHA=
97 boolean has_binding = binding.hasVariable('WORKSPACE')
Matteo Scandolob47a6fd2021-10-27 17:02:49 -070098
Joey Armstrong97643b32022-11-14 17:33:27 -050099 // WIP: Probe to find out what is available
Joey Armstrong96158a92022-11-25 10:36:06 -0500100 print("""
101** have_released: ${have_released}
102** has_binding: ${has_binding}
103""")
Matteo Scandolob47a6fd2021-10-27 17:02:49 -0700104
Joey Armstrong97643b32022-11-14 17:33:27 -0500105 // ---------------------------------------------
106 // Sanity check: released version must be frozen
107 // ---------------------------------------------
108 if (is_release && ! released.containsKey(branch)) {
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400109 // Fingers crossed: jenkins may rewrite the callstack.
Joey Armstrong97af2192023-09-05 17:06:41 -0400110 String myname = this.class.getName()
Joey Armstrong97643b32022-11-14 17:33:27 -0500111
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400112 String url = [
113 'https://docs.voltha.org/master',
114 'howto/release/installVoltctl.html',
115 ].join('/')
Joey Armstrong97643b32022-11-14 17:33:27 -0500116
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400117 String error = [
Joey Armstrong97af2192023-09-05 17:06:41 -0400118 myname, 'ERROR:',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400119 "Detected release version=[$branch]",
Joey Armstrong97af2192023-09-05 17:06:41 -0400120 'but voltctl is not frozen.',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400121 '',
Joey Armstrong97af2192023-09-05 17:06:41 -0400122 'See Also:', url,
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400123 ].join(' ')
Joey Armstrong97af2192023-09-05 17:06:41 -0400124 throw new Exception(error) // groovylint-disable-line ThrowException
Joey Armstrong97643b32022-11-14 17:33:27 -0500125 }
126
127 // --------------------------------
128 // General answer: latest available
129 // --------------------------------
130 if (! have_released) {
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400131 url = 'https://api.github.com/repos/opencord/voltctl/releases/latest'
132 get_version = [
133 "curl -sSL ${url}",
Joey Armstrong97af2192023-09-05 17:06:41 -0400134 'jq -r .tag_name',
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400135 "sed -e 's/^v//g'",
136 ].join(' | ')
137
138 print(" ** RUNNING: ${get_version}")
Joey Armstrong97af2192023-09-05 17:06:41 -0400139 released[branch] = sh(
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400140 script: get_version,
141 returnStdout: true
142 ).trim()
Joey Armstrong97643b32022-11-14 17:33:27 -0500143 }
144
145 voltctlVersion = released[branch]
146 println "Installing voltctl version ${voltctlVersion} on branch ${branch}"
147
148 // -----------------------------------------------------------------------
149 // groovy expansion: ${var}
150 // shell expansion: \${var}
151 // -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -0400152 sh(
153 label : 'Install Voltctl',
154 returnStdout: false,
155 script: """#!/bin/bash
Joey Armstrong97643b32022-11-14 17:33:27 -0500156
Joey Armstrong97af2192023-09-05 17:06:41 -0400157 # set -eu -o pipefail
Joey Armstrong97643b32022-11-14 17:33:27 -0500158
159 bin_voltctl="$WORKSPACE/bin/voltctl"
160
161 mkdir -p "$WORKSPACE/bin"
162 cd "$WORKSPACE" || { echo "ERROR: cd $WORKSPACE failed"; exit 1; }
163
164 HOSTOS="\$(uname -s | tr '[:upper:]' '[:lower:]')"
165 HOSTARCH="\$(uname -m | tr '[:upper:]' '[:lower:]')"
166 if [ "\$HOSTARCH" == "x86_64" ]; then
Hardik Windlass9658cd22021-10-25 11:13:25 +0000167 HOSTARCH="amd64"
168 fi
Joey Armstrong97643b32022-11-14 17:33:27 -0500169
170 # Retrieve versioned voltctl binary
171 download_url="https://github.com/opencord/voltctl/releases/download"
172 vol_ver="v${voltctlVersion}"
173 vol_name="voltctl-${voltctlVersion}-\${HOSTOS}-\${HOSTARCH}"
Joey Armstrongb2f89812022-11-24 08:09:40 -0500174 curl -o "\$bin_voltctl" -sSL "\${download_url}/\${vol_ver}/\${vol_name}"
Joey Armstrong97643b32022-11-14 17:33:27 -0500175
Joey Armstronga935e712022-11-24 08:03:15 -0500176 chmod u=rwx,go=rx "\$bin_voltctl"
Joey Armstrong97643b32022-11-14 17:33:27 -0500177
Joey Armstrong379660e2022-12-14 19:21:00 -0500178 # ---------------------------------------------------------
179 # /usr/local/bin/voltctl --version != bin/voltctl --version
180 # Problem when default searchpath has not been modified.
181 # ---------------------------------------------------------
Joey Armstronga935e712022-11-24 08:03:15 -0500182 "\${bin_voltctl}" version --clientonly
Hardik Windlass9658cd22021-10-25 11:13:25 +0000183 voltctl version --clientonly
Joey Armstrong97af2192023-09-05 17:06:41 -0400184""")
Joey Armstrong299f1f32022-11-24 08:45:25 -0500185
Joey Armstrong97af2192023-09-05 17:06:41 -0400186 enter('process')
187 return(ans)
Hardik Windlass9658cd22021-10-25 11:13:25 +0000188}
Joey Armstrong97643b32022-11-14 17:33:27 -0500189
Joey Armstrong379660e2022-12-14 19:21:00 -0500190// -----------------------------------------------------------------------
191// -----------------------------------------------------------------------
Joey Armstrong97af2192023-09-05 17:06:41 -0400192def call(String branch) {
193 try {
194 enter('main')
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400195 fixPerms()
196 process(branch)
Joey Armstrong379660e2022-12-14 19:21:00 -0500197 }
Joey Armstrong97af2192023-09-05 17:06:41 -0400198 catch (Exception err) { // groovylint-disable-line CatchException
199 String iam = getIam('main')
Joey Armstrong9d35c0f2023-08-25 21:35:54 -0400200 println("** ${iam}: EXCEPTION ${err}")
201 throw err
Joey Armstrong379660e2022-12-14 19:21:00 -0500202 }
Joey Armstrong97af2192023-09-05 17:06:41 -0400203 finally {
204 leave('main')
Joey Armstrong379660e2022-12-14 19:21:00 -0500205 }
206 return
207}
208
Joey Armstrong97643b32022-11-14 17:33:27 -0500209// [EOF]