-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
288 lines (262 loc) · 14.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Copyright (c) Simply Code Ltd. All rights reserved.
# Licensed under the MIT License.
# See LICENSE file in the project root for full license information.
required_plugins = %w( vagrant-disksize vagrant-reload vagrant-vbguest )
required_plugins.each do |plugin|
system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
end
DATABASES = {
postgresql: {
server_location: "/",
guest_port: "5432",
host_port: "5000",
connectionString: "Server=localhost;Port=5432;User Id=postgres;Password=postgres;"
}
}
SERVICES = {
packit: {
repo: "https://github.com/SimplyCodeUK/packer-strategy.git",
branch: "master",
project_file: "PackIt.csproj",
build_dir: "PackIt/src",
binary: "PackIt.dll",
server_location: "/",
guest_port: "8000",
host_port: "8100",
database: "postgresql"
},
packit_draw: {
repo: "https://github.com/SimplyCodeUK/packer-strategy.git",
branch: "master",
project_file: "PackItDraw.csproj",
build_dir: "PackItDraw/src",
binary: "PackItDraw.dll",
server_location: "/",
guest_port: "8010",
host_port: "8200",
database: "postgresql"
},
packitui: {
repo: "https://github.com/SimplyCodeUK/packer-strategy.git",
branch: "master",
project_file: "PackItUI.csproj",
build_dir: "PackItUI/src",
binary: "PackItUI.dll",
server_location: "/",
guest_port: "9000",
host_port: "8080"
}
}
MACHINES = {
packit: {
dbs: [
"postgresql"
],
services: [
"packit",
"packit_draw",
"packitui"
]
}
}
SERVICES_DIR = "/srv"
COMMON_INSTALL = <<-SCRIPT
echo "ubuntu:ubuntu" | chpasswd
SCRIPT
DATABASE_PRE_INSTALL = <<-SCRIPT
SCRIPT
SERVICE_PRE_INSTALL = <<-SCRIPT
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-focal-prod focal main" > /etc/apt/sources.list.d/dotnetdev.list'
apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
systemctl disable hv-kvp-daemon.service
SCRIPT
BASE_PRE_INSTALL = <<-SCRIPT
apt install apt-transport-https
apt install libxt6 libxmu6
apt update
SCRIPT
DATABASE_INSTALL = <<-SCRIPT
echo 'deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main' | tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt update
apt install postgresql-10=10.* -y
service postgresql stop
echo "-------------------- fixing listen_addresses on /etc/postgresql/10/main/postgresql.conf"
sed -i "s/#listen_address.*/listen_addresses '*'/" /etc/postgresql/10/main/postgresql.conf
echo "-------------------- fixing postgres /etc/postgresql/10/main/pg_hba.conf"
cat >> /etc/postgresql/10/main/pg_hba.conf <<EOF
# Accept all IPv4 connections - FOR DEVELOPMENT ONLY!!!
host all all 0.0.0.0/0 md5
EOF
echo "-------------------- set default client_encoding /var/lib/postgresql/10/main"
echo "client_encoding = utf8" >> /var/lib/postgresql/10/main
service postgresql restart
sudo -u postgres psql --command "ALTER USER postgres WITH PASSWORD 'postgres';"
SCRIPT
SERVICE_INSTALL = <<-SCRIPT
apt install python3-software-properties -y
apt install nuget -y
apt install git -y
apt install dotnet-sdk-7.0 -y
apt install nginx -y
service nginx stop
rm /etc/nginx/sites-enabled/default 2> /dev/null
SCRIPT
MACHINES.each do |_key, machine|
buildScript = COMMON_INSTALL
if machine[:dbs]
buildScript += DATABASE_PRE_INSTALL
end
if machine[:services]
buildScript += SERVICE_PRE_INSTALL
end
buildScript += BASE_PRE_INSTALL
if machine[:dbs]
buildScript += DATABASE_INSTALL
end
if machine[:services]
buildScript += SERVICE_INSTALL
end
machine[:dbs].each do |db|
buildScript += <<-DB_SCRIPT
echo "server {" > "/etc/nginx/sites-available/#{db}"
DB_SCRIPT
if DATABASES[db.to_sym].key?(:host_port)
buildScript += <<-DB_SCRIPT
echo " listen #{DATABASES[db.to_sym][:host_port]};" >> "/etc/nginx/sites-available/#{db}"
echo " listen [::]:#{DATABASES[db.to_sym][:host_port]} default_server;" >> "/etc/nginx/sites-available/#{db}"
DB_SCRIPT
end
buildScript += <<-DB_SCRIPT
echo " location #{DATABASES[db.to_sym][:server_location]} {" >> "/etc/nginx/sites-available/#{db}"
echo " proxy_pass http://localhost:#{DATABASES[db.to_sym][:guest_port]};" >> "/etc/nginx/sites-available/#{db}"
echo " }" >> "/etc/nginx/sites-available/#{db}"
echo "}" >> "/etc/nginx/sites-available/#{db}"
ln -sf /etc/nginx/sites-available/#{db} /etc/nginx/sites-enabled/#{db}
DB_SCRIPT
end
machine[:services].each do |service|
workingDir = "#{SERVICES_DIR}/#{service}/#{SERVICES[service.to_sym][:build_dir]}/bin/Release/net8.0/publish"
buildScript += <<-SRV_SCRIPT
systemctl stop #{service}.service
cd #{SERVICES_DIR}
if [[ -d "${service}" && ! -L "${service}" ]]
then
git pull
else
git clone --branch #{SERVICES[service.to_sym][:branch]} #{SERVICES[service.to_sym][:repo]} #{service}
fi
cd #{SERVICES_DIR}/#{service}/#{SERVICES[service.to_sym][:build_dir]}
nuget restore #{SERVICES[service.to_sym][:project_file]}
dotnet restore #{SERVICES[service.to_sym][:project_file]} --disable-parallel
dotnet publish -c Release
if [ -d "./node_modules" ]
then
mkdir ./wwwroot/lib
cp -R ./node_modules/** ./wwwroot/lib/
fi
SRV_SCRIPT
if SERVICES[service.to_sym].key?(:database)
db = SERVICES[service.to_sym][:database]
connectionString = "#{DATABASES[db.to_sym][:connectionString]}"
buildScript += <<-SRV_SCRIPT
echo '{' > "#{workingDir}/appsettings.local.json"
echo ' "Connections": {' >> "#{workingDir}/appsettings.local.json"
echo ' "MaterialContext": {' >> "#{workingDir}/appsettings.local.json"
echo ' "Type": "postgres",' >> "#{workingDir}/appsettings.local.json"
echo ' "ConnectionString": "#{connectionString}Database=material;"' >> "#{workingDir}/appsettings.local.json"
echo ' },' >> "#{workingDir}/appsettings.local.json"
echo ' "PackContext": {' >> "#{workingDir}/appsettings.local.json"
echo ' "Type": "postgres",' >> "#{workingDir}/appsettings.local.json"
echo ' "ConnectionString": "#{connectionString}Database=pack;"' >> "#{workingDir}/appsettings.local.json"
echo ' },' >> "#{workingDir}/appsettings.local.json"
echo ' "PlanContext": {' >> "#{workingDir}/appsettings.local.json"
echo ' "Type": "postgres",' >> "#{workingDir}/appsettings.local.json"
echo ' "ConnectionString": "#{connectionString}Database=plan;"' >> "#{workingDir}/appsettings.local.json"
echo ' },' >> "#{workingDir}/appsettings.local.json"
echo ' "DrawingContext": {' >> "#{workingDir}/appsettings.local.json"
echo ' "Type": "postgres",' >> "#{workingDir}/appsettings.local.json"
echo ' "ConnectionString": "#{connectionString}Database=drawing;"' >> "#{workingDir}/appsettings.local.json"
echo ' }' >> "#{workingDir}/appsettings.local.json"
echo ' }' >> "#{workingDir}/appsettings.local.json"
echo '}' >> "#{workingDir}/appsettings.local.json"
SRV_SCRIPT
end
buildScript += <<-SRV_SCRIPT
echo "[Unit]" > "/etc/systemd/system/#{service}.service"
echo "Description=Example .NET Web API Application running on Ubuntu" >> "/etc/systemd/system/#{service}.service"
echo "" >> "/etc/systemd/system/#{service}.service"
echo "[Service]" >> "/etc/systemd/system/#{service}.service"
echo "WorkingDirectory=#{workingDir}" >> "/etc/systemd/system/#{service}.service"
echo "ExecStart=/usr/bin/dotnet #{SERVICES[service.to_sym][:binary]} --urls http://*:#{SERVICES[service.to_sym][:guest_port]}" >> "/etc/systemd/system/#{service}.service"
echo "Restart=always" >> "/etc/systemd/system/#{service}.service"
echo "RestartSec=60s" >> "/etc/systemd/system/#{service}.service"
echo "SyslogIdentifier=#{service}" >> "/etc/systemd/system/#{service}.service"
echo "User=www-data" >> "/etc/systemd/system/#{service}.service"
echo "Environment=ASPNETCORE_ENVIRONMENT=Development" >> "/etc/systemd/system/#{service}.service"
echo "Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false" >> "/etc/systemd/system/#{service}.service"
echo "Environment=DOTNET_CLI_TELEMETRY_OPTOUT=1" >> "/etc/systemd/system/#{service}.service"
echo "" >> "/etc/systemd/system/#{service}.service"
echo "[Install]" >> "/etc/systemd/system/#{service}.service"
echo "WantedBy=multi-user.target" >> "/etc/systemd/system/#{service}.service"
echo "server {" > "/etc/nginx/sites-available/#{service}"
SRV_SCRIPT
if SERVICES[service.to_sym].key?(:host_port)
buildScript += <<-SRV_SCRIPT
echo " listen #{SERVICES[service.to_sym][:host_port]};" >> "/etc/nginx/sites-available/#{service}"
echo " listen [::]:#{SERVICES[service.to_sym][:host_port]} default_server;" >> "/etc/nginx/sites-available/#{service}"
SRV_SCRIPT
end
buildScript += <<-SRV_SCRIPT
echo " location #{SERVICES[service.to_sym][:server_location]} {" >> "/etc/nginx/sites-available/#{service}"
echo " proxy_pass http://localhost:#{SERVICES[service.to_sym][:guest_port]};" >> "/etc/nginx/sites-available/#{service}"
echo " }" >> "/etc/nginx/sites-available/#{service}"
echo "}" >> "/etc/nginx/sites-available/#{service}"
systemctl enable #{service}.service
systemctl start #{service}.service
ln -sf /etc/nginx/sites-available/#{service} /etc/nginx/sites-enabled/#{service}
SRV_SCRIPT
end
buildScript += <<-SCRIPT
service nginx start
SCRIPT
machine[:build_script] = buildScript
end
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "generic/ubuntu2204"
config.vm.provider "virtualbox" do |vb|
vb.cpus = 4
vb.memory = 2048
vb.customize ['modifyvm', :id, '--graphicscontroller', 'vmsvga']
vb.customize ['modifyvm', :id, '--clipboard-mode', 'bidirectional']
vb.customize ['modifyvm', :id, '--draganddrop', 'bidirectional']
end
config.vm.network "forwarded_port", guest: 80, host: 80
MACHINES.each do |key, machine|
config.vm.define "#{key}" do |node|
machine[:dbs].each do |db|
if DATABASES[db.to_sym].key?(:host_port) && DATABASES[db.to_sym].key?(:guest_port)
node.vm.network "forwarded_port", guest: DATABASES[db.to_sym][:guest_port], host: DATABASES[db.to_sym][:host_port], id: "#{db}"
end
end
machine[:services].each do |service|
if SERVICES[service.to_sym].key?(:host_port) && SERVICES[service.to_sym].key?(:guest_port)
node.vm.network "forwarded_port", guest: SERVICES[service.to_sym][:guest_port], host: SERVICES[service.to_sym][:host_port], id: "#{service}"
end
end
node.vm.provision "shell", inline: machine[:build_script]
end
end
end