Commit 17b4666b authored by Profpatsch's avatar Profpatsch

generate minimal postgres docker container with sangha volume

Creates a docker image with a layer containing the postgres closure (~200MB) and
a bit of minimal system setup needed for docker to run (/etc/hosts, /tmp, &c.).
A volume is initialized with the sangha mock data.
parent e42a9ddc
......@@ -11,13 +11,15 @@ let
imports = [ ./postgres.nix ];
config = {
org.techcultivation.deploy.database.enable = true;
org.techcultivation.deploy.database.buildDockerImage = true;
_module.args.pkgs = pkgs;
};
};
};
sanghaPostgresTemplate = eval.config.test;
sanghaPostgresTemplate = eval.config.passthru;
sanghaPostgresDockerImage = eval.config.generated.dockerImages.sangha;
in {
inherit sanghaPostgresTemplate pkgs;
inherit sanghaPostgresTemplate pkgs sanghaPostgresDockerImage;
}
......@@ -4,15 +4,37 @@ let
postgresConnectionData = {
Host = "";
Port = 0;
Port = 5432;
DbName = "sangha";
User = "sanghauser";
Password = "sanghapass";
SslMode = "disable";
};
sanghaDatabaseLocale = "en_US.UTF-8";
# TODO: use pgtune(1) for full deployment setup
sanghaPostgresConfig =
let
l = sanghaDatabaseLocale;
format = with lib.generators; toKeyValue {
mkKeyValue = mkKeyValueDefault {
mkValueString = v: if builtins.isString v then "'${v}'" else toString v;
} "=";
};
in format {
listen_addresses = "*";
max_connections = 100;
shared_buffers = "128MB";
log_timezone = "UTC";
timezone = "UTC";
lc_messages = l;
lc_monetary = l;
lc_numeric = l;
lc_time = l;
};
sangha = pkgs.sangha;
# sanghaSrc = /home/philip/go/src/gitlab.techcultivation.org/sangha/sangha;
sanghaDatabaseInit = with postgresConnectionData;
pkgs.writeText "sangha-create-db.sql" ''
CREATE DATABASE ${DbName};
......@@ -20,14 +42,42 @@ let
GRANT ALL PRIVILEGES ON DATABASE ${DbName} TO ${User};
'';
postgresInitDatabase = pkgs.runCommand "sangha-db-template" {
nativeBuildInputs = [ pkgs.postgresql pkgs.jq ];
localeFiles = pkgs.glibcLocales.override {
locales = [ (sanghaDatabaseLocale + "/UTF-8") ];
allLocales = false;
};
prependPrefix = prefix: drv: pkgs.runCommand "${drv.name}-prefixed" {} ''
new="$out/${prefix}"
mkdir -p $(dirname "$new")
cp -r ${drv} "$new"
# nix-support folder should remain on the top level
cp -r $new/nix-support $out/
'';
postgresInitDatabase =
let sanghaConfig = pkgs.runCommand "sangha.conf" {} ''
# copy sangha configuration with correct connection data
${lib.getBin pkgs.jq}/bin/jq '.Connections.PostgreSQLConnection *= ${
lib.generators.toJSON {} postgresConnectionData
}' \
${sangha.src}/config.example.json \
> $out
'';
in pkgs.runCommand "sangha-db-template" {
nativeBuildInputs = [ pkgs.postgresql localeFiles ];
passthru = { inherit sanghaConfig; };
} ''
mkdir $out
export PGDATA=$out
# init the postgres database in $out
initdb --nosync --username=postgres
initdb --nosync --username=postgres \
--encoding=UTF8 --locale=${sanghaDatabaseLocale}
# overwrite generated config file
mv $PGDATA/postgresql.conf $PGDATA/postgresql.example.conf
cp ${pkgs.writeText "sangha-postgresql.conf" sanghaPostgresConfig} \
$PGDATA/postgresql.conf
# start postgres server in build environment
pg_ctl start -w
......@@ -35,15 +85,10 @@ let
# init database
psql --file=${sanghaDatabaseInit} --username=postgres
# copy sangha configuration with correct connection data
jq '.Connections.PostgreSQLConnection *= ${
lib.generators.toJSON {} postgresConnectionData
}' \
${sangha.src}/config.example.json \
> ./config.json
# init database tables
${lib.getBin sangha}/bin/sangha database init
# TODO: use command line flag of sangha (buggy)
cp ${sanghaConfig} ./config.json
${lib.getBin sangha}/bin/sangha database init
# insert mock data
psql --file=${sangha.src}/mocks/projects.sql --username=postgres sangha
......@@ -70,10 +115,23 @@ in {
# and https://www.postgresql.org/docs/9.1/static/runtime-config-connection.html
};
options.test = lib.mkOption {
type = lib.types.package;
# options.passthru = lib.mkOption {
# type = with lib.types; attrsOf unspecified;
# default = {};
# description = ''
# Free-form attrset to pass stuff through the build.
# '';
# };
options.generated.dockerImages = lib.mkOption {
type = with lib.types; attrsOf package;
default = {};
description = ''
Generated images for use with docker.
'';
};
config =
let
nixosConfig = {
......@@ -81,10 +139,45 @@ in {
# enable = true;
# initialScript = postgresInit;
# };
test = postgresInitDatabase;
passthru = postgresInitDatabase;
};
dockerConfig = {
# TODO
generated.dockerImages.sangha = with pkgs.dockerTools;
let dbDir = "/database";
uidGid = 999;
uidGidStr = toString uidGid;
in (buildImage {
name = "sangha";
tag = "unstable";
contents = [ pkgs.postgresql (prependPrefix "/usr" localeFiles) ];
config = {
User = "postgres";
Cmd = [ "/bin/postgres" ];
Env = [
"PGDATA=${dbDir}"
];
ExposedPorts."${toString postgresConnectionData.Port}/tcp" = {};
Volumes."${dbDir}" = {};
};
runAsRoot = ''
${pkgs.stdenv.shell}
${shadowSetup}
# some needed system structure
umask 000
mkdir /tmp
echo "postgres::${uidGidStr}:${uidGidStr}::::" > /etc/passwd
echo "127.0.0.1 localhost" > /etc/hosts
echo "hosts: files" > /etc/nsswitch.conf
# postgres setup
cp -r "${postgresInitDatabase}" "./${dbDir}"
chmod --recursive u=rwX,go= "./${dbDir}"
chown --recursive ${uidGidStr}:${uidGidStr} "./${dbDir}"
'';
});
};
in
lib.mkMerge [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment