krebs.build: refactor a bit

This commit is contained in:
tv 2016-02-16 05:09:09 +01:00
parent 43ed24ed66
commit 064d0111a0

View File

@ -20,35 +20,19 @@ let
type = types.user; type = types.user;
}; };
options.krebs.build.source = let options.krebs.build.source = mkOption {
raw = types.either types.str types.path; type = with types; attrsOf (either str (submodule {
url = types.submodule {
options = { options = {
url = mkOption { url = str;
type = types.str; rev = str;
};
rev = mkOption {
type = types.str;
};
dev = mkOption {
type = types.str;
};
}; };
}; }));
in mkOption {
type = types.attrsOf (types.either types.str url);
apply = let f = mapAttrs (_: value: {
string = value;
path = toString value;
set = f value;
}.${typeOf value}); in f;
default = {}; default = {};
}; };
options.krebs.build.populate = mkOption { options.krebs.build.populate = mkOption {
type = types.str; type = types.str;
default = let default = let
source = config.krebs.build.source;
target-user = maybeEnv "target_user" "root"; target-user = maybeEnv "target_user" "root";
target-host = maybeEnv "target_host" config.krebs.build.host.name; target-host = maybeEnv "target_host" config.krebs.build.host.name;
target-port = maybeEnv "target_port" "22"; target-port = maybeEnv "target_port" "22";
@ -75,24 +59,21 @@ let
tmpdir=$(mktemp -dt stockholm.XXXXXXXX) tmpdir=$(mktemp -dt stockholm.XXXXXXXX)
chmod 0755 "$tmpdir" chmod 0755 "$tmpdir"
${concatStringsSep "\n" ${concatStringsSep "\n" (mapAttrsToList (name: symlink: ''
(mapAttrsToList verbose ln -s ${shell.escape symlink.target} \
(name: spec: let dst = removePrefix "symlink:" (get-url spec); in "$tmpdir"/${shell.escape name}
"verbose ln -s ${shell.escape dst} $tmpdir/${shell.escape name}") '') source-by-method.symlink)}
symlink-specs)}
verbose proot \ verbose proot \
-b $tmpdir:${shell.escape target-path} \ -b "$tmpdir":${shell.escape target-path} \
${concatStringsSep " \\\n " ${concatStringsSep " \\\n " (mapAttrsToList (name: file:
(mapAttrsToList "-b ${shell.escape "${file.path}:${target-path}/${name}"}"
(name: spec: ) source-by-method.file)} \
"-b ${shell.escape "${get-url spec}:${target-path}/${name}"}")
file-specs)} \
rsync \ rsync \
-f ${shell.escape "P /*"} \ -f ${shell.escape "P /*"} \
${concatMapStringsSep " \\\n " ${concatMapStringsSep " \\\n " (name:
(name: "-f ${shell.escape "R /${name}"}") "-f ${shell.escape "R /${name}"}"
(attrNames file-specs)} \ ) (attrNames source-by-method.file)} \
--delete \ --delete \
-vFrlptD \ -vFrlptD \
-e ${shell.escape "ssh -p ${target-port}"} \ -e ${shell.escape "ssh -p ${target-port}"} \
@ -100,30 +81,6 @@ let
${shell.escape "${target-user}@${target-host}:${target-path}"} ${shell.escape "${target-user}@${target-host}:${target-path}"}
''; '';
get-schema = uri:
if substring 0 1 uri == "/"
then "file"
else head (splitString ":" uri);
has-schema = schema: uri: get-schema uri == schema;
get-url = spec: {
string = spec;
path = toString spec;
set = get-url spec.url;
}.${typeOf spec};
git-specs =
filterAttrs (_: spec: has-schema "https" (get-url spec)) source //
filterAttrs (_: spec: has-schema "http" (get-url spec)) source //
filterAttrs (_: spec: has-schema "git" (get-url spec)) source;
file-specs =
filterAttrs (_: spec: has-schema "file" (get-url spec)) source;
symlink-specs =
filterAttrs (_: spec: has-schema "symlink" (get-url spec)) source;
git-script = '' git-script = ''
#! /bin/sh #! /bin/sh
set -efu set -efu
@ -162,20 +119,42 @@ let
git clean -dxf git clean -dxf
)} )}
${concatStringsSep "\n" ${concatStringsSep "\n" (mapAttrsToList (name: git: ''
(mapAttrsToList verbose fetch_git ${concatMapStringsSep " " shell.escape [
(name: spec: toString (map shell.escape [ "${target-path}/${name}"
"verbose" git.url
"fetch_git" git.rev
"${target-path}/${name}" ]}
spec.url '') source-by-method.git)}
spec.rev
]))
git-specs)}
''; '';
in out; in out;
}; };
}; };
source-by-method = let
known-methods = ["git" "file" "symlink"];
in genAttrs known-methods (const {}) // recursiveUpdate source-by-scheme {
git = source-by-scheme.http or {} //
source-by-scheme.https or {};
};
source-by-scheme = foldl' (out: { k, v }: recursiveUpdate out {
${v.scheme}.${k} = v;
}) {} (mapAttrsToList (k: v: { inherit k v; }) normalized-source);
normalized-source = mapAttrs (name: let f = x: getAttr (typeOf x) {
path = f (toString x);
string = f {
url = if substring 0 1 x == "/" then "file://${x}" else x;
};
set = let scheme = head (splitString ":" x.url); in recursiveUpdate x {
inherit scheme;
} // {
symlink.target = removePrefix "symlink:" x.url;
file.path = # TODO file://host/...
assert hasPrefix "file:///" x.url;
removePrefix "file://" x.url;
}.${scheme} or {};
}; in f) config.krebs.build.source;
in out in out