-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
397 lines (338 loc) · 12.6 KB
/
setup.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#! /bin/bash
#
# This script sets up a local development environment on an Ubuntu 20.04/22.04 machine
# to work with Poetry managed Python3.X projects.
#
# Targets:
# - Python3.10
# - Poetry 1.5.1
# - Docker 24.0.6
# - Terraform v1.6.6
#
# Requirements:
# - Ubuntu 20.04/22.04
# - Python3.7+
#
# -----------------------------------------------------------------------------------------------------------
# 0) Config: here we'll set default variables and handle options for controlling
# the target versions of Python and Poetry installed during set up.
# -----------------------------------------------------------------------------------------------------------
package="setup.sh"
TARGET_PYTHON_VERSION="3.10"
TARGET_POETRY_VERSION="1.5.1"
show_help() {
cat <<END
Sets up Poetry managed Python environment for machine learning
development on Ubuntu 20.04/22.04.
${package} [options] [arguments]
options:
-h, --help Display help message
--python=VERSION Python version to use (default 3.10)
--poetry=VERSION Poetry version to use (default 1.5.1)
END
exit 0
}
while getopts 'h-:' flag; do
case "${flag}" in
h) show_help ;;
-)
# Prefix substring removal matching "*="
LONG_OPTARG_VALUE="${OPTARG#*=}"
case ${OPTARG} in
help)
show_help
;;
python=?*)
TARGET_PYTHON_VERSION="${LONG_OPTARG_VALUE}"
;;
poetry=?*)
TARGET_POETRY_VERSION="${LONG_OPTARG_VALUE}"
;;
python* | poetry*)
echo "No arg for --${OPTARG} option" >&2
exit 2
;;
help*)
echo "No arg allowed for --${OPTARG} option" >&2;
exit 2
;;
*)
echo "Unknown option --$OPTARG" >&2
exit 2
;;
esac
;;
# getopts already reported the unknown option error, so exit
\?) exit 2 ;;
esac
done
shift $((OPTIND-1))
readonly TARGET_PYTHON_VERSION
readonly TARGET_POETRY_VERSION
# -----------------------------------------------------------------------------------------------------------
# 1) Base Requirements: this will ensure that you base requirements along with some data science
# CLI tools installed.
# -----------------------------------------------------------------------------------------------------------
# Check if ca-certificates is in the apt-cache
if ( apt-cache show ca-certificates > /dev/null ); then
echo 'ca-certificates is already cached 🟢'
else
sudo apt update
fi
# Ensure ca-certificates package is installed on the machine
if ( which update-ca-certificates > /dev/null ); then
echo 'ca-certificates is already installed 🟢'
else
echo 'Installing ca-certificates 📜'
sudo apt-get install -y ca-certificates
fi
# Check if curl is in the apt-cache
if ( apt-cache show curl > /dev/null ); then
echo 'curl is already cached 🟢'
else
sudo apt update
fi
# Ensure curl is installed on the machine
if ( which curl > /dev/null ); then
echo 'curl is already installed 🟢'
else
echo 'Installing curl 🌀'
sudo apt install -y curl
fi
# Check if make is in the apt-cache
if ( apt-cache show make > /dev/null ); then
echo 'make is already cached 🟢'
else
sudo apt update
fi
# Ensure make is installed on the machine
if ( which make > /dev/null ); then
echo 'make is already installed 🟢'
else
echo 'Installing make 🔧'
sudo apt install -y make
fi
# Check if gnupg is in the apt-cache
if ( apt-cache show gpg > /dev/null ); then
echo 'gnupg is already cached 🟢'
else
sudo apt update
fi
# Ensure gnupg is installed on the machine
if ( which gpg > /dev/null ); then
echo 'make is already installed 🟢'
else
echo 'Installing gnugp 🔧'
sudo apt install -y gnupg
fi
# Check if bat is in the apt-cache
if ( apt-cache show bat > /dev/null ); then
echo 'batcat is already cached 🟢'
else
sudo apt update
fi
# Ensure bat is installed on the machine
if ( which batcat > /dev/null ); then
echo 'batcat is already installed 🟢'
else
echo 'Installing batcat 🔧'
sudo apt install -y bat
fi
# Check if jq is in the apt-cache
if ( apt-cache show jq > /dev/null ); then
echo 'jq is already cached 🟢'
else
sudo apt update
fi
# Ensure jq is installed on the machine
if ( which jq > /dev/null ); then
echo 'jq is already installed 🟢'
else
echo 'Installing jq 🔧'
sudo apt install -y jq
fi
# Check if csvkit is in the apt-cache
if ( apt-cache show csvkit > /dev/null ); then
echo 'csvkit is already cached 🟢'
else
sudo apt update
fi
# Ensure csvkit is installed on the machine (commands csvlook, csvcut, in2csv, sql2csv)
if ( which csvlook > /dev/null ); then
echo 'csvkit is already installed 🟢'
else
echo 'Installing csvkit 🔧'
sudo apt install -y csvkit
fi
# -----------------------------------------------------------------------------------------------------------
# 2) Python3.X Install: here we'll install Python3.10 (default) - swap this for a version you'd like.
# -----------------------------------------------------------------------------------------------------------
# Check if software-properties-common is in the apt-cache
if ( apt-cache show software-properties-common > /dev/null ); then
echo 'software-properties-common is already cached 🟢'
else
sudo apt update
fi
# Check for the software-properties-common requirement
if ( dpkg -L software-properties-common > /dev/null ); then
echo 'software-properties-common requirement met 🟢'
else
echo 'Installing software-properties-common 🔧'
sudo apt install -y software-properties-common
fi
# Add this apt repository for Python 3.X
if [[ -n "$(ls /etc/apt/sources.list.d | grep deadsnakes)" ]]; then
echo 'ppa:deadsnakes/ppa apt repository present 🟢'
else
echo 'Adding deadsnakes to the apt-repository 💀🐍'
sudo add-apt-repository -y ppa:deadsnakes/ppa
# Refresh the package list again
sudo apt update -y
fi
# Now you can download Python3.X
if ( which python${TARGET_PYTHON_VERSION} > /dev/null ); then
echo "Python${TARGET_PYTHON_VERSION} already installed 🐍"
else
echo "Installing Python${TARGET_PYTHON_VERSION} 🔧"
sudo apt install -y python${TARGET_PYTHON_VERSION}
fi
# Verify Python3.X installation
if ( which python${TARGET_PYTHON_VERSION} > /dev/null ); then
echo "$(python${TARGET_PYTHON_VERSION} --version) 🐍 🚀 ✨"
else
echo "Python ${TARGET_PYTHON_VERSION} was not installed successfully 🔴"
fi
# Install pip (deadsnakes Python3.X installations do not come with pip)
if ( python${TARGET_PYTHON_VERSION} -m pip > /dev/null ); then
echo 'pip is already installed 🟢'
else
echo "Installing pip for Python ${TARGET_PYTHON_VERSION} 🔧"
sudo apt install python${TARGET_PYTHON_VERSION}-venv
python${TARGET_PYTHON_VERSION} -m ensurepip
fi
# Verify pip installation
if ( python${TARGET_PYTHON_VERSION} -m pip > /dev/null ); then
echo "python${TARGET_PYTHON_VERSION} pip successfully installed installed 🟢"
else
echo "python${TARGET_PYTHON_VERSION} pip was not installed successfully 🔴"
fi
# -----------------------------------------------------------------------------------------------------------
# 3) Poetry Install: here we'll install and configure Poetry, as well as add Poetry to the PATH.
# -----------------------------------------------------------------------------------------------------------
# Install Poetry using the official installer
if ( which poetry > /dev/null ); then
echo "Poetry ${TARGET_POETRY_VERSION} is already installed 🟢"
else
echo 'Installing Poetry 🧙♂️'
curl -sSL https://install.python-poetry.org \
| POETRY_VERSION=${TARGET_POETRY_VERSION} python3 -
fi
# Add Poetry to the path in the current user's .bashrc
if ( poetry --version > /dev/null ); then
echo 'Poetry is already in PATH 🟢'
else
echo -e \
"# Add Poetry (Python Package Manager) to PATH
export PATH="/home/${USER}/.local/bin:${PATH}"" \
>> ~/.bashrc
source ~/.bashrc
fi
# Configure Poetry to put build all virtual environments in the project's directory
if [[ "$(poetry config virtualenvs.in-project)" == "true" ]]; then
echo 'Poetry already configured to create virtual envs within projects 🟢'
else
echo 'Configuring Poetry to create virtual envs in projects 🪐'
poetry config virtualenvs.in-project true
fi
# -----------------------------------------------------------------------------------------------------------
# 4) Docker Install: here we'll install Docker
# -----------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------
# 4.1) Set up the repository: Before you install Docker Engine for the first time on a new host machine,
# you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
# -----------------------------------------------------------------------------------------------------------
# Pull the current machine's distro for GPG key targeting
readonly DISTRO="$(lsb_release -d | awk -F ' ' '{print tolower($2)}')"
# Add Docker’s official GPG key
if [[ -f /etc/apt/keyrings/docker.gpg ]]; then
echo 'Docker GPG Key already installed at /etc/apt/keyrings/docker.gpg 🟢'
else
echo 'Installing Docker GPG Key at /etc/apt/keyrings/docker.gpg 🔧'
# Create the /etc/apt/keyrings directory with appropriate permissions
sudo install -m 0755 -d /etc/apt/keyrings
# Download the GPG key from Docker
curl -fsSL https://download.docker.com/linux/${DISTRO}/gpg \
| sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
fi
# Set up the repository
if [[ -f /etc/apt/sources.list.d/docker.list ]]; then
echo 'docker.list repository already exists at /etc/apt/sources.list.d/docker.list 🟢'
else
echo 'Installing docker.list repository at /etc/apt/sources.list.d/docker.list 🔧'
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/$DISTRO \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" \
| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
# -----------------------------------------------------------------------------------------------------------
# 4.2) Install Docker Engine
# -----------------------------------------------------------------------------------------------------------
# Check if docker-ce is in the apt-cache
if ( apt-cache show docker-ce > /dev/null ); then
echo 'docker-ce is already cached 🟢'
else
sudo apt update
fi
# Install Docker Engine, containerd, and Docker Compose
if ( docker --version > /dev/null ); then
echo 'Docker is already installed 🟢'
echo "Using $(docker --version)"
else
echo 'Installing Docker 🐳'
# Installs
sudo apt-get install -y docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin
# Verify that the Docker Engine installation is successful by running the hello-world image
sudo docker run --rm hello-world
fi
# -----------------------------------------------------------------------------------------------------------
# 5) Terraform Install: here we'll install Terraform
# -----------------------------------------------------------------------------------------------------------
# Add HashiCorp's official GPG key
if [[ -f /usr/share/keyrings/hashicorp-archive-keyring.gpg ]]; then
echo 'Hashicorp GPG Key already installed at /usr/share/keyrings/hashicorp-archive-keyring.gpg 🟢'
else
echo 'Installing Hashicorp GPG key at /usr/share/keyrings/hashicorp-archive-keyring.gpg 🔧'
wget -O- https://apt.releases.hashicorp.com/gpg \
| gpg --dearmor \
| sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
fi
# Add HashiCorp's repository
if [[ -f /etc/apt/sources.list.d/hashicorp.list ]]; then
echo 'hashicorp.list repository already exists at /etc/apt/sources.list.d/hashicorp.list 🟢'
else
echo 'Installing hashicorp.list repository at /etc/apt/sources.list.d/hashicorp.list 🔧'
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/hashicorp.list
# Refresh the package list
sudo apt update -y
fi
# Install Terraform
if ( terraform --version > /dev/null ); then
echo 'Terraform is already installed 🟢'
else
echo 'Installing Terraform 🌎'
sudo apt-get install terraform
fi
# Verify Terraform installation
if ( terraform --version > /dev/null ); then
echo "$(terraform --version) 🌎"
else
echo 'Terraform was not installed successfully 🔴'
fi