summaryrefslogtreecommitdiffstats
path: root/Vagrantfile
blob: b1b4288c0a6bd633ba2c61c002db506b7e04523c (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Vagrant.configure(2) do |config|

  # We use Ubuntu instead of Debian because the image comes with two-way
  # shared folder support by default.
  UBUNTU = 'hashicorp/bionic64'

  config.vm.define(:exa) do |config|
    config.vm.provider :virtualbox do |v|
      v.name = 'exa'
      v.memory = 2048
      v.cpus = `nproc`.chomp.to_i
    end

    config.vm.provider :vmware_desktop do |v|
      v.vmx['memsize'] = '2048'
      v.vmx['numvcpus'] = `nproc`.chomp
    end

    config.vm.box = UBUNTU
    config.vm.hostname = 'exa'


    # Make sure we know the VM image’s default user name. The ‘cassowary’ user
    # (specified later) is used for most of the test *output*, but we still
    # need to know where the ‘target’ and ‘.cargo’ directories go.
    developer = 'vagrant'


    # Install the dependencies needed for exa to build, as quietly as
    # apt can do.
    config.vm.provision :shell, privileged: true, inline: <<-EOF
      if hash fish &>/dev/null; then
        echo "Tools are already installed"
      else
        trap 'exit' ERR
        echo "Installing tools"
        apt-get update -qq
        apt-get install -qq -o=Dpkg::Use-Pty=0 \
          git gcc curl attr libgit2-dev zip \
          fish zsh bash bash-completion
      fi
    EOF


    # Install Rust.
    # This is done as vagrant, not root, because it’s vagrant
    # who actually uses it. Sent to /dev/null because the progress
    # bar produces a ton of output.
    config.vm.provision :shell, privileged: false, inline: <<-EOF
      if hash rustc &>/dev/null; then
        echo "Rust is already installed"
      else
        trap 'exit' ERR
        echo "Installing Rust"
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --profile minimal --component rustc,rust-std,cargo,clippy -y > /dev/null
        source $HOME/.cargo/env
        echo "Installing cargo-hack"
        cargo install -q cargo-hack
        echo "Installing specsheet"
        cargo install -q --git https://github.com/ogham/specsheet
      fi
    EOF


    # Privileged installation and setup scripts.
    config.vm.provision :shell, privileged: true, inline: <<-EOF

      # Install Just, the command runner.
      if hash just &>/dev/null; then
        echo "just is already installed"
      else
        trap 'exit' ERR
        echo "Installing just"
        wget -q "https://github.com/casey/just/releases/download/v0.8.3/just-v0.8.3-x86_64-unknown-linux-musl.tar.gz"
        tar -xf "just-v0.8.3-x86_64-unknown-linux-musl.tar.gz"
        cp just /usr/local/bin
      fi

      # Guarantee that the timezone is UTC — some of the tests
      # depend on this (for now).
      timedatectl set-timezone UTC


      # Use a different ‘target’ directory on the VM than on the host.
      # By default it just uses the one in /vagrant/target, which can
      # cause problems if it has different permissions than the other
      # directories, or contains object files compiled for the host.
      echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/#{developer}/.cargo/bin"' > /etc/environment
      echo 'CARGO_TARGET_DIR="/home/#{developer}/target"'                                                     >> /etc/environment


      # Create a variety of misc scripts.

      ln -sf /vagrant/devtools/dev-run-debug.sh   /usr/bin/exa
      ln -sf /vagrant/devtools/dev-run-release.sh /usr/bin/rexa

      echo -e "#!/bin/sh\ncargo build --manifest-path /vagrant/Cargo.toml \\$@" > /usr/bin/build-exa
      ln -sf /usr/bin/build-exa /usr/bin/b

      echo -e "#!/bin/sh\ncargo test --manifest-path /vagrant/Cargo.toml \\$@ -- --quiet" > /usr/bin/test-exa
      ln -sf /usr/bin/test-exa /usr/bin/t

      echo -e "#!/bin/sh\n/vagrant/xtests/run.sh" > /usr/bin/run-xtests
      ln -sf /usr/bin/run-xtests /usr/bin/x

      echo -e "#!/bin/sh\nbuild-exa && test-exa && run-xtests" > /usr/bin/compile-exa
      ln -sf /usr/bin/compile-exa /usr/bin/c

      echo -e "#!/bin/sh\nbash /vagrant/devtools/dev-package-for-linux.sh \\$@" > /usr/bin/package-exa
      echo -e "#!/bin/sh\ncat /etc/motd" > /usr/bin/halp

      chmod +x /usr/bin/{exa,rexa,b,t,x,c,build-exa,test-exa,run-xtests,compile-exa,package-exa,halp}


      # Configure the welcoming text that gets shown:

      # Capture the help text so it gets displayed first
      rm -f /etc/update-motd.d/*
      bash /vagrant/devtools/dev-help.sh > /etc/motd

      # Tell bash to execute a bunch of stuff when a session starts
      echo "source /vagrant/devtools/dev-bash.sh" > /home/#{developer}/.bash_profile
      chown #{developer} /home/#{developer}/.bash_profile

      # Disable last login date in sshd
      sed -i '/PrintLastLog yes/c\PrintLastLog no' /etc/ssh/sshd_config
      systemctl restart sshd


      # Link the completion files so they’re “installed”:

      # bash
      test -h /etc/bash_completion.d/exa \
        || ln -s /vagrant/contrib/completions.bash /etc/bash_completion.d/exa

      # zsh
      test -h /usr/share/zsh/vendor-completions/_exa \
        || ln -s /vagrant/contrib/completions.zsh /usr/share/zsh/vendor-completions/_exa

      # fish
      test -h /usr/share/fish/completions/exa.fish \
        || ln -s /vagrant/contrib/completions.fish /usr/share/fish/completions/exa.fish
    EOF


    # Install kcov for test coverage
    # This doesn’t run coverage over the xtests so it’s less useful for now
    if ENV.key?('INSTALL_KCOV')
      config.vm.provision :shell, privileged: false, inline: <<-EOF
        trap 'exit' ERR

        test -e ~/.cargo/bin/cargo-kcov \
          || cargo install cargo-kcov

        sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y \
          cmake g++ pkg-config \
          libcurl4-openssl-dev libdw-dev binutils-dev libiberty-dev

        cargo kcov --print-install-kcov-sh | sudo sh
      EOF
    end

    config.vm.provision :shell, privileged: true,  path: 'devtools/dev-set-up-environment.sh'
    config.vm.provision :shell, privileged: false, path: 'devtools/dev-create-test-filesystem.sh'
  end
end