Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
SAMPLE_DIR=./examples/

CI_SAMPLE_DIR=${SAMPLE_DIR}/cloud-init/010-init/
VG_SAMPLE_DIR=${SAMPLE_DIR}/vagrants/010-init/
MD_SAMPLE_DIR=./

GENISOIMAGE := $(shell genisoimage -version 2>/dev/null)

Expand All @@ -8,7 +13,28 @@ else
endif

nocloud.iso: meta-data user-data
${MAKE} check
$(MKISOFS) \
-joliet -rock \
-volid "cidata" \
-output nocloud.iso meta-data user-data
-joliet -rock \
-volid "cidata" \
-output nocloud.iso \
${MD_SAMPLE_DIR}/meta-data \
${CI_SAMPLE_DIR}/user-data

all:
${MAKE} link
${MAKE} nocloud.iso
vagrant up

recreate_all: ## "make all" with forced deletion of the vagrant box, use with care
${MAKE} link
${MAKE} nocloud.iso
vagrant destroy -f && \
vagrant up

check: ## Check cloud-init syntax
cloud-init devel schema --config-file user-data

link: ## Link to the current directory to ease development
rm -f ./Vagrantfile && ln -s ${VG_SAMPLE_DIR}/Vagrantfile ./Vagrantfile
rm -f ./user-data && ln -s ${CI_SAMPLE_DIR}/user-data ./user-data
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ sudo port install cdrtools

## Usage

Create a new nocloud.iso file, and start Vagrant:
Link to the desired example to execute, create a new nocloud.iso file, and start Vagrant:

```
make
vagrant up
make all
```

To use other examples, modify the ```CI_SAMPLE_DIR``` and ```VG_SAMPLE_DIR``` in the ```Makefile``` to the desired sample directory for both cloud-init user-data configuration and Vagrantfile


### Examples
Each Vagrantfile shall be able to run a specific range of user-data files based on the following mapping, new cloud-init examples shall be as much as possible compatible with the default Vagrantfile

- Vagrantfile:
- list of user-data files

- 010-init:
- 010-init
- 020-users
- 030-cmd
- 040-network
- 050-rsyslog
- 020-custom-user:
- 020-users
File renamed without changes.
22 changes: 22 additions & 0 deletions examples/cloud-init/020-users/user-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#cloud-config
manage_etc_hosts: localhost

users:
- name: walid
primary-group: users
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
# dialout: access usb
# input: access usb too?
# plugdev: mount and umount
# netdev: manage network interfaces
# adm: logs
# audio: access mic or soundcard
# video: access videocard
# reference: https://wiki.debian.org/SystemGroups
groups: users,dialout,input,plugdev,netdev,adm,audio,video
ssh-import-id: None
lock_passwd: true
ssh-authorized-keys:
- ssh-rsa ADD_YOUR_PUBLIC_KEY_HERE

27 changes: 27 additions & 0 deletions examples/cloud-init/030-cmd/user-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#cloud-config
manage_etc_hosts: localhost

# the commands will be run last thing after the machine is totally initialized
# output shall be logged to /var/log/cloud-init-output.log
runcmd:
# avahi-daemon doesn't exist, but cloud-init won't fail for such error
- [ systemctl, restart, avahi-daemon ]
- [ ls, -l, / ]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ sh, -c, echo "=========hello world=========" ]
- [ echo, "=========hello world=========" ]
- ls -l /root
- [ wget, "http://example.org", -O, /tmp/index.html ]
- [ systemctl, disable, dhcpcd ]
- [ systemctl, enable, networking ]
# Rebooting after disabling and enabling services will be needed in case we don't want to stop the services
# It will also be needed in following examples where we setup custom networks
# However, if we reboot without a wait time, Vagrant will face the reboot while trying to communicate with the machine
# which will result in vagrant failing
# of course if there's provisioning using Ansible on Vagrant, that will be more tricky, cause we'll need to sleep enough time till the provisioning is over
# or we could use the provisioning from the cloud-init side, if desired!
# In any case, be careful while utilizing the reboot here
# Finally be ready to face the reboot while investigating the output log in /var/log/cloud-init-output.log :P
- [ sleep, 60 ]
- [ reboot ]

13 changes: 13 additions & 0 deletions examples/cloud-init/040-network/meta-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# THIS EXAMPLE DOES NOT WORK, only left for illustration purposes

instance-id: iid-0123456789abcdef
local-hostname: ubuntu-xenial

network-interfaces: |
iface eth0 inet static
address 192.168.1.174
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.254

16 changes: 16 additions & 0 deletions examples/cloud-init/040-network/network-config.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# THIS EXAMPLE DOES NOT WORK, only left for illustration purposes

version: 1
config:
- type: physical
name: enp0s3
subnets:
- type: static
address: 192.168.1.165
- type: nameserver
address: 192.168.1.1
- type: nameserver
address: 8.8.8.8
- type: nameserver
address: 8.8.4.4

