From 9edecbfc860aa94df8c726093fa8ac34f0d43b48 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Wed, 27 Jan 2021 23:07:57 +0100 Subject: [PATCH] Bootstrap Packer template for libvirt and Vbox Signed-off-by: Gianluca Arbezzano --- deploy/vagrant/Vagrantfile | 3 +- deploy/vagrant/basebox/.gitignore | 1 + deploy/vagrant/basebox/README.md | 30 +++++++++++ .../vagrant/basebox/ubuntu1804/provision.sh | 47 ++++++++++++++++ .../vagrant/basebox/ubuntu1804/template.json | 27 ++++++++++ deploy/vagrant/scripts/tinkerbell.sh | 54 ------------------- shell.nix | 2 +- 7 files changed, 108 insertions(+), 56 deletions(-) create mode 100644 deploy/vagrant/basebox/.gitignore create mode 100644 deploy/vagrant/basebox/README.md create mode 100644 deploy/vagrant/basebox/ubuntu1804/provision.sh create mode 100644 deploy/vagrant/basebox/ubuntu1804/template.json diff --git a/deploy/vagrant/Vagrantfile b/deploy/vagrant/Vagrantfile index 9456564..fa4dcaf 100644 --- a/deploy/vagrant/Vagrantfile +++ b/deploy/vagrant/Vagrantfile @@ -15,7 +15,8 @@ end Vagrant.configure('2') do |config| config.vm.define :provisioner do |provisioner| - provisioner.vm.box = 'generic/ubuntu1804' + provisioner.vm.box = "tinkerbelloss/sandbox-ubuntu1804" + provisioner.vm.box_version = "0.1.0" provisioner.vm.hostname = 'provisioner' provisioner.vm.synced_folder './../../', '/vagrant' provisioner.vm.provision :shell, path: './scripts/tinkerbell.sh' diff --git a/deploy/vagrant/basebox/.gitignore b/deploy/vagrant/basebox/.gitignore new file mode 100644 index 0000000..d2c19f1 --- /dev/null +++ b/deploy/vagrant/basebox/.gitignore @@ -0,0 +1 @@ +output* diff --git a/deploy/vagrant/basebox/README.md b/deploy/vagrant/basebox/README.md new file mode 100644 index 0000000..0ff98c8 --- /dev/null +++ b/deploy/vagrant/basebox/README.md @@ -0,0 +1,30 @@ +This directory contains a provisioning mechanism for the Vagrant boxes we ship +as part of Sandbox. + +In order to self contain and distribute the required dependencies for Tinkerbell +and Sandbox without having to download all of them runtime we decided to use +[Packer.io](https://packer.io) to build boxes that you can use when provisioning +Tinkerbell on Vagrant. + +Currently the generated boxes are available via [Vagrant +Cloud](https://app.vagrantup.com/tinkerbelloss). + +--- + +## Build + +To build the boxes checkout the right directory and run + +```terminal +$ packer build --parallel-builds=1 ./template.json +``` + +`-parallel-builds=1` is required because the template builds images for multiple +providers using the [Vagrant +builder](https://www.packer.io/docs/builders/vagrant) and I didn't manage to get +it to work in parallel yet. + +## Deploy to Vagrant Cloud + +I didn't find a way to make the Vagrant Cloud post processor to work. But I use +the vagrant cli `vagrant cloud publish` command. diff --git a/deploy/vagrant/basebox/ubuntu1804/provision.sh b/deploy/vagrant/basebox/ubuntu1804/provision.sh new file mode 100644 index 0000000..f74d423 --- /dev/null +++ b/deploy/vagrant/basebox/ubuntu1804/provision.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# abort this script on errors +set -euxo pipefail + +setup_docker() ( + # steps from https://docs.docker.com/engine/install/ubuntu/ + sudo apt-get install -y \ + apt-transport-https \ + ca-certificates \ + curl \ + gnupg-agent \ + software-properties-common + + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | + sudo apt-key add - + + local repo + repo=$( + printf "deb [arch=amd64] https://download.docker.com/linux/ubuntu %s stable" \ + "$(lsb_release -cs)" + ) + sudo add-apt-repository "$repo" + + sudo apt-get update + sudo apt-get install -y docker-ce docker-ce-cli containerd.io +) + +setup_docker_compose() ( + # from https://docs.docker.com/compose/install/ + sudo curl -L \ + "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" \ + -o /usr/local/bin/docker-compose + + sudo chmod +x /usr/local/bin/docker-compose +) + +main() ( + export DEBIAN_FRONTEND=noninteractive + + sudo apt-get update + setup_docker + setup_docker_compose + sudo apt-get install -y jq + sudo usermod -aG docker vagrant +) + +main diff --git a/deploy/vagrant/basebox/ubuntu1804/template.json b/deploy/vagrant/basebox/ubuntu1804/template.json new file mode 100644 index 0000000..5e84947 --- /dev/null +++ b/deploy/vagrant/basebox/ubuntu1804/template.json @@ -0,0 +1,27 @@ +{ + "builders": [ + { + "communicator": "ssh", + "name": "vagrant-virtualbox", + "provider": "virtualbox", + "source_path": "generic/ubuntu1804", + "type": "vagrant" + }, + { + "communicator": "ssh", + "name": "vagrant-libvirt", + "provider": "libvirt", + "source_path": "generic/ubuntu1804", + "type": "vagrant" + } + ], + "provisioners": [ + { + "script": "provision.sh", + "type": "shell" + } + ], + "variables": { + "cloud_token": "{{ env `VAGRANT_CLOUD_TOKEN` }}" + } +} diff --git a/deploy/vagrant/scripts/tinkerbell.sh b/deploy/vagrant/scripts/tinkerbell.sh index 6f80dc0..b139e89 100644 --- a/deploy/vagrant/scripts/tinkerbell.sh +++ b/deploy/vagrant/scripts/tinkerbell.sh @@ -7,38 +7,6 @@ whoami cd /vagrant -setup_docker() ( - # steps from https://docs.docker.com/engine/install/ubuntu/ - sudo apt-get install -y \ - apt-transport-https \ - ca-certificates \ - curl \ - gnupg-agent \ - software-properties-common - - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | - sudo apt-key add - - - local repo - repo=$( - printf "deb [arch=amd64] https://download.docker.com/linux/ubuntu %s stable" \ - "$(lsb_release -cs)" - ) - sudo add-apt-repository "$repo" - - sudo apt-get update - sudo apt-get install -y docker-ce docker-ce-cli containerd.io -) - -setup_docker_compose() ( - # from https://docs.docker.com/compose/install/ - sudo curl -L \ - "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" \ - -o /usr/local/bin/docker-compose - - sudo chmod +x /usr/local/bin/docker-compose -) - make_certs_writable() ( local certdir="/etc/docker/certs.d/$TINKERBELL_HOST_IP" sudo mkdir -p "$certdir" @@ -50,13 +18,7 @@ secure_certs() ( sudo chown "root" "$certdir" ) -command_exists() ( - command -v "$@" >/dev/null 2>&1 -) - configure_vagrant_user() ( - sudo usermod -aG docker vagrant - echo -n "$TINKERBELL_REGISTRY_PASSWORD" | sudo -iu vagrant docker login \ --username="$TINKERBELL_REGISTRY_USERNAME" \ @@ -72,20 +34,6 @@ setup_nat() ( main() ( export DEBIAN_FRONTEND=noninteractive - apt-get update - - if ! command_exists docker; then - setup_docker - fi - - if ! command_exists docker-compose; then - setup_docker_compose - fi - - if ! command_exists jq; then - sudo apt-get install -y jq - fi - if [ ! -f ./.env ]; then ./generate-envrc.sh eth1 >.env fi @@ -98,9 +46,7 @@ main() ( ./setup.sh setup_nat - secure_certs - configure_vagrant_user ) diff --git a/shell.nix b/shell.nix index 0ea41da..9a9f1f9 100644 --- a/shell.nix +++ b/shell.nix @@ -14,5 +14,5 @@ in with pkgs; mkShell { - buildInputs = [ go nodePackages.prettier shellcheck shfmt terraform gpgme ]; + buildInputs = [ go nodePackages.prettier shellcheck shfmt terraform gpgme packer vagrant ]; }