-
Notifications
You must be signed in to change notification settings - Fork 180
/
install-guard.sh
133 lines (119 loc) · 3.2 KB
/
install-guard.sh
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
#!/bin/sh
# This script downloads and installs cfn-guard from GitHub releases.
# It uses the latest release by default, but can be used to install a specific version using the -v option.
# It detects platforms, downloads the pre-built binary for the specified version (default latest), installs
# it in the ~/.guard/$MAJOR_VER/cfn-guard-v$MAJOR_VER-$OS_TYPE-latest/cfn-guard and symlinks ~/.guard/bin
# to the last installed binary.
main() {
if ! (check_cmd curl || check_cmd wget); then
err "need 'curl' or 'wget' (command not found)"
fi
need_cmd awk
need_cmd mkdir
need_cmd rm
need_cmd uname
need_cmd tar
need_cmd ln
get_os_type
get_arch_type
get_version "$@" |
while
read -r VERSION
do
echo "Installing cfn-guard version '${VERSION}'..."
MAJOR_VER=$(echo "$VERSION" | awk -F '.' '{ print $1 }')
mkdir -p ~/.guard/"$MAJOR_VER" ~/.guard/bin ||
err "unable to make directories ~/.guard/$MAJOR_VER, ~/.guard/bin"
get_os_type
download https://github.com/aws-cloudformation/cloudformation-guard/releases/download/"$VERSION"/cfn-guard-v"$MAJOR_VER"-"$ARCH_TYPE"-"$OS_TYPE"-latest.tar.gz >/tmp/guard.tar.gz ||
err "unable to download https://github.com/aws-cloudformation/cloudformation-guard/releases/download/$VERSION/cfn-guard-v$MAJOR_VER-$ARCH_TYPE-$OS_TYPE-latest.tar.gz"
tar -C ~/.guard/"$MAJOR_VER" -xzf /tmp/guard.tar.gz ||
err "unable to untar /tmp/guard.tar.gz"
ln -sf ~/.guard/"$MAJOR_VER"/cfn-guard-v"$MAJOR_VER"-"$ARCH_TYPE"-"$OS_TYPE"-latest/cfn-guard ~/.guard/bin ||
err "unable to symlink to ~/.guard/bin directory"
~/.guard/bin/cfn-guard help ||
err "cfn-guard was not installed properly"
echo "Remember to SET PATH include PATH=\${PATH}:~/.guard/bin"
done
}
get_os_type() {
_ostype="$(uname -s)"
case "$_ostype" in
Darwin)
OS_TYPE="macos"
;;
Linux)
# IS this RIGHT, we need to build for different ARCH as well.
# Need more ARCH level detections
OS_TYPE="ubuntu"
;;
*)
err "unsupported OS type $_ostype"
;;
esac
}
get_version() {
# Get the version from the -v option, if provided.
while getopts 'v:' OPTION; do
case "$OPTION" in
v)
VERSION="$OPTARG"
;;
?)
err "Usage: install-guard.sh [-v <version>]"
;;
esac
done
# If version is not provided default to the latest version.
if [ -z "$VERSION" ] ; then
get_latest_release
else
echo "$VERSION"
fi
}
get_latest_release() {
download https://api.github.com/repos/aws-cloudformation/cloudformation-guard/releases/latest |
awk -F '"' '/tag_name/ { print $4 }'
}
err() {
echo "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" >/dev/null 2>&1
}
download() {
if check_cmd curl; then
if ! (curl -fsSL "$1"); then
err "error attempting to download from the github repository"
fi
else
if ! (wget -qO- "$1"); then
err "error attempting to download from the github repository"
fi
fi
}
get_arch_type() {
_archtype="$(uname -m)"
case "$_archtype" in
arm64)
ARCH_TYPE="aarch64"
;;
aarch64)
ARCH_TYPE="aarch64"
;;
x86_64)
ARCH_TYPE="x86_64"
;;
*)
err "unsupported architecture type $_archtype"
;;
esac
}
# Pass any arguments provided to main function.
main "$@"