Skip to content

Instantly share code, notes, and snippets.

@brettmilford
Last active October 14, 2022 13:30
Show Gist options
  • Save brettmilford/0af6a75011adb2755ff003e5ea999992 to your computer and use it in GitHub Desktop.
Save brettmilford/0af6a75011adb2755ff003e5ea999992 to your computer and use it in GitHub Desktop.
#!/bin/bash -eux
_DIR=$(dirname `readlink -f $0`)
_CONF="${_DIR}/etc"
_vars() {
VERSION=${1:-"2.9"}
RELEASE=${2:-"focal"}
PACKAGE=${3:-"snap"}
_warnings
}
_warnings() {
if [ "$RELEASE" = "focal" ] && [ "$PACKAGE" = "deb" ]; then
#*Focal deb deploys a snap
echo "WARNING: ${RELEASE} only supports a snap based deployment."
fi
if [ "$VERSION" = "edge" ] && [ "$PACKAGE" = "deb" ]; then
echo "WARNING: ${VERSION} only supports a snap based deployment."
PACKAGE="snap"
fi
if [ "$VERSION" = "devel" ]; then
#^devel environment requires a PPA which is currently only being built for focal+
echo "WARNING: ${VERSION} only supports focal release."
RELEASE="focal"
unset PACKAGE
fi
}
_lxd () {
lxc profile ls | grep -q kvm || lxc profile create kvm
lxc profile edit kvm < ${_CONF}/lxd/profile/kvm.yaml
lxc launch ubuntu:"${RELEASE}" maas-"${RELEASE}-${VERSION/./-}" --profile default --profile kvm \
--config user.user-data="$(cat ${_CONF}/cloud-init/maas_${VERSION}${PACKAGE:+_$PACKAGE}.yaml)"
sleep 3 # wait for IP
echo "MAAS URL: http://$(lxc ls maas-"${RELEASE}-${VERSION/./-}" -c 4 | awk '/\(br0\)/{ print $2 }'):5240/MAAS/"
}
_multipass () {
multipass launch \
-c 2 \
-m 4G \
-d 40G \
-n maas-"${RELEASE}-${VERSION/./-}" \
--cloud-init etc/cloud-init/maas_${VERSION}${PACKAGE:+_$PACKAGE}.yaml \
release:${RELEASE}
}
case "${1:-h}" in
-d|--debug)
set -x
DEBUG="true"
;;
mp|multipass)
shift
cd $_DIR
_vars $@
_multipass
cd -
;;
lxd)
shift
_vars $@
_lxd
;;
-h|--help|help|*)
echo "${0} [ multipass | lxd ] [ edge | devel | 2.9 | 2.8 | 2.7 ] [ focal | bionic ] [ deb | snap ]"
esac
---
name: kvm
config:
linux.kernel_modules: vhost_net,macvlan,br_netfilter,kvm_intel
user.network-config: |
version: 1
config:
- type: physical
name: eth0
- type: bridge
name: br0
bridge_interfaces:
- eth0
params:
bridge_stp: 'off'
bridge_fd: 0
subnets:
- type: dhcp
control: auto
user.user-data: |
#cloud-config
write_files:
- path: /etc/sysctl.conf
content: |
net.core.netdev_max_backlog=182757
- path: /etc/udev/rules.d/80-netdev.rules
content: |
SUBSYSTEM=="net", ACTION=="add|change", KERNEL=="*" ATTR{tx_queue_len}="10000"
- path: /etc/sysctl.d/90-bridge.conf
content: |
net.bridge.bridge-nf-call-ip6tables=0
net.bridge.bridge-nf-call-iptables=0
net.bridge.bridge-nf-call-arptables=0
- path: /etc/udev/rules.d/90-bridge.rules
content: |
ACTION=="add", SUBSYSTEM=="module", KERNEL=="br_netfilter", RUN+="/sbin/sysctl -p /etc/sysctl.d/90-bridge.conf"
- path: /etc/libvirt/qemu.conf
content: |
remember_owner = 0
runcmd:
- sysctl -p
devices:
kvm:
path: /dev/kvm # not supported/present in multipass (on osx)
type: unix-char
vhost-net:
path: /dev/vhost-net
type: unix-char
#cloud-config
---
package_update: true
ssh_import_id: lp:brettmilford
packages:
- squashfuse
- qemu-kvm
- qemu-system-x86
- libvirt-daemon-system
- virt-manager
- postgresql
- jq
snap:
commands:
- snap install maas --channel=2.9/stable
write_files:
- path: /etc/sysctl.conf
owner: root:root
permissions: '0644'
content: |
fs.inotify.max_queued_events=1048576
fs.inotify.max_user_instances=1048576
fs.inotify.max_user_watches=1048576
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv6.neigh.default.gc_thresh3=8192
net.core.netdev_max_backlog=182757
kernel.keys.maxkeys=2000
vm.max_map_count=262144
vm.swappiness=1
- path: /etc/udev/rules.d/80-netdev.rules
owner: root:root
permissions: '0644'
content: |
SUBSYSTEM=="net", ACTION=="add|change", KERNEL=="*" ATTR{tx_queue_len}="10000"
- path: /root/maas-net.xml
content: |
<network>
<name>maas</name>
<forward mode='nat'/>
<dns enable='no'/>
<bridge name='maasbr0' stp='off' delay='0'/>
<mtu size="9000"/>
<ip address='172.16.64.1' netmask='255.255.255.0'/>
</network>
runcmd:
# virsh setup
- sysctl -p
- virsh net-define /root/maas-net.xml
- virsh net-start maas
- virsh net-autostart maas
# postgresql setup
- sudo -u postgres psql -c "CREATE USER \"maas\" WITH ENCRYPTED PASSWORD 'maasdbpassword'"
- sudo -u postgres createdb -O "maas" "maasdb"
- grep -q 'bionic' && echo "host maasdb maas 0/0 md5" >> /etc/postgresql/10/main/pg_hba.conf
# NOTE: postgresql 12 shipped with focal
- grep -q 'focal' && echo "host maasdb maas 0/0 md5" >> /etc/postgresql/12/main/pg_hba.conf
# maas init
# NOTE: init --mode all only works for 2.7, deprecated in 2.8, removed in 2.9
- sleep 2
- maas init region+rack --database-uri "postgres://maas:maasdbpassword@localhost/maasdb" --maas-url "http://$(ip addr show br0 | awk '/inet /{print $2}' | cut -f1 -d '/'):5240/MAAS" --enable-debug --enable-debug-queries
- maas createadmin --username admin --password 'password' --email admin@noreply.com --ssh-import lp:brettmilford
- maas login admin http://localhost:5240/MAAS/ $(maas apikey --username=admin)
# fabric/vlan/subnet setup
# NOTE: If fabrics are missing, likely the rackd has failed to register properly and none of this will work
- maas admin subnet update `maas admin subnets read | jq -r '.[] | select(.name=="172.16.64.0/24") | .id'` gateway_ip='172.16.64.1'
- maas admin ipranges create subnet=`maas admin subnets read | jq -r '.[] | select(.name=="172.16.64.0/24") | .id'` type=dynamic start_ip=172.16.64.2 end_ip=172.16.64.254
- maas admin vlan update `maas admin subnets read | jq -r '.[] | select(.name=="172.16.64.0/24") | .vlan.fabric_id'` `maas admin subnets read | jq -r '.[] | select(.name=="172.16.64.0/24") | .vlan.vid'` mtu=9000 dhcp_on=true primary_rack=`maas admin rack-controllers read | jq -r .[].system_id`
# pod setup
- mkdir -p /var/snap/maas/current/root/.ssh
- ssh-keygen -t rsa -f /var/snap/maas/current/root/.ssh/id_rsa -N ''
- mkdir -p /root/.ssh
- cat /var/snap/maas/current/root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
- maas admin pods create type=virsh power_address='qemu+ssh:///system'
- maas admin pod update 1 cpu_over_commit_ratio=10 memory_over_commit_ratio=10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment