Skip to content

Instantly share code, notes, and snippets.

@xunleii
Last active July 5, 2022 16:38
Show Gist options
  • Save xunleii/dc2d8abf3bc68d47909b456d50a89497 to your computer and use it in GitHub Desktop.
Save xunleii/dc2d8abf3bc68d47909b456d50a89497 to your computer and use it in GitHub Desktop.
CoreOS on Scaleway (with packer)
{
"builders": [
{
"type": "scaleway",
"api_token": "00000000-0000-0000-0000-000000000000",
"organization_id": "00000000-0000-0000-0000-000000000000",
"region": "par1",
"image_name": "CoreOS",
"server_name": "packer-CoreOS-{{ uuid }}",
"snapshot_name": "coreos-rootfs-{{ uuid }}",
"bootscript": "bf4cfb66-a2f8-4f77-9b1c-665cdfdf208e",
"boottype": null,
"commercial_type": "DEV1-S",
"image": "f974feac-abae-4365-b988-8ec7d1cec10d",
"remove_volume": true,
"ssh_agent_auth": false,
"ssh_username": "root"
}
],
"provisioners": [
{
"destination": "/root/specs.yaml",
"source": "specs.yaml",
"type": "file"
},
{
"inline": [
"apt update",
"apt install -y wget bzip2",
"rm -rf /var/cache/apt/*"
],
"type": "shell"
},
{
"inline": [
"curl -sLo /usr/bin/container-linux-config-transpiler https://github.com/coreos/container-linux-config-transpiler/releases/download/v0.9.0/ct-v0.9.0-x86_64-unknown-linux-gnu",
"chmod +x /usr/bin/container-linux-config-transpiler",
"/usr/bin/container-linux-config-transpiler --in-file /root/specs.yaml > /root/ignition.json"
],
"type": "shell"
},
{
"inline": [
"curl -sLo /usr/bin/coreos-install https://raw.githubusercontent.com/coreos/init/master/bin/coreos-install",
"chmod +x /usr/bin/coreos-install",
"coreos-install -d /dev/vda -C stable -i /root/ignition.json"
],
"type": "shell"
},
{
"inline": [
"echo 'Install Scaleway metadata tools on /opt'",
"mkdir /mnt/root",
"mount /dev/vda9 /mnt/root",
"mkdir -p /mnt/root/opt/{bin,sbin}",
"curl -sLo /mnt/root/opt/bin/scw-metadata https://raw.githubusercontent.com/scaleway/image-tools/cloud-init-18.3+24.gf6249277/bases/overlay-common/usr/local/bin/scw-metadata",
"curl -sLo /mnt/root/opt/bin/scw-metadata-json https://raw.githubusercontent.com/scaleway/image-tools/cloud-init-18.3+24.gf6249277/bases/overlay-common/usr/local/bin/scw-metadata-json",
"curl -sLo /mnt/root/opt/sbin/scw-set-hostname https://raw.githubusercontent.com/scaleway/image-tools/cloud-init-18.3+24.gf6249277/bases/overlay-systemd/usr/local/sbin/scw-set-hostname",
"curl -sLo /mnt/root/opt/sbin/scw-signal-state https://raw.githubusercontent.com/scaleway/image-tools/cloud-init-18.3+24.gf6249277/bases/overlay-common/usr/local/sbin/scw-signal-state",
"curl -sLo /mnt/root/opt/sbin/scw-userdata https://raw.githubusercontent.com/scaleway/image-tools/cloud-init-18.3+24.gf6249277/bases/overlay-common/usr/local/sbin/scw-userdata",
"curl -sLo /mnt/root/opt/sbin/scw-fetch-ssh-keys https://gist.githubusercontent.com/xunleii/dc2d8abf3bc68d47909b456d50a89497/raw/d67153cbf885bb7923d6eb4b235c9adfb7ba510b/scw-fetch-ssh-keys",
"curl -sLo /mnt/root/opt/sbin/scw-coreos-custom-metadata https://gist.githubusercontent.com/xunleii/dc2d8abf3bc68d47909b456d50a89497/raw/d67153cbf885bb7923d6eb4b235c9adfb7ba510b/scw-coreos-custom-metadata",
"chmod +x /mnt/root/opt/{bin,sbin}/*",
"echo 'Add /opt/{bin,sbin} to default Systemd.Units PATH'",
"mkdir -p /mnt/root/etc/systemd/system.conf.d",
"echo -e '[Manager]\\nDefaultEnvironment=PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin' > /mnt/root/etc/systemd/system.conf.d/10-default-env.conf",
"umount /dev/vda9"
],
"inline_shebang": "/bin/bash -e",
"type": "shell"
}
]
}

CoreOS on Scaleway with Packer

This gist use packer to deploy a CoreOS image, compliant with Scaleway:

  • Retrieve and install SSH keys
  • Retrieve some metadata (Public/Private IP, hostname, tags ... available in /run/metadata/coreos)
  • Set hostname using Scaleway instance name
  • Retrieve and use cloudinit configuration (not tested)
  • Send a boot signal to Scaleway

Unfortunately, this image cannot works with Baremetal instances (and not tested with ARM).

Thanks to https://github.com/jbonachera/scaleway-coreos for the main ideas ... but I made this because I prefer using official bash scripts to talk with the metadata server.

#!/bin/sh
SCW_METADATA="/opt/bin/scw-metadata-json"
COREOS_METADATA="/run/metadata/coreos"
${SCW_METADATA} | jq --raw-output \
'[
@text "COREOS_CUSTOM_HOSTNAME=\(.hostname)",
@text "COREOS_CUSTOM_PRIVATE_IPV4=\(.private_ip)",
@text "COREOS_CUSTOM_PUBLIC_IPV4=\(.public_ip.address)",
@text "COREOS_CUSTOM_ZONE_ID=\(.location.zone_id)"
] + [.tags[] | split("=") | @text "COREOS_CUSTOM_TAG_\(.[0] | ascii_upcase)=\(.[1])"]
| join("\n")' | sed 's/null//g' > ${COREOS_METADATA}
#!/bin/sh
SCW_METADATA="/opt/bin/scw-metadata-json"
USER=$1
SSH_PUBLIC_KEYS="/home/${USER}/.ssh/authorized_keys.d/scw-public-keys"
su -c "mkdir -p '$(dirname ${SSH_PUBLIC_KEYS})'" ${USER}
su -c "${SCW_METADATA} | jq --raw-output '.ssh_public_keys[].key' > '${SSH_PUBLIC_KEYS}'" ${USER}
if [ -s "${SSH_PUBLIC_KEYS}" ]
then
update-ssh-keys -u ${USER}
else
echo >&2 "Failed to fetch public SSH keys... Abort"
exit 1
fi
---
passwd:
users:
- name: core
groups:
- sudo
- docker
systemd:
units:
- name: metadata.target
enabled: true
contents: |
[Unit]
Description=Metadata injection for Scaleway
Requires=network-online.target
After=network.target network-online.target
[Install]
WantedBy=basic.target
- name: [email protected]
enabled: true
dropins:
- name: inject-sshkeys.conf
contents: |
[Service]
ExecStart=
ExecStart=/opt/sbin/scw-fetch-ssh-keys %i
- name: coreos-metadata.service
enabled: true
dropins:
- name: inject-metadata.conf
contents: |
[Service]
ExecStart=
ExecStart=/opt/sbin/scw-coreos-custom-metadata
- name: [email protected]
enabled: true
dropins:
- name: fetch-userdata.conf
contents: |
[Service]
ExecStartPre=/opt/sbin/scw-userdata cloud-init > %i
- name: system-set-hostname.service
enabled: true
contents: |
[Unit]
Description=Set CoreOS hostname from metadata on first boot
Requires=network-online.target
After=network.target network-online.target
[Service]
Type=oneshot
ExecStart=/opt/sbin/scw-set-hostname
[Install]
WantedBy=basic.target
- name: scw-signal-booted.service
enabled: true
contents: |
[Unit]
Description=Send signal successful boot to Scaleway
Requires=network-online.target
After=network.target network-online.target
[Service]
Type=oneshot
ExecStart=/opt/sbin/scw-signal-state booted
[Install]
WantedBy=basic.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment