Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostctl: init module #249

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/devenv-devShell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pkgs.writeScriptBin "devenv" ''
case $command in
up)
procfilescript=${config.procfileScript}
cat $procfilescript
if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
echo "No 'processes' option defined: https://devenv.sh/processes/"
exit 1
Expand Down
1 change: 0 additions & 1 deletion src/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pkgs.writeScriptBin "devenv" ''
shell
eval "$env"
procfilescript=$($CUSTOM_NIX/bin/nix $NIX_FLAGS build --no-link --print-out-paths --impure '.#procfileScript')
cat $procfilescript
if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
echo "No 'processes' option defined: https://devenv.sh/processes/"
exit 1
Expand Down
47 changes: 47 additions & 0 deletions src/modules/integrations/hostctl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ pkgs, lib, config, ... }:

let
entries = lib.mapAttrsToList (hostname: ip: { inherit hostname ip; }) config.hosts;
entriesByIp = builtins.groupBy ({ ip, ... }: ip) entries;
hostnamesByIp = builtins.mapAttrs (hostname: entries: builtins.map ({ hostname, ... }: hostname) entries) entriesByIp;
lines = lib.mapAttrsToList (ip: hostnames: "${ip} ${lib.concatStringsSep " " hostnames}") hostnamesByIp;
hostContent = lib.concatStringsSep "\n" lines;
hostHash = builtins.hashString "sha256" hostContent;
file = pkgs.writeText "hosts" ''
# ${hostHash}
${hostContent}
'';
in
{
options = {
hostsProfileName = lib.mkOption {
type = lib.types.str;
default = "devenv-${builtins.hashString "sha256" config.env.DEVENV_ROOT}";
description = "Profile name to use.";
};

hosts = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = { };
description = "List of hosts entries.";
example = {
"example.com" = "127.0.0.1";
};
};
};

config = lib.mkIf (hostContent != "") {
process.before = ''
if [[ ! -f "$DEVENV_STATE/hostctl" || "$(cat "$DEVENV_STATE/hostctl")" != "${hostHash}" ]]; then
sudo ${pkgs.hostctl}/bin/hostctl replace ${config.hostsProfileName} --from ${file}
mkdir -p "$DEVENV_STATE"
echo "${hostHash}" > "$DEVENV_STATE/hostctl"
fi
'';

process.after = ''
rm -f "$DEVENV_STATE/hostctl"
sudo ${pkgs.hostctl}/bin/hostctl remove ${config.hostsProfileName}
'';
};
}
77 changes: 56 additions & 21 deletions src/modules/processes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ let
builtins.removeAttrs config.env [ "DEVENV_PROFILE" ]
else
config.env);

procfileScripts = {
honcho = ''
${pkgs.honcho}/bin/honcho start -f ${config.procfile} --env ${config.procfileEnv} &
'';

overmind = ''
OVERMIND_ENV=${config.procfileEnv} ${pkgs.overmind}/bin/overmind start --procfile ${config.procfile} &
'';

process-compose = ''
${pkgs.process-compose}/bin/process-compose --config ${config.procfile} \
--port $PC_HTTP_PORT \
--tui=$PC_TUI_ENABLED &
'';

hivemind = ''
${pkgs.hivemind}/bin/hivemind --print-timestamps ${config.procfile} &
'';
};
in
{
options = {
Expand Down Expand Up @@ -77,6 +97,18 @@ in
log_level = "fatal";
};
};

before = lib.mkOption {
type = types.lines;
description = "Bash code to execute before starting processes.";
default = "";
};

after = lib.mkOption {
type = types.lines;
description = "Bash code to execute after stopping processes.";
default = "";
};
};

# INTERNAL
Expand Down Expand Up @@ -117,27 +149,30 @@ in
procfileEnv =
pkgs.writeText "procfile-env" (lib.concatStringsSep "\n" envList);

procfileScript = {
honcho = pkgs.writeShellScript "honcho-up" ''
echo "Starting processes ..." 1>&2
echo "" 1>&2
${pkgs.honcho}/bin/honcho start -f ${config.procfile} --env ${config.procfileEnv}
'';

overmind = pkgs.writeShellScript "overmind-up" ''
OVERMIND_ENV=${config.procfileEnv} ${pkgs.overmind}/bin/overmind start --procfile ${config.procfile}
'';

process-compose = pkgs.writeShellScript "process-compose-up" ''
${pkgs.process-compose}/bin/process-compose --config ${config.procfile} \
--port $PC_HTTP_PORT \
--tui=$PC_TUI_ENABLED
'';

hivemind = pkgs.writeShellScript "hivemind-up" ''
${pkgs.hivemind}/bin/hivemind --print-timestamps ${config.procfile}
'';
}.${implementation};
procfileScript = pkgs.writeShellScript "devenv-up" ''
${config.process.before}

${procfileScripts.${implementation}}

if [[ ! -d "$DEVENV_STATE" ]]; then
mkdir -p "$DEVENV_STATE"
fi

stop_up() {
echo "Stopping processes..."
kill -TERM $(cat "$DEVENV_STATE/devenv.pid")
rm "$DEVENV_STATE/devenv.pid"
wait
${config.process.after}
echo "Processes stopped."
}

trap stop_up SIGINT SIGTERM

echo $! > "$DEVENV_STATE/devenv.pid"

wait
'';

ci = [ config.procfileScript ];

Expand Down