Skip to content

Commit 138d3b6

Browse files
committed
Load all env vars from .env
1 parent 50982d8 commit 138d3b6

File tree

3 files changed

+60
-79
lines changed

3 files changed

+60
-79
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ LEARN_OPS_DJANGO_SECRET_KEY="replace_me"
1515
LEARN_OPS_ALLOWED_HOSTS="api.learning.local,127.0.0.1,localhost"
1616
LEARN_OPS_SUPERUSER_NAME=replace_me
1717
LEARN_OPS_SUPERUSER_PASSWORD=replace_me
18+
19+
SLACK_TOKEN=slack_api_token

setup_ubuntu.sh

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,7 @@
1818
set +o histexpand
1919
set -eu
2020

21-
for i in "$@"
22-
do
23-
case $i in
24-
-h=*|--hosts=*) HOSTS="${i#*=}" ;;
25-
-p=*|--password=*) PASSWORD="${i#*=}" ;;
26-
-s=*|--secret=*) OAUTHSECRET="${i#*=}" ;;
27-
-c=*|--client=*) CLIENT="${i#*=}" ;;
28-
-d=*|--django=*) DJANGOSECRET="${i#*=}" ;;
29-
-k=*|--slack=*) SLACKTOKEN="${i#*=}" ;;
30-
-u=*|--suser=*) SUPERUSER="${i#*=}" ;;
31-
-w=*|--supass=*) SUPERPASS="${i#*=}" ;;
32-
--default) DEFAULT=YES ;;
33-
*) # unknown option ;;
34-
esac
35-
done
36-
37-
38-
export LEARN_OPS_CLIENT_ID="$CLIENT"
39-
export LEARN_OPS_SECRET_KEY="$OAUTHSECRET"
40-
export LEARN_OPS_DB=learnops
41-
export LEARN_OPS_USER=learnops
42-
export LEARN_OPS_PASSWORD="$PASSWORD"
43-
export LEARN_OPS_HOST=localhost
44-
export LEARN_OPS_PORT=5432
45-
export LEARN_OPS_DJANGO_SECRET_KEY="$DJANGOSECRET"
46-
export LEARN_OPS_ALLOWED_HOSTS="$HOSTS"
47-
export SLACK_BOT_TOKEN="$SLACKTOKEN"
21+
source .env
4822

4923
#####
5024
# Create the Ubuntu user account
@@ -61,26 +35,6 @@ else
6135
sudo chsh -s /bin/bash $LEARN_OPS_USER
6236
fi
6337

64-
65-
#####
66-
# Create shell init file and reload
67-
#####
68-
sudo tee $USER_HOME/.bashrc <<EOF
69-
export LEARN_OPS_CLIENT_ID=$CLIENT
70-
export LEARN_OPS_SECRET_KEY=$OAUTHSECRET
71-
export LEARN_OPS_DB=learnops
72-
export LEARN_OPS_USER=learnops
73-
export LEARN_OPS_PASSWORD=$PASSWORD
74-
export LEARN_OPS_HOST=localhost
75-
export LEARN_OPS_PORT=5432
76-
export LEARN_OPS_DJANGO_SECRET_KEY=$LEARN_OPS_DJANGO_SECRET_KEY
77-
export LEARN_OPS_ALLOWED_HOSTS=$HOSTS
78-
export SLACK_BOT_TOKEN=$SLACKTOKEN
79-
export PATH=$PATH:/home/learnops/.local/bin
80-
EOF
81-
sudo su - learnops -c "bash -c 'source ~/.bashrc'"
82-
83-
8438
#####
8539
# Install required software
8640
#####
@@ -212,7 +166,7 @@ sudo tee $API_HOME/LearningAPI/fixtures/socialaccount.json <<EOF
212166
EOF
213167

214168
echo "Generating Django password"
215-
DJANGO_GENERATED_PASSWORD=$(python3 ./djangopass.py "$SUPERPASS" >&1)
169+
DJANGO_GENERATED_PASSWORD=$(python3 ./djangopass.py "$LEARN_OPS_SUPERUSER_PASSWORD" >&1)
216170

217171
sudo tee $API_HOME/LearningAPI/fixtures/superuser.json <<EOF
218172
[
@@ -223,7 +177,7 @@ sudo tee $API_HOME/LearningAPI/fixtures/superuser.json <<EOF
223177
"password": "$DJANGO_GENERATED_PASSWORD",
224178
"last_login": null,
225179
"is_superuser": true,
226-
"username": "$SUPERUSER",
180+
"username": "$LEARN_OPS_SUPERUSER_NAME",
227181
"first_name": "Admina",
228182
"last_name": "Straytor",
229183
"email": "[email protected]",
@@ -286,8 +240,8 @@ Environment="LEARNING_GITHUB_CALLBACK=https://learning.nss.team/auth/github"
286240
Environment="SLACK_BOT_TOKEN=${SLACKTOKEN}"
287241
Environment="LEARN_OPS_DB=${LEARN_OPS_USER}"
288242
Environment="LEARN_OPS_USER=${LEARN_OPS_USER}"
289-
Environment="LEARN_OPS_PASSWORD=${PASSWORD}"
290-
Environment="LEARN_OPS_HOST=localhost"
243+
Environment="LEARN_OPS_PASSWORD=${LEARN_OPS_PASSWORD}"
244+
Environment="LEARN_OPS_HOST=${LEARN_OPS_HOST}"
291245
Environment="LEARN_OPS_PORT=5432"
292246
Environment="LEARN_OPS_CLIENT_ID=${LEARN_OPS_CLIENT_ID}"
293247
Environment="LEARN_OPS_SECRET_KEY=${LEARN_OPS_SECRET_KEY}"

setup_ubuntu_local.sh

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@
33
set +o histexpand
44
set -eu
55
source .env
6+
7+
# If changes need to be made to user's config file, detect the correct one
8+
[[ $0 == *bash ]] && config_file=".bashrc" || config_file=".zshrc"
9+
10+
manage_service() {
11+
local service=$1
12+
local action=$2 # start, stop, or restart
13+
local service_command=""
14+
15+
# Detect the init system
16+
if [[ $(/sbin/init --version) =~ upstart ]]; then
17+
service_command="sudo service $service $action"
18+
elif [[ $(systemctl) =~ -\.mount ]]; then
19+
service_command="sudo systemctl $action $service"
20+
elif [[ -f /etc/init.d/postgresql ]]; then
21+
service_command="sudo /etc/init.d/$service $action"
22+
else
23+
echo "Init system not supported!"
24+
return 1
25+
fi
26+
27+
# Execute the service command
28+
echo "Executing: $service_command"
29+
$service_command
30+
}
31+
632
#####
733
# Install required software
834
#####
@@ -14,45 +40,29 @@ if ! command -v psql &>/dev/null; then
1440
sudo apt install postgresql postgresql-contrib -y
1541
fi
1642

17-
1843
# Check if systemd is running by examining the presence of systemd's runtime directory
1944
echo "Restarting Postgresql"
20-
if [ -d /run/systemd/system ]; then
21-
echo "Systemd is enabled"
22-
sudo systemctl start postgresql >> /dev/null
23-
else
24-
echo "Systemd is not enabled"
25-
sudo service postgresql start >> /dev/null
26-
fi
45+
manage_service postgresql start
2746

2847
#####
2948
# Get Postgres version
3049
#####
3150
echo "Checking version of Postgres"
32-
VERSION=$( $(sudo find /usr -wholename '*/bin/postgres') -V | (grep -E -oah -m 1 '[0-9]{1,}') | head -1)
51+
VERSION=$($(sudo find /usr -wholename '*/bin/postgres') -V | (grep -E -oah -m 1 '[0-9]{1,}') | head -1)
3352
echo "Found version $VERSION"
3453

35-
36-
3754
#####
3855
# Replace `peer` with `md5` in the pg_hba file to enable peer authentication
56+
# and restart the postgres service to enable the changes
3957
#####
4058
sudo sed -i -e 's/peer/trust/g' /etc/postgresql/"$VERSION"/main/pg_hba.conf
41-
42-
43-
#####
44-
# Check for systemd
45-
#####
46-
pidof systemd && sudo systemctl restart postgresql || sudo service postgresql restart
47-
59+
manage_service postgresql restart
4860

4961
#####
5062
# Create the role in the database
5163
#####
5264
echo "Creating Postgresql role and database"
5365

54-
set -e
55-
5666
sudo su - postgres <<COMMANDS
5767
psql -c "DROP DATABASE IF EXISTS $LEARN_OPS_DB WITH (FORCE);"
5868
psql -c "CREATE DATABASE $LEARN_OPS_DB;"
@@ -64,13 +74,30 @@ psql -c "GRANT ALL PRIVILEGES ON DATABASE $LEARN_OPS_DB TO $LEARN_OPS_USER;"
6474
psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO $LEARN_OPS_USER;"
6575
COMMANDS
6676

67-
6877
#####
6978
# Install Pyenv and required Python version
7079
#####
7180
if command -v pyenv &>/dev/null; then
7281
echo "pyenv is installed."
7382
else
83+
echo "Installing pyenv..."
84+
85+
directory_path="$HOME/.pyenv"
86+
if [ -d "$directory_path" ]; then
87+
# Directory exists, now delete it
88+
echo "Directory exists. Deleting $directory_path..."
89+
rm -rf "$directory_path"
90+
91+
# Check if deletion was successful
92+
if [ ! -d "$directory_path" ]; then
93+
echo "Directory successfully deleted."
94+
else
95+
echo "Failed to delete directory."
96+
fi
97+
else
98+
echo "Directory does not exist."
99+
fi
100+
74101
# Install dependency packages that are necessary to install pyenv on WSL Ubuntu environment
75102
sudo apt install -y curl git build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
76103

@@ -80,7 +107,7 @@ else
80107
# Add necessary config to shell profile (.zshrc)
81108
echo 'export PATH="$HOME/.pyenv/bin:$PATH"
82109
eval "$(pyenv init --path)"
83-
eval "$(pyenv virtualenv-init -)"' >>$HOME/.zshrc
110+
eval "$(pyenv virtualenv-init -)"' >>$HOME/$config_file
84111

85112
# Update path of current subshell execution
86113
export PATH="$HOME/.pyenv/bin:$PATH"
@@ -108,9 +135,8 @@ else
108135
pyenv global 3.9.1
109136
fi
110137

111-
112-
pip3 install pipenv
113-
138+
# Install pipenv for managing virtual environment
139+
pip3 install pipenv django
114140

115141
#####
116142
# Create socialaccount.json fixture
@@ -173,7 +199,6 @@ sudo tee ./LearningAPI/fixtures/superuser.json <<EOF
173199
]
174200
EOF
175201

176-
177202
#####
178203
# Install project requirements and run migrations
179204
#####
@@ -187,7 +212,7 @@ pipenv run bash -c "python3 manage.py flush --no-input \
187212
&& python3 manage.py loaddata complete_backup \
188213
&& python3 manage.py loaddata superuser"
189214

190-
rm ./LearningAPI/fixtures/superuser.json -y
191-
rm ./LearningAPI/fixtures/socialaccount.json -y
215+
sudo rm -f ./LearningAPI/fixtures/superuser.json
216+
sudo rm -f ./LearningAPI/fixtures/socialaccount.json
192217

193218
echo "LEARNING_GITHUB_CALLBACK=http://localhost:3000/auth/github" >>.env

0 commit comments

Comments
 (0)