refactor: update digitalocean image building to match what Digitalocean does

The scripts run/installed on the server are based on what was given to me by Digitalocean in 2019, so they may have diverged a bit since.
This commit is contained in:
Jose Diaz-Gonzalez
2023-02-16 14:19:10 -05:00
parent eba867a3bb
commit 96c737eae4
13 changed files with 333 additions and 32 deletions

View File

@@ -0,0 +1,22 @@
#!/bin/sh
#
# Configured as part of the DigitalOcean 1-Click Image build process
myip=$(hostname -I | awk '{print$1}')
cat <<EOF
********************************************************************************
Welcome to DigitalOcean's One-Click Dokku Droplet.
To keep this Droplet secure, the UFW firewall is enabled.
All ports are BLOCKED except 22 (SSH), 80 (HTTP), 443 (HTTPS),
2375 (Docker) and 2376 (Docker).
In a web browser, you can view:
* The Dokku One-Click Quickstart guide: http://do.co/dokku1804#start
* Your Dokku setup page: http://$myip
For help and more information, visit http://do.co/dokku1804
********************************************************************************
To delete this message of the day: rm -rf $(readlink -f ${0})
EOF

View File

@@ -0,0 +1,25 @@
#!/bin/bash -x
set +e
exec > >(tee /var/log/one_click_setup.log) 2>&1
ufw --force enable
printf "dokku dokku/hostname string %s" hostname | debconf-set-selections
export DEBIAN_FRONTEND=noninteractive
export LANG=C
export LC_ALL=C
# Install Dokku....
for count in {1..30}; do
dpkg -i /var/lib/digitalocean/debs/*deb || /bin/true
apt-get -f install || /bin/true
version=$(dpkg-query --showformat='${Version}' -W dokku)
if [[ -n "$version" ]]; then
ran=0
exit 0
else
count=$(($count + 1))
fi
sleep 5
done

View File

@@ -0,0 +1,9 @@
#!/bin/bash
exec > >(tee /var/log/one_click_setup.log) 2>&1
# Remove the force command
sed -e '/Match user root/d' \
-e '/.*ForceCommand.*droplet.*/d' \
-i /etc/ssh/sshd_config
systemctl restart ssh

View File

@@ -0,0 +1,43 @@
#!/bin/bash
set -eo pipefail
export DEBIAN_FRONTEND=noninteractive
pkgs=(apt-transport-https
ca-certificates
curl
jq
linux-image-extra-virtual
software-properties-common
)
echo '--> Updating apt repositories'
apt-get -qqy update
echo "--> Installing basic dependencies: ${pkgs[*]}"
apt-get -qqy install "${pkgs[@]}"
echo '--> Setting up docker apt repository'
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
cat >/etc/apt/sources.list.d/docker.list <<EOM
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -c -s) stable
EOM
echo '--> Installing docker-ce'
apt-get -y update
apt-get -y install docker-ce docker-compose-plugin
echo '--> Enabling docker-ce'
systemctl enable docker
systemctl start docker
echo '--> Enabling docker live-restore'
if [[ ! -f /etc/docker/daemon.json ]]; then
echo "{}" >/etc/docker/daemon.json
fi
config="$(jq '. + {"live-restore": true}' /etc/docker/daemon.json)"
echo "$config" >/etc/docker/daemon.json
systemctl reload docker
docker info | grep -i live

View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -eo pipefail
echo '--> Prevent login until after setup is complete'
# Be a bit of a dork about login in
cat >>/etc/ssh/sshd_config <<EOM
Match User root
ForceCommand echo "Please wait while we get your droplet ready..."
EOM

View File

