2022-07-27 16:38:33 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
set -exu
|
|
|
|
cd "$(dirname "$0")"
|
2022-07-27 16:38:33 +00:00
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
LXD_HOSTNAME="${1:-}"
|
2022-07-27 16:38:33 +00:00
|
|
|
|
|
|
|
# generate the custom cloud-init file. Cloud init installs and configures sshd
|
2023-02-01 19:44:05 +00:00
|
|
|
SSH_AUTHORIZED_KEY=$(<"$SSH_PUBKEY_PATH")
|
2022-07-27 16:38:33 +00:00
|
|
|
eval "$(ssh-agent -s)"
|
|
|
|
ssh-add "$SSH_HOME/id_rsa"
|
|
|
|
export SSH_AUTHORIZED_KEY="$SSH_AUTHORIZED_KEY"
|
|
|
|
|
2022-08-20 21:44:37 +00:00
|
|
|
export FILENAME="$LXD_HOSTNAME.yml"
|
2022-08-24 14:09:09 +00:00
|
|
|
mkdir -p "$PROJECT_PATH/cloud-init"
|
|
|
|
YAML_PATH="$PROJECT_PATH/cloud-init/$FILENAME"
|
2022-07-27 16:38:33 +00:00
|
|
|
|
|
|
|
# If we are deploying the www, we attach the vm to the underlay via macvlan.
|
|
|
|
cat > "$YAML_PATH" <<EOF
|
2022-05-20 15:06:41 +00:00
|
|
|
config:
|
2022-12-12 22:16:47 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
|
|
if [ "$VIRTUAL_MACHINE" = www ]; then
|
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
limits.cpu: "${WWW_SERVER_CPU_COUNT}"
|
|
|
|
limits.memory: "${WWW_SERVER_MEMORY_MB}MB"
|
2022-07-27 16:38:33 +00:00
|
|
|
|
|
|
|
EOF
|
|
|
|
|
2022-12-12 22:16:47 +00:00
|
|
|
else [ "$VIRTUAL_MACHINE" = btcpayserver ];
|
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
limits.cpu: "${BTCPAY_SERVER_CPU_COUNT}"
|
|
|
|
limits.memory: "${BTCPAY_SERVER_MEMORY_MB}MB"
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
if [ "$LXD_HOSTNAME" = "$BASE_IMAGE_VM_NAME" ]; then
|
2022-07-27 16:38:33 +00:00
|
|
|
# this is for the base image only...
|
|
|
|
cat >> "$YAML_PATH" <<EOF
|
2022-05-20 15:06:41 +00:00
|
|
|
user.vendor-data: |
|
|
|
|
#cloud-config
|
|
|
|
apt_mirror: http://us.archive.ubuntu.com/ubuntu/
|
|
|
|
package_update: true
|
|
|
|
package_upgrade: false
|
|
|
|
package_reboot_if_required: false
|
|
|
|
|
|
|
|
preserve_hostname: false
|
2023-02-01 19:44:05 +00:00
|
|
|
fqdn: ${BASE_IMAGE_VM_NAME}
|
|
|
|
|
2022-05-20 15:06:41 +00:00
|
|
|
packages:
|
|
|
|
- curl
|
|
|
|
- ssh-askpass
|
|
|
|
- apt-transport-https
|
|
|
|
- ca-certificates
|
|
|
|
- gnupg-agent
|
|
|
|
- software-properties-common
|
|
|
|
- lsb-release
|
|
|
|
- net-tools
|
|
|
|
- htop
|
|
|
|
- rsync
|
|
|
|
- duplicity
|
|
|
|
- sshfs
|
|
|
|
- fswatch
|
|
|
|
- jq
|
|
|
|
- git
|
|
|
|
- nano
|
2022-05-24 18:15:39 +00:00
|
|
|
- wait-for-it
|
2022-06-22 17:42:51 +00:00
|
|
|
- dnsutils
|
|
|
|
- wget
|
|
|
|
|
2022-05-20 15:06:41 +00:00
|
|
|
groups:
|
|
|
|
- docker
|
|
|
|
|
|
|
|
users:
|
|
|
|
- name: ubuntu
|
2022-06-22 17:42:51 +00:00
|
|
|
groups: docker
|
2022-05-20 15:06:41 +00:00
|
|
|
shell: /bin/bash
|
|
|
|
lock_passwd: false
|
2022-06-22 17:42:51 +00:00
|
|
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
2022-05-20 15:06:41 +00:00
|
|
|
ssh_authorized_keys:
|
|
|
|
- ${SSH_AUTHORIZED_KEY}
|
|
|
|
|
|
|
|
write_files:
|
|
|
|
- path: /etc/ssh/ssh_config
|
|
|
|
content: |
|
2022-06-22 17:42:51 +00:00
|
|
|
Port 22
|
|
|
|
ListenAddress 0.0.0.0
|
|
|
|
Protocol 2
|
|
|
|
ChallengeResponseAuthentication no
|
|
|
|
PasswordAuthentication no
|
|
|
|
UsePAM no
|
|
|
|
LogLevel INFO
|
2022-05-20 15:06:41 +00:00
|
|
|
|
2023-03-06 16:47:27 +00:00
|
|
|
- path: /etc/docker/daemon.json
|
|
|
|
content: |
|
|
|
|
{
|
|
|
|
"registry-mirrors": ["${REGISTRY_URL}"]
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:06:41 +00:00
|
|
|
runcmd:
|
2023-03-03 13:59:52 +00:00
|
|
|
- sudo mkdir -m 0755 -p /etc/apt/keyrings
|
|
|
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
|
|
|
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
|
|
|
|
- sudo apt-get update
|
|
|
|
- sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
2022-05-20 15:06:41 +00:00
|
|
|
- sudo apt-get install -y openssh-server
|
2022-12-25 18:04:20 +00:00
|
|
|
|
2022-07-27 16:38:33 +00:00
|
|
|
EOF
|
2023-03-03 13:59:52 +00:00
|
|
|
|
2023-03-06 16:47:27 +00:00
|
|
|
# - path: /etc/docker/daemon.json
|
|
|
|
# content: |
|
|
|
|
# {
|
|
|
|
# "registry-mirrors": "${REGISTRY_URL}",
|
|
|
|
# "labels": "githead=${LATEST_GIT_COMMIT}"
|
|
|
|
# }
|
|
|
|
#"labels": [githead="${LATEST_GIT_COMMIT}"]
|
2023-03-03 13:59:52 +00:00
|
|
|
|
|
|
|
# apt:
|
|
|
|
# sources:
|
|
|
|
# docker.list:
|
|
|
|
# source: "deb [arch=amd64] https://download.docker.com/linux/ubuntu ${LXD_UBUNTU_BASE_VERSION} stable"
|
|
|
|
# keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
|
|
|
|
|
2023-03-06 16:47:27 +00:00
|
|
|
|
2023-03-03 13:59:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# - sudo apt-get update
|
|
|
|
#- sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
2022-07-27 16:38:33 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
# all other machines.
|
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
user.vendor-data: |
|
|
|
|
#cloud-config
|
|
|
|
apt_mirror: http://us.archive.ubuntu.com/ubuntu/
|
|
|
|
package_update: false
|
|
|
|
package_upgrade: false
|
|
|
|
package_reboot_if_required: false
|
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
preserve_hostname: true
|
2022-07-27 16:38:33 +00:00
|
|
|
fqdn: ${FQDN}
|
|
|
|
|
2022-08-19 19:30:30 +00:00
|
|
|
user.network-config: |
|
|
|
|
version: 2
|
|
|
|
ethernets:
|
|
|
|
enp5s0:
|
|
|
|
dhcp4: true
|
|
|
|
match:
|
|
|
|
macaddress: ${MAC_ADDRESS_TO_PROVISION}
|
|
|
|
set-name: enp5s0
|
|
|
|
|
|
|
|
enp6s0:
|
2022-08-20 21:42:38 +00:00
|
|
|
dhcp4: false
|
|
|
|
EOF
|
|
|
|
|
2022-08-24 14:11:50 +00:00
|
|
|
if [[ "$LXD_HOSTNAME" = $WWW_HOSTNAME-* ]]; then
|
2022-08-20 21:42:38 +00:00
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
addresses: [10.139.144.5/24]
|
|
|
|
nameservers:
|
|
|
|
addresses: [10.139.144.1]
|
2022-08-24 14:11:50 +00:00
|
|
|
|
2022-07-27 16:38:33 +00:00
|
|
|
EOF
|
2022-08-20 21:42:38 +00:00
|
|
|
fi
|
2022-05-20 15:06:41 +00:00
|
|
|
|
2022-08-24 14:11:50 +00:00
|
|
|
if [[ "$LXD_HOSTNAME" = $BTCPAY_HOSTNAME-* ]]; then
|
2022-08-20 21:42:38 +00:00
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
addresses: [10.139.144.10/24]
|
|
|
|
nameservers:
|
|
|
|
addresses: [10.139.144.1]
|
|
|
|
|
|
|
|
EOF
|
|
|
|
fi
|
2022-07-27 16:38:33 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If we are deploying the www, we attach the vm to the underlay via macvlan.
|
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
description: Default LXD profile for ${FILENAME}
|
2022-05-20 15:06:41 +00:00
|
|
|
devices:
|
|
|
|
root:
|
|
|
|
path: /
|
2023-02-01 19:44:05 +00:00
|
|
|
pool: ss-base
|
2022-05-20 15:06:41 +00:00
|
|
|
type: disk
|
|
|
|
config:
|
|
|
|
source: cloud-init:config
|
|
|
|
type: disk
|
2022-07-27 16:38:33 +00:00
|
|
|
EOF
|
|
|
|
|
2022-08-24 14:11:50 +00:00
|
|
|
# Stub out the network piece for the base image.
|
2023-02-01 19:44:05 +00:00
|
|
|
if [ "$LXD_HOSTNAME" = "$BASE_IMAGE_VM_NAME" ] ; then
|
2022-07-27 16:38:33 +00:00
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
#
|
2022-07-27 16:38:33 +00:00
|
|
|
cat >> "$YAML_PATH" <<EOF
|
2023-02-01 19:44:05 +00:00
|
|
|
enp6s0:
|
|
|
|
name: enp6s0
|
|
|
|
network: lxdbr0
|
2022-07-27 16:38:33 +00:00
|
|
|
type: nic
|
2022-08-15 13:35:01 +00:00
|
|
|
name: ${FILENAME}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
else
|
2023-02-01 19:44:05 +00:00
|
|
|
# If we are deploying a VM that attaches to the network underlay.
|
2022-08-15 13:35:01 +00:00
|
|
|
cat >> "$YAML_PATH" <<EOF
|
|
|
|
enp5s0:
|
|
|
|
nictype: macvlan
|
|
|
|
parent: ${DATA_PLANE_MACVLAN_INTERFACE}
|
2022-07-27 16:38:33 +00:00
|
|
|
type: nic
|
2022-08-19 19:30:30 +00:00
|
|
|
enp6s0:
|
|
|
|
name: enp6s0
|
2023-03-02 14:46:17 +00:00
|
|
|
network: lxdbr0
|
2022-08-19 19:30:30 +00:00
|
|
|
type: nic
|
2022-08-15 13:35:01 +00:00
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
name: ${PRIMARY_DOMAIN}
|
2022-07-27 16:38:33 +00:00
|
|
|
EOF
|
|
|
|
|
2022-08-15 13:35:01 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-27 16:38:33 +00:00
|
|
|
# let's create a profile for the BCM TYPE-1 VMs. This is per VM.
|
2022-08-20 21:44:37 +00:00
|
|
|
if ! lxc profile list --format csv | grep -q "$LXD_HOSTNAME"; then
|
|
|
|
lxc profile create "$LXD_HOSTNAME"
|
2022-12-12 22:16:47 +00:00
|
|
|
fi
|
2023-02-01 19:44:05 +00:00
|
|
|
|
|
|
|
# configure the profile with our generated cloud-init.yml file.
|
|
|
|
cat "$YAML_PATH" | lxc profile edit "$LXD_HOSTNAME"
|