Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Makefile - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0e0b5689de528e825b295c77bff702ca2345d29 (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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
.NOTPARALLEL:

-include env.mk

gitlab_repo = https://gitlab.com/gitlab-org/gitlab-ce.git
gitlab_repo_base = $(basename ${gitlab_repo})
gitlab_repo_ruby_version = $(shell curl -s "${gitlab_repo_base}/raw/master/.ruby-version")
gitlab_shell_repo = https://gitlab.com/gitlab-org/gitlab-shell.git
gitlab_shell_clone_dir = go-gitlab-shell/src/gitlab.com/gitlab-org/gitlab-shell
gitlab_workhorse_repo = https://gitlab.com/gitlab-org/gitlab-workhorse.git
gitlab_workhorse_clone_dir = gitlab-workhorse/src/gitlab.com/gitlab-org/gitlab-workhorse
gitaly_repo = https://gitlab.com/gitlab-org/gitaly.git
gitaly_proto_repo = https://gitlab.com/gitlab-org/gitaly-proto.git
gitaly_gopath = $(abspath ./gitaly)
gitaly_clone_dir = ${gitaly_gopath}/src/gitlab.com/gitlab-org/gitaly
gitaly_proto_clone_dir = ${gitaly_gopath}/src/gitlab.com/gitlab-org/gitaly-proto
gitlab_pages_repo = https://gitlab.com/gitlab-org/gitlab-pages.git
gitlab_pages_clone_dir = gitlab-pages/src/gitlab.com/gitlab-org/gitlab-pages
gitlab_docs_repo = https://gitlab.com/gitlab-com/gitlab-docs.git
gitlab_development_root = $(shell pwd)
gitaly_assembly_dir = ${gitlab_development_root}/gitaly/assembly
postgres_bin_dir ?= $(shell ruby support/pg_bindir)
postgres_replication_user = gitlab_replication
postgres_dir = $(abspath ./postgresql)
postgres_replica_dir = $(abspath ./postgresql-replica)
postgres_geo_dir = $(abspath ./postgresql-geo)
postgres_data_dir = ${postgres_dir}/data
auto_devops_enabled = $(shell cat auto_devops_enabled 2>/dev/null || echo 'false')
auto_devops_gitlab_port = $(shell cat auto_devops_gitlab_port)
auto_devops_registry_port = $(shell cat auto_devops_registry_port)
hostname = $(if $(filter true,$(auto_devops_enabled)),"$(auto_devops_gitlab_port).qa-tunnel.gitlab.info",$(shell cat hostname 2>/dev/null || echo 'localhost'))
port = $(shell (${auto_devops_enabled} && echo '443') || cat port 2>/dev/null || echo '3000')
https = $(shell (${auto_devops_enabled} && echo 'true') || cat https_enabled 2>/dev/null || echo 'false')
relative_url_root = $(shell cat relative_url_root 2>/dev/null || echo '')
username = $(shell whoami)
sshd_bin = $(shell which sshd)
webpack_port = $(shell cat webpack_port 2>/dev/null || echo '3808')
registry_enabled = $(shell cat registry_enabled 2>/dev/null || echo 'false')
registry_host = $(if $(filter true,$(auto_devops_enabled)),"$(auto_devops_registry_port).qa-tunnel.gitlab.info",$(shell cat registry_host 2>/dev/null || echo '127.0.0.1'))
registry_external_port = $(if $(filter true,$(auto_devops_enabled)),443,$(shell cat registry_external_port 2>/dev/null || echo '5000'))
registry_port = $(shell cat registry_port 2>/dev/null || echo '5000')
gitlab_from_container = $(shell [ "$(shell uname)" = "Linux" ] && echo 'localhost' || echo 'docker.for.mac.localhost')
postgresql_port = $(shell cat postgresql_port 2>/dev/null || echo '5432')
postgresql_geo_port = $(shell cat postgresql_geo_port 2>/dev/null || echo '5432')
object_store_enabled = $(shell cat object_store_enabled 2>/dev/null || echo 'false')
object_store_port = $(shell cat object_store_port 2>/dev/null || echo '9000')
gitlab_pages_port = $(shell cat gitlab_pages_port 2>/dev/null || echo '3010')
rails_bundle_install_cmd := bundle install --jobs 4 --without production $(if $(shell mysql_config --libs 2>/dev/null),--with,--without) mysql
elasticsearch_version = 6.5.1
elasticsearch_tar_gz_sha1 = 5903e1913a7c96aad96a8227517c40490825f672
ruby_version = $(shell ruby -e 'puts RUBY_VERSION')
workhorse_version = $(shell bin/resolve-dependency-commitish "${gitlab_development_root}/gitlab/GITLAB_WORKHORSE_VERSION")
gitlab_shell_version = $(shell bin/resolve-dependency-commitish "${gitlab_development_root}/gitlab/GITLAB_SHELL_VERSION")
gitaly_version = $(shell bin/resolve-dependency-commitish "${gitlab_development_root}/gitlab/GITALY_SERVER_VERSION")
pages_version = $(shell bin/resolve-dependency-commitish "${gitlab_development_root}/gitlab/GITLAB_PAGES_VERSION")
tracer_build_tags = tracer_static tracer_static_jaeger
jaeger_server_enabled ?= true
jaeger_version = 1.10.1
ifeq ($(shallow_clone),true)
git_depth_param = --depth=1
endif

all: gitlab-setup gitlab-shell-setup gitlab-workhorse-setup gitlab-pages-setup support-setup gitaly-setup prom-setup object-storage-setup

# Set up the GitLab Rails app

check-ruby-version:
	@if [ "${gitlab_repo_ruby_version}" != "${ruby_version}" ]; then \
		echo "WARNING: You're using Ruby version ${ruby_version}."; \
		echo "WARNING: However we recommend using Ruby version ${gitlab_repo_ruby_version} for this repository."; \
		test "${IGNORE_INSTALL_WARNINGS}" = "true" || \
		(echo "WARNING: Press <ENTER> to continue installation or <CTRL-C> to abort" && read v;) \
	fi

check-go-version:
	bin/$@

gitlab-setup: check-ruby-version gitlab/.git gitlab-config bundler .gitlab-bundle yarn .gitlab-yarn .gettext

gitlab/.git:
	git clone ${git_depth_param} ${gitlab_repo} gitlab

gitlab-config: gitlab/config/gitlab.yml gitlab/config/database.yml gitlab/config/unicorn.rb gitlab/config/resque.yml gitlab/public/uploads gitlab/config/puma.rb

auto_devops_enabled:
	echo 'false' > $@

auto_devops_gitlab_port:
	awk -v min=20000 -v max=24999 'BEGIN{srand(); print int(min+rand()*(max-min+1))}' > $@

auto_devops_registry_port: auto_devops_gitlab_port
	expr ${auto_devops_gitlab_port} + 5000 > $@

gitlab/config/gitlab.yml: support/templates/gitlab.yml.erb auto_devops_enabled auto_devops_gitlab_port auto_devops_registry_port
	hostname=${hostname} port=${port} relative_url_root=${relative_url_root}\
		https=${https}\
		webpack_port=${webpack_port}\
		registry_host=${registry_host} registry_external_port=${registry_external_port}\
		registry_enabled=${registry_enabled} registry_port=${registry_port}\
		object_store_enabled=${object_store_enabled} object_store_port=${object_store_port}\
		gitlab_pages_port=${gitlab_pages_port}\
		support/edit-gitlab-yml gitlab/config/gitlab.yml

gitlab/config/database.yml: database.yml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		-e "s|5432|${postgresql_port}|" \
		"$<"

# Versions older than GitLab 11.5 won't have this file
gitlab/config/puma.example.development.rb:
	touch $@

gitlab/config/puma.rb: gitlab/config/puma.example.development.rb
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

gitlab/config/unicorn.rb: gitlab/config/unicorn.rb.example.development
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

gitlab/config/resque.yml: redis/resque.yml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

gitlab/public/uploads:
	mkdir $@

.gitlab-bundle:
	cd ${gitlab_development_root}/gitlab && $(rails_bundle_install_cmd)
	touch $@

.gitlab-yarn:
	cd ${gitlab_development_root}/gitlab && yarn install --pure-lockfile
	touch $@

.gettext:
	cd ${gitlab_development_root}/gitlab && bundle exec rake gettext:compile > ${gitlab_development_root}/gettext.log 2>&1
	git -C ${gitlab_development_root}/gitlab checkout locale/*/gitlab.po
	touch $@

.PHONY: bundler
bundler:
	command -v $@ > /dev/null || gem install $@ -v 1.17.3

.PHONY: yarn
yarn:
	@command -v $@ > /dev/null || {\
		echo "Error: Yarn executable was not detected in the system.";\
		echo "Download Yarn at https://yarnpkg.com/en/docs/install";\
		exit 1;\
	}

# Set up gitlab-shell

gitlab-shell-setup: symlink-gitlab-shell ${gitlab_shell_clone_dir}/.git gitlab-shell/config.yml bundler .gitlab-shell-bundle gitlab-shell/.gitlab_shell_secret
	if [ -x gitlab-shell/bin/compile ] ; then gitlab-shell/bin/compile; fi

symlink-gitlab-shell:
	support/symlink gitlab-shell ${gitlab_shell_clone_dir}

${gitlab_shell_clone_dir}/.git:
	git clone --quiet --branch "${gitlab_shell_version}" ${git_depth_param} ${gitlab_shell_repo} ${gitlab_shell_clone_dir}

gitlab-shell/config.yml: gitlab-shell/config.yml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		-e "s|^gitlab_url:.*|gitlab_url: http+unix://$(subst /,%2F,${gitlab_development_root}/gitlab.socket)|" \
		-e "s|/usr/bin/redis-cli|$(shell which redis-cli)|" \
		-e "s|^  socket: .*|  socket: ${gitlab_development_root}/redis/redis.socket|" \
		-e "s|^# migration|migration|" \
		"$<"

.gitlab-shell-bundle:
	cd ${gitlab_development_root}/gitlab-shell && $(rails_bundle_install_cmd)
	touch $@

gitlab-shell/.gitlab_shell_secret:
	ln -s ${gitlab_development_root}/gitlab/.gitlab_shell_secret $@

# Set up gitaly

gitaly-setup: gitaly/bin/gitaly gitaly/config.toml ${gitaly_proto_clone_dir}/.git

${gitaly_clone_dir}/.git:
	git clone --quiet --branch "${gitaly_version}" ${git_depth_param} ${gitaly_repo} ${gitaly_clone_dir}

${gitaly_proto_clone_dir}/.git:
	git clone ${git_depth_param} --quiet ${gitaly_proto_repo} ${gitaly_proto_clone_dir}

gitaly/config.toml: $(gitaly_clone_dir)/config.toml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		-e "s|^socket_path.*|socket_path = \"${gitlab_development_root}/gitaly.socket\"|" \
		-e "s|^bin_dir.*|bin_dir = \"${gitlab_development_root}/gitaly/bin\"|" \
		-e "s|# prometheus_listen_addr|prometheus_listen_addr|" \
		-e "s|# \[logging\]|\[logging\]|" \
		-e "s|# level = \"warn\"|level = \"warn\"|" \
		-e "s|^#[^[]*\[git\].*|\[git\]|" \
		-e "s|^# catfile_cache_size.*|catfile_cache_size = 5|" \
		"$<"

prom-setup:
	if [ "$(uname -s)" = "Linux" ]; then \
		sed -i -e 's/docker\.for\.mac\.localhost/localhost/g' ${gitlab_development_root}/prometheus/prometheus.yml; \
	fi

# Set up gitlab-docs

gitlab-docs-setup: gitlab-docs/.git gitlab-docs-bundle gitlab-docs/nanoc.yaml symlink-gitlab-docs

gitlab-docs/.git:
	git clone ${git_depth_param} ${gitlab_docs_repo} gitlab-docs

gitlab-docs/.git/pull:
	cd gitlab-docs && \
		git stash && \
		git checkout master &&\
		git pull --ff-only


# We need to force delete since there's already a nanoc.yaml file
# in the docs folder which we need to overwrite.
gitlab-docs/rm-nanoc.yaml:
	rm -f gitlab-docs/nanoc.yaml

gitlab-docs/nanoc.yaml: gitlab-docs/rm-nanoc.yaml
	cp nanoc.yaml.example $@

gitlab-docs-bundle:
	cd ${gitlab_development_root}/gitlab-docs && bundle install --jobs 4

symlink-gitlab-docs:
	support/symlink ${gitlab_development_root}/gitlab-docs/content/docs ${gitlab_development_root}/gitlab/doc

gitlab-docs-update: gitlab-docs/.git/pull gitlab-docs-bundle gitlab-docs/nanoc.yaml

# Update GDK itself

self-update: unlock-dependency-installers
	@echo ""
	@echo "--------------------------"
	@echo "Running self-update on GDK"
	@echo "--------------------------"
	@echo ""
	cd ${gitlab_development_root} && \
		git stash && \
		git checkout master && \
		git fetch && \
		support/self-update-git-worktree

# Update gitlab, gitlab-shell, gitlab-workhorse, gitlab-pages and gitaly
# Pull gitlab directory first since dependencies are linked from there.
update: ensure-postgres-running unlock-dependency-installers gitlab/.git/pull gitlab-shell-update gitlab-workhorse-update gitlab-pages-update gitaly-update gitlab-update

ensure-postgres-running:
	@test -f ${postgres_data_dir}/postmaster.pid || \
	test "${IGNORE_INSTALL_WARNINGS}" = "true" || \
	(echo "WARNING: Postgres is not running. Run 'gdk run db' or 'gdk run' in another shell." && echo "WARNING: Hit <ENTER> to ignore or <CTRL-C> to quit." && read v;)

gitlab-update: ensure-postgres-running gitlab/.git/pull gitlab-setup
	cd ${gitlab_development_root}/gitlab && \
		bundle exec rake db:migrate db:test:prepare

gitlab-shell-update: gitlab-shell/.git/pull gitlab-shell-setup

gitlab/.git/pull:
	cd ${gitlab_development_root}/gitlab && \
		git checkout -- Gemfile.lock db/schema.rb && \
		git stash && \
		git checkout master && \
		git pull --ff-only

gitlab-shell/.git/pull:
	cd ${gitlab_development_root}/gitlab-shell && \
		git stash && \
		git fetch --all --tags --prune && \
		git checkout "${gitlab_shell_version}"

gitaly-update: gitaly/.git/pull gitaly-clean gitaly/bin/gitaly

.PHONY: gitaly/.git/pull
gitaly/.git/pull: ${gitaly_clone_dir}/.git ${gitaly_proto_clone_dir}/.git
	cd ${gitaly_clone_dir} && \
		git stash && \
		git fetch --all --tags --prune && \
		git checkout "${gitaly_version}"
	cd ${gitaly_proto_clone_dir} && \
		git stash && \
		git checkout master && \
		git pull --ff-only

gitaly-clean:
	rm -rf ${gitaly_assembly_dir}
	rm -rf gitlab/tmp/tests/gitaly

.PHONY: gitaly/bin/gitaly
gitaly/bin/gitaly: check-go-version ${gitaly_clone_dir}/.git
	make -C ${gitaly_clone_dir} assemble ASSEMBLY_ROOT=${gitaly_assembly_dir} BUNDLE_FLAGS=--no-deployment BUILD_TAGS="${tracer_build_tags}"
	mkdir -p ${gitlab_development_root}/gitaly/bin
	ln -sf ${gitaly_assembly_dir}/bin/* ${gitlab_development_root}/gitaly/bin
	rm -rf ${gitlab_development_root}/gitaly/ruby
	ln -sf ${gitaly_assembly_dir}/ruby ${gitlab_development_root}/gitaly/ruby

# Set up supporting services

support-setup: .ruby-version foreman Procfile redis gitaly-setup jaeger-setup postgresql openssh-setup nginx-setup registry-setup elasticsearch-setup
	@echo ""
	@echo "*********************************************"
	@echo "************** Setup finished! **************"
	@echo "*********************************************"
	cat HELP
	@echo "*********************************************"
	@if ${auto_devops_enabled}; then \
		echo "Tunnel URLs"; \
		echo ""; \
		echo "GitLab: https://${hostname}"; \
		echo "Registry: https://${registry_host}"; \
		echo "*********************************************"; \
	fi

gdk.yml:
	touch $@

Procfile: gdk.yml gdk-defaults.yml.erb auto_devops_enabled auto_devops_gitlab_port auto_devops_registry_port
	rake $@

redis: redis/redis.conf

redis/redis.conf: redis/redis.conf.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

postgresql: postgresql/data

postgresql/data:
	${postgres_bin_dir}/initdb --locale=C -E utf-8 ${postgres_data_dir}
	support/bootstrap-rails

postgresql/port:
	./support/postgres-port ${postgres_dir} ${postgresql_port}

postgresql-sensible-defaults:
	./support/postgresql-sensible-defaults ${postgres_dir}

postgresql-replication-primary: postgresql-replication/access postgresql-replication/role postgresql-replication/config

postgresql-replication-secondary: postgresql-replication/data postgresql-replication/access postgresql-replication/backup postgresql-replication/config

postgresql-replication-primary-create-slot: postgresql-replication/slot

postgresql-replication/data:
	${postgres_bin_dir}/initdb --locale=C -E utf-8 ${postgres_data_dir}

postgresql-replication/access:
	cat support/pg_hba.conf.add >> ${postgres_data_dir}/pg_hba.conf

postgresql-replication/role:
	${postgres_bin_dir}/psql -h ${postgres_dir} -p ${postgresql_port} -d postgres -c "CREATE ROLE ${postgres_replication_user} WITH REPLICATION LOGIN;"

postgresql-replication/backup:
	$(eval postgres_primary_dir := $(realpath postgresql-primary))
	$(eval postgres_primary_port := $(shell cat ${postgres_primary_dir}/../postgresql_port 2>/dev/null || echo '5432'))

	psql -h ${postgres_primary_dir} -p ${postgres_primary_port} -d postgres -c "select pg_start_backup('base backup for streaming rep')"
	rsync -cva --inplace --exclude="*pg_xlog*" --exclude="*.pid" ${postgres_primary_dir}/data postgresql
	psql -h ${postgres_primary_dir} -p ${postgres_primary_port} -d postgres -c "select pg_stop_backup(), current_timestamp"
	./support/recovery.conf ${postgres_primary_dir} ${postgres_primary_port} > ${postgres_data_dir}/recovery.conf
	$(MAKE) postgresql/port

postgresql-replication/slot:
	${postgres_bin_dir}/psql -h ${postgres_dir} -p ${postgresql_port} -d postgres -c "SELECT * FROM pg_create_physical_replication_slot('gitlab_gdk_replication_slot');"

postgresql-replication/list-slots:
	${postgres_bin_dir}/psql -h ${postgres_dir} -p ${postgresql_port} -d postgres -c "SELECT * FROM pg_replication_slots;"

postgresql-replication/drop-slot:
	${postgres_bin_dir}/psql -h ${postgres_dir} -p ${postgresql_port} -d postgres -c "SELECT * FROM pg_drop_replication_slot('gitlab_gdk_replication_slot');"

postgresql-replication/config:
	./support/postgres-replication ${postgres_dir}

# Setup GitLab Geo databases

.PHONY: geo-setup geo-cursor
geo-setup: Procfile geo-cursor gitlab/config/database_geo.yml postgresql/geo

geo-cursor:
	grep '^geo-cursor:' Procfile || (printf ',s/^#geo-cursor/geo-cursor/\nwq\n' | ed -s Procfile)

gitlab/config/database_geo.yml: database_geo.yml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

postgresql/geo:
	${postgres_bin_dir}/initdb --locale=C -E utf-8 postgresql-geo/data
	grep '^postgresql-geo:' Procfile || (printf ',s/^#postgresql-geo/postgresql-geo/\nwq\n' | ed -s Procfile)
	support/bootstrap-geo

postgresql/geo-fdw: postgresql/geo-fdw/development/create postgresql/geo-fdw/test/create

# Function to read values from database.yml, parameters:
#   - file: e.g. database, database_geo
#   - environment: e.g. development, test
#   - value: e.g. host, port
from_db_config = $(shell grep -A6 "$(2):" ${gitlab_development_root}/gitlab/config/$(1).yml | grep -m1 "$(3):" | cut -d ':' -f 2 | tr -d ' ')

postgresql/geo-fdw/%: dbname = $(call from_db_config,database_geo,$*,database)
postgresql/geo-fdw/%: fdw_dbname = $(call from_db_config,database,$*,database)
postgresql/geo-fdw/%: fdw_host = $(call from_db_config,database,$*,host)
postgresql/geo-fdw/%: fdw_port = $(call from_db_config,database,$*,port)
postgresql/geo-fdw/test/%: rake_namespace = test:

postgresql/geo-fdw/%/create:
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "CREATE EXTENSION IF NOT EXISTS postgres_fdw;"
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "CREATE SERVER gitlab_secondary FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '$(fdw_host)', dbname '${fdw_dbname}', port '$(fdw_port)' );"
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "CREATE USER MAPPING FOR current_user SERVER gitlab_secondary OPTIONS (user '$(USER)');"
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "CREATE SCHEMA IF NOT EXISTS gitlab_secondary;"
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "GRANT USAGE ON FOREIGN SERVER gitlab_secondary TO current_user;"
	cd ${gitlab_development_root}/gitlab && bundle exec rake geo:db:${rake_namespace}refresh_foreign_tables

postgresql/geo-fdw/%/drop:
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "DROP SERVER gitlab_secondary CASCADE;"
	${postgres_bin_dir}/psql -h ${postgres_geo_dir} -p ${postgresql_geo_port} -d ${dbname} -c "DROP SCHEMA gitlab_secondary;"

postgresql/geo-fdw/%/rebuild:
	$(MAKE) postgresql/geo-fdw/$*/drop || true
	$(MAKE) postgresql/geo-fdw/$*/create

.PHONY: foreman
foreman:
	command -v $@ > /dev/null || gem install $@

.ruby-version:
	ln -s ${gitlab_development_root}/gitlab/.ruby-version ${gitlab_development_root}/$@

localhost.crt: localhost.key

localhost.key:
	openssl req -new -subj "/CN=localhost/" -x509 -days 365 -newkey rsa:2048 -nodes -keyout "localhost.key" -out "localhost.crt"
	chmod 600 $@

gitlab-workhorse-setup: gitlab-workhorse/bin/gitlab-workhorse gitlab-workhorse/config.toml

gitlab-workhorse/config.toml: gitlab-workhorse/config.toml.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

gitlab-workhorse-update: ${gitlab_workhorse_clone_dir}/.git gitlab-workhorse/.git/pull gitlab-workhorse-clean-bin gitlab-workhorse/bin/gitlab-workhorse

gitlab-workhorse-clean-bin:
	rm -rf gitlab-workhorse/bin

.PHONY: gitlab-workhorse/bin/gitlab-workhorse
gitlab-workhorse/bin/gitlab-workhorse: check-go-version ${gitlab_workhorse_clone_dir}/.git
	GOPATH=${gitlab_development_root}/gitlab-workhorse GOBIN=${gitlab_development_root}/gitlab-workhorse/bin GO111MODULE=off go install -tags "${tracer_build_tags}" gitlab.com/gitlab-org/gitlab-workhorse/...

${gitlab_workhorse_clone_dir}/.git:
	git clone --quiet --branch "${workhorse_version}" ${git_depth_param} ${gitlab_workhorse_repo} ${gitlab_workhorse_clone_dir}

gitlab-workhorse/.git/pull:
	cd ${gitlab_workhorse_clone_dir} && \
		git stash && \
		git fetch --all --tags --prune && \
		git checkout "${workhorse_version}"

gitlab-pages-setup: gitlab-pages/bin/gitlab-pages

gitlab-pages-update: ${gitlab_pages_clone_dir}/.git gitlab-pages/.git/pull gitlab-pages-clean-bin gitlab-pages/bin/gitlab-pages

gitlab-pages-clean-bin:
	rm -rf gitlab-pages/bin

.PHONY: gitlab-pages/bin/gitlab-pages
gitlab-pages/bin/gitlab-pages: check-go-version ${gitlab_pages_clone_dir}/.git
	GOPATH=${gitlab_development_root}/gitlab-pages GOBIN=${gitlab_development_root}/gitlab-pages/bin GO111MODULE=off go install gitlab.com/gitlab-org/gitlab-pages

${gitlab_pages_clone_dir}/.git:
	git clone --quiet --branch "${pages_version}" ${git_depth_param} ${gitlab_pages_repo} ${gitlab_pages_clone_dir}

gitlab-pages/.git/pull:
	cd ${gitlab_pages_clone_dir} && \
		git stash &&\
		git fetch --all --tags --prune && \
		git checkout "${pages_version}"

influxdb-setup: influxdb/influxdb.conf influxdb/bin/influxd influxdb/meta/meta.db

influxdb/bin/influxd:
	cd influxdb && ${MAKE}

influxdb/meta/meta.db: Procfile
	grep '^influxdb:' Procfile || (printf ',s/^#influxdb/influxdb/\nwq\n' | ed -s Procfile)
	support/bootstrap-influxdb 8086

influxdb/influxdb.conf: influxdb/influxdb.conf.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

grafana-setup: grafana/grafana.ini grafana/bin/grafana-server grafana/gdk-pg-created grafana/gdk-data-source-created

grafana/bin/grafana-server:
	cd grafana && ${MAKE}

grafana/grafana.ini: grafana/grafana.ini.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		-e "s/GDK_USERNAME/${username}/g" \
		"$<"

grafana/gdk-pg-created:
	PATH=${postgres_bin_dir}:${PATH} support/create-grafana-db
	touch $@

grafana/gdk-data-source-created:
	grep '^grafana:' Procfile || (printf ',s/^#grafana/grafana/\nwq\n' | ed -s Procfile)
	support/bootstrap-grafana
	touch $@

performance-metrics-setup: Procfile influxdb-setup grafana-setup

openssh-setup: openssh/sshd_config openssh/ssh_host_rsa_key

openssh/sshd_config: openssh/sshd_config.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		-e "s/GDK_USERNAME/${username}/g" \
		"$<"

openssh/ssh_host_rsa_key:
	ssh-keygen -f $@ -N '' -t rsa

nginx-setup: nginx/conf/nginx.conf nginx/logs nginx/tmp

nginx/conf/nginx.conf: nginx/conf/nginx.conf.example
	bin/safe-sed "$@" \
		-e "s|/home/git|${gitlab_development_root}|g" \
		"$<"

nginx/logs:
	mkdir -p $@

nginx/tmp:
	mkdir -p $@

registry-setup: registry/storage registry/config.yml localhost.crt

registry/storage:
	mkdir -p $@

registry/config.yml: auto_devops_enabled
	cp registry/config.yml.example $@
	if ${auto_devops_enabled}; then \
		protocol='https' gitlab_host=${hostname} gitlab_port=${port} registry_port=${registry_port} \
		support/edit-registry-config.yml $@; \
	else \
		gitlab_host=${gitlab_from_container} gitlab_port=${port} registry_port=${registry_port} \
		support/edit-registry-config.yml $@; \
	fi

elasticsearch-setup: elasticsearch/bin/elasticsearch

elasticsearch/bin/elasticsearch: elasticsearch-${elasticsearch_version}.tar.gz
	rm -rf elasticsearch
	tar zxf elasticsearch-${elasticsearch_version}.tar.gz
	mv elasticsearch-${elasticsearch_version} elasticsearch
	touch $@

elasticsearch-${elasticsearch_version}.tar.gz:
	curl -L -o $@.tmp https://artifacts.elastic.co/downloads/elasticsearch/$@
	echo "${elasticsearch_tar_gz_sha1}  $@.tmp" | shasum -a1 -c -
	mv $@.tmp $@

object-storage-setup: minio/data/lfs-objects minio/data/artifacts minio/data/uploads minio/data/packages

minio/data/%:
	mkdir -p $@

pry:
	grep '^#rails-web:' Procfile || (printf ',s/^rails-web/#rails-web/\nwq\n' | ed -s Procfile)
	@echo ""
	@echo "Commented out 'rails-web' line in the Procfile. Use 'make pry-off' to reverse."
	@echo "You can now use Pry for debugging by using 'gdk run' in one terminal, and 'gdk run thin' in another."

pry-off:
	grep '^rails-web:' Procfile || (printf ',s/^#rails-web/rails-web/\nwq\n' | ed -s Procfile)
	@echo ""
	@echo "Re-enabled 'rails-web' in the Procfile. Debugging with Pry will no longer work."

ifeq ($(jaeger_server_enabled),true)
.PHONY: jaeger-setup
jaeger-setup: jaeger/jaeger-${jaeger_version}/jaeger-all-in-one
else
.PHONY: jaeger-setup
jaeger-setup:
	@echo Skipping jaeger-setup as Jaeger has been disabled.
endif

jaeger-artifacts/jaeger-${jaeger_version}.tar.gz:
	mkdir -p $(@D)
	./bin/download-jaeger "${jaeger_version}" "$@"
	# To save disk space, delete old versions of the download,
	# but to save bandwidth keep the current version....
	find jaeger-artifacts ! -path "$@" -type f -exec rm -f {} + -print

jaeger/jaeger-${jaeger_version}/jaeger-all-in-one: jaeger-artifacts/jaeger-${jaeger_version}.tar.gz
	mkdir -p "jaeger/jaeger-${jaeger_version}"
	tar -xf "$<" -C "jaeger/jaeger-${jaeger_version}" --strip-components 1

clean-config:
	rm -rf \
	gitlab/config/gitlab.yml \
	gitlab/config/database.yml \
	gitlab/config/unicorn.rb \
	gitlab/config/puma.rb \
	gitlab/config/resque.yml \
	gitlab-shell/config.yml \
	gitlab-shell/.gitlab_shell_secret \
	redis/redis.conf \
	.ruby-version \
	Procfile \
	gitlab-workhorse/config.toml \
	gitaly/config.toml \
	nginx/conf/nginx.conf \
	registry/config.yml \
	jaeger

touch-examples:
	touch \
	$(gitaly_clone_dir)/config.toml.example \
	Procfile.erb \
	database.yml.example \
	database_geo.yml.example \
	gitlab-shell/config.yml.example \
	gitlab-workhorse/config.toml.example \
	gitlab/config/database.yml.example \
	gitlab/config/puma.example.development.rb \
	gitlab/config/unicorn.rb.example.development \
	grafana/grafana.ini.example \
	influxdb/influxdb.conf.example \
	nginx/conf/nginx.conf.example \
	openssh/sshd_config.example \
	redis/redis.conf.example \
	redis/resque.yml.example \
	registry/config.yml.example \
	support/templates/gitlab.yml.erb

unlock-dependency-installers:
	rm -f \
	.gitlab-bundle \
	.gitlab-shell-bundle \
	.gitlab-yarn \
	.gettext \

.PHONY: verify
verify: verify-editorconfig

.PHONY: verify-editorconfig
verify-editorconfig: install-eclint
	eclint check $$(git ls-files) || (echo "editorconfig check failed. Please run \`make correct\`" && exit 1)

.PHONY: correct
correct: correct-editorconfig

.PHONY: correct-editorconfig
correct-editorconfig: install-eclint
	eclint fix $$(git ls-files)

.PHONY: install-eclint
install-eclint:
	# Some distros come with `npm`, some with `yarn`
	# So, we attempt to install eclint with either package manager
	(command -v eclint > /dev/null) || \
	((command -v npm > /dev/null) && npm install -g eclint) || \
	((command -v yarn > /dev/null) && yarn global add eclint)