#!/bin/bash # # # logit() { # Simple logging function that prepends an easy-to-find marker '=> ' and a timestamp to a message TIMESTAMP=$(date -u +'%m/%d %H:%M:%S') MSG="=> ${TIMESTAMP} $1" echo ${MSG} } set_ssh_key() { if [[ -n "${SSH_KEY}" ]]; then logit "Setting root ssh key" mkdir -p /root/.ssh chmod 700 /root/.ssh echo "$SSH_KEY" > /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys chown -R root:root /root/.ssh fi } set_passwordless_ssh() { logit "Turn off password authentication and root login for SSH" echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config service ssh restart } system_primary_ip() { # returns the primary IP assigned to eth0 ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }' } set_hostname() { logit "Set up hostname" if [[ -n "${HOSTNAME}" ]]; then echo $HOSTNAME > /etc/hostname echo $IPADDR $FQDN $HOSTNAME >> /etc/hosts else system_primary_ip > /etc/hostname echo "$(system_primary_ip) localhost" >> /etc/hosts fi IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://') hostname -F /etc/hostname } postfix_install_loopback_only() { logit "Installing and configuring Postfix" # Installs postfix and configure to listen only on the local interface. Also # allows for local mail delivery echo "postfix postfix/destinations string localhost.localdomain, localhost" | debconf-set-selections echo "postfix postfix/mailname string localhost" | debconf-set-selections echo "postfix postfix/main_mailer_type select Internet Site" | debconf-set-selections echo "postfix postfix/myhostname string localhost" | debconf-set-selections sudo apt-get install -qq -y postfix > /dev/null 2>&1 /usr/sbin/postconf -e "inet_interfaces = loopback-only" #/usr/sbin/postconf -e "local_transport = error:local delivery is disabled" touch /tmp/restart-postfix } notify_install_via_email() { if [[ -n "${NOTIFY_EMAIL}" ]]; then logit "Sending notification email to ${NOTIFY_EMAIL}" /usr/sbin/sendmail "${NOTIFY_EMAIL}" < Your Dokku installation is complete and now ready to be configured: http://$(system_primary_ip) . Please visit this url to complete the setup of your Dokku instance. Enjoy using Dokku! EOD fi } notify_restart_via_email() { if [[ -n "${NOTIFY_EMAIL}" ]]; then logit "Sending notification email to ${NOTIFY_EMAIL} of required restart" /usr/sbin/sendmail "${NOTIFY_EMAIL}" < The following linode instance must be restarted: ${LINODE_LISHUSERNAME} Before restarting, please go to this url: https://manager.linode.com/linodes/dashboard/${LINODE_LISHUSERNAME} Then click "Edit" next to the selected configuration profile and make the following changes: - Change the "Kernel" option to the current "pv-grub" release - Set the "Distro helper" option to "no" Then save your changes. Next, reboot the instance from the Linode Dashboard. You'll receive an email once the instance is available to continue the dokku installation. EOD fi } setup_linode() { logit "Installing via linode" DEBIAN_FRONTEND=noninteractive apt-get install -qq -y linux-virtual DEBIAN_FRONTEND=noninteractive apt-get purge -qq -y grub2 grub-pc DEBIAN_FRONTEND=noninteractive apt-get install -qq -y 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 cp /etc/rc.local /etc/rc.local-bak cat << "EOF" > /etc/rc.local #!/bin/sh -e sudo apt-get update >> /root/setup_linode.txt 2>&1 sudo apt-get install -y linux-image-extra-"$(uname -r)" >> /root/setup_linode.txt 2>&1 modprobe aufs >> /root/setup_linode.txt 2>&1 sudo apt-get install -y apparmor cgroup-lite >> /root/setup_linode.txt 2>&1 sudo apt-get install -y dokku >> /root/setup_linode.txt 2>&1 # Clean up this script so it only runs once rm -f /etc/rc.local mv /etc/rc.local-bak /etc/rc.local exit 0 EOF chmod +x /etc/rc.local } install_prerequisites() { logit "Installing pre-requisites" sudo apt-get update -qq > /dev/null sudo apt-get install -qq -y apt-transport-https curl > /dev/null 2>&1 logit "Installing docker" curl -sSL https://get.docker.com/ | sh logit "Installing dokku" curl -sSL https://packagecloud.io/gpg.key 2> /dev/null | apt-key add - > /dev/null 2>&1 echo "deb https://packagecloud.io/dokku/dokku/ubuntu/ trusty main" > /etc/apt/sources.list.d/dokku.list sudo apt-get update -qq > /dev/null } install_dokku() { logit "Installing pre-requisites" sudo apt-get install -qq -y linux-image-extra-"$(uname -r)" > /dev/null 2>&1 logit "Installing dokku" sudo apt-get install -qq -y dokku > /dev/null 2>&1 logit "Done!" } exec &> /root/stackscript.log set_ssh_key set_passwordless_ssh postfix_install_loopback_only set_hostname install_prerequisites if [[ -n "$LINODE_ID" ]]; then setup_linode notify_restart_via_email else install_dokku notify_install_via_email fi