forked from mysociety/alaveteli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
245 lines (219 loc) · 6.95 KB
/
Vagrantfile
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
require 'pp'
require 'yaml'
# Welcome! Thanks for taking an interest in contributing to Alaveteli.
# This Vagrantfile should get you started with the minimum of fuss.
#
# Usage
# =====
#
# Install a vagrant plugin (if you don't already have it) that
# automatically install guest additions
#
# # Host
# $ vagrant plugin install vagrant-vbguest
# $ vagrant vbguest
#
# Get a copy of Alaveteli from GitHub and create the Vagrant instance
#
# # Host
# $ git clone [email protected]:mysociety/alaveteli.git
# $ cd alaveteli
# $ git submodule update --init
# $ vagrant --no-color up
#
# You should now be able to ssh in to the guest and run the test suite
#
# # Host
# $ vagrant ssh
#
# # Guest
# $ cd /home/vagrant/alaveteli
# $ bundle exec rake spec
#
# Run the rails server and visit the application in your host browser
# at http://10.10.10.30:3000
#
# # Guest
# bundle exec rails server -b 0.0.0.0
#
#
# Log-in to the Vagrant instance
# ================================
#
# Once the application is running, you can login with any of the sample users
# that get created automatically. You can find more details of these users in
# spec/fixtures/users.yml.
#
# Customizing the Vagrant instance
# ================================
#
# This Vagrantfile allows customisation of some aspects of the virtaual machine
# See the customization options below for details.
#
# The options can be set either by prefixing the vagrant command, using
# `.vagrant.yml`, or by exporting to the environment.
#
# # Prefixing the command
# $ ALAVETELI_VAGRANT_MEMORY=2048 vagrant up
#
# # .vagrant.yml
# $ echo "memory: 2048" >> .vagrant.yml
# $ vagrant up
#
# # Exporting to the environment
# $ export ALAVETELI_VAGRANT_MEMORY=2048
# $ vagrant up
#
# All have the same effect, but exporting will retain the variable for the
# duration of your shell session, whereas `.vagrant.yml` will be persistent.
# The environment takes precedence over `.vagrant.yml`.
#
# Using Themes
# ------------
#
# You can also use the built in theme switcher (script/switch-theme.rb). The
# ALAVETELI_THEMES_DIR will be shared in to /home/vagrant/alaveteli-themes so
# that the default location is used on the guest. You can use the env var
# ALAVETELI_THEMES_DIR to change where this Vagrantfile looks for the themes
# directory on the host.
def cpu_count
host = RbConfig::CONFIG['host_os']
# Give VM access to all cpu cores on the host
if host =~ /darwin/
`sysctl -n hw.ncpu`.to_i
elsif host =~ /linux/
`nproc`.to_i
else # sorry Windows folks, I can't help you
1
end
end
# Customization Options
# =====================
#
# Defaults can be overridden either in `.vagrant.yml` with the same key name, or
# via the environment by prefixing the key with `ALAVETELI_VAGRANT_` and
# upcasing. Boolean values can be set to `false` in the environment with "0",
# "false" or "no".
DEFAULTS = {
'fqdn' => 'alaveteli.10.10.10.30.nip.io',
'ip' => '10.10.10.30',
'public_network' => false,
'memory' => 1536,
'themes_dir' => '../alaveteli-themes',
'os' => 'bullseye64',
'name' => 'default',
'use_nfs' => false,
'show_settings' => false,
'cpus' => cpu_count
}.freeze
env = DEFAULTS.keys.reduce({}) do |memo, key|
value = ENV["ALAVETELI_VAGRANT_#{ key.upcase }"]
value = false if %w(0 false no).include?(value)
memo[key] = value unless value.nil?
memo
end
settings_file_path = File.dirname(__FILE__) + '/.vagrant.yml'
settings_file = if File.exist?(settings_file_path)
YAML.load(File.read(settings_file_path))
else
{}
end
SUPPORTED_OPERATING_SYSTEMS = {
'focal64' => {
box: 'ubuntu/focal64',
box_url: 'https://app.vagrantup.com/ubuntu/boxes/focal64'
},
'bullseye64' => {
box: 'debian/bullseye64',
box_url: 'https://app.vagrantup.com/debian/boxes/bullseye64'
}
}
def os
SUPPORTED_OPERATING_SYSTEMS.fetch(SETTINGS['os'], box: SETTINGS['os'])
end
SETTINGS = DEFAULTS.merge(settings_file).merge(env).freeze
if SETTINGS['show_settings']
puts 'Current machine settings:'
puts "\n"
pp SETTINGS
puts "\n"
puts 'Current OS settings:'
puts "\n"
pp os
puts "\n"
end
VAGRANTFILE_API_VERSION = '2'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = os[:box]
config.vm.define SETTINGS['name']
config.vm.box_url = os[:box_url]
config.vm.hostname = "alaveteli-#{ SETTINGS['os'] }"
if SETTINGS['public_network']
config.vm.network :public_network
end
config.vm.network :private_network, ip: SETTINGS['ip']
config.vm.synced_folder '.', '/vagrant', disabled: true
if SETTINGS['use_nfs']
config.vm.synced_folder '.', '/home/vagrant/alaveteli', nfs: true
else
config.vm.synced_folder '.',
'/home/vagrant/alaveteli',
owner: 'vagrant',
group: 'vagrant'
end
if File.directory?(SETTINGS['themes_dir'])
if SETTINGS['use_nfs']
config.vm.synced_folder SETTINGS['themes_dir'],
'/home/vagrant/alaveteli-themes',
nfs: true
else
config.vm.synced_folder SETTINGS['themes_dir'],
'/home/vagrant/alaveteli-themes',
owner: 'vagrant',
group: 'vagrant'
end
end
config.ssh.forward_agent = true
# The bundle install fails unless you have quite a large amount of
# memory; insist on 1.5GiB:
config.vm.provider 'virtualbox' do |vb|
vb.customize ['modifyvm', :id, '--memory', SETTINGS['memory']]
vb.customize ['modifyvm', :id, '--cpus', SETTINGS['cpus']]
end
config.vm.provision :shell, keep_color: true, inline: <<-EOF
if [[ -f "/home/vagrant/alaveteli/commonlib/bin/install-site.sh" ]]
then
/home/vagrant/alaveteli/commonlib/bin/install-site.sh \
--dev \
alaveteli \
vagrant \
#{ SETTINGS['fqdn'] }
else
echo "Couldn't find provisioning script." >&2
echo "Did you forget to run git submodule update --init?" >&2
exit 1
fi
EOF
# Append basic usage instructions to the MOTD
motd = <<-EOF
To start your alaveteli instance:
* cd alaveteli
* bundle exec rails server -b 0.0.0.0
EOF
config.vm.provision :shell, keep_color: true, inline: "echo '#{ motd }' >> /etc/motd.tail"
# Display next steps info at the end of a successful install
instructions = <<-EOF
Welcome to your new Alaveteli development site!
If you are planning to use a custom theme, you should create
an `alaveteli-themes` folder at the same level as your `alaveteli`
code folder to hold your theme repositories so that your
Vagrant box will see your theme folders when using the
switch-theme.rb script (take a look at the documentation in
the script/switch-theme.rb file for more information).
Full instructions for customising your install can be found online:
http://alaveteli.org/docs/customising/
Type `vagrant ssh` to log into the Vagrant box to start the site
or run the test suite
EOF
config.vm.provision :shell, keep_color: true, inline: "echo '#{ instructions }'"
end