-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
executable file
·278 lines (252 loc) · 9.06 KB
/
config.rb
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
#!/usr/bin/env ruby
require 'fileutils'
require 'json'
require 'yaml'
require './src/ruby/credentials.rb'
PROFILE = [:static, :dynamic, :neo4j]
STAGING = File::dirname(File::expand_path(__FILE__)).include?('staging')
PROJECT_NAME = 'workspace'
DEV_NGINX_PORT = 8025
DEV_NEO4J_PORT = 8021
LOGS_PATH = DEVELOPMENT ? './logs' : "/home/micha/logs/#{PROJECT_NAME}"
DATA_PATH = DEVELOPMENT ? './data' : "/mnt/hackschule/#{PROJECT_NAME}"
MYSQL_DATA_PATH = File.join(DATA_PATH, 'mysql')
POSTGRES_DATA_PATH = File.join(DATA_PATH, 'postgres')
PGADMIN_DATA_PATH = File.join(DATA_PATH, 'pgadmin')
# NEO4J_USER_DATA_PATH = File.join(DATA_PATH, 'neo4j_user')
USER_PATH = File.join(DATA_PATH, 'user')
INTERNAL_PATH = File.join(DATA_PATH, 'internal')
WEB_CACHE_PATH = File.join(DATA_PATH, 'cache')
DOWNLOAD_PATH = File.join(DATA_PATH, 'dl')
NGINX_PATH = File.join(DATA_PATH, 'nginx')
NEO4J_LOGS_PATH = File::join(LOGS_PATH, 'neo4j')
NEO4J_DATA_PATH = File::join(DATA_PATH, 'neo4j')
docker_compose = {
:services => {},
}
FileUtils::mkpath(NGINX_PATH)
if PROFILE.include?(:static)
docker_compose[:services][:nginx] = {
:build => './docker/nginx',
:volumes => [
'./src/static:/usr/share/nginx/html:ro',
"#{WEB_CACHE_PATH}:/webcache:ro",
"#{DOWNLOAD_PATH}:/dl:ro",
"#{LOGS_PATH}:/var/log/nginx",
"#{DATA_PATH}/nginx:/etc/nginx/conf.d"
]
}
if !DEVELOPMENT
docker_compose[:services][:nginx][:environment] = [
'VIRTUAL_HOST=workspace.hackschule.de',
'LETSENCRYPT_HOST=workspace.hackschule.de',
]
docker_compose[:services][:nginx][:expose] = ['80']
end
docker_compose[:services][:nginx][:links] = [
"ruby:#{PROJECT_NAME}_ruby_1",
"phpmyadmin:#{PROJECT_NAME}_phpmyadmin_1",
"pgadmin:#{PROJECT_NAME}_pgadmin_1",
]
nginx_config = <<~END_OF_STRING
log_format custom '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$request_time"';
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
~font/ max;
application/x-font-ttf max;
application/x-font-otf max;
application/font-woff max;
application/font-woff2 max;
}
server {
listen 80;
server_name localhost;
client_max_body_size 100M;
expires $expires;
gzip on;
gzip_comp_level 6;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
access_log /var/log/nginx/access.log custom;
charset utf-8;
location / {
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
try_files $uri @ruby;
}
location @ruby {
proxy_pass http://ruby_1:9292;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
}
END_OF_STRING
File::open(File.join(NGINX_PATH, 'default.conf'), 'w') do |f|
f.write nginx_config
end
if PROFILE.include?(:dynamic)
docker_compose[:services][:nginx][:depends_on] = [:ruby, :phpmyadmin, :pgadmin]
end
end
if PROFILE.include?(:dynamic)
env = []
env << 'DEVELOPMENT=1' if DEVELOPMENT
env << 'STAGING=1' if STAGING
docker_compose[:services][:ruby] = {
:build => './docker/ruby',
:volumes => ['./src:/src:ro',
"#{WEB_CACHE_PATH}:/webcache",
"#{USER_PATH}:/user",
"#{INTERNAL_PATH}:/internal",
"#{DATA_PATH}/tic80:/tic80",
"/var/run/docker.sock:/var/run/docker.sock",
"#{NGINX_PATH}:/nginx",
"#{DOWNLOAD_PATH}:/dl",
],
:environment => env,
:working_dir => '/src/ruby',
:privileged => true,
:entrypoint => DEVELOPMENT ?
'rerun -b --dir /src/ruby -s SIGKILL \'rackup --host 0.0.0.0\'' :
'rackup --host 0.0.0.0'
}
if PROFILE.include?(:neo4j)
docker_compose[:services][:ruby][:depends_on] ||= []
docker_compose[:services][:ruby][:depends_on] << :neo4j
docker_compose[:services][:ruby][:links] = ['neo4j:neo4j', 'mysql:mysql']
end
end
if PROFILE.include?(:neo4j)
docker_compose[:services][:neo4j] = {
:build => './docker/neo4j',
:volumes => ["#{NEO4J_DATA_PATH}:/data",
"#{NEO4J_LOGS_PATH}:/logs"]
}
docker_compose[:services][:neo4j][:environment] = [
'NEO4J_AUTH=none',
'NEO4J_dbms_logs__timezone=SYSTEM',
'NEO4J_dbms_allow__upgrade=true',
]
docker_compose[:services][:neo4j][:user] = '1000'
end
docker_compose[:services][:mysql] = {
:image => 'mysql/mysql-server',
:command => ["--default-authentication-plugin=mysql_native_password"],
:volumes => ["#{MYSQL_DATA_PATH}:/var/lib/mysql"],
:user => '1000',
:restart => 'always',
:environment => {
'MYSQL_ROOT_HOST' => '%',
'MYSQL_ROOT_PASSWORD' => MYSQL_ROOT_PASSWORD
},
}
docker_compose[:services][:phpmyadmin] = {
:image => 'phpmyadmin/phpmyadmin',
:restart => 'always',
# :expose => ['80'],
:depends_on => [:mysql],
:links => ['mysql:db'],
:environment => {
'PMA_ABSOLUTE_URI' => PHPMYADMIN_WEB_ROOT,
'UPLOAD_LIMIT' => '128M',
},
}
docker_compose[:services][:postgres] = {
:image => 'postgres',
:volumes => ["#{POSTGRES_DATA_PATH}:/var/lib/postgresql/data"],
:restart => 'always',
:user => '1000',
:environment => {
'POSTGRES_PASSWORD' => POSTGRES_ROOT_PASSWORD,
},
}
docker_compose[:services][:pgadmin] = {
:image => 'dpage/pgadmin4',
:restart => 'always',
:volumes => [
"#{PGADMIN_DATA_PATH}:/var/lib/pgadmin",
"#{File.expand_path('docker/pgadmin4')}:/etc/pgadmin:ro",
],
# :expose => ['80'],
:depends_on => [:postgres],
:links => ['postgres:postgres'],
:user => '1000',
:environment => {
'PGADMIN_DEFAULT_EMAIL' => '[email protected]',
'PGADMIN_DEFAULT_PASSWORD' => PGADMIN_PASSWORD,
'PGADMIN_CONFIG_WTF_CSRF_ENABLED' => 'False',
'PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION' => 'False',
'SCRIPT_NAME' => '/pgadmin',
},
}
docker_compose[:services][:tensorflowjs] = {
:image => 'evenchange4/docker-tfjs-converter',
:volumes => ["#{INTERNAL_PATH}:/internal"],
:restart => 'always',
:tty => true,
}
docker_compose[:services].values.each do |x|
x[:network_mode] = 'default'
end
if DEVELOPMENT
docker_compose[:services][:nginx][:ports] = ["0.0.0.0:#{DEV_NGINX_PORT}:80"]
if PROFILE.include?(:neo4j)
docker_compose[:services][:neo4j][:ports] ||= []
docker_compose[:services][:neo4j][:ports] << "127.0.0.1:#{DEV_NEO4J_PORT}:7474"
docker_compose[:services][:neo4j][:ports] << "127.0.0.1:7687:7687"
end
end
unless DEVELOPMENT
docker_compose[:services].values.each do |x|
x[:restart] = :always
end
end
File::open('docker-compose.yaml', 'w') do |f|
f.puts "# NOTICE: don't edit this file directly, use config.rb instead!\n"
f.write(JSON::parse(docker_compose.to_json).to_yaml)
end
FileUtils::mkpath(LOGS_PATH)
FileUtils::mkpath(File.join(LOGS_PATH, 'neo4j'))
if PROFILE.include?(:dynamic)
FileUtils::cp('src/ruby/Gemfile', 'docker/ruby/')
end
if PROFILE.include?(:neo4j)
FileUtils::mkpath(NEO4J_DATA_PATH)
end
FileUtils::mkpath(USER_PATH)
FileUtils::mkpath(INTERNAL_PATH)
FileUtils::mkpath(WEB_CACHE_PATH)
FileUtils::mkpath(File.join(DATA_PATH, 'tic80'))
FileUtils::mkpath(MYSQL_DATA_PATH)
FileUtils::mkpath(POSTGRES_DATA_PATH)
FileUtils::mkpath(PGADMIN_DATA_PATH)
FileUtils::mkpath(File.join(DATA_PATH, 'internal'))
FileUtils::mkpath(File.join(DATA_PATH, 'mysql'))
FileUtils::mkpath(File.join(DATA_PATH, 'pgadmin'))
FileUtils::mkpath(File.join(DATA_PATH, 'postgres'))
FileUtils::mkpath(File.join(DATA_PATH, 'dl'))
`docker compose 2> /dev/null`
DOCKER_COMPOSE = ($? == 0) ? 'docker compose' : 'docker-compose'
system("#{DOCKER_COMPOSE} --compatibility --project-name #{PROJECT_NAME} #{ARGV.map { |x| '"' + x + '"'}.join(' ')}")