2
votes

How do I run, for development purposes, cloud-init yaml file that will be normally run via user-data?

I know how I can re-run cloud-init, but I want to develop complicated cloud-init file and to do that it is rather difficult to continually build new instances.

3

3 Answers

1
votes

Sorry to say, you're going to have to run it on a new clean instance (or at least a snapshot of one). Even if you did manually go back and start at different steps, there are potentially side effects.

I think you'll find that if you get used to managing local VMs, you can debug your scripts fairly quickly.

1
votes

The quickest path for iterating on user-data input to cloud-init is probably via lxd. You can quickly set up lxd on a vm host or a bare metal system. Once set up, launches are very quick.

$ cat ud.yaml
#cloud-config
runcmd:
  - "read up idle < /proc/uptime; echo Up $up seconds | tee /run/runcmd.log"

$ lxc launch ubuntu-daily:bionic ud-test "--config=user.user-data=$(cat ud.yaml)"
Creating ud-test
Starting ud-test

$ lxc exec ud-test cat /run/runcmd.log
Up 8.05 seconds

$ lxc stop ud-test
$ lxc delete ud-test
0
votes

You might be able to get away with just running cloud-init clean and then re-running it.

I'm experimenting with cloud-init and using an Ubuntu box with KVM as a virtualization lab. I made a simple Makefile to build the cloud-init image and launch it in an KVM instance.

You can see my code here:

https://github.com/brennancheung/playbooks/blob/master/cloud-init-lab/Makefile

all: clean build run

INSTANCE_NAME := "vm"
CLOUD_IMAGE_FILE = "bionic-server-cloudimg-amd64.img"
CLOUD_IMAGE_BASE_URL := "http://cloud-images.ubuntu.com/bionic/current"
CLOUD_IMAGE_URL := "$(CLOUD_IMAGE_BASE_URL)/$(CLOUD_IMAGE_FILE)"

download:
    wget $(CLOUD_IMAGE_URL)

clean:
    @echo "Removing build artifacts"
    -@rm -f config.img 2>/dev/null
    -@virsh destroy $(INSTANCE_NAME) 2>/dev/null || true
    -@virsh undefine $(INSTANCE_NAME) 2>/dev/null || true
    -@rm -f $(INSTANCE_NAME).img

build: 
    @echo "Building cloud config drive"
    cloud-localds config.img config.yaml
    cp $(CLOUD_IMAGE_FILE) $(INSTANCE_NAME).img

run:
    @echo "Spawning instance $(INSTANCE_NAME)"
    virt-install \
        --name $(INSTANCE_NAME) \
        --memory 8192 \
        --disk ./$(INSTANCE_NAME).img,device=disk,bus=virtio \
        --disk ./config.img,device=cdrom \
        --os-type linux \
        --os-variant ubuntu18.04 \
        --virt-type kvm \
        --graphics none \
        --network bridge=br0