# GitLab software-release
[buildout]
extends =
    buildout.hash.cfg
    ../../stack/slapos.cfg
    ../../stack/nodejs.cfg
    ../../stack/monitor/buildout.cfg
    ../../component/ruby/buildout.cfg
    ../../component/golang/buildout.cfg
    ../../component/postgresql/buildout.cfg
    ../../component/redis/buildout.cfg
    ../../component/cmake/buildout.cfg
    ../../component/icu/buildout.cfg
    ../../component/pkgconfig/buildout.cfg
    ../../component/openssl/buildout.cfg
    ../../component/nginx/buildout.cfg
    ../../component/xz-utils/buildout.cfg
    ../../component/zlib/buildout.cfg
    gowork.cfg

#   for instance
    ../../component/coreutils/buildout.cfg
    ../../component/bash/buildout.cfg
    ../../component/grep/buildout.cfg
    ../../component/bzip2/buildout.cfg
    ../../component/curl/buildout.cfg
    ../../component/tar/buildout.cfg
    ../../component/gzip/buildout.cfg
    ../../component/dcron/buildout.cfg
    ../../component/logrotate/buildout.cfg

parts =
    golang1.13
    git
    postgresql10
    redis28
    cmake
    icu
    pkgconfig
    nginx-output

    gowork
    gitlab-workhorse
    gitaly-build
    gitlab-shell/vendor
    gitlab/vendor/bundle
    gitlab_npm
    gitlab-backup

#   for instance
    instance.cfg

    slapos-cookbook
    eggs

    bash
    curl
    watcher
    gitlab-export
    gzip
    dcron-output
    logrotate

[slapos.cookbook-repository]
revision = 571d6514f7290e8faa9439c4b86aa2f6c87df261

[nodejs]
<= nodejs-12.18.3
[yarn]
<= yarn-1.16.0

# Gitlab backup (git-backup) is failing (segfault) with recent git version > 2.30.9
# We will use git 2.30.9 version for production upgrade
# TODO: fix the issue with git and use latest version
[git]
url = https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.30.9.tar.xz
md5sum = c1d42936036cc44a448738329c821569

############################
#   Software compilation   #
############################

# python with eggs, that will be used in gitlab
# gitlab-markup call the command `python3 /path/to/commands/rest2html` which
# require docutils
# https://gitlab.com/gitlab-org/gitlab-markup/-/blob/master/lib/github/markups.rb
[docutils-download]
recipe = slapos.recipe.build:download
shared = true
url = https://files.pythonhosted.org/packages/2f/e0/3d435b34abd2d62e8206171892f174b180cd37b09d57b924ca5c2ef2219d/${:filename}
filename = docutils-0.16.tar.gz
md5sum = 44952782107930ddfcd37ae48eee0857

[python-4gitlab]
recipe = slapos.recipe.build
docutils = ${docutils-download:target}
init =
  # add the python executable in the options dict so that
  # buildout signature changes if python executable changes
  import os, sys
  options['bin'] = python = os.path.join(location, 'bin')

install =
  import os, sys
  python = self.buildout['python3']['executable']
  call([python, '-m', 'venv', '--clear', location])
  pip = os.path.join(location, 'bin', 'pip')
  call([pip, 'install', '--no-index', options['docutils']])
  call([pip, 'uninstall', '-y', 'pip', 'setuptools'])
  # selftest
  python = os.path.join(location, 'bin', 'python')
  call([python, '-c', 'import docutils'])

# Need ruby 2.6.5
# rubygemsrecipe with fixed url and this way pinned rubygems version
[rubygemsrecipe]
recipe  = rubygemsrecipe
url     = https://rubygems.org/rubygems/rubygems-3.1.2.zip

# bundler, that we'll use to
# - install gems for gitlab
# - run gitlab services / jobs  (via `bundle exec ...`)
[bundler-4gitlab]
<= rubygemsrecipe
ruby-location = ${ruby2.6:location}
ruby-executable = ${:ruby-location}/bin/ruby
gems    =
  bundler==1.17.3

# bin installed here
bundle  = ${buildout:bin-directory}/bundle
# Gitaly need bundler 1.17.3 which is not the default version at the end
bundle1.17.3 = ${buildout:parts-directory}/${:_buildout_section_name_}/lib/ruby/gems/gems/bundler-1.17.3/exe/

# install together with dependencies of gitlab, which we cannot specify using
#   --with-... gem option
# ( reason: rubygemsrecipe hardcodes PATH inside generated bin/* and it is
#   impossible to adjust it later )
#
# bundle exec <smth>                ; <smth> starts with `#!/usr/bin/env ruby` as rubygems
# Rugged needs: cmake, pkgconfig
# execjs needs: nodejs
# rails needs db client program on path: psql
# gitlab wants to check redis version via running: redis-cli
# gitlab wants git to be really on path ( it uses git from abspath defined in
#   gitlab.yml, but there are not all cases like this, e.g. in
#   https://gitlab.com/gitlab-org/gitlab_git/blob/2f0d3c1a/lib/gitlab_git/repository.rb#L259 )
# gitlab (via github-markup) wants to convert rst -> html via running: python (with docutils egg)
environment =

  PATH    = ${python-4gitlab:bin}:${yarn:location}/bin:${:ruby-location}/bin:${cmake:location}/bin:${pkgconfig:location}/bin:${nodejs:location}/bin:${postgresql10:location}/bin:${redis28:location}/bin:${git:location}/bin:${buildout:bin-directory}:%(PATH)s


# gitlab, gitlab-shell & gitlab-workhorse checked out as git repositories
# pinned to exact commit
[git-repository]
recipe  = slapos.recipe.build:gitclone
git-executable = ${git:location}/bin/git

[gitlab-repository]
<= git-repository
repository = https://lab.nexedi.com/nexedi/gitlab-ce.git
revision = v12.10.14-12-g7ce27b49193
location = ${buildout:parts-directory}/gitlab

[gitlab-shell-repository]
<= git-repository
repository = https://gitlab.com/gitlab-org/gitlab-shell.git
revision = v12.2.0
location = ${buildout:parts-directory}/gitlab-shell

[gitaly-repository]
<= git-repository
repository = https://gitlab.com/gitlab-org/gitaly.git
revision = v12.10.14
location = ${buildout:parts-directory}/gitaly

[gitlab-workhorse-repository]
<= git-repository
repository = https://lab.nexedi.com/nexedi/gitlab-workhorse.git
revision = v8.30.3-19-g919c9b532c

# build needed-by-gitlab gems via bundler
[gitlab/vendor/bundle]
recipe  = slapos.recipe.cmmi
path    = ${gitlab-repository:location}
bundle  = ${bundler-4gitlab:bundle}

configure-command = cd ${:path} &&
    ${:bundle} config --local build.charlock_holmes --with-icu-dir=${icu:location}  &&
    ${:bundle} config --local build.pg --with-pg-config=${postgresql10:location}/bin/pg_config &&
    ${:bundle} config --local build.re2 --with-re2-dir=${re2:location} &&
    ${:bundle} config --local build.nokogiri --with-zlib-dir=${zlib:location} --with-cflags=-I${xz-utils:location}/include --with-ldflags="-L${xz-utils:location}/lib -Wl,-rpath=${xz-utils:location}/lib"
    ${:bundle} config set without 'development test mysql aws kerberos'
    ${:bundle} config set deployment 'true'

make-binary =
make-targets= cd ${:path} && ${:bundle} install
environment =
  PKG_CONFIG_PATH=${openssl-1.0:location}/lib/pkgconfig:${re2:location}/lib/pkgconfig:${icu:location}/lib/pkgconfig:${xz-utils:location}/lib/pkgconfig
  PATH=${pkgconfig:location}/bin:%(PATH)s
  CFLAGS=-I${xz-utils:location}/include

################## Google re2
[re2]
recipe = slapos.recipe.cmmi
url = https://github.com/google/re2/archive/2019-12-01.tar.gz
md5sum = 527eab0c75d6a1a0044c6eefd816b2fb
configure-command = :


[gitlab_npm]
recipe  = slapos.recipe.cmmi
path    = ${gitlab-repository:location}

configure-command = :
make-binary =
make-targets= cd ${:path} && npm install

environment =
  PATH=${nodejs:location}/bin/:%(PATH)s

#our go infrastructure not currently supporting submodules, IIRC
# https://lab.nexedi.com/nexedi/slapos/merge_requests/337
[go_github.com_libgit2_git2go_prepare]
recipe  = slapos.recipe.cmmi
path = ${go_github.com_libgit2_git2go:location}
configure-command = :
make-binary =
make-targets= cd ${go_github.com_libgit2_git2go:location}
              && git submodule update --init
              && sed -i 's/.*--build.*/cmake --build . --target install/' script/build-libgit2-static.sh
              && make install
environment =
  PKG_CONFIG_PATH=${openssl-1.0:location}/lib/pkgconfig:${zlib:location}/lib/pkgconfig
  PATH=${cmake:location}/bin:${pkgconfig:location}/bin:${git:location}/bin:${golang1.13:location}/bin:${buildout:bin-directory}:%(PATH)s
  GOPATH=${gowork:directory}

[gowork.goinstall]
git2go = ${go_github.com_libgit2_git2go_prepare:path}/vendor/libgit2/install
command = bash -c ". ${gowork:env.sh} && CGO_CFLAGS=-I${:git2go}/include CGO_LDFLAGS='-L${:git2go}/lib -lgit2' go install ${gowork:buildflags} -v $(echo -n '${gowork:install}' |tr '\n' ' ') && go test -v lab.nexedi.com/kirr/git-backup"

[gowork]
golang  = ${golang1.13:location}
# gitlab.com/gitlab-org/gitlab-workhorse
# gitlab.com/gitlab-org/gitlab-workhorse/cmd/gitlab-zip-cat
# gitlab.com/gitlab-org/gitlab-workhorse/cmd/gitlab-zip-metadata
install =
  lab.nexedi.com/kirr/git-backup
cpkgpath =
    ${openssl-1.0:location}/lib/pkgconfig
    ${zlib:location}/lib/pkgconfig
    ${go_github.com_libgit2_git2go_prepare:path}/vendor/libgit2/install/lib/pkgconfig
buildflags = --tags "static"

[gitlab-workhorse]
recipe = slapos.recipe.cmmi
path = ${gitlab-workhorse-repository:location}
configure-command = :
make-binary =
make-targets =
 . ${gowork:env.sh} && make test && make install PREFIX=${gowork:directory}

[gitlab-backup]
recipe = plone.recipe.command
stop-on-error = true
command =
  cp -a ${go_lab.nexedi.com_kirr_git-backup:location}/contrib/gitlab-backup ${gowork:bin}
update-command = ${:command}

[gitaly-build]
recipe = slapos.recipe.cmmi
path = ${gitaly-repository:location}
bundle  = ${bundler-4gitlab:bundle}

configure-command = cd ${:path}/ruby &&
    ${:bundle} config --local build.charlock_holmes --with-icu-dir=${icu:location}
make-binary =
make-targets =
  . ${gowork:env.sh} &&
  unset GOBIN &&
  make
post-install =
  # solve the problem error="not executable: ruby/git-hooks/pre-receive"
  chmod 755 ${:path}/ruby/git-hooks/gitlab-shell-hook
environment =
  PKG_CONFIG_PATH=${openssl-1.0:location}/lib/pkgconfig:${icu:location}/lib/pkgconfig
  PATH=${pkgconfig:location}/bin:${ruby2.6:location}/bin:%(PATH)s

[xnice-repository]
# to get kirr's misc repo containing xnice script for executing processes
# with lower priority (used for backup script inside the cron)
<= git-repository
repository = https://lab.nexedi.com/kirr/misc.git
revision =  4073572ea700bf1b115f3a135aebebe5b3b824e4
location = ${buildout:parts-directory}/misc

# build needed-by-gitlab-shell gems via bundler
# ( there is not vendor/ dir in gitlab-shell, so to avoid having buildout error
#   on mkdir vendor/bundle, this part name is just /vendor )
[gitlab-shell/vendor]
recipe  = slapos.recipe.cmmi
path    = ${gitlab-shell-repository:location}
bundle  = ${bundler-4gitlab:bundle}

configure-command = true
make-binary =
make-targets= cd ${:path} &&
# Compile go binary
    . ${gowork:env.sh} && make build &&
    ${:bundle} install --deployment  --without development test
environment =
  PATH=${ruby2.6:location}/bin:%(PATH)s

###############################
#   Trampoline for instance   #
###############################

# eggs for instance.cfg
[eggs]
recipe  = zc.recipe.egg
eggs    =
    plone.recipe.command
    cns.recipe.symlink
    collective.recipe.template


[instance.cfg]
recipe  = slapos.recipe.template
url     = ${:_profile_base_location_}/${:filename}
output  = ${buildout:directory}/instance.cfg

# macro: download a shell script and put it rendered into <software>/bin/
[binsh]
recipe  = slapos.recipe.template:jinja2
url = ${:_profile_base_location_}/${:_update_hash_filename_}
output= ${buildout:bin-directory}/${:_buildout_section_name_}
mode    = 0755
context =
    section bash    bash

[watcher]
<= binsh

[gitlab-export]
<= binsh



# macro: download a file named in buildout.hash.cfg via _update_hash_filename_
#
#   [filename]
#   <= download-file
[download-file]
recipe  = slapos.recipe.build:download
url     = ${:_profile_base_location_}/${:_update_hash_filename_}
destination = ${buildout:directory}/${:_buildout_section_name_}

[database.yml.in]
<= download-file

[gitconfig.in]
<= download-file

[gitlab-parameters.cfg]
<= download-file

[gitlab-shell-config.yml.in]
<= download-file

[gitlab-unicorn-startup.in]
<= download-file

[gitlab.yml.in]
<= download-file

[gitaly-config.toml.in]
<= download-file

[instance-gitlab.cfg.in]
<= download-file

[instance-gitlab-export.cfg.in]
<= download-file

[macrolib.cfg.in]
<= download-file

[nginx-gitlab-http.conf.in]
<= download-file

[nginx.conf.in]
<= download-file

[rack_attack.rb.in]
<= download-file

[resque.yml.in]
<= download-file

[smtp_settings.rb.in]
<= download-file

[template-gitlab-resiliency-restore.sh.in]
<= download-file

[unicorn.rb.in]
<= download-file

[gitlab-demo-backup.git]
recipe = slapos.recipe.build:download-unpacked
url = https://lab.nexedi.com/alain.takoudjou/labdemo.backup/repository/archive.tar.gz?ref=master
md5sum = d40e5e211dc9a4e5ada9c0250377c639

[versions]
docutils = 0.16
cns.recipe.symlink = 0.2.3
plone.recipe.command = 1.1
z3c.recipe.scripts = 1.0.1