API

from fastcore.test import test_eq,test_fail
from jsonschema import ValidationError

source

ufw

 ufw (logging='off', def_incoming='deny', def_outgoing='allow',
      internal=None, **allows)
ufw(internal="enp7s0", OpenSSH=22)
['ufw default deny incoming',
 'ufw default allow outgoing',
 'ufw logging off',
 'ufw allow 22/tcp',
 'ufw allow in on enp7s0',
 'ufw --force enable']

source

user

 user (name, pub_keys, groups=None, shell='/bin/bash', sudo=True)
user('jph', 'mykey', 'docker')
{'name': 'jph',
 'groups': ['docker', 'sudo'],
 'shell': '/bin/bash',
 'sudo': ['ALL=(ALL) NOPASSWD:ALL'],
 'ssh_authorized_keys': ['mykey']}

source

source

 source (url, keyid, keyserver)
dsource = source("https://download.docker.com/linux/ubuntu",
                 keyid="9DC858229FC7DD38854AE2D88D81803C0EBFCD88",
                 keyserver="https://download.docker.com/linux/ubuntu/gpg")
dsource
{'source': 'deb [signed-by=$KEY_FILE] https://download.docker.com/linux/ubuntu $RELEASE stable',
 'keyid': '9DC858229FC7DD38854AE2D88D81803C0EBFCD88',
 'keyserver': 'https://download.docker.com/linux/ubuntu/gpg'}

source

apt

 apt (unattended=False, autoclean=30, email='', auto_reboot=False,
      **sources)
apt(docker=dsource)
{'apt': {'conf': 'APT::Periodic::Update-Package-Lists "1";\nAPT::Periodic::Download-Upgradeable-Packages "1";\nAPT::Periodic::AutocleanInterval "7";\nAPT::Periodic::Unattended-Upgrade "0";\nUnattended-Upgrade::Automatic-Reboot "false";\n',
  'sources': {'docker': {'source': 'deb [signed-by=$KEY_FILE] https://download.docker.com/linux/ubuntu $RELEASE stable',
    'keyid': '9DC858229FC7DD38854AE2D88D81803C0EBFCD88',
    'keyserver': 'https://download.docker.com/linux/ubuntu/gpg'}}}}

source

systemd

 systemd (items)
systemd({"systemd-journald.service":"[Journal]\nSystemMaxUse=50M\n"})
[{'path': '/etc/systemd/system/systemd-journald.service.d/override.conf',
  'owner': 'root:root',
  'permissions': '0644',
  'content': '[Journal]\nSystemMaxUse=50M\n'}]

source

log_rotate

 log_rotate (freq='weekly', num_keep=7, fname='00-cloud-init-global')

source

phone_home

 phone_home (url)

source

reboot

 reboot (enable=True, message='Rebooting')

source

mounts

 mounts (devices)

source

runcmd

 runcmd (cmds)
def cc_validate(d):
    vsc = xget('https://raw.githubusercontent.com/canonical/cloud-init/main/cloudinit/config/schemas/versions.schema.cloud-config.json').text
    validate(d, schema=json.loads(vsc))
samp = '''#cloud-config
hostname: example-host
fqdn: example-host.example.com
# User setup configuration
users:
  - name: exampleuser
    gecos: Example User
    sudo: ['ALL=(ALL) NOPASSWD:ALL']'''
cc_validate(yaml.safe_load(samp))

This example has an intentional key error (“hostna” instead of “hostname”):

samp = '''#cloud-config
hostna: example-host
fqdn: example-host.example.com'''
try: cc_validate(yaml.safe_load(samp))
except ValidationError: pass
else: raise Exception('Expected validation error')

source

cloud_init_base

 cloud_init_base (hostname, packages=None, check=True, **kw)

source

cloud_init_config

 cloud_init_config (hostname, username, pub_keys, email='', groups=None,
                    internal=None, cmds=None, devices=None,
                    ping_host=None, packages=None, dropins=None,
                    **sources)
packages = [ "unattended-upgrades", "vim-nox", "python3", "rsync", "ubuntu-drivers-common", "python3-pip", "ack", "lsyncd", "wget", "bzip2", "ca-certificates", "git", "build-essential", "software-properties-common", "curl", "grep", "sed", "dpkg", "tmux", "less", "htop", "openssh-client", "python-is-python3", "python3-dev", "cron", "gnupg", "docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", "docker-compose-plugin" ]

dsource = source(
    "https://download.docker.com/linux/ubuntu", 
    keyid="9DC858229FC7DD38854AE2D88D81803C0EBFCD88", 
    keyserver="https://download.docker.com/linux/ubuntu/gpg")
devices = [ ['mydevice', "/data", "ext4", "defaults,nofail", "0", "0"] ]
dropins = {"systemd-journald.service":"[Journal]\nSystemMaxUse=250M\n"}

res = cloud_init_config('myhost', 'jph', 'mykey', 'j@answer.ai', dropins=dropins, groups="docker", internal="enp7s0",
                        devices=devices, ping_host='https://ping.me', packages=packages, docker=dsource)
print(res[:50])
#cloud-config
hostname: myhost
preserve_hostname: