This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fly-pipeline
executable file
·210 lines (167 loc) · 6.62 KB
/
fly-pipeline
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
set -euo pipefail
BASEDIR="$(cd "$(dirname "$0")" && pwd)"
GREEN='\033[0;32m'
NC='\033[0m'
GetSystemName() {
case "$(uname)" in
Darwin)
echo "darwin"
;;
Linux)
# shellcheck disable=SC1091
source /etc/os-release
echo "${ID_LIKE:-$ID}"
;;
*)
echo "unknown"
;;
esac
}
CheckPreReqs() {
for TOOL in jq spruce fly lpass; do
if ! hash "${TOOL}" 2>/dev/null; then
echo -e "Required prerequisite \\033[1m${TOOL}\\033[0m is not installed. Unable to proceed.\\n"
case "$(GetSystemName)" in
darwin)
cat <<'EOF'
Install suggestions:
- jq, the lightweight and flexible command-line JSON processor:
brew install jq
- spruce, general purpose YAML & JSON merging tool:
brew install starkandwayne/cf/spruce
- fly, concourse command line tool:
curl --progress-bar --location 'https://ci.flintstone.cf.cloud.ibm.com/api/v1/cli?arch=amd64&platform=darwin' --output /usr/local/bin/fly && chmod a+rx /usr/local/bin/fly
- lpass, LastPass command line tool:
brew install lastpass-cli
EOF
;;
debian)
cat <<'EOF'
Install suggestions:
- jq, the lightweight and flexible command-line JSON processor, and curl, command line tool for transferring data with URLs:
sudo apt-get install -y jq curl
- spruce, general purpose YAML & JSON merging tool:
curl --silent --location "https://github.com/geofffranks/spruce/releases/download/$(curl --silent --location "https://api.github.com/repos/geofffranks/spruce/releases/latest" | jq --raw-output .tag_name)/spruce-linux-amd64" --output /usr/local/bin/spruce && chmod a+rx /usr/local/bin/spruce
- fly, concourse command line tool:
curl --progress-bar --location 'https://ci.flintstone.cf.cloud.ibm.com/api/v1/cli?arch=amd64&platform=linux' --output /usr/local/bin/fly && chmod a+rx /usr/local/bin/fly
- lpass, LastPass command line tool:
apt-get update >/dev/null && \
apt-get --no-install-recommends -yqq install \
bash-completion \
build-essential \
cmake \
libcurl3 \
libcurl3-openssl-dev \
libssl1.0 \
libssl1.0-dev \
libxml2 \
libxml2-dev \
pkg-config \
ca-certificates \
xclip \
git-core && \
git clone https://github.com/lastpass/lastpass-cli /tmp/lpass && \
make -C /tmp/lpass install && \
rm -rf /tmp/lpass
EOF
;;
suse)
cat <<'EOF'
Install suggestions:
- jq, the lightweight and flexible command-line JSON processor, and curl, command line tool for transferring data with URLs:
zypper install -y jq curl
- spruce, general purpose YAML & JSON merging tool:
curl --silent --location "https://github.com/geofffranks/spruce/releases/download/$(curl --silent --location "https://api.github.com/repos/geofffranks/spruce/releases/latest" | jq --raw-output .tag_name)/spruce-linux-amd64" --output /usr/local/bin/spruce && chmod a+rx /usr/local/bin/spruce
- fly, concourse command line tool:
curl --progress-bar --location 'https://ci.flintstone.cf.cloud.ibm.com/api/v1/cli?arch=amd64&platform=linux' --output /usr/local/bin/fly && chmod a+rx /usr/local/bin/fly
- lpass, LastPass command line tool:
zypper install -y -t pattern devel_C_C++ && \
zypper install -y make cmake gcc gcc-c++ libxml2-devel libopenssl-devel libcurl-devel && \
git clone https://github.com/lastpass/lastpass-cli /tmp/lpass && \
make -C /tmp/lpass install && \
rm -rf /tmp/lpass
EOF
;;
unknown)
echo "Sorry, we do not have any install suggestions for your platform. Feel free to PR it."
;;
esac
exit 1
fi
done
}
PipelineInfo() {
local -r PIPELINE=$1
local -r TARGET=$2
CONCOURSE_URL=$(spruce json "$HOME/.flyrc" | jq -r ".targets[\"$TARGET\"].api")
TEAM_NAME=$(spruce json "$HOME/.flyrc" | jq -r ".targets[\"$TARGET\"].team")
API_PIPELINE_PATH="${CONCOURSE_URL}/api/v1/teams/${TEAM_NAME}/pipelines"
TOKEN=$(spruce json "$HOME/.flyrc" | jq -r ".targets[\"$TARGET\"].token.value")
if [[ ${TOKEN} == "null" ]]; then
echo "Not able to retrieve the token, skipping info section"
return
fi
PIPELINE_EXISTS=$(curl --silent --header "Authorization: Bearer $TOKEN" -X GET "${API_PIPELINE_PATH}/${PIPELINE}")
if [[ -z ${PIPELINE_EXISTS} ]]; then
echo "Pipeline is not deployed."
return
fi
ALL_GROUPS=$(curl -s --header "Authorization: Bearer $TOKEN" -X GET "${API_PIPELINE_PATH}/${PIPELINE}" | jq -r '.groups[]?.name')
PIPELINE_STATUS=$(curl -s --header "Authorization: Bearer $TOKEN" -X GET "${API_PIPELINE_PATH}/${PIPELINE}" | jq -r .paused)
PIPELINE_RESOURCES=$(curl -s --header "Authorization: Bearer $TOKEN" -X GET "${API_PIPELINE_PATH}/${PIPELINE}/resources" | jq '. | length')
echo -e "Displaying some insights of the $GREEN${PIPELINE}$NC pipeline:"
echo -e ""
echo -e "Paused: $GREEN${PIPELINE_STATUS}$NC"
echo -e "Resources: $GREEN${PIPELINE_RESOURCES}$NC"
if [ "$ALL_GROUPS" != "" ]; then
echo -e "Groups: "
for GROUP in $ALL_GROUPS; do
echo -e " ---->" "$GREEN${GROUP}$NC"
done
fi
CONCOURSE_URL=$(spruce json "${HOME}"/.flyrc | jq -r ".targets[\"${TARGET}\"].api")
TEAM_NAME=$(spruce json "${HOME}"/.flyrc | jq -r ".targets[\"${TARGET}\"].team")
echo
echo "On a Mac with iTerm2, just press 'Cmd' and click on the link to open a browser tab with the pipeline:"
echo -e '\033[4;94m'"${CONCOURSE_URL}/teams/${TEAM_NAME}/pipelines/${PIPELINE}"'\033[0m'
echo
}
FlyPipeline() {
local -r target=$1
local -r pipeline=$2
pushd "${BASEDIR}/pipelines/${pipeline}" >/dev/null 2>&1
if [ -f "pipeline.yml" ] && [ -f "vars.yml" ]; then
fly --target "${target}" set-pipeline \
--pipeline="${pipeline}" \
--config=<(erb "pipeline.yml") \
--load-vars-from=<(lpass show "Shared-CF-Containerization/ContainerizedCF-CI-Secrets" --notes) \
--load-vars-from="vars.yml"
PipelineInfo "$pipeline" "$target"
else
echo "Error: required ${pipeline} pipeline files are missing: pipeline.yml, and vars.yml"
fi
popd >/dev/null 2>&1
}
# --- --- ---
CheckPreReqs
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <CONCOURSE_TARGET> <PIPELINE>"
echo ""
# Usage help, part 1, show available concourse targets
echo -e "Available targets:"
spruce json "$HOME/.flyrc" | jq --raw-output '.targets | keys[]' | while read -r target; do
echo -e "- $GREEN${target}$NC"
done
echo ""
# Usage help, part 2, show available pipelines (directories under pipelines)
echo -e "Available pipelines:"
(cd "${BASEDIR}/pipelines" 2>/dev/null && ls -1) | while read -r item; do
echo -e "- $GREEN${item}$NC"
done
echo ""
exit 1
fi
echo -e '\n'
echo "Info: Flying pipeline $2 in concourse target $1..."
FlyPipeline "$1" "$2"