Merge pull request #28 from hercules-ci/exec-shell

Exec shell
This commit is contained in:
Robert Hensing 2019-03-22 06:43:28 +01:00 committed by GitHub
commit 000b938893
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 128 additions and 2 deletions

View file

@ -46,6 +46,11 @@ while test $# != 0; do
shift shift
break break
;; ;;
exec)
command="$1"
shift
break
;;
docker-compose) docker-compose)
command="docker-compose" command="docker-compose"
shift shift
@ -231,6 +236,45 @@ do_repl_cleanup() {
rm -f $REPL_TMP rm -f $REPL_TMP
} }
run_exec() {
case "${#docker_compose_args[@]}" in
0)
echo "As an argument to exec, please specify a service"
exit 1
;;
1)
case "${docker_compose_args[0]}" in
-*|--*)
echo "As an argument to exec, please specify a service"
echo "Note that executing the default command currently does not support"
echo "docker-compose options."
# This requires parsing the options, in order to figure out
# which service to invoke.
exit 1
;;
*)
serviceName="${docker_compose_args[0]}"
do_eval
default_args=()
while read arg; do
default_args+=("$arg")
done < <(
jq < "$docker_compose_yaml" \
--arg serviceName "$serviceName" \
-r \
'.["x-arion"].serviceInfo.webserver.defaultExec | tostream | .[1] | select(.)'
)
docker-compose -f $docker_compose_yaml exec "$serviceName" "${default_args[@]}"
;;
esac
;;
*)
do_eval
docker-compose -f $docker_compose_yaml exec "${docker_compose_args[@]}"
;;
esac
}
case "$command" in case "$command" in
cat) cat)
do_eval do_eval
@ -239,6 +283,9 @@ case "$command" in
repl) repl)
do_repl do_repl
;; ;;
exec)
run_exec "$@"
;;
docker-compose) docker-compose)
if [[ ${#docker_compose_args[@]} != 0 if [[ ${#docker_compose_args[@]} != 0
&& ${docker_compose_args[0]} != "help" && ${docker_compose_args[0]} != "help"

View file

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

View file

@ -9,7 +9,9 @@ let
builtinModules = [ builtinModules = [
argsModule argsModule
./modules/service/default-exec.nix
./modules/service/docker-compose-service.nix ./modules/service/docker-compose-service.nix
./modules/service/extended-info.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/image.nix

View file

@ -26,7 +26,11 @@ in
}; };
docker-compose.raw = lib.mkOption { docker-compose.raw = lib.mkOption {
type = lib.types.attrs; type = lib.types.attrs;
description = "Nested attribute set that will be turned into the docker-compose.yaml file, using Nix's toJSON builtin."; description = "Attribute set that will be turned into the docker-compose.yaml file, using Nix's toJSON builtin.";
};
docker-compose.extended = lib.mkOption {
type = lib.types.attrs;
description = "Attribute set that will be turned into the x-arion section of the docker-compose.yaml file.";
}; };
docker-compose.services = lib.mkOption { docker-compose.services = lib.mkOption {
default = {}; default = {};
@ -47,6 +51,7 @@ in
docker-compose.raw = { docker-compose.raw = {
version = "3.4"; 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;
x-arion = config.docker-compose.extended;
}; };
}; };
} }

View file

@ -31,6 +31,6 @@ in
}; };
config = { config = {
build.imagesToLoad = lib.attrValues serviceImages; build.imagesToLoad = lib.attrValues serviceImages;
docker-compose.raw.x-arion.images = config.build.imagesToLoad; docker-compose.extended.images = config.build.imagesToLoad;
}; };
} }

View file

@ -0,0 +1,24 @@
/*
Adds extendedInfo from services to the Docker Compose file.
This contains fields that are not in Docker Compose schema.
*/
{ config, lib, ... }:
let
serviceInfo =
lib.mapAttrs getInfo (
lib.filterAttrs filterFunction config.docker-compose.evaluatedServices
);
filterFunction = _serviceName: service:
# shallow equality suffices for emptiness test
builtins.attrNames service.config.build.extendedInfo != [];
getInfo = _serviceName: service: service.config.build.extendedInfo;
in
{
config = {
docker-compose.extended.serviceInfo = serviceInfo;
};
}

View file

@ -0,0 +1,4 @@
{ config, utils, ... }:
{
config.system.build.x-arion-defaultShell = utils.toShellPath config.users.defaultUserShell;
}

View file

@ -0,0 +1,19 @@
{ config, lib, ... }:
let
inherit (lib) types mkOption;
in
{
options = {
service.defaultExec = mkOption {
type = types.listOf types.str;
default = ["/bin/sh"];
description = ''
Container program and arguments to invoke when calling
<code>arion exec &lt;service.name></code> without further arguments.
'';
};
};
config = {
build.extendedInfo.defaultExec = config.service.defaultExec;
};
}

View file

@ -0,0 +1,20 @@
{ config, lib, ... }:
let
inherit (lib) mkOption;
inherit (lib.types) attrsOf unspecified;
in
{
options = {
build.extendedInfo = mkOption {
type = attrsOf unspecified;
description = ''
Information about a service to include in the Docker Compose file,
but that will not be used by the <code>docker-compose</code> command
itself.
It will be inserted in <code>x-arion.serviceInfo.&lt;service.name></code>.
'';
default = {};
};
};
}

View file

@ -21,6 +21,7 @@ in
config = lib.mkIf (config.nixos.useSystemd) { config = lib.mkIf (config.nixos.useSystemd) {
nixos.configuration.imports = [ nixos.configuration.imports = [
../nixos/container-systemd.nix ../nixos/container-systemd.nix
../nixos/default-shell.nix
(pkgs.path + "/nixos/modules/profiles/minimal.nix") (pkgs.path + "/nixos/modules/profiles/minimal.nix")
]; ];
image.command = [ "${config.nixos.build.toplevel}/init" ]; image.command = [ "${config.nixos.build.toplevel}/init" ];
@ -35,5 +36,6 @@ in
service.stop_signal = "SIGRTMIN+3"; service.stop_signal = "SIGRTMIN+3";
service.tty = true; service.tty = true;
service.defaultExec = [config.nixos.build.x-arion-defaultShell "-l"];
}; };
} }

View file

@ -51,6 +51,8 @@ in
subtest "full-nixos", sub { subtest "full-nixos", sub {
$machine->succeed("cp -r ${../../examples/full-nixos} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d"); $machine->succeed("cp -r ${../../examples/full-nixos} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d");
$machine->waitUntilSucceeds("curl localhost:8000"); $machine->waitUntilSucceeds("curl localhost:8000");
# Also test exec with defaultExec
$machine->succeed("cd work && export NIX_PATH=nixpkgs='${pkgs.path}' && (echo 'nix run -f ~/h/arion arion -c arion exec webserver'; echo 'target=world; echo Hello \$target'; echo exit) | script /dev/null | grep 'Hello world'");
$machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down && rm -rf work"); $machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down && rm -rf work");
$machine->waitUntilFails("curl localhost:8000"); $machine->waitUntilFails("curl localhost:8000");
}; };