summaryrefslogtreecommitdiffstatshomepage
path: root/debian/Vagrantfile
blob: 2b38b0f038831866b02c5f9eee921624355099b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- mode: ruby -*-
# vi: set ft=ruby :

NAME = 'Egor Tensin'
EMAIL = 'egor@tensin.name'
GPG_KEY = '09AEA2BAD52882A1AF9D6042F218DB829A9905B5'
UID = Process.euid

REQUIRED_PLUGINS = %w(
  vagrant-libvirt
  vagrant-reload
)
exit unless REQUIRED_PLUGINS.all? do |plugin|
  Vagrant.has_plugin?(plugin) || (
    puts "The #{plugin} plugin is required. Please install it with:"
    puts "    vagrant plugin install #{plugin}"
    false
  )
end

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"

  # Weirdly enough, VirtualBox's shared folders didn't quite work for me.
  # gbp buildpackage would just exit with FileExistsError, which doesn't
  # happen with NFS shares.
  config.vm.synced_folder "../", "/vagrant",
    type: "nfs",
    nfs_udp: false,
    nfs_version: "4"

  config.vm.provider "libvirt" do |v|
    v.cpus = 2
    v.memory = 4096
  end

  # Working with the git repository:
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
git config --global user.name '#{NAME}'
git config --global user.email '#{EMAIL}'
  SHELL
  config.ssh.forward_agent = true

  # Set DEB* environment variables:
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
tee -a ~/.bashrc <<'EOF'
export DEBFULLNAME='#{NAME}'
export DEBEMAIL='#{EMAIL}'
EOF
  SHELL

  # gpg-agent forwarding (https://wiki.gnupg.org/AgentForwarding):
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
gpg --keyserver hkp://keyserver.ubuntu.com/ --recv-keys '0x#{GPG_KEY}'
gpg --import-ownertrust <<'EOF'
#{GPG_KEY}:6:
EOF
  SHELL
  config.vm.provision "shell", inline: <<-SHELL
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
echo 'StreamLocalBindUnlink yes' >> /etc/ssh/sshd_config
  SHELL
  # "agent-extra-socket" on the host.
  local_gpg_socket = one_line(run('gpgconf', '--list-dirs', 'agent-extra-socket'))
  # "agent-socket" on the VM.
  remote_gpg_socket = "/run/user/#{UID}/gnupg/S.gpg-agent"
  config.ssh.extra_args = ['-R', "#{remote_gpg_socket}:#{local_gpg_socket}"]

  # Install the required packages:
  config.vm.provision "shell", inline: <<-SHELL
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
apt update
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends make
cd /vagrant/debian
make deps
  SHELL

  config.vm.provision :reload
end

require "open3"

def run(*cmd)
  stdout, stderr, status = Open3.capture3(*cmd)
  abort stderr unless status.success?
  stdout
end

def one_line(output)
  lines = output.lines.map(&:chomp)
  abort "Must be a single line: #{lines}" unless lines.length == 1
  lines.first
end