|
| 1 | +# prompt the user for the variables |
| 2 | +# ask for the instance name |
| 3 | +#echo "Enter the instance name (e.g. defectdojo-002):" |
| 4 | +#read instanceName |
| 5 | +#echo "Instance name is: $instanceName" |
| 6 | +# set as environment variable |
| 7 | +export instanceName="app-defectdojo-ek005" |
| 8 | + |
| 9 | +# set all variables here: |
| 10 | +export USERNAME="ddadmin" |
| 11 | +export PASSWORD="booWgDmaYdgNxO5eNWql" |
| 12 | +export postgresHostname="$instanceName-postgresql.devopsshield.com" |
| 13 | +export NEWHOSTNAME="$instanceName.devopsshield.com" |
| 14 | +export nginx_folder="$HOME/django-DefectDojo/nginx" |
| 15 | +export databaseName="defectdojo" |
| 16 | +export databasePort="5432" |
| 17 | +export doPauses="false" # set to "true" to pause after each step |
| 18 | +# wait time for the container to be up and migrations to be done |
| 19 | +export waitTime=300 |
| 20 | + |
| 21 | +export EMAIL="emmanuel.knafo@devopsshield.com" |
| 22 | +export adminUser="emmanuel" |
| 23 | +export adminPassword="N9rw04entPmou3Rbf6JP!" |
| 24 | + |
| 25 | +# check that the hostname is correct |
| 26 | +echo "The hostname is: $NEWHOSTNAME" |
| 27 | +# check that it resolves to the correct IP |
| 28 | +echo "The IP address for the hostname is:" |
| 29 | +dig +short $NEWHOSTNAME |
| 30 | + |
| 31 | + |
| 32 | +# update the system |
| 33 | +# skip a line |
| 34 | +echo "" |
| 35 | +echo "STEP 1: Updating the system" |
| 36 | +echo "===========================" |
| 37 | + |
| 38 | +# disble auto updates |
| 39 | +sudo apt-get update -y |
| 40 | +sudo apt-get upgrade -y |
| 41 | +#sudo apt-get autoremove -y |
| 42 | +#sudo apt-get autoclean -y |
| 43 | +sudo NEEDRESTART_MODE=a apt-get dist-upgrade --yes |
| 44 | + |
| 45 | +# find and replace the variables in the files |
| 46 | +find $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env -type f -exec sed -i "s/__DD_DATABASE_USER__/$USERNAME/g" {} \; |
| 47 | +find $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env -type f -exec sed -i "s/__DD_DATABASE_PASSWORD__/$PASSWORD/g" {} \; |
| 48 | +find $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env -type f -exec sed -i "s/__DD_DATABASE_HOST__/$postgresHostname/g" {} \; |
| 49 | +find $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env -type f -exec sed -i "s/__DD_DATABASE_NAME__/$databaseName/g" {} \; |
| 50 | +find $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env -type f -exec sed -i "s/__DD_DATABASE_PORT__/$databasePort/g" {} \; |
| 51 | + |
| 52 | +# show the files |
| 53 | +cat $HOME/OSS_django-DefectDojo/docker/environments/postgres-redis.env |
| 54 | + |
| 55 | +# pause if needed |
| 56 | +if [ "$doPauses" = "true" ]; then |
| 57 | + read -p "Press enter to continue" |
| 58 | +fi |
| 59 | + |
| 60 | +echo "" |
| 61 | +echo "STEP 2: Installing Docker and Docker Compose" |
| 62 | +echo "============================================" |
| 63 | + |
| 64 | +# install docker and docker compose |
| 65 | +# on Ubuntu 22.04 |
| 66 | +# Add Docker's official GPG key: |
| 67 | +sudo apt-get update -y |
| 68 | +sudo apt-get install ca-certificates curl -y |
| 69 | +sudo install -m 0755 -d /etc/apt/keyrings |
| 70 | +sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc |
| 71 | +sudo chmod a+r /etc/apt/keyrings/docker.asc |
| 72 | + |
| 73 | +# Add the repository to Apt sources: |
| 74 | +echo \ |
| 75 | +"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ |
| 76 | +$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ |
| 77 | +sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
| 78 | +sudo apt-get update -y |
| 79 | + |
| 80 | +# install docker-compose |
| 81 | +sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y |
| 82 | + |
| 83 | +# allow to run docker without sudo |
| 84 | +sudo groupadd docker |
| 85 | +sudo usermod -aG docker $USER |
| 86 | +sudo apt install acl -y |
| 87 | +sudo setfacl -m user:$USER:rw /var/run/docker.sock |
| 88 | +docker run hello-world |
| 89 | + |
| 90 | +# pause if needed |
| 91 | +if [ "$doPauses" = "true" ]; then |
| 92 | + read -p "Press enter to continue" |
| 93 | +fi |
| 94 | + |
| 95 | +echo "" |
| 96 | +echo "STEP 3: Fix hostname and create certificates" |
| 97 | +echo "============================================" |
| 98 | + |
| 99 | +# hostname change |
| 100 | +# set the hostname |
| 101 | +sudo hostnamectl set-hostname $NEWHOSTNAME |
| 102 | +sudo hostnamectl set-hostname "Defect Dojo VM" --pretty |
| 103 | +sudo hostnamectl set-hostname $NEWHOSTNAME --static |
| 104 | +sudo hostnamectl set-hostname $NEWHOSTNAME --transient |
| 105 | + |
| 106 | +# check hostname |
| 107 | +hostnamectl |
| 108 | + |
| 109 | +# create certificates using certbot -- should be done after the DNS is set |
| 110 | +# may need to set dns entry for the hostname |
| 111 | +sudo apt-get update -y |
| 112 | +sudo apt-get install certbot -y |
| 113 | +sudo certbot certonly --standalone --non-interactive -d $NEWHOSTNAME --agree-tos --email $EMAIL |
| 114 | + |
| 115 | +# copy certificates to the nginx folder |
| 116 | +sudo cp /etc/letsencrypt/live/$NEWHOSTNAME/fullchain.pem $nginx_folder/nginx.crt |
| 117 | +sudo cp /etc/letsencrypt/live/$NEWHOSTNAME/privkey.pem $nginx_folder/nginx.key |
| 118 | +ls -ls $nginx_folder |
| 119 | + |
| 120 | +# pause if needed |
| 121 | +if [ "$doPauses" = "true" ]; then |
| 122 | + read -p "Press enter to continue" |
| 123 | +fi |
| 124 | + |
| 125 | +echo "" |
| 126 | +echo "STEP 4: Install Postgresql client and create database" |
| 127 | +echo "=====================================================" |
| 128 | + |
| 129 | +# install postgresql client |
| 130 | +sudo apt update -y |
| 131 | +sudo apt install gnupg2 wget vim -y |
| 132 | +sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' |
| 133 | +sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - |
| 134 | +sudo apt update -y |
| 135 | +sudo apt-get install postgresql-client-16 -y |
| 136 | + |
| 137 | +#psql "postgres://$USERNAME:$PASSWORD@$postgresHostname:5432/postgres" |
| 138 | + |
| 139 | +# create database and user in psql client |
| 140 | + |
| 141 | +# create the database with psql then exit |
| 142 | +echo "CREATE DATABASE defectdojo on $postgresHostname" |
| 143 | +psql "postgres://$USERNAME:$PASSWORD@$postgresHostname:5432/postgres" -c 'create database defectdojo;' |
| 144 | + |
| 145 | +#psql "postgres://$USERNAME:$PASSWORD@$postgresHostname:5432/postgres" -c 'create database defectdojo; quit();' |
| 146 | + |
| 147 | +# pause if needed |
| 148 | +if [ "$doPauses" = "true" ]; then |
| 149 | + read -p "Press enter to continue" |
| 150 | +fi |
| 151 | + |
| 152 | +echo "" |
| 153 | +echo "STEP 5: Install DefectDojo" |
| 154 | +echo "==========================" |
| 155 | + |
| 156 | +rm -f docker-compose.override.yml |
| 157 | +ln -s docker-compose.override.https.yml docker-compose.override.yml |
| 158 | + |
| 159 | +echo "current docker-compose.override.yml" |
| 160 | +cat docker-compose.override.yml |
| 161 | + |
| 162 | +# build and run the docker-compose |
| 163 | +# first time should take a while |
| 164 | +sudo ./dc-build.sh |
| 165 | +#sudo ./dc-up.sh |
| 166 | +sudo ./dc-up-d.sh |
| 167 | + |
| 168 | +# check the logs if your quick |
| 169 | +# look for the Admin user |
| 170 | +# loop until the logs are available |
| 171 | +#docker logs dojo-initializer-1 | grep Admin |
| 172 | + |
| 173 | +# wait for container to be up and migrations to be done |
| 174 | +echo "Waiting for the container to be up and migrations to be done for $waitTime seconds..." |
| 175 | +sleep $waitTime |
| 176 | + |
| 177 | +# pause if needed |
| 178 | +if [ "$doPauses" = "true" ]; then |
| 179 | + read -p "Press enter to continue" |
| 180 | +fi |
| 181 | + |
| 182 | +echo "" |
| 183 | +echo "STEP 6: Create a superuser" |
| 184 | +echo "==========================" |
| 185 | + |
| 186 | +# create a superuser |
| 187 | +#docker exec -it oss_django-defectdojo-uwsgi-1 /bin/bash |
| 188 | +#python manage.py createsuperuser |
| 189 | +#export DJANGO_SUPERUSER_PASSWORD="admin7"; python manage.py createsuperuser --no-input --username admin7 --email admin7@defectdojo.local; |
| 190 | +#docker exec oss_django-defectdojo-uwsgi-1 /bin/bash -c 'export DJANGO_SUPERUSER_PASSWORD="admin9"; python manage.py createsuperuser --no-input --username admin9 --email admin9@defectdojo.local;' |
| 191 | + |
| 192 | +export COMMAND="export DJANGO_SUPERUSER_PASSWORD="$adminPassword"; export DJANGO_SUPERUSER_USERNAME="$adminUser"; export DJANGO_SUPERUSER_EMAIL="$EMAIL"; python manage.py createsuperuser --no-input" |
| 193 | +echo "COMMAND: $COMMAND" |
| 194 | +docker exec oss_django-defectdojo-uwsgi-1 /bin/bash -c "$COMMAND" |
| 195 | + |
| 196 | +# test the application |
| 197 | +# open a browser and go to https://$NEWHOSTNAME:8443/dashboard |
| 198 | +echo "https://$NEWHOSTNAME:8443/dashboard" |
| 199 | + |
| 200 | +# pause if needed |
| 201 | +if [ "$doPauses" = "true" ]; then |
| 202 | + read -p "Press enter to continue" |
| 203 | +fi |
| 204 | + |
| 205 | +echo "" |
| 206 | +echo "STEP 7: Setup systemd service for docker-compose automation" |
| 207 | +echo "===========================================================" |
| 208 | + |
| 209 | +# put back DD_INITIALIZE to false |
| 210 | +rm -f docker-compose.override.yml |
| 211 | +ln -s docker-compose.override.https.initializefalse.yml docker-compose.override.yml |
| 212 | + |
| 213 | +echo "current docker-compose.override.yml" |
| 214 | +cat docker-compose.override.yml |
| 215 | + |
| 216 | +# docker-compose service |
| 217 | +sudo cp $HOME/OSS_django-DefectDojo/systemd/defectdojo-composer.service /etc/systemd/system/ |
| 218 | +sudo systemctl enable defectdojo-composer |
| 219 | + |
| 220 | +echo "" |
| 221 | +echo "STEP 8: Double check there are 5 running containers" |
| 222 | +echo "===================================================" |
| 223 | + |
| 224 | +docker ps |
| 225 | + |
| 226 | +echo "Done!" |
| 227 | +echo "The DefectDojo instance is now available at https://$NEWHOSTNAME:8443/dashboard" |
| 228 | +echo "The superuser is $adminUser with password $adminPassword" |
| 229 | +echo "Don't forget to change the password for the superuser!" |
0 commit comments