From dc294b034e7908731ff01c903925c89e763303e1 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 17 Dec 2018 19:08:38 +0100 Subject: [PATCH] Code taken from Hercules CI repo - renamed to Arion - minor changes - readme WIP --- README.md | 48 +++++++ default.nix | 26 ++++ src/arion | 199 ++++++++++++++++++++++++++++++ src/arion-image/Dockerfile | 2 + src/arion-image/passwd | 2 + src/nix/docker-compose-module.nix | 49 ++++++++ src/nix/eval-docker-compose.nix | 33 +++++ src/nix/service-host-store.nix | 23 ++++ src/nix/service.nix | 74 +++++++++++ 9 files changed, 456 insertions(+) create mode 100644 README.md create mode 100644 default.nix create mode 100755 src/arion create mode 100644 src/arion-image/Dockerfile create mode 100644 src/arion-image/passwd create mode 100644 src/nix/docker-compose-module.nix create mode 100644 src/nix/eval-docker-compose.nix create mode 100644 src/nix/service-host-store.nix create mode 100644 src/nix/service.nix diff --git a/README.md b/README.md new file mode 100644 index 0000000..43728b7 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ + +# Run docker-compose without images with Nix + +*Wait, what?* + +With Arion you can fire up containers without creating images for each +service. Instead, it uses a mostly empty image, sort of like a base +image, and makes the host's Nix store available in the container, +allowing the container to run programs without having to re-package +them into a docker image. + +Arion is configured using Nix with modules, like those in +NixOS. Similar to `docker-compose` it can therefore combine +configuration from multiple files. For managing the network and +containers it delegates to the `docker-compose` command. + +# Project Status + +This project was born out of a process supervision need for local +development environments while working +on [Hercules CI](https://www.hercules-ci.com). (It was also born out +of ancient Greek deities disguised as horses. More on that later.) + +We don't use it to deploy to 'real' environments and we have no plans +to do so in the future. + +If you do want to use Arion for 'real' environments, you'll probably +want to either build images or manage garbage collection roots if you +control the deployment host. Either of these has yet to be +implemented. + +# How do I use Arion? + +The command line inherits most `docker-compose` commands. These +examples assume you're in this repo's `examples/` directory. + +TODO examples + +# Why "Arion"? + +Arion comes from Greek mythology. Poseidon, the god of ~Docker~ the +seas had his eye on Demeter. Demeter tried to trick him by disguising +as a horse, but Poseidon saw through the deception and they had Arion. + +So Arion is a super fast divine horse; the result of some weird +mixing. Also it talks. + +(And we feel morally obliged to name our stuff after Greek mythology) diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..440e4b5 --- /dev/null +++ b/default.nix @@ -0,0 +1,26 @@ +{ pkgs ? import {} }: + +let + inherit (pkgs) lib stdenv; + + arion = stdenv.mkDerivation { + name = "arion"; + src = ./src; + unpackPhase = ""; + buildPhase = ""; + installPhase = '' + mkdir -p $out/bin $out/share/arion + cp -a nix $out/share/arion/ + cp -a arion-image $out/share/arion/ + substitute arion $out/bin/arion \ + --subst-var-by path ${lib.makeBinPath [pkgs.jq pkgs.coreutils]} \ + --subst-var-by nix_dir $out/share/arion/nix \ + ; + chmod a+x $out/bin/arion + ''; + }; + +in +{ + inherit arion; +} diff --git a/src/arion b/src/arion new file mode 100755 index 0000000..f7f85f8 --- /dev/null +++ b/src/arion @@ -0,0 +1,199 @@ +#!/usr/bin/env bash + +set -euo pipefail + +export PATH="$PATH:@path@" + +nix_dir="@nix_dir@" +docker_compose_args=() +files=() +command="docker-compose" +pkgs_argument="./arion-pkgs.nix" + +debug() { + # echo "$@" + : +} + +while test $# != 0; do + case "$1" in + -f|--file) + shift + files+=("$1") + ;; + -f*) + files+=("${1/#-f/}") + ;; + --file=*) + files+=("${1/#--file=}") + ;; + --pkgs) + shift + pkgs_argument="$1" + ;; + -h|--help|help) + command="help" + shift + break + ;; + cat) + command="$1" + shift + break + ;; + repl) + command="$1" + shift + break + ;; + docker-compose) + command="docker-compose" + shift + break + ;; + *) + break + ;; + esac + shift +done + +while test $# != 0; do + docker_compose_args+=("$1") + shift +done + +case "$command" in + help) + cat <&2 "Building configuration..." + nix-build \ + "$nix_dir/eval-docker-compose.nix" \ + --out-link $docker_compose_yaml \ + --argstr uid "$UID" \ + --arg modules "$modules" \ + --arg pkgs "$pkgs_argument" \ + --show-trace \ + --attr 'config.build.dockerComposeYaml' \ + >/dev/null ; +} + + +do_repl() { + # nix repl doesn't autocall its arguments + # so we improvise. We need a file in this directory + # to make sure that all paths are as expected :( + trap do_repl_cleanup EXIT; + + REPL_TMP=.tmp-repl-$$-$RANDOM + cat <"$REPL_TMP" <