Add image building and loading

This commit is contained in:
Robert Hensing 2019-03-11 15:03:27 +01:00
parent ed2d58c8bd
commit 9922cb6b82
8 changed files with 160 additions and 2 deletions

View file

@ -163,6 +163,32 @@ do_build() {
--show-trace \ --show-trace \
--attr 'config.build.dockerComposeYaml' \ --attr 'config.build.dockerComposeYaml' \
>/dev/null ; >/dev/null ;
echo 1>&2 "Ensuring required images are loaded..."
jq -r <"$docker_compose_yaml" \
'.["x-arion"].images | map(" - " + .imageName + ":" + .imageTag) | join("\n")'
eval "$(
jq -r '.["docker-compose"]["x-arion"].images as $images
| .["existing-images"] as $loaded
| $images
| map(
if $loaded[.imageName + ":" + .imageTag]
then ""
else "docker load <" + .image + ";" end
)
| join("\n")
' <<EOF
{
"docker-compose": $(cat $docker_compose_yaml),
"existing-images": {
$(docker images \
--filter "dangling=false" \
--format '"{{.Repository}}:{{.Tag}}": true,')
"": false
}
}
EOF
)"
} }

View file

@ -20,6 +20,7 @@ let
argsModule argsModule
./modules/composition/docker-compose.nix ./modules/composition/docker-compose.nix
./modules/composition/host-environment.nix ./modules/composition/host-environment.nix
./modules/composition/images.nix
]; ];
argsModule = { argsModule = {

View file

@ -12,6 +12,7 @@ let
./modules/service/docker-compose-service.nix ./modules/service/docker-compose-service.nix
./modules/service/host-store.nix ./modules/service/host-store.nix
./modules/service/host.nix ./modules/service/host.nix
./modules/service/image.nix
./modules/service/nixos.nix ./modules/service/nixos.nix
./modules/service/nixos-init.nix ./modules/service/nixos-init.nix
]; ];

View file

@ -45,7 +45,7 @@ in
docker-compose.evaluatedServices = lib.mapAttrs evalService config.docker-compose.services; docker-compose.evaluatedServices = lib.mapAttrs evalService config.docker-compose.services;
docker-compose.raw = { docker-compose.raw = {
version = "3"; version = "3.4";
services = lib.mapAttrs (k: c: c.config.build.service) config.docker-compose.evaluatedServices; services = lib.mapAttrs (k: c: c.config.build.service) config.docker-compose.evaluatedServices;
}; };
}; };

View file

@ -0,0 +1,36 @@
{ pkgs, lib, config, ... }:
let
inherit (lib.types) listOf package unspecified;
serviceImages =
lib.mapAttrs addDetails (
lib.filterAttrs filterFunction config.docker-compose.evaluatedServices
);
filterFunction = _serviceName: service:
service.config.image.nixBuild;
addDetails = _serviceName: service:
let
inherit (service.config) build;
in {
image = build.image.outPath;
imageName = build.imageName or service.image.name;
imageTag =
if build.image.imageTag != ""
then build.image.imageTag
else lib.head (lib.strings.splitString "-" (baseNameOf build.image.outPath));
};
in
{
options = {
build.imagesToLoad = lib.mkOption {
type = listOf unspecified;
description = "List of dockerTools image derivations.";
};
};
config = {
build.imagesToLoad = lib.attrValues serviceImages;
docker-compose.raw.x-arion.images = config.build.imagesToLoad;
};
}

View file

@ -8,6 +8,7 @@
let let
inherit (lib) mkOption types mkIf; inherit (lib) mkOption types mkIf;
escape = s: lib.replaceStrings ["$"] ["$$"] s;
in in
{ {
options = { options = {
@ -23,11 +24,13 @@ in
}; };
}; };
config = mkIf config.service.useHostStore { config = mkIf config.service.useHostStore {
image.nixBuild = false; # no need to build and load
service.image = "arion-base"; service.image = "arion-base";
service.build.context = "${../../../arion-image}"; service.build.context = "${../../../arion-image}";
service.volumes = [ service.volumes = [
"${config.host.nixStorePrefix}/nix/store:/nix/store" "${config.host.nixStorePrefix}/nix/store:/nix/store"
"${config.host.nixStorePrefix}${pkgs.buildEnv { name = "container-system-env"; paths = [ pkgs.bashInteractive pkgs.coreutils ]; }}:/run/system" "${config.host.nixStorePrefix}${pkgs.buildEnv { name = "container-system-env"; paths = [ pkgs.bashInteractive pkgs.coreutils ]; }}:/run/system"
] ++ lib.optional config.service.useHostNixDaemon "/nix/var/nix/daemon-socket:/nix/var/nix/daemon-socket"; ] ++ lib.optional config.service.useHostNixDaemon "/nix/var/nix/daemon-socket:/nix/var/nix/daemon-socket";
service.command = lib.mkDefault (map escape (config.image.rawConfig.Cmd or []));
}; };
} }

View file

@ -0,0 +1,91 @@
{ pkgs, lib, config, ... }:
let
inherit (lib) types mkOption;
inherit (types) attrsOf listOf nullOr package str unspecified bool;
in
{
options = {
build.image = mkOption {
type = nullOr package;
description = ''
Docker image derivation to be <code>docker load</code>ed.
'';
internal = true;
};
build.imageName = mkOption {
type = str;
description = "Derived from build.image";
internal = true;
};
build.imageTag = mkOption {
type = str;
description = "Derived from build.image";
internal = true;
};
image.nixBuild = mkOption {
type = bool;
description = ''
Whether to build this image with Nixpkgs'
<code>dockerTools.buildLayeredImage</code>
and then load it with <code>docker load</code>.
'';
default = true;
};
image.name = mkOption {
type = str;
default = config.service.name;
defaultText = lib.literalExample "config.service.name";
description = ''
A human readable name for the docker image.
Shows up in the <code>docker ps</code> output in the
<code>IMAGE</code> column, among other places.
'';
};
image.contents = mkOption {
type = listOf package;
default = [];
description = ''
Top level paths in the container.
'';
};
image.rawConfig = mkOption {
type = attrsOf unspecified;
default = {};
description = ''
This is a low-level fallback for when a container option has not
been modeled in the Arion module system.
This attribute set does not have an appropriate merge function.
Please use the specific <code>image</code> options instead.
Run-time configuration of the container. A full list of the
options are available at in the <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions">Docker Image Specification
v1.2.0</link>.
'';
};
image.command = mkOption {
type = listOf str;
default = [];
description = ''
'';
};
};
config = {
build.image = pkgs.dockerTools.buildLayeredImage {
inherit (config.image)
name
contents
;
config = config.image.rawConfig;
};
build.imageName = config.build.image.imageName;
build.imageTag =
if config.build.image.imageTag != ""
then config.build.image.imageTag
else lib.head (lib.strings.splitString "-" (baseNameOf config.build.image.outPath));
service.image = lib.mkDefault "${config.build.imageName}:${config.build.imageTag}";
image.rawConfig.Cmd = config.image.command;
};
}

View file

@ -23,7 +23,7 @@ in
../nixos/container-systemd.nix ../nixos/container-systemd.nix
(pkgs.path + "/nixos/modules/profiles/minimal.nix") (pkgs.path + "/nixos/modules/profiles/minimal.nix")
]; ];
service.command = [ "${config.nixos.build.toplevel}/init" ]; image.command = [ "${config.nixos.build.toplevel}/init" ];
service.environment.container = "docker"; service.environment.container = "docker";
service.volumes = [ service.volumes = [
"/sys/fs/cgroup:/sys/fs/cgroup:ro" "/sys/fs/cgroup:/sys/fs/cgroup:ro"