@@ -0,0 +1,64 @@
#!/bin/bash
set -eo pipefail
export DEBIAN_FRONTEND=noninteractive
pkgs=(apache2-utils
bash-completion
locales
git
cpio
curl
man-db
netcat
net-tools
software-properties-common
parallel
python3-software-properties
rsync
rsyslog
dos2unix
jq
unzip
nginx
dnsutils
cgroupfs-mount
cgroup-lite
sudo
python3
debconf
)
dokku_pkgs=(docker-image-labeler
gliderlabs-sigil
lambda-builder
netrc
plugn
procfile-util
sshcommand
)
echo '--> Install core dependencies for dokku'
apt-get -y install "${pkgs[@]}"
echo '--> Setting up dokku apt repository'
curl -fsSL https://packagecloud.io/dokku/dokku/gpgkey -o /etc/apt/keyrings/dokku.asc
cat >/etc/apt/sources.list.d/dokku.list <<EOM
deb [arch=amd64 signed-by=/etc/apt/keyrings/dokku.asc] https://packagecloud.io/dokku/dokku/ubuntu/ bionic main
EOM
echo '--> Install dependencies for dokku from new repository'
apt-get -y clean
apt-get -y update
apt-get -y install "${dokku_pkgs[@]}"
echo '--> Pre-cache dokku bits that are must run on boot'
apt-get -y clean
apt-get -y install --download-only "dokku=${DOKKU_VERSION}" dokku-event-listener herokuish
mkdir -p /var/lib/digitalocean/debs
cp -au /var/cache/apt/archives/*deb /var/lib/digitalocean/debs

View File

@@ -0,0 +1,8 @@
#!/bin/bash
set -eo pipefail
echo '--> Update grub boot loader'
sed -e 's|GRUB_CMDLINE_LINUX="|GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1|g' \
-i /etc/default/grub
update-grub

View File

@@ -0,0 +1,6 @@
#!/bin/bash
set -eo pipefail
echo '--> Enable Google DNS for Docker'
sed -e 's|#DOCKER_OPTS|DOCKER_OPTS|g' \
-i /etc/default/docker

View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -eo pipefail
echo '--> Enable and configure UFW'
sed -e 's|DEFAULT_FORWARD_POLICY=.*|DEFAULT_FORWARD_POLICY="ACCEPT"|g' \
-i /etc/default/ufw
ufw allow ssh
ufw allow http
ufw allow https
ufw allow 2375/tcp
ufw allow 2376/tcp

View File

@@ -0,0 +1,20 @@
#!/bin/bash
set -eo pipefail
echo '--> Write the application info'
application_version="$DOKKU_VERSION"
build_date="$(date -I)"
distro_arch="$(uname -m)"
distro_codename="$(lsb_release -sc)"
distro_release="$(lsb_release -sr)"
distro="$(lsb_release -si)"
cat >/var/lib/digitalocean/application.info <<EOM
appiication_name="Dokku"
build_date="${build_date}"
distro="${distro}"
distro_release="${distro_release}"
distro_codename="${distro_codename}"
distro_arch="${distro_arch}"
application_version="${application_version}"
EOM

View File

@@ -0,0 +1,19 @@
#!/bin/bash
set -eo pipefail
echo '--> Cleaning up'
rm -rf /tmp/* /var/tmp/*
history -c
cat /dev/null > /root/.bash_history
unset HISTFILE
apt-get -y autoremove
apt-get -y autoclean
rm -rf /var/lib/cloud/instances/*
echo '--> Removing keys'
rm -f /root/.ssh/authorized_keys /etc/ssh/*key*
echo '--> Clearing log files'
find /var/log -mtime -1 -type f -exec truncate -s 0 {} \;
rm -rf /var/log/*.gz /var/log/*.[0-9] /var/log/*-????????
cat /dev/null > /var/log/lastlog; cat /dev/null > /var/log/wtmp

View File

@@ -0,0 +1,96 @@
packer {
required_plugins {
digitalocean = {
version = ">= 1.0.4"
source = "github.com/digitalocean/digitalocean"
}
}
}
variable "source_image" {
type = string
default = "ubuntu-22-04-x64"
}
variable "dokku_version" {
type = string
}
source "digitalocean" "ubuntu" {
image = var.source_image
region = "nyc3"
size = "s-2vcpu-2gb"
ssh_username = "root"
snapshot_name = format("dokku-%s-%s", var.dokku_version, formatdate("YYYYMMDDhhmm", timestamp()))
}
build {
name = "dokku"
sources = [
"source.digitalocean.ubuntu"
]
provisioner "shell" {
inline = [
"echo '--> Waiting until cloud-init is complete'",
"/usr/bin/cloud-init status --wait",
]
}
provisioner "file" {
source = "${path.root}/files/etc/update-motd.d/99-one-click"
destination = "/etc/update-motd.d/99-one-click"
}
provisioner "file" {
source = "${path.root}/files/var/lib/cloud/scripts/per-once/001_setup"
destination = "/var/lib/cloud/scripts/per-once/001_setup"
}
provisioner "file" {
source = "${path.root}/files/var/lib/cloud/scripts/per-once/002_enable_ssh"
destination = "/var/lib/cloud/scripts/per-once/002_enable_ssh"
}
provisioner "shell" {
script = "${path.root}/in_parts/011-docker"
}
provisioner "shell" {
script = "${path.root}/in_parts/011-ssh-message"
}
provisioner "shell" {
environment_vars = [
"DOKKU_VERSION=${var.dokku_version}"
]
script = "${path.root}/in_parts/012-dokku-packages"
}
provisioner "shell" {
script = "${path.root}/in_parts/012-grub-opts"
}
provisioner "shell" {
script = "${path.root}/in_parts/014-docker-dns"
}
provisioner "shell" {
script = "${path.root}/in_parts/014-ufw-rules"
}
provisioner "shell" {
environment_vars = [
"DOKKU_VERSION=${var.dokku_version}"
]
script = "${path.root}/in_parts/099-application_tag"
}
provisioner "shell" {
script = "${path.root}/in_parts/099-cleanup"
}
}
post-processor "manifest" {
output = "digitalocean-manifest.json"
}

View File

@@ -1,32 +0,0 @@
{
"builders": [
{
"type": "digitalocean",
"region": "nyc3",
"image": "ubuntu-14-04-x64"
}
],
"provisioners": [
{
"type": "file",
"source": "deb.mk",
"destination": "/tmp/Makefile"
},
{
"type": "shell",
"inline": [
"echo '--> Waiting 30 seconds for core services to be available'",
"sleep 30",
"echo '--> Updating apt repositories'",
"sudo apt-get update -qq >/dev/null",
"echo '--> Installing make requirement'",
"sudo apt-get -qq -y --no-install-recommends install build-essential",
"cd /tmp && make install-from-deb",
"rm /root/.ssh/authorized_keys"
]
}
]
}