-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathVagrantfile.flatcar
225 lines (203 loc) · 6.95 KB
/
Vagrantfile.flatcar
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
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
#
# Nomad 1.x required due to Nomad jobs manifests
#
echo "Installing Nomad..."
sudo mkdir -p /etc/nomad.d /etc/nomad /opt/nomad/data/
# Error due to Flatcar default filesystems permissons restrictions
#nomad -autocomplete-install
# Consul service systemd startup script
(
cat <<-EOF
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target
[Service]
Restart=on-failure
ExecStart=/opt/bin/consul agent -dev -client=0.0.0.0
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
) | sudo tee /etc/systemd/system/consul.service
sudo systemctl enable consul.service
sudo systemctl start consul
(
cat <<-EOF
data_dir = "/opt/nomad/data/"
bind_addr = "0.0.0.0"
plugin "docker" {
config {
volumes {
enabled = true
}
}
}
EOF
) | sudo tee /etc/nomad/config.hcl
(
cat <<-EOF
[Unit]
Description=nomad dev agent
Requires=network-online.target
After=network-online.target
[Service]
Environment=PATH=/opt/bin:/usr/sbin:/usr/bin:/sbin:/bin
Restart=on-failure
ExecStart=/opt/bin/nomad agent -dev-connect -config=/etc/nomad/config.hcl
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
) | sudo tee /etc/systemd/system/nomad.service
sudo systemctl enable nomad.service
sudo systemctl start nomad
echo "Setting up iptable to forward dns request to consul..."
sudo iptables -t nat -A PREROUTING -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600
sudo iptables -t nat -A PREROUTING -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600
sudo iptables -t nat -A OUTPUT -d localhost -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600
sudo iptables -t nat -A OUTPUT -d localhost -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600
echo "Pulling Docker images"
if [ -n "$DOCKERHUBID" ] && [ -n "$DOCKERHUBPASSWD" ]; then
echo "Login to Docker Hub as $DOCKERHUBID"
if ! echo "$DOCKERHUBPASSWD" | sudo docker login --username "$DOCKERHUBID" --password-stdin; then
echo 'Error login to Docker Hub'
exit 2
fi
fi
find /tmp/jobs -maxdepth 1 -type f -name '*.hcl' | xargs grep -E 'image\s*=\s*' | awk '{print $NF}' | sed -e 's/"//g' -e 's/:demo//' | while read j; do
echo "Pulling $j Docker image"
if ! sudo docker pull $j >/dev/null; then
echo "Exiting"
exit 1
fi
if ! echo "$j" | grep -q ':'; then
sudo docker tag "$j":latest "$j":demo
fi
done
if [ $? -ne 0 ]; then
exit 1
fi
if [ -n "$DOCKERHUBID" ] && [ -n "$DOCKERHUBPASSWD" ]; then
echo "Logout from Docker Hub as $DOCKERHUBID"
if ! sudo docker logout; then
echo 'Error logging out from Docker Hub'
fi
fi
echo "Installing Grafana stack..."
until nomad status
do
echo "Waiting nomad to be ready...."
sleep 3
done
# Handle all Nomad job files one at a time
# Use the naming of Nomad job files to determine scheduling order of services
find /tmp/jobs -maxdepth 1 -type f -name '*.hcl' | sort | while read j; do
# Job can be successfully planed (enough resources left)
svc=$(basename $j | sed -e 's/\.nomad\.hcl//' -e 's/^[0-9][0-9]-//')
if nomad plan $j | grep -Eq 'All tasks successfully allocated'; then
echo "Scheduling $svc"
nomad run -detach $j
else
echo "Error can not schedule $svc"
fi
done
SCRIPT
Vagrant.configure(2) do |config|
#
# Get local host timezone
# PLEASE NOTE: the result can be tricked if you have any VPN
# or system-wide HTTP proxy configured on your Vagrant host
#
# PLEASE ALSO NOTE: the service at ipapi.co is protected from DDos
# by CloudFlare and therefore it might happen that you'll be denied
# to query the API too often if you respawn your environment too often
#
# Set default to your timezone
ltz = 'Europe/Paris'
# There is no clean way to set the ltz variable only in a trigger up
# case for a dirty way see:
# https://gist.github.com/jamiejackson/4ec92bef2e148d58d207b15f2c92b066
# config.trigger.before :up do |trigger|
# trigger.info = "Retrieving host timezone"
# trigger.ruby do |env,machine|
puts "Retrieving host timezone"
require 'uri'
require 'net/http'
require 'json'
begin
uri = URI('https://ipapi.co/timezone')
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
ltz = res.body
puts "Timezone set from ipapi.co to: " + ltz
elsif res.is_a?(Net::HTTPTooManyRequests)
puts "Unable to get timezone from ipapi.co: trying fallback"
uri = URI('https://ipapi.co/json')
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
jsres = JSON.parse(res.body)
if ! jsres['timezone'].to_s.strip.empty?
ltz = jsres['timezone']
puts "Timezone set from ipapi.co fallback to: " + ltz
else
puts "Unable to get timezone from ipapi.co fallback. Using default: " + ltz
end
elsif res.is_a?(Net::HTTPTooManyRequests)
puts "Unable to get timezone from ipapi.co fallback. Using default: " + ltz
else
puts "Unknown response received from ipapi.co: "
puts res
# abort("Exiting")
end
else
puts "Unknown response received from ipapi.co: "
puts res
# abort("Exiting")
end
rescue => e
puts e
abort("Exiting")
end
# end
# end
config.vm.box = "obourdon/flatcar-linux-observability"
config.vm.box_version = "2765.2.6"
config.vm.hostname = "flatcar-nomad"
# Flatcar Linux does not support VirtualBox Guest Additions therefore the copy
config.vm.provision "file", source: "./jobs", destination: "/tmp/jobs"
# Set the timezone the same as the host so that metrics & logs ingested have the right timestamp.
config.vm.provision :shell, :inline => "echo setting Timezone to " + ltz + "; sudo timedatectl set-timezone " + ltz, run: "always"
config.vm.provision "shell", inline: $script, env: {"DOCKERHUBID"=>ENV['DOCKERHUBID'], "DOCKERHUBPASSWD"=>ENV['DOCKERHUBPASSWD']}, privileged: false
# Expose the nomad api and ui to the host
config.vm.network "forwarded_port", guest: 4646, host: 4646
# consul
config.vm.network "forwarded_port", guest: 8500, host: 8500
# grafana
config.vm.network "forwarded_port", guest: 3000, host: 3000
# prometheus
config.vm.network "forwarded_port", guest: 9090, host: 9090
# loki
config.vm.network "forwarded_port", guest: 3100, host: 3100
# promtail
config.vm.network "forwarded_port", guest: 3200, host: 3200
# tns app
config.vm.network "forwarded_port", guest: 8001, host: 8001
# Increase memory for Parallels Desktop
config.vm.provider "parallels" do |p, o|
p.memory = "2048"
end
# Increase memory for Virtualbox
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
# Increase memory for VMware
["vmware_fusion", "vmware_workstation"].each do |p|
config.vm.provider p do |v|
v.vmx["memsize"] = "2048"
end
end
end