git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/gitlab: Use postgresql module options to provision local db

Use the postgresql module to provision a local db (if databaseCreateLocally is true) instead of doing this locally. Switch to using the local unix socket for db connections by default; this is needed since dbs created by the postgresql module only support peer authentication. Instead of running the rake tasks db:schema:load, db:migrate and db:seed_fu, run gitlab:db:configure, which in turn runs these tasks when needed. Solves issue #53852 for gitlab.

commitb351454cac1eca1dc76e54e880e86d7c2bb21ffa authortalyz <kim.lindberger@gmail.com> dateThu 1 Aug 2019, 16:59 (7 years ago) parentcbdf94c
1 file changed+76−53
nixos/modules/services/misc/gitlab.nix+76−53
nixos/modules/services/misc/gitlab.nix+76−53
@@ -12,7 +12,6 @@ let
1212 gitlabSocket = "${cfg.statePath}/tmp/sockets/gitlab.socket";
1313 gitalySocket = "${cfg.statePath}/tmp/sockets/gitaly.socket";
1414 pathUrlQuote = url: replaceStrings ["/"] ["%2F"] url;
15− pgSuperUser = config.services.postgresql.superUser;
1615
1716 databaseConfig = {
1817 production = {
@@ -237,8 +236,11 @@ in {
237236
238237 databaseHost = mkOption {
239238 type = types.str;
240− default = "127.0.0.1";
241− description = "Gitlab database hostname.";
239+ default = "";
240+ description = ''
241+ Gitlab database hostname. An empty string means <quote>use
242+ local unix socket connection</quote>.
243+ '';
242244 };
243245
244246 databasePasswordFile = mkOption {
@@ -252,6 +254,17 @@ in {
252254 '';
253255 };
254256
257+ databaseCreateLocally = mkOption {
258+ type = types.bool;
259+ default = true;
260+ description = ''
261+ Whether a database should be automatically created on the
262+ local host. Set this to <literal>false</literal> if you plan
263+ on provisioning a local database yourself or use an external
264+ one.
265+ '';
266+ };
267+
255268 databaseName = mkOption {
256269 type = types.str;
257270 default = "gitlab";
@@ -498,12 +511,16 @@ in {
498511
499512 assertions = [
500513 {
501− assertion = cfg.initialRootPasswordFile != null;
502− message = "services.gitlab.initialRootPasswordFile must be set!";
514+ assertion = cfg.databaseCreateLocally -> (cfg.user == cfg.databaseUsername);
515+ message = "For local automatic database provisioning services.gitlab.user and services.gitlab.databaseUsername should be identical.";
516+ }
517+ {
518+ assertion = (cfg.databaseHost != "") -> (cfg.databasePasswordFile != null);
519+ message = "When services.gitlab.databaseHost is customized, services.gitlab.databasePasswordFile must be set!";
503520 }
504521 {
505− assertion = cfg.databasePasswordFile != null;
506− message = "services.gitlab.databasePasswordFile must be set!";
522+ assertion = cfg.initialRootPasswordFile != null;
523+ message = "services.gitlab.initialRootPasswordFile must be set!";
507524 }
508525 {
509526 assertion = cfg.secrets.secretFile != null;
@@ -527,8 +544,31 @@ in {
527544
528545 # Redis is required for the sidekiq queue runner.
529546 services.redis.enable = mkDefault true;
547+
530548 # We use postgres as the main data store.
531− services.postgresql.enable = mkDefault true;
549+ services.postgresql = optionalAttrs cfg.databaseCreateLocally {
550+ enable = true;
551+ ensureUsers = singleton { name = cfg.databaseUsername; };
552+ };
553+ # The postgresql module doesn't currently support concepts like
554+ # objects owners and extensions; for now we tack on what's needed
555+ # here.
556+ systemd.services.postgresql.postStart = mkAfter (optionalString cfg.databaseCreateLocally ''
557+ $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${cfg.databaseName}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${cfg.databaseName}" OWNER "${cfg.databaseUsername}"'
558+ current_owner=$($PSQL -tAc "SELECT pg_catalog.pg_get_userbyid(datdba) FROM pg_catalog.pg_database WHERE datname = '${cfg.databaseName}'")
559+ if [[ "$current_owner" != "${cfg.databaseUsername}" ]]; then
560+ $PSQL -tAc 'ALTER DATABASE "${cfg.databaseName}" OWNER TO "${cfg.databaseUsername}"'
561+ if [[ -e "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}" ]]; then
562+ echo "Reassigning ownership of database ${cfg.databaseName} to user ${cfg.databaseUsername} failed on last boot. Failing..."
563+ exit 1
564+ fi
565+ touch "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
566+ $PSQL "${cfg.databaseName}" -tAc "REASSIGN OWNED BY \"$current_owner\" TO \"${cfg.databaseUsername}\""
567+ rm "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
568+ fi
569+ $PSQL '${cfg.databaseName}' -tAc "CREATE EXTENSION IF NOT EXISTS pg_trgm"
570+ '');
571+
532572 # Use postfix to send out mails.
533573 services.postfix.enable = mkDefault true;
534574
@@ -675,15 +715,15 @@ in {
675715 gnupg
676716 ];
677717 preStart = ''
678− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
679− ${pkgs.sudo}/bin/sudo -u ${cfg.user} rm -rf ${cfg.statePath}/db/*
680− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
681− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
718+ cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
719+ rm -rf ${cfg.statePath}/db/*
720+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
721+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
682722
683− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${cfg.packages.gitlab-shell}/bin/install
723+ ${cfg.packages.gitlab-shell}/bin/install
684724
685725 ${optionalString cfg.smtp.enable ''
686− install -o ${cfg.user} -g ${cfg.group} -m u=rw ${smtpSettings} ${cfg.statePath}/config/initializers/smtp_settings.rb
726+ install -m u=rw ${smtpSettings} ${cfg.statePath}/config/initializers/smtp_settings.rb
687727 ${optionalString (cfg.smtp.passwordFile != null) ''
688728 smtp_password=$(<'${cfg.smtp.passwordFile}')
689729 ${pkgs.replace}/bin/replace-literal -e '@smtpPassword@' "$smtp_password" '${cfg.statePath}/config/initializers/smtp_settings.rb'
@@ -695,6 +735,24 @@ in {
695735
696736 ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
697737
738+ ${if cfg.databasePasswordFile != null then ''
739+ export db_password="$(<'${cfg.databasePasswordFile}')"
740+
741+ if [[ -z "$db_password" ]]; then
742+ >&2 echo "Database password was an empty string!"
743+ exit 1
744+ fi
745+
746+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
747+ '.production.password = $ENV.db_password' \
748+ >'${cfg.statePath}/config/database.yml'
749+ ''
750+ else ''
751+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
752+ >'${cfg.statePath}/config/database.yml'
753+ ''
754+ }
755+
698756 if [[ -h '${cfg.statePath}/config/secrets.yml' ]]; then
699757 rm '${cfg.statePath}/config/secrets.yml'
700758 fi
@@ -708,54 +766,19 @@ in {
708766 db_key_base: $ENV.otp,
709767 openid_connect_signing_key: $ENV.jws}}' \
710768 > '${cfg.statePath}/config/secrets.yml'
711−
712− chown ${cfg.user}:${cfg.group} '${cfg.statePath}/config/secrets.yml' '${cfg.statePath}/gitlab_shell_secret'
713769 )
714770
715− export db_password="$(<'${cfg.databasePasswordFile}')"
716−
717− if [[ -z "$db_password" ]]; then
718− >&2 echo "Database password was an empty string!"
719− exit 1
720− fi
721−
722− ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
723− '.production.password = $ENV.db_password' \
724− >'${cfg.statePath}/config/database.yml'
725− chown ${cfg.user}:${cfg.group} '${cfg.statePath}/config/database.yml'
726−
727− if ! test -e "${cfg.statePath}/db-created"; then
728− if [ "${cfg.databaseHost}" = "127.0.0.1" ]; then
729− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql postgres -c "CREATE ROLE ${cfg.databaseUsername} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '$db_password'"
730− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} ${config.services.postgresql.package}/bin/createdb --owner ${cfg.databaseUsername} ${cfg.databaseName}
731−
732− # enable required pg_trgm extension for gitlab
733− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql ${cfg.databaseName} -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"
734− fi
735−
736− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:schema:load
737−
738− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-created"
739− fi
740−
741− # Always do the db migrations just to be sure the database is up-to-date
742− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:migrate
743−
744− if ! test -e "${cfg.statePath}/db-seeded"; then
745− initial_root_password="$(<'${cfg.initialRootPasswordFile}')"
746− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${gitlab-rake}/bin/gitlab-rake db:seed_fu \
747− GITLAB_ROOT_PASSWORD="$initial_root_password" GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
748− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-seeded"
749− fi
771+ initial_root_password="$(<'${cfg.initialRootPasswordFile}')"
772+ ${gitlab-rake}/bin/gitlab-rake gitlab:db:configure GITLAB_ROOT_PASSWORD="$initial_root_password" \
773+ GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
750774
751775 # We remove potentially broken links to old gitlab-shell versions
752776 rm -Rf ${cfg.statePath}/repositories/**/*.git/hooks
753777
754− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${pkgs.git}/bin/git config --global core.autocrlf "input"
778+ ${pkgs.git}/bin/git config --global core.autocrlf "input"
755779 '';
756780
757781 serviceConfig = {
758− PermissionsStartOnly = true; # preStart must be run as root
759782 Type = "simple";
760783 User = cfg.user;
761784 Group = cfg.group;