Skip to content

Instantly share code, notes, and snippets.

@sramam
Created January 20, 2022 16:13
Show Gist options
  • Save sramam/8ee8fd5541c09c76fa753bb7cbfa15d0 to your computer and use it in GitHub Desktop.
Save sramam/8ee8fd5541c09c76fa753bb7cbfa15d0 to your computer and use it in GitHub Desktop.
deployment: Dokku, Azure, node.js from the shell
# create an SSH key-pair
# Constants: make sure this is identical to the block midway in the file.
REGION=eastus
RG=acuity-trial
VM=nv
APPNAME=nvs
USERNAME=sramam
EMAIL=sramam@gmail.com
PORT=1234
# used by localhost to ssh to $VM once created
SSH_AZ=~/.ssh/id_rsa.${RG}.${VM}
# used by dokky
SSH_DOKKU=~/.ssh/id_rsa.${RG}.${VM}.dokku
echo "\n# Generating SSH key-pair to manage Azure VM"
ssh-keygen -m PEM -t rsa -b 4096 -C $EMAIL -f $SSH_AZ
echo "\n# We also need to generate an SSH key-pair for dokku"
ssh-keygen -m PEM -t rsa -b 4096 -C "dokku@${DNS}" -f $SSH_DOKKU
echo "\n# Creating Azure Resource Group ${RG} in ${REGION}"
az group create --name $RG --location $REGION
echo "\n# Create the Azure VM"
az vm create \
--resource-group $RG \
--name $VM \
--image UbuntuLTS\
--admin-username $USERNAME \
--public-ip-sku Standard \
--ssh-key-value ${SSH_AZ}.pub
echo "\n# Configure VM to auto-shutdown at 9:00 pm PST"
# auto-shutdown the VM. time is HHMM in UTC. 0500 => 9:00pm PST
az vm auto-shutdown -g $RG -n $VM --time 0500 --email "$EMAIL"
IP=`az vm show -d -g $RG -n $VM --query publicIps -o tsv`
DNS=$IP.sslip.io
echo "\n# Copy dokku ssk public key to VM"
cat ${SSH_DOKKU}.pub | ssh ${USERNAME}@$IP "sudo sshcommand acl-add dokku custom-identifier"
# scp -i $SSH_AZ ${SSH_DOKKU}.pub ${USERNAME}@${IP}:${SSH_DOKKU}.pub
##############
# <VM-SHELL>
echo "\n\n# SSH into the VM to configure it"
## ssh into the VM and prepare it to be a Dokku server
ssh -i $SSH_AZ ${USERNAME}@${IP}
sudo apt-get update
sudo apt-get upgrade
# allow package manager to install dependencies over HTTPS when it is available.
sudo apt-get -qq -y --no-install-recommends install apt-transport-https
# Install Dokku
wget https://raw.githubusercontent.com/dokku/dokku/v0.26.6/bootstrap.sh; sudo DOKKU_TAG=v0.26.6 bash bootstrap.sh
# create ProcFile
# echo "web: npm run dev" > Procfile
# Constants: make sure this is identical to the block at the head of this file
REGION=eastus
RG=acuity-trial
VM=nv
APPNAME=nvs
USERNAME=sramam
EMAIL=sramam@gmail.com
PORT=1234
IP=`curl ipinfo.io/ip`
DNS=$IP.sslip.io
# The Azure ubuntu LTS 20.x image seems to have node@8.x
# Let's fix that.
# curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# nvm install 16
# nvm use 16
cat ~/.ssh/authorized_keys | sudo dokku ssh-keys:add dokku
dokku domains:set-global $DNS
dokku apps:create $APPNAME
# configure dokku to run in development mode
dokku config:set $APPNAME NPM_CONFIG_PRODUCTION=false
dokku config:set $APPNAME YARN_PRODUCTION=false
dokku config:set $APPNAME NODE_ENV=dev
dokku config:set $APPNAME UNSPLASH_ACCESS_KEY=$UNSPLASH
# letsencrypt setup
# DOKKU's lets encryt plugin requires a server to be responding on port 80 to work.
# Before we start any dokky apps, we'll start a simple static python server on port 80,
# get our SSL cert and shut it down. Leaving port 80 open seems like a bad idea.
# pushd .
# mkdir -p /tmp/le
# pushd /tmp/le
# nohup sudo python -m SimpleHTTPServer 80 > /tmp/le/server.log 2>&1 &
# serverPID=$!
# popd
# sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
# dokku config:set --no-restart $APPNAME DOKKU_LETSENCRYPT_EMAIL=$EMAIL
# dokku letsencrypt:enable $APPNAME
# dokku letsencrypt:cron-job --add $APPNAME
# kill $serverPID
# popd
# add github to known hosts, since we need `npm install` from
# `github/tufan-io/noun_and_verb` for this to work.
sudo dokku plugin:install https://github.com/cedricziel/dokku-hostkeys-plugin.git hostkeys-keys
dokku hostkeys:shared:autoadd github.com
# inform dokku of the port we will be working with
dokku proxy:ports-add $APPNAME http:80:$PORT
# drop out of ssh back to localhost
~.
# </VM-SHELL>
##############
git clone https://github.com/acuity-sr/nv-shopping-cart
cd nv-shopping-cart
npm install
echo "web: npm run dokku" > Procfile
# we also need to setup ~/.ssh/config for git to use SSH_DOKKU to push
# this only needs to be done once.
# assumes a global install of jq and sponge on localhost
jq -r '.scripts.dokku |= "micro-dev -p ${PORT}"' package.json | sponge package.json
echo "{}" > app.json
jq -r '.scripts.dokku.predeploy |= "npm run build && npx prisma generate"' app.json | sponge app.json
# also setup dokku in the ~/.ssh/config file, so git can use the dokku credentials
# while interacting with dokku, like so:
# Host dokku
# Hostname 52.146.26.31.sslip.io
# IdentityFile ~/.ssh/id_rsa.nv.dokku
# IdentitiesOnly yes
az vm open-port --name $VM -g $RG --port $PORT
git remote add dokku dokku@dokku.acuity.2:${APPNAME}
git remote -v
git push dokku main:master
# az group delete --name $RG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment