-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-nomad.sh
executable file
·206 lines (169 loc) · 4.91 KB
/
install-nomad.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
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
#!/bin/bash
# This script can be used to install Nomad and its dependencies. This script has been tested with the following
# operating systems:
#
# 1. Ubuntu 16.04
# 2. Ubuntu 18.04
# 3. Amazon Linux 2
set -e
readonly DEFAULT_INSTALL_PATH="/opt/nomad"
readonly DEFAULT_NOMAD_USER="nomad"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SYSTEM_BIN_DIR="/usr/local/bin"
readonly SUPERVISOR_DIR="/etc/supervisor"
readonly SUPERVISOR_CONF_DIR="$SUPERVISOR_DIR/conf.d"
readonly SCRIPT_NAME="$(basename "$0")"
function print_usage {
echo
echo "Usage: install-nomad [OPTIONS]"
echo
echo "This script can be used to install Nomad and its dependencies. This script has been tested with Ubuntu 16.04, Ubuntu 18.04 and Amazon Linux 2."
echo
echo "Options:"
echo
echo -e " --version\t\tThe version of Nomad to install. Required."
echo -e " --path\t\tThe path where Nomad should be installed. Optional. Default: $DEFAULT_INSTALL_PATH."
echo -e " --user\t\tThe user who will own the Nomad install directories. Optional. Default: $DEFAULT_NOMAD_USER."
echo
echo "Example:"
echo
echo " install-nomad --version 0.5.4"
}
function log {
local readonly level="$1"
local readonly message="$2"
local readonly timestamp=$(date +"%Y-%m-%d %H:%M:%S")
>&2 echo -e "${timestamp} [${level}] [$SCRIPT_NAME] ${message}"
}
function log_info {
local readonly message="$1"
log "INFO" "$message"
}
function log_warn {
local readonly message="$1"
log "WARN" "$message"
}
function log_error {
local readonly message="$1"
log "ERROR" "$message"
}
function assert_not_empty {
local readonly arg_name="$1"
local readonly arg_value="$2"
if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty"
print_usage
exit 1
fi
}
function has_yum {
[ -n "$(command -v yum)" ]
}
function has_apt_get {
[ -n "$(command -v apt-get)" ]
}
function install_dependencies {
log_info "Installing dependencies"
if $(has_apt_get); then
sudo apt-get update -y
sudo apt-get install -y awscli curl unzip jq
elif $(has_yum); then
sudo yum update -y
sudo yum install -y aws curl unzip jq
else
log_error "Could not find apt-get or yum. Cannot install dependencies on this OS."
exit 1
fi
}
function user_exists {
local readonly username="$1"
id "$username" >/dev/null 2>&1
}
function create_nomad_user {
local readonly username="$1"
if $(user_exists "$username"); then
echo "User $username already exists. Will not create again."
else
log_info "Creating user named $username"
sudo useradd "$username"
fi
# Add the nomad user to the docker and sudo groups
sudo usermod -a -G docker "$username"
sudo usermod -a -G sudo "$username"
}
function create_nomad_install_paths {
local readonly path="$1"
local readonly username="$2"
log_info "Creating install dirs for Nomad at $path"
sudo mkdir -p "$path"
sudo mkdir -p "$path/bin"
sudo mkdir -p "$path/config"
sudo mkdir -p "$path/data"
log_info "Changing ownership of $path to $username"
sudo chown -R "$username:$username" "$path"
}
function install_binaries {
local readonly version="$1"
local readonly path="$2"
local readonly username="$3"
local readonly url="https://releases.hashicorp.com/nomad/${version}/nomad_${version}_linux_amd64.zip"
local readonly download_path="/tmp/nomad_${version}_linux_amd64.zip"
local readonly bin_dir="$path/bin"
local readonly nomad_dest_path="$bin_dir/nomad"
log_info "Downloading Nomad $version from $url to $download_path"
curl -o "$download_path" "$url"
unzip -d /tmp "$download_path"
log_info "Moving Nomad binary to $nomad_dest_path"
sudo mv "/tmp/nomad" "$nomad_dest_path"
sudo chown "$username:$username" "$nomad_dest_path"
sudo chmod a+x "$nomad_dest_path"
local readonly symlink_path="$SYSTEM_BIN_DIR/nomad"
if [[ -f "$symlink_path" ]]; then
log_info "Symlink $symlink_path already exists. Will not add again."
else
log_info "Adding symlink to $nomad_dest_path in $symlink_path"
sudo ln -s "$nomad_dest_path" "$symlink_path"
fi
}
function install {
local version=""
local path="$DEFAULT_INSTALL_PATH"
local user="$DEFAULT_NOMAD_USER"
while [[ $# > 0 ]]; do
local key="$1"
case "$key" in
--version)
version="$2"
shift
;;
--path)
path="$2"
shift
;;
--user)
user="$2"
shift
;;
--help)
print_usage
exit
;;
*)
log_error "Unrecognized argument: $key"
print_usage
exit 1
;;
esac
shift
done
assert_not_empty "--version" "$version"
assert_not_empty "--path" "$path"
assert_not_empty "--user" "$user"
log_info "Starting Nomad install"
install_dependencies
create_nomad_user "$user"
create_nomad_install_paths "$path" "$user"
install_binaries "$version" "$path" "$user"
log_info "Nomad install complete!"
}
install "$@"