forked from fediversity/fediversity
parent
1f7c7c8f1e
commit
03a22b1cb8
4 changed files with 65 additions and 81 deletions
|
@ -29,6 +29,33 @@ let
|
||||||
else
|
else
|
||||||
lib.strings.toJSON v
|
lib.strings.toJSON v
|
||||||
);
|
);
|
||||||
|
withPackages = packages: {
|
||||||
|
makeWrapperArgs = [
|
||||||
|
"--prefix"
|
||||||
|
"PATH"
|
||||||
|
":"
|
||||||
|
"${lib.makeBinPath packages}"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
writeConfig =
|
||||||
|
{
|
||||||
|
system,
|
||||||
|
module,
|
||||||
|
root-path,
|
||||||
|
deployment-type,
|
||||||
|
deployment-name,
|
||||||
|
args,
|
||||||
|
}:
|
||||||
|
builtins.toString (
|
||||||
|
pkgs.writers.writeText "configuration.nix" ''
|
||||||
|
import ${root-path}/deployment/nixos.nix {
|
||||||
|
system = "${system}";
|
||||||
|
configuration = (import "${root-path}/${module}" (builtins.fromJSON "${
|
||||||
|
lib.replaceStrings [ "\"" ] [ "\\\"" ] (lib.strings.toJSON args)
|
||||||
|
}")).${deployment-name}.${deployment-type}.nixos-configuration;
|
||||||
|
}
|
||||||
|
''
|
||||||
|
);
|
||||||
|
|
||||||
functionType = submodule ./function.nix;
|
functionType = submodule ./function.nix;
|
||||||
application-resources = submodule {
|
application-resources = submodule {
|
||||||
|
@ -85,6 +112,8 @@ let
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
# FIXME allow custom deployment types
|
||||||
|
# FIXME make deployments environment resources?
|
||||||
deployment-type = attrTag {
|
deployment-type = attrTag {
|
||||||
ssh-host = mkOption {
|
ssh-host = mkOption {
|
||||||
description = "A deployment by SSH to update a single existing NixOS host.";
|
description = "A deployment by SSH to update a single existing NixOS host.";
|
||||||
|
@ -134,30 +163,27 @@ let
|
||||||
;
|
;
|
||||||
environment = {
|
environment = {
|
||||||
key_file = key-file;
|
key_file = key-file;
|
||||||
deployment_name = deployment-name;
|
|
||||||
root_path = root-path;
|
|
||||||
ssh_opts = sshOpts;
|
ssh_opts = sshOpts;
|
||||||
inherit
|
inherit
|
||||||
system
|
|
||||||
host
|
host
|
||||||
username
|
username
|
||||||
module
|
|
||||||
args
|
|
||||||
;
|
;
|
||||||
deployment_type = "ssh-host";
|
nixos_conf = writeConfig {
|
||||||
|
inherit
|
||||||
|
system
|
||||||
|
module
|
||||||
|
args
|
||||||
|
deployment-name
|
||||||
|
root-path
|
||||||
|
;
|
||||||
|
deployment-type = "ssh-host";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
pkgs.writers.writeBashBin "deploy-sh.sh"
|
pkgs.writers.writeBashBin "deploy-sh.sh"
|
||||||
{
|
(withPackages [
|
||||||
makeWrapperArgs = [
|
pkgs.jq
|
||||||
"--prefix"
|
])
|
||||||
"PATH"
|
|
||||||
":"
|
|
||||||
"${lib.makeBinPath [
|
|
||||||
pkgs.jq
|
|
||||||
]}"
|
|
||||||
];
|
|
||||||
}
|
|
||||||
''
|
''
|
||||||
env ${
|
env ${
|
||||||
toString (lib.mapAttrsToList (k: v: "${k}=\"${toBash v}\"") environment)
|
toString (lib.mapAttrsToList (k: v: "${k}=\"${toBash v}\"") environment)
|
||||||
|
@ -219,32 +245,29 @@ let
|
||||||
;
|
;
|
||||||
environment = {
|
environment = {
|
||||||
key_file = key-file;
|
key_file = key-file;
|
||||||
deployment_name = deployment-name;
|
|
||||||
root_path = root-path;
|
|
||||||
ssh_opts = sshOpts;
|
ssh_opts = sshOpts;
|
||||||
inherit
|
inherit
|
||||||
system
|
|
||||||
host
|
host
|
||||||
username
|
username
|
||||||
module
|
|
||||||
args
|
|
||||||
;
|
;
|
||||||
deployment_type = "tf-host";
|
nixos_conf = writeConfig {
|
||||||
|
inherit
|
||||||
|
system
|
||||||
|
module
|
||||||
|
args
|
||||||
|
deployment-name
|
||||||
|
root-path
|
||||||
|
;
|
||||||
|
deployment-type = "tf-host";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
tf-env = pkgs.callPackage ./run/tf-single-host/tf-env.nix { };
|
tf-env = pkgs.callPackage ./run/tf-single-host/tf-env.nix { };
|
||||||
in
|
in
|
||||||
pkgs.writers.writeBashBin "deploy-tf.sh"
|
pkgs.writers.writeBashBin "deploy-tf.sh"
|
||||||
{
|
(withPackages [
|
||||||
makeWrapperArgs = [
|
pkgs.jq
|
||||||
"--prefix"
|
(pkgs.callPackage ./run/tf-single-host/tf.nix { inherit sources; })
|
||||||
"PATH"
|
])
|
||||||
":"
|
|
||||||
"${lib.makeBinPath [
|
|
||||||
pkgs.jq
|
|
||||||
(pkgs.callPackage ./run/tf-single-host/tf.nix { inherit sources; })
|
|
||||||
]}"
|
|
||||||
];
|
|
||||||
}
|
|
||||||
''
|
''
|
||||||
env ${toString (lib.mapAttrsToList (k: v: "TF_VAR_${k}=\"${toBash v}\"") environment)} \
|
env ${toString (lib.mapAttrsToList (k: v: "TF_VAR_${k}=\"${toBash v}\"") environment)} \
|
||||||
tf_env=${tf-env} bash ./deployment/run/tf-single-host/run.sh
|
tf_env=${tf-env} bash ./deployment/run/tf-single-host/run.sh
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#! /usr/bin/env bash
|
#! /usr/bin/env bash
|
||||||
set -xeuo pipefail
|
set -xeuo pipefail
|
||||||
declare username host system module args deployment_name deployment_type key_file root_path ssh_opts
|
declare username host key_file ssh_opts nixos_conf
|
||||||
IFS=" " read -r -a ssh_opts <<< "$( (echo "$ssh_opts" | jq -r '@sh') | tr -d \'\")"
|
IFS=" " read -r -a ssh_opts <<< "$( (echo "$ssh_opts" | jq -r '@sh') | tr -d \'\")"
|
||||||
|
|
||||||
# DEPLOY
|
# DEPLOY
|
||||||
|
@ -20,18 +20,16 @@ if [[ -n "$key_file" ]]; then
|
||||||
fi
|
fi
|
||||||
destination="$username@$host"
|
destination="$username@$host"
|
||||||
|
|
||||||
command=(nix-instantiate --show-trace --expr "
|
command=(nix-instantiate --show-trace "${nixos_conf}")
|
||||||
import $root_path/deployment/nixos.nix {
|
|
||||||
system = \"$system\";
|
|
||||||
configuration = (import \"$root_path/$module\" (builtins.fromJSON ''$args'')).$deployment_name.$deployment_type.nixos-configuration;
|
|
||||||
}
|
|
||||||
")
|
|
||||||
|
|
||||||
# INSTANTIATE
|
# INSTANTIATE
|
||||||
# instantiate the config in /nix/store
|
# instantiate the config in /nix/store
|
||||||
"${command[@]}" -A out_path
|
"${command[@]}" -A out_path
|
||||||
|
|
||||||
# get the realized derivation to deploy
|
# get the realized derivation to deploy
|
||||||
|
"${command[@]}" --show-trace --eval --strict --json
|
||||||
|
|
||||||
|
# FIXME explore import/readFile as ways to instantiate the derivation, potentially allowing to realize the store path up-front from Nix?
|
||||||
outPath=$(nix-store --realize "$("${command[@]}" --show-trace --eval --strict --json | jq -r '.drv_path')")
|
outPath=$(nix-store --realize "$("${command[@]}" --show-trace --eval --strict --json | jq -r '.drv_path')")
|
||||||
# deploy the config by nix-copy-closure
|
# deploy the config by nix-copy-closure
|
||||||
NIX_SSHOPTS="${sshOpts[*]}" nix-copy-closure --to "$destination" "$outPath" --gzip --use-substitutes
|
NIX_SSHOPTS="${sshOpts[*]}" nix-copy-closure --to "$destination" "$outPath" --gzip --use-substitutes
|
||||||
|
|
|
@ -18,11 +18,8 @@ resource "terraform_data" "nixos" {
|
||||||
# - `data` always runs, which is slow for deploy and especially build.
|
# - `data` always runs, which is slow for deploy and especially build.
|
||||||
triggers_replace = [
|
triggers_replace = [
|
||||||
data.external.hash.result,
|
data.external.hash.result,
|
||||||
|
var.nixos_conf,
|
||||||
var.host,
|
var.host,
|
||||||
var.module,
|
|
||||||
var.args,
|
|
||||||
var.root_path,
|
|
||||||
var.deployment_type,
|
|
||||||
]
|
]
|
||||||
|
|
||||||
provisioner "local-exec" {
|
provisioner "local-exec" {
|
||||||
|
@ -33,17 +30,11 @@ resource "terraform_data" "nixos" {
|
||||||
# so are generally discouraged in TF.
|
# so are generally discouraged in TF.
|
||||||
working_dir = path.root
|
working_dir = path.root
|
||||||
environment = {
|
environment = {
|
||||||
system = var.system
|
nixos_conf = var.nixos_conf
|
||||||
username = var.username
|
username = var.username
|
||||||
host = var.host
|
host = var.host
|
||||||
module = var.module
|
|
||||||
host = var.host
|
|
||||||
args = var.args
|
|
||||||
key_file = var.key_file
|
key_file = var.key_file
|
||||||
deployment_name = var.deployment_name
|
|
||||||
root_path = var.root_path
|
|
||||||
ssh_opts = var.ssh_opts
|
ssh_opts = var.ssh_opts
|
||||||
deployment_type = var.deployment_type
|
|
||||||
}
|
}
|
||||||
# TODO: refactor back to command="ignoreme" interpreter=concat([]) to protect sensitive data from error logs?
|
# TODO: refactor back to command="ignoreme" interpreter=concat([]) to protect sensitive data from error logs?
|
||||||
# TODO: build on target?
|
# TODO: build on target?
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
variable "system" {
|
variable "nixos_conf" {
|
||||||
description = "The architecture of the system to deploy to."
|
description = "The path to the NixOS configuration to deploy."
|
||||||
type = string
|
type = string
|
||||||
default = "x86_64-linux"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "username" {
|
variable "username" {
|
||||||
|
@ -15,40 +14,13 @@ variable "host" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "module" {
|
|
||||||
description = "The module to call to obtain the NixOS configuration from."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "args" {
|
|
||||||
description = "The arguments with which to call the module to obtain the NixOS configuration."
|
|
||||||
type = string
|
|
||||||
default = "{}"
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "key_file" {
|
variable "key_file" {
|
||||||
description = "path to the user's SSH private key"
|
description = "path to the user's SSH private key"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "deployment_name" {
|
|
||||||
description = "The name of the deployment for which to obtain the NixOS configuration."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "root_path" {
|
|
||||||
description = "The path to the root of the repository."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "ssh_opts" {
|
variable "ssh_opts" {
|
||||||
description = "Extra SSH options (`-o`) to use."
|
description = "Extra SSH options (`-o`) to use."
|
||||||
type = string
|
type = string
|
||||||
default = "[]"
|
default = "[]"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "deployment_type" {
|
|
||||||
description = "A `deployment-type` from the Fediversity data model, for grabbing the desired NixOS configuration."
|
|
||||||
type = string
|
|
||||||
default = "tf-host"
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue