Skip to content

Instantly share code, notes, and snippets.

@Sovenger
Last active February 21, 2017 04:58
Show Gist options
  • Save Sovenger/997ff197c807c0777926 to your computer and use it in GitHub Desktop.
Save Sovenger/997ff197c807c0777926 to your computer and use it in GitHub Desktop.
This is a cloud-config example from a working PXE boot to disk installation for CoreOS
#cloud-config
#Credit: many portions of this config came from the great work detailed here by:
#Behner, brianclements, Metamogul and especially crawford for the cloud-config inside a cloud-config idea:
#https://github.com/coreos/coreos-cloudinit/issues/205
ssh_authorized_keys:
- ssh-rsa <core user key goes here>
#This writes a cloud-config to the PXE booted system that
#we will use to install the machine to disk
write_files:
- path: /run/cloud-config.yml
permissions: '0644'
content: |
#cloud-config
ssh_authorized_keys:
- ssh-rsa <core user key goes here>
#Add addition users you need to log into the box.
#We have one set here for ease of testing
#users:
#- name: testuser
#passwd: <create a hash of the password and put it here>
#groups:
#- sudo
#- systemd-journal
write_files:
- path: /etc/environment
permissions: 0644
owner: root
content: |
COREOS_PUBLIC_IPV4=$_public_ipv4
COREOS_PRIVATE_IPV4=$_public_ipv4
coreos:
update:
reboot-strategy: etcd-lock
etcd:
# generate a new token for each unique cluster from https://discovery.etcd.io/new?size=2
#Ensure its a size 2 token so you can test a cluster using only two machines
discovery: https://discovery.etcd.io/<cluster ID goes here>
#You'll notice we use the variables that cloud providers use, except
#we renamed them and populate them on our own
addr: $_public_ipv4:4001
peer-addr: $_public_ipv4:7001
peer-election-timeout: 500
peer-heartbeat-interval: 100
fleet:
#A test to see how metadata is added for use in building out a profile of a box
metadata: ip=$_public_ipv4
units:
- name: etcd.service
command: start
- name: fleet.service
command: start
- name: docker-tcp.socket
command: start
content: |
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=2375
Service=docker.service
BindIPv6Only=both
[Install]
WantedBy=sockets.target
- name: enable-docker-tcp.service
command: start
content: |
[Unit]
Description=Enable the Docker Socket for the API
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl enable docker-tcp.socket
#This script will loop until the network card comes up and gives us an IP
#to latch onto and save in /etc/environment for variable substitution
- path: /run/setup-environment.sh
permissions: '0755'
content: |
#!/bin/bash
ENV="/etc/environment"
# Test for read write access to $1
touch $ENV
if [ $? -ne 0 ]; then
echo exiting, unable to modify: $ENV
exit 1
fi
# Setup environment target
sed -i -e '/^COREOS_PUBLIC_IPV4=/d' \
-e '/^COREOS_PRIVATE_IPV4=/d' \
"${ENV}"
# We loop until the the IP addresses are set
function get_ip () {
IF=$1
IP=
while [ 1 ]; do
IP=$(ifconfig $IF | awk '/inet / {print $2}')
if [ "$IP" != "" ]; then
break
fi
sleep .1
done
echo $IP
}
# Echo results of IP queries to environment file as soon as network interfaces
# get assigned IPs
# Note: in a PXE setup where you're not guaranteed to get a network interface
#name of eth0, we need another way to get the name of the interface as seen below
#We use this interface name to get the IP of that interface
INTERFACE=$(ip route get 8.8.8.8 | awk '{ print $5; exit }')
echo COREOS_PUBLIC_IPV4=$(get_ip $INTERFACE) >> $ENV
echo COREOS_PRIVATE_IPV4=$(get_ip $INTERFACE) >> $ENV
#This will substitute the variables in our second cloud-config to the
#values we pulled earlier
- path: /run/prep-cloud-config.sh
permissions: '0755'
content: |
#!/bin/bash
#Grab the IP address from the /etc/environment file
IP=$(awk 'BEGIN { FS = "=" }; /COREOS_PUBLIC_IPV4/ {print $NF};' /etc/environment)
sed "s/\$_public_ipv4/$IP/g" /run/cloud-config.yml > /tmp/cconfigtmp
sed -i "s/\$_public_ipv4/$IP/g" /tmp/cconfigtmp
mv /tmp/cconfigtmp /run/cloud-config.yml
- path: /etc/resolv.conf
permissions: 0644
owner: root
content: |
nameserver <set your nameserver here>
nameserver <set another nameserver here>
nameserver 8.8.8.8
nameserver 8.8.4.4
domain <your domain name here>
#This is a duplicate of the user section in the second cloud-config above
#So we can log into the templorary PXE boot version and the installed version with the same creds
#users:
#- name: testuser
#passwd: <password hash goes here>
#groups:
#- sudo
#- systemd-journal
coreos:
units:
- name: setup-environment.service
command: start
runtime: true
content: |
[Unit]
Description=Setup environment with private (and public) IP addresses
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/run/setup-environment.sh
- name: prep-cloud-config.service
command: start
runtime: true
content: |
[Unit]
Description=Re-write variables in cloud-config for proper expansion.
Requires=setup-environment.service
After=setup-environment.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/run/prep-cloud-config.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment