Skip to content

Commit fa4572d

Browse files
committed
(tasks) Extract a set of common.sh methods from install_linux.sh
Pave the way for a second task that can download and install build artifact packages directly (similarly to how the base install task downloads and installs the collection release package).
1 parent e655eb7 commit fa4572d

File tree

4 files changed

+157
-132
lines changed

4 files changed

+157
-132
lines changed

.github/workflows/pr_testing.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Install ShellCheck
1818
run: sudo apt-get install shellcheck
1919
- name: Run ShellCheck
20-
run: shellcheck -x tasks/*.sh
20+
run: shellcheck -x tasks/*.sh files/*.sh
2121

2222
test-install-task-on-ubuntu:
2323
strategy:
@@ -62,6 +62,8 @@ jobs:
6262
container: ${{ matrix.image }}
6363
steps:
6464
- uses: actions/checkout@v4
65+
with:
66+
path: openvox_bootstrap
6567
- uses: actions/checkout@v4
6668
with:
6769
repository: puppetlabs/puppetlabs-facts
@@ -98,6 +100,6 @@ jobs:
98100
PT_collection: openvox8
99101
PT_apt_source: "https://apt.overlookinfratech.com"
100102
PT_yum_source: "https://yum.overlookinfratech.com"
101-
run: ./tasks/install_linux.sh
103+
run: ./openvox_bootstrap/tasks/install_linux.sh
102104
- name: Verify openvox-agent is installed
103105
run: /opt/puppetlabs/bin/puppet --version | grep -E '[0-9]+\.[0-9]+'

files/common.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#! /usr/bin/env bash
2+
3+
# PT_* variables are set by Bolt.
4+
# shellcheck disable=SC2154
5+
installdir=$PT__installdir
6+
7+
tempdir=$(mktemp -d)
8+
trap 'rm -rf $tempdir' EXIT
9+
10+
log() {
11+
local _level="$1"
12+
shift
13+
14+
local ts
15+
ts=$(date '+%Y-%m-%dT%H:%M:%S')
16+
17+
echo "${ts} [${_level}]: $*"
18+
}
19+
20+
info() {
21+
log 'INFO' "$*"
22+
}
23+
24+
err() {
25+
log 'ERROR' "$*"
26+
}
27+
28+
fail() {
29+
err "$*"
30+
exit 1
31+
}
32+
33+
# Log the value of a variable.
34+
assigned() {
35+
local _var="$1"
36+
37+
info "Assigned ${_var}=${!_var}"
38+
}
39+
40+
# Check if a command exists.
41+
exists() {
42+
command -v "$1" > /dev/null 2>&1
43+
}
44+
45+
# Log and execute a command in a subshell, capturing and echoing its
46+
# output before returning its exit status.
47+
exec_and_capture() {
48+
local _cmd="$*"
49+
50+
info "Executing: ${_cmd}"
51+
52+
local _result
53+
set +e
54+
_result=$(${_cmd} 2>&1)
55+
local _status=$?
56+
set -e
57+
58+
echo "${_result}"
59+
info "Status: ${_status}"
60+
return $_status
61+
}
62+
63+
# Download the given url to the given local file path.
64+
download() {
65+
local _url="$1"
66+
local _file="$2"
67+
68+
if exists 'wget'; then
69+
exec_and_capture wget -O "${_file}" "${_url}"
70+
elif exists 'curl'; then
71+
exec_and_capture curl -sSL -o "${_file}" "${_url}"
72+
else
73+
fail "Unable to download ${_url}. Neither wget nor curl are installed."
74+
fi
75+
}
76+
77+
# Set platform and full_version variables by reaching out to the
78+
# puppetlabs-facts bash task as an executable.
79+
set_platform() {
80+
local facts="${installdir}/facts/tasks/bash.sh"
81+
if [ -e "${facts}" ]; then
82+
platform=$(bash "${facts}" platform)
83+
full_version=$(bash "${facts}" release)
84+
else
85+
fail "Unable to find the puppetlabs-facts bash task to determine platform at '${facts}'."
86+
fi
87+
export platform # quiets shellcheck SC2034
88+
assigned 'platform'
89+
export full_version # quiets shellcheck SC2034
90+
assigned 'full_version'
91+
}
92+
93+
# Set the OS family variable based on the platform.
94+
set_family() {
95+
local _platform="$1"
96+
97+
case $_platform in
98+
Amazon)
99+
family='amazon'
100+
;;
101+
RHEL|RedHat|CentOS|Scientific|OracleLinux|Rocky|AlmaLinux)
102+
family='el'
103+
;;
104+
Fedora)
105+
family='fedora'
106+
;;
107+
SLES|Suse)
108+
family='sles'
109+
;;
110+
Debian)
111+
family='debian'
112+
;;
113+
Ubuntu)
114+
family='ubuntu'
115+
;;
116+
*)
117+
fail "Unhandled platform: '${_platform}'"
118+
;;
119+
esac
120+
export family # quiets shellcheck SC2034
121+
assigned 'family'
122+
}
123+
124+
# Based on platform family set:
125+
# package_type - rpm or deb or...
126+
# package_file_suffix - the file extension for the release package name
127+
set_package_type() {
128+
local _family="$1"
129+
130+
case $_family in
131+
amazon|fedora|el|sles)
132+
package_type='rpm'
133+
package_file_suffix='noarch.rpm'
134+
;;
135+
debian|ubuntu)
136+
package_type='deb'
137+
package_file_suffix='deb'
138+
;;
139+
esac
140+
export package_type # quiets shellcheck SC2034
141+
assigned 'package_type'
142+
export package_file_suffix # quiets shellcheck SC2034
143+
assigned 'package_file_suffix'
144+
}

tasks/install.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
{
2626
"name": "install_linux.sh",
2727
"requirements": ["shell"],
28-
"files": ["facts/tasks/bash.sh"]
28+
"files": [
29+
"facts/tasks/bash.sh",
30+
"openvox_bootstrap/files/common.sh"
31+
]
2932
}
3033
]
3134
}

tasks/install_linux.sh

Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,8 @@ yum_source=${PT_yum_source:-'https://yum.overlookinfratech.com'}
1212
# shellcheck disable=SC2154
1313
apt_source=${PT_apt_source:-'https://apt.overlookinfratech.com'}
1414

15-
tempdir=$(mktemp -d)
16-
trap 'rm -rf $tempdir' EXIT
17-
18-
log() {
19-
local _level="$1"
20-
shift
21-
22-
local ts
23-
ts=$(date '+%Y-%m-%dT%H:%M:%S')
24-
25-
echo "${ts} [${_level}]: $*"
26-
}
27-
28-
info() {
29-
log 'INFO' "$*"
30-
}
31-
32-
err() {
33-
log 'ERROR' "$*"
34-
}
35-
36-
fail() {
37-
err "$*"
38-
exit 1
39-
}
40-
41-
# Log the value of a variable.
42-
assigned() {
43-
local _var="$1"
44-
45-
info "Assigned ${_var}=${!_var}"
46-
}
47-
48-
# Check if a command exists.
49-
exists() {
50-
command -v "$1" > /dev/null 2>&1
51-
}
52-
53-
# Set platform and full_version variables by reaching out to the
54-
# puppetlabs-facts bash task as an executable.
55-
set_platform() {
56-
# PT__installdir is set by Bolt.
57-
local facts="${installdir}/facts/tasks/bash.sh"
58-
if [ -e "${facts}" ]; then
59-
platform=$(bash "${facts}" platform)
60-
assigned 'platform'
61-
full_version=$(bash "${facts}" release)
62-
assigned 'full_version'
63-
else
64-
fail "Unable to find the puppetlabs-facts bash task to determine platform at '${facts}'."
65-
fi
66-
}
15+
# shellcheck source=files/common.sh
16+
source "${installdir}/openvox_bootstrap/files/common.sh"
6717

6818
# Based on platform family set:
6919
# repository - the package repository to download from
@@ -81,59 +31,15 @@ set_repository() {
8131
assigned 'repository'
8232
}
8333

84-
# Based on platform family set:
85-
# package_type - rpm or deb or...
86-
# package_file_suffix - the file extension for the release package name
87-
set_package_type() {
88-
local _family="$1"
89-
90-
case $_family in
91-
amazon|fedora|el|sles)
92-
package_type='rpm'
93-
package_file_suffix='noarch.rpm'
94-
;;
95-
debian|ubuntu)
96-
package_type='deb'
97-
package_file_suffix='deb'
98-
;;
99-
esac
100-
assigned 'package_type'
101-
assigned 'package_file_suffix'
102-
}
103-
10434
# Based on the platform set:
10535
# package_name - the name of the release package
10636
# package_url - the url to download the release package
10737
set_collection_url() {
10838
local _platform="$1"
10939

110-
case $_platform in
111-
Amazon)
112-
family='amazon'
113-
;;
114-
RHEL|RedHat|CentOS|Scientific|OracleLinux|Rocky|AlmaLinux)
115-
family='el'
116-
;;
117-
Fedora)
118-
family='fedora'
119-
;;
120-
SLES|Suse)
121-
family='sles'
122-
;;
123-
Debian)
124-
family='debian'
125-
;;
126-
Ubuntu)
127-
family='ubuntu'
128-
;;
129-
*)
130-
fail "Unhandled platform: '${platform}'"
131-
;;
132-
esac
133-
assigned 'family'
134-
135-
set_repository $family
136-
set_package_type $family
40+
set_family "${_platform}"
41+
set_repository "${family}"
42+
set_package_type "${family}"
13743

13844
if [ "${package_type}" == 'rpm' ]; then
13945
major_version=${full_version%%.*}
@@ -148,36 +54,6 @@ set_collection_url() {
14854
assigned 'package_url'
14955
}
15056

151-
exec_and_capture() {
152-
local _cmd="$*"
153-
154-
info "Executing: ${_cmd}"
155-
local _result
156-
157-
set +e
158-
result=$(${_cmd} 2>&1)
159-
local _status=$?
160-
set -e
161-
162-
echo "${result}"
163-
info "Status: ${_status}"
164-
return $_status
165-
}
166-
167-
# Download the given url to the given local file path.
168-
download() {
169-
local _url="$1"
170-
local _file="$2"
171-
172-
if exists 'wget'; then
173-
exec_and_capture wget -O "${_file}" "${_url}"
174-
elif exists 'curl'; then
175-
exec_and_capture curl -sSL -o "${_file}" "${_url}"
176-
else
177-
fail "Unable to download ${_url}. Neither wget nor curl are installed."
178-
fi
179-
}
180-
18157
# Download the release package to the tempdir.
18258
# Sets:
18359
# local_release_package - the path to the downloaded release package.

0 commit comments

Comments
 (0)