7 changes: 7 additions & 0 deletions examples/cloud-init/040-network/network-config.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# THIS EXAMPLE DOES NOT WORK, only left for illustration purposes

version: 2
ethernets:
enp0s3:
addresses:
- 192.168.1.171
17 changes: 17 additions & 0 deletions examples/cloud-init/040-network/network-v1.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# THIS EXAMPLE DOES NOT WORK, only left for illustration purposes

network:
version: 1
config:
- type: physical
name: enp0s3
subnets:
- type: static
address: 192.168.1.165
- type: nameserver
address: 192.168.1.1
- type: nameserver
address: 8.8.8.8
- type: nameserver
address: 8.8.4.4

8 changes: 8 additions & 0 deletions examples/cloud-init/040-network/network-v2.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS EXAMPLE DOES NOT WORK, only left for illustration purposes

network:
version: 2
ethernets:
enp0s3:
addresses:
- 192.168.1.171
44 changes: 44 additions & 0 deletions examples/cloud-init/040-network/user-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#cloud-config
manage_etc_hosts: localhost


# This is one of four ways to edit the network configurations, and it depends on editing the deprecated ENI "/etc/network/interfaces.d/*.cfg" the restarting the network
# The second way is by adding a network-config file to the image, but doing so will make the vagrant panic and not being able to connect to the vm, as the default network is overwritten and not reachable anymore, however I noticed also the machine doesn't get the static ip that it requests, so I can't even ssh into it aside from vagrant
# The third way is by adding network-interfaces section to the meta-data file, which yields to the same output of the second way.
# Notice that network-config shall be supplied next to meta-data and user-data in the toplevel of nocloud.iso
# or it could be supplied as config file that shall be placed in /etc/cloud/cloud.conf.d/config.cfg "But in this case it will have a different syntax by the addition of ```network:``` top level"
# examples are supplied
# The fourth way is by adding a static ip configuration in Vagrantfile, which is a good option if only work with vagrant, but not a portable solution for a cloudinit cfg

# Note: You'll need to edit
# - the logical device name, "enp0s3 in our case" based on ones system, one could know it by running any other user-data example and checking ```ip a``` will show logical network devices, or by running ```sudo lshw -C network```
# - the IP Addresses

write_files:

- content: |
nameserver 192.168.1.1
nameserver 8.8.4.4
nameserver 8.8.8.8
path: /etc/resolv.conf
append: true

- content: |
auto enp0s3
iface enp0s3 inet static
address 192.168.1.160
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.1 8.8.8.8 8.8.4.4
iface default inet static
path: /etc/network/interfaces.d/10-mystatic-ip.cfg

# Check 030_cmd example for details
runcmd:
- [ sh, -xc, "echo $(date): hello world!" ]
- [ systemctl, enable, networking ]
- [ sleep, 60 ]
- [ reboot ]

12 changes: 12 additions & 0 deletions examples/cloud-init/050-rsyslog/user-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#cloud-config
manage_etc_hosts: localhost

rsyslog:
configs:
- content: "*.* @192.168.1.50:514"
filename: 01-logfetcher.conf
# Using this manual reload command with the ubuntu/xenial version
# prevents the vm from being accessed!
# Anyway we shouldn't need it as the default value is auto and it should be working correctly on all distros
# service_reload_command: [systemctl, restart, rsyslog]

3 changes: 3 additions & 0 deletions Vagrantfile → examples/vagrants/010-init/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Vagrant.require_version ">= 1.8.0"
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"

# Use for a static ip
# config.vm.network "public_network", ip: "192.168.4.174"

# Disable SSH password for 16.04 - we'll add the insecure Vagrant key
# (don't worry, it's just an example and gets replaced anyway)
config.ssh.password = nil
Expand Down
47 changes: 47 additions & 0 deletions examples/vagrants/020-custom-user/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :


CLOUD_CONFIG_PATH = File.join(File.dirname(__FILE__), "nocloud.iso")


Vagrant.require_version ">= 1.8.0"

Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"

# Disable SSH password for 16.04 - we'll add the insecure Vagrant key
# (don't worry, it's just an example and gets replaced anyway)
config.ssh.password = nil

# To use your main public/private key pair, uncomment these lines:
config.ssh.private_key_path = File.expand_path("~/.ssh/id_rsa")
config.ssh.insert_key = false

config.ssh.username = "walid"

# Disable shared folders
config.vm.synced_folder ".", "/vagrant", disabled: true

# Tweak virtualbox
config.vm.provider :virtualbox do |vb|
# Attach nocloud.iso to the virtual machine
vb.customize [
"storageattach", :id,
"--storagectl", "SCSI",
"--port", "1",
"--type", "dvddrive",
"--medium", CLOUD_CONFIG_PATH
]

# Speed up machine startup by using linked clones
vb.linked_clone = true

# Workarounds for 16.04 Vagrantfile problems:
# - serial console slows down the boot process dramatically.
vb.customize [
"modifyvm", :id,
"--uartmode1", "disconnected"
]
end
end