Skip to content

Instantly share code, notes, and snippets.

@turley
Last active March 4, 2021 19:57
Show Gist options
  • Save turley/11088958 to your computer and use it in GitHub Desktop.
Save turley/11088958 to your computer and use it in GitHub Desktop.
Linode StackScript for creating a Dokku server
#!/bin/bash
# <UDF name="hostname" label="The hostname for the new Linode" example="apps">
# <UDF name="fqdn" label="The new Linode's Fully Qualified Domain Name" example="apps.example.com">
# <UDF name="adminuser" label="Username for new admin user (cannot be dokku)">
# <UDF name="adminpass" label="Password for new admin user">
# <UDF name="adminkey" label="SSH public key authorized for admin user (password SSH auth will be disabled)">
# <UDF name="dokkukey" label="SSH public key authorized for dokku user (used when deploying apps)">
# Turn off password authentication and root login for SSH
echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
service ssh restart
# Set up admin user
if [ -n "$ADMINUSER" ] && [ -n "$ADMINPASS" ]; then
useradd $ADMINUSER -s /bin/bash -p `mkpasswd $ADMINPASS`
usermod -a -G admin $ADMINUSER
if [ -n "$ADMINKEY" ]; then
mkdir -p /home/$ADMINUSER/.ssh
chmod 700 /home/$ADMINUSER/.ssh
echo "$ADMINKEY" > /home/$ADMINUSER/.ssh/authorized_keys
chmod 600 /home/$ADMINUSER/.ssh/authorized_keys
chown -R $ADMINUSER:$ADMINUSER /home/$ADMINUSER/.ssh
fi
fi
# Workaround for apt-get IPv6 issue
[[ `lsb_release -sr` == "12.04" ]] && echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf
# Update system and install some dependencies
apt-get update
apt-get -y upgrade
apt-get -y install ufw fail2ban lxc wget bsdtar git man
# Set up hostname
IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://')
echo $HOSTNAME > /etc/hostname
hostname -F /etc/hostname
echo $IPADDR $FQDN $HOSTNAME >> /etc/hosts
# Prepare system for pv-grub kernel
DEBIAN_FRONTEND=noninteractive apt-get -y install linux-virtual
DEBIAN_FRONTEND=noninteractive apt-get -y purge grub2 grub-pc
DEBIAN_FRONTEND=noninteractive apt-get -y install grub
mkdir -p /boot/grub
update-grub -y
sed -i 's/kopt=root=UUID=.* ro/kopt=root=\/dev\/xvda console=hvc0 ro quiet/g' /boot/grub/menu.lst
sed -i 's/# groot=(hd0,0)/# groot=(hd0)/g' /boot/grub/menu.lst
update-grub
# Install dokku with a startup script on the next boot (after we've switched to the pv-grub kernel)
cat << EOF > /etc/init.d/install_dokku
#!/bin/bash
case "\$1" in
start)
[ -n "$ADMINUSER" ] && rm -f /home/$ADMINUSER/ready-for-kernel-change.txt
# Dokku dependencies
apt-get update
apt-get -y install lxc wget bsdtar linux-image-extra-\$(uname -r)
modprobe aufs
# Install dokku
wget -qO- https://raw.github.com/progrium/dokku/master/bootstrap.sh | sudo bash
# Installation on 14.04 seems to fail the first time, but succeed on a retry
test -d dokku || wget -qO- https://raw.github.com/progrium/dokku/master/bootstrap.sh | sudo bash
# Set up SSH access for dokku user
[ -n "$DOKKUKEY" ] && echo "$DOKKUKEY" | /usr/local/bin/sshcommand acl-add dokku deploy
echo "$FQDN" > /home/dokku/VHOST
chown dokku:dokku /home/dokku/VHOST
# Clean up this script so it only runs once
update-rc.d -f install_dokku remove
rm -f /etc/init.d/install_dokku
;;
stop)
;;
esac
EOF
chmod +x /etc/init.d/install_dokku
update-rc.d install_dokku defaults 97 03
# Indicate to admin user that we're ready for kernel change
# (Is there any way to do this automatically with StackScripts?)
[ -n "$ADMINUSER" ] && echo 'Change this Linode configuration to use the pv-grub kernel, disable the "Xenify distro" option, and reboot (from the Linode dashboard!) to finish installation.' > /home/$ADMINUSER/ready-for-kernel-change.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment