3
3
set +o histexpand
4
4
set -eu
5
5
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
+
6
32
# ####
7
33
# Install required software
8
34
# ####
@@ -14,45 +40,29 @@ if ! command -v psql &>/dev/null; then
14
40
sudo apt install postgresql postgresql-contrib -y
15
41
fi
16
42
17
-
18
43
# Check if systemd is running by examining the presence of systemd's runtime directory
19
44
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
27
46
28
47
# ####
29
48
# Get Postgres version
30
49
# ####
31
50
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)
33
52
echo " Found version $VERSION "
34
53
35
-
36
-
37
54
# ####
38
55
# Replace `peer` with `md5` in the pg_hba file to enable peer authentication
56
+ # and restart the postgres service to enable the changes
39
57
# ####
40
58
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
48
60
49
61
# ####
50
62
# Create the role in the database
51
63
# ####
52
64
echo " Creating Postgresql role and database"
53
65
54
- set -e
55
-
56
66
sudo su - postgres << COMMANDS
57
67
psql -c "DROP DATABASE IF EXISTS $LEARN_OPS_DB WITH (FORCE);"
58
68
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;"
64
74
psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO $LEARN_OPS_USER ;"
65
75
COMMANDS
66
76
67
-
68
77
# ####
69
78
# Install Pyenv and required Python version
70
79
# ####
71
80
if command -v pyenv & > /dev/null; then
72
81
echo " pyenv is installed."
73
82
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
+
74
101
# Install dependency packages that are necessary to install pyenv on WSL Ubuntu environment
75
102
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
76
103
80
107
# Add necessary config to shell profile (.zshrc)
81
108
echo ' export PATH="$HOME/.pyenv/bin:$PATH"
82
109
eval "$(pyenv init --path)"
83
- eval "$(pyenv virtualenv-init -)"' >> $HOME /.zshrc
110
+ eval "$(pyenv virtualenv-init -)"' >> $HOME /$config_file
84
111
85
112
# Update path of current subshell execution
86
113
export PATH=" $HOME /.pyenv/bin:$PATH "
108
135
pyenv global 3.9.1
109
136
fi
110
137
111
-
112
- pip3 install pipenv
113
-
138
+ # Install pipenv for managing virtual environment
139
+ pip3 install pipenv django
114
140
115
141
# ####
116
142
# Create socialaccount.json fixture
@@ -173,7 +199,6 @@ sudo tee ./LearningAPI/fixtures/superuser.json <<EOF
173
199
]
174
200
EOF
175
201
176
-
177
202
# ####
178
203
# Install project requirements and run migrations
179
204
# ####
@@ -187,7 +212,7 @@ pipenv run bash -c "python3 manage.py flush --no-input \
187
212
&& python3 manage.py loaddata complete_backup \
188
213
&& python3 manage.py loaddata superuser"
189
214
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
192
217
193
218
echo " LEARNING_GITHUB_CALLBACK=http://localhost:3000/auth/github" >> .env
0 commit comments