Skip to content

Commit

Permalink
Merge branch 'main' of github.com:stevebrownlee/learn-ops-api
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebrownlee committed Feb 19, 2024
2 parents 273705b + 5ad161b commit e29ff9b
Showing 1 changed file with 8 additions and 142 deletions.
150 changes: 8 additions & 142 deletions setup_ubuntu_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,6 @@
set +o histexpand
set -eu

for i in "$@"
do
case $i in
-h=*|--hosts=*) HOSTS="${i#*=}" ;;
-p=*|--password=*) PASSWORD="${i#*=}" ;;
-s=*|--secret=*) OAUTHSECRET="${i#*=}" ;;
-c=*|--client=*) CLIENT="${i#*=}" ;;
-d=*|--django=*) DJANGOSECRET="${i#*=}" ;;
-k=*|--slack=*) SLACKTOKEN="${i#*=}" ;;
-u=*|--suser=*) SUPERUSER="${i#*=}" ;;
-w=*|--supass=*) SUPERPASS="${i#*=}" ;;
--default) DEFAULT=YES ;;
*) # unknown option ;;
esac
done


export LEARN_OPS_CLIENT_ID="$CLIENT"
export LEARN_OPS_SECRET_KEY="$OAUTHSECRET"
export LEARN_OPS_DB='learnops'
export LEARN_OPS_USER='learnops'
export LEARN_OPS_PASSWORD="$PASSWORD"
export LEARN_OPS_HOST='localhost'
export LEARN_OPS_PORT=5432
export LEARN_OPS_DJANGO_SECRET_KEY="$DJANGOSECRET"
export LEARN_OPS_ALLOWED_HOSTS="$HOSTS"
export SLACK_BOT_TOKEN="$SLACKTOKEN"

#####
# Create the Ubuntu user account
#####
USER_HOME="/home/$LEARN_OPS_USER"
if id "$LEARN_OPS_USER" >>/dev/null 2>&1; then
echo "User exists"
else
echo "Creating Linux user matching Postgres database"
sudo useradd -p "$(openssl passwd -1 "$LEARN_OPS_PASSWORD")" "$LEARN_OPS_USER"
sudo mkdir -p "$USER_HOME"
sudo usermod -d $USER_HOME $LEARN_OPS_USER
sudo chown -R $LEARN_OPS_USER $USER_HOME
sudo chsh -s /bin/bash $LEARN_OPS_USER
fi


#####
# Create shell init file and reload
#####
sudo tee $USER_HOME/.bashrc <<EOF
export LEARN_OPS_CLIENT_ID=$CLIENT
export LEARN_OPS_SECRET_KEY=$OAUTHSECRET
export LEARN_OPS_DB=learnops
export LEARN_OPS_USER=learnops
export LEARN_OPS_PASSWORD=$PASSWORD
export LEARN_OPS_HOST=localhost
export LEARN_OPS_PORT=5432
export LEARN_OPS_DJANGO_SECRET_KEY=$LEARN_OPS_DJANGO_SECRET_KEY
export LEARN_OPS_ALLOWED_HOSTS=$HOSTS
export SLACK_BOT_TOKEN=$SLACKTOKEN
export PATH=$PATH:/home/learnops/.local/bin
EOF
sudo su - learnops -c "bash -c 'source ~/.bashrc'"


#####
# Install required software
#####
Expand All @@ -75,7 +12,7 @@ sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_rele
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update -y

packages=("gcc" "git" "curl" "nginx" "certbot" "python3-django" "postgresql-12" "postgresql-contrib-12" "python3-pip" "python3.10-venv")
packages=("gcc" "git" "curl" "nginx" "certbot" "python3-django" "postgresql" "postgresql-contrib" "python3-pip" "python3.10-venv")

for package in "${packages[@]}"; do
if ! dpkg-query -W -f='${Status}\n' "$package" | grep -q "ok installed"; then
Expand Down Expand Up @@ -140,7 +77,7 @@ COMMANDS
#####
# Create directory to store static files and take ownership
#####
API_HOME=/mnt/learnops
API_HOME="${pwd}"
echo "Creating static file directory"
sudo mkdir -p /var/www/learning.nss.team
sudo chown "$LEARN_OPS_USER":www-data /var/www/learning.nss.team
Expand Down Expand Up @@ -212,16 +149,14 @@ EOF
#####
# Install project requirements and run migrations
#####
VENV_DIR="/home/learnops/venv"
echo "Installing project requirements and migrating"
sudo su - learnops << EOF
source /home/learnops/.bashrc
export PATH=$PATH:/home/learnops/.local/bin
cd $API_HOME
export VIRTUAL_ENV="$(pwd)/.venv"
echo "VIRTUAL_ENV=$VIRTUAL_ENV" >>.env
echo "PIPENV_VENV_IN_PROJECT=1" >>.env
echo "LEARNING_GITHUB_CALLBACK=http://localhost:3000/auth/github" >>.env

pip3 install --upgrade pip setuptools
python3 -m venv $VENV_DIR
source $VENV_DIR/bin/activate
python3 -m venv $VIRTUAL_ENV
source $VIRTUAL_ENV/bin/activate
pip3 install django
pip3 install wheel
pip3 install -r requirements.txt
Expand All @@ -232,74 +167,5 @@ python3 manage.py loaddata complete_backup
python3 manage.py loaddata superuser
rm $API_HOME/LearningAPI/fixtures/superuser.json
python3 manage.py collectstatic --noinput
EOF


#####
# Create gunicorn service file and start service
#####
sudo tee /etc/systemd/system/learning.service <<EOF
[Unit]
Description=learnops gunicorn daemon
After=network.target
[Service]
Environment="DEBUG=True"
Environment="DEVELOPMENT_MODE=True"
Environment="LEARNING_GITHUB_CALLBACK=http://api.learning.local/auth/github"
Environment="SLACK_BOT_TOKEN=$SLACKTOKEN"
Environment="LEARN_OPS_DB=$LEARN_OPS_USER"
Environment="LEARN_OPS_USER=$LEARN_OPS_USER"
Environment="LEARN_OPS_PASSWORD=$PASSWORD"
Environment="LEARN_OPS_HOST=localhost"
Environment="LEARN_OPS_PORT=5432"
Environment="LEARN_OPS_CLIENT_ID=$LEARN_OPS_CLIENT_ID"
Environment="LEARN_OPS_SECRET_KEY=$LEARN_OPS_SECRET_KEY"
Environment="LEARN_OPS_DJANGO_SECRET_KEY=$LEARN_OPS_DJANGO_SECRET_KEY"
Environment="LEARN_OPS_ALLOWED_HOSTS=$LEARN_OPS_ALLOWED_HOSTS"
User=$LEARN_OPS_USER
Group=www-data
WorkingDirectory=$API_HOME
ExecStart=$VENV_DIR/bin/gunicorn -w 3 --bind 127.0.0.1:8000 --log-file /mnt/learnops/logs/learning.log --access-logfile /mnt/learnops/logs/learning-access.log LearningPlatform.wsgi
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

sudo systemctl enable learning
sudo systemctl daemon-reload
if [ "${SYSTEMD_PID}" == "" ]; then
sudo systemctl start learning >> /dev/null
else
sudo service learning start >> /dev/null
fi


#####
# Create nginx reverse proxy for API
#####
sudo tee /etc/nginx/sites-available/api <<EOF
server {
listen 80;
server_name api.learning.local;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_redirect off;
}
location /static/ {
autoindex off;
root /var/www/learning.nss.team/;
}
}
EOF

if [ ! -f /etc/nginx/sites-enabled/api ]; then
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api
fi
sudo systemctl restart nginx

0 comments on commit e29ff9b

Please sign in to comment.