Skip to content

Commit 2976c1c

Browse files
authored
deploy_nixos: add ssh_private_key. Attempt 2 (#38)
Sometimes it's useful to pass the SSH key content directly. Change the default of `ssh_agent` to only enable when `ssh_private_key` or `ssh_private_key_file` is being passed. Also, refactor things a little bit.
1 parent da091b4 commit 2976c1c

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

deploy_nixos/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,18 @@ see also:
9999

100100
| Name | Description | Type | Default | Required |
101101
|------|-------------|------|---------|:--------:|
102-
| NIX\_PATH | Allow to pass custom NIX\_PATH. Ignored if `-`. | `string` | `"-"` | no |
102+
| NIX\_PATH | Allow to pass custom NIX\_PATH | `string` | `""` | no |
103103
| build\_on\_target | Avoid building on the deployer. Must be true or false. Has no effect when deploying from an incompatible system. Unlike remote builders, this does not require the deploying user to be trusted by its host. | `string` | `false` | no |
104104
| config | NixOS configuration to be evaluated. This argument is required unless 'nixos\_config' is given | `string` | `""` | no |
105105
| config\_pwd | Directory to evaluate the configuration in. This argument is required if 'config' is given | `string` | `""` | no |
106106
| extra\_build\_args | List of arguments to pass to the nix builder | `list(string)` | `[]` | no |
107107
| extra\_eval\_args | List of arguments to pass to the nix evaluation | `list(string)` | `[]` | no |
108108
| keys | A map of filename to content to upload as secrets in /var/keys | `map(string)` | `{}` | no |
109109
| nixos\_config | Path to a NixOS configuration | `string` | `""` | no |
110-
| ssh\_agent | Whether to use an SSH agent | `bool` | `true` | no |
111-
| ssh\_private\_key\_file | Path to private key used to connect to the target\_host. Ignored if `-` or empty. | `string` | `"-"` | no |
112-
| target\_host | DNS host to deploy to | `any` | n/a | yes |
110+
| ssh\_agent | Whether to use an SSH agent. True if not ssh\_private\_key is passed | `bool` | `null` | no |
111+
| ssh\_private\_key | Content of private key used to connect to the target\_host | `string` | `""` | no |
112+
| ssh\_private\_key\_file | Path to private key used to connect to the target\_host | `string` | `""` | no |
113+
| target\_host | DNS host to deploy to | `string` | n/a | yes |
113114
| target\_port | SSH port used to connect to the target\_host | `number` | `22` | no |
114115
| target\_system | Nix system string | `string` | `"x86_64-linux"` | no |
115116
| target\_user | SSH user used to connect to the target\_host | `string` | `"root"` | no |

deploy_nixos/main.tf

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,71 @@
1+
variable "target_host" {
2+
type = string
3+
description = "DNS host to deploy to"
4+
}
5+
16
variable "target_user" {
7+
type = string
28
description = "SSH user used to connect to the target_host"
39
default = "root"
410
}
511

6-
variable "target_host" {
7-
description = "DNS host to deploy to"
8-
}
9-
1012
variable "target_port" {
11-
description = "SSH port used to connect to the target_host"
1213
type = number
14+
description = "SSH port used to connect to the target_host"
1315
default = 22
1416
}
1517

18+
variable "ssh_private_key" {
19+
type = string
20+
description = "Content of private key used to connect to the target_host"
21+
default = ""
22+
}
23+
1624
variable "ssh_private_key_file" {
17-
description = "Path to private key used to connect to the target_host. Ignored if `-` or empty."
18-
default = "-"
25+
type = string
26+
description = "Path to private key used to connect to the target_host"
27+
default = ""
1928
}
2029

2130
variable "ssh_agent" {
22-
description = "Whether to use an SSH agent"
2331
type = bool
24-
default = true
32+
description = "Whether to use an SSH agent. True if not ssh_private_key is passed"
33+
default = null
2534
}
2635

2736
variable "NIX_PATH" {
28-
description = "Allow to pass custom NIX_PATH. Ignored if `-`."
29-
default = "-"
37+
type = string
38+
description = "Allow to pass custom NIX_PATH"
39+
default = ""
3040
}
3141

3242
variable "nixos_config" {
43+
type = string
3344
description = "Path to a NixOS configuration"
3445
default = ""
3546
}
3647

3748
variable "config" {
49+
type = string
3850
description = "NixOS configuration to be evaluated. This argument is required unless 'nixos_config' is given"
3951
default = ""
4052
}
4153

4254
variable "config_pwd" {
55+
type = string
4356
description = "Directory to evaluate the configuration in. This argument is required if 'config' is given"
4457
default = ""
4558
}
4659

4760
variable "extra_eval_args" {
48-
description = "List of arguments to pass to the nix evaluation"
4961
type = list(string)
62+
description = "List of arguments to pass to the nix evaluation"
5063
default = []
5164
}
5265

5366
variable "extra_build_args" {
54-
description = "List of arguments to pass to the nix builder"
5567
type = list(string)
68+
description = "List of arguments to pass to the nix builder"
5669
default = []
5770
}
5871

@@ -75,9 +88,9 @@ variable "keys" {
7588
}
7689

7790
variable "target_system" {
78-
type = string
91+
type = string
7992
description = "Nix system string"
80-
default = "x86_64-linux"
93+
default = "x86_64-linux"
8194
}
8295

8396
# --------------------------------------------------------------------------
@@ -95,16 +108,18 @@ locals {
95108
var.extra_build_args,
96109
)
97110
ssh_private_key_file = var.ssh_private_key_file == "" ? "-" : var.ssh_private_key_file
98-
build_on_target = data.external.nixos-instantiate.result["currentSystem"] != var.target_system ? true : tobool(var.build_on_target)
111+
ssh_private_key = local.ssh_private_key_file == "-" ? var.ssh_private_key : file(local.ssh_private_key_file)
112+
ssh_agent = var.ssh_agent == null ? (local.ssh_private_key != "") : var.ssh_agent
113+
build_on_target = data.external.nixos-instantiate.result["currentSystem"] != var.target_system ? true : tobool(var.build_on_target)
99114
}
100115

101116
# used to detect changes in the configuration
102117
data "external" "nixos-instantiate" {
103118
program = concat([
104119
"${path.module}/nixos-instantiate.sh",
105-
var.NIX_PATH,
120+
var.NIX_PATH == "" ? "-" : var.NIX_PATH,
106121
var.config != "" ? var.config : var.nixos_config,
107-
var.config_pwd != "" ? var.config_pwd : ".",
122+
var.config_pwd == "" ? "." : var.config_pwd,
108123
# end of positional arguments
109124
# start of pass-through arguments
110125
"--argstr", "system", var.target_system
@@ -121,26 +136,23 @@ resource "null_resource" "deploy_nixos" {
121136
host = var.target_host
122137
port = var.target_port
123138
user = var.target_user
124-
agent = var.ssh_agent
139+
agent = local.ssh_agent
125140
timeout = "100s"
126-
private_key = local.ssh_private_key_file != "-" ? file(var.ssh_private_key_file) : null
141+
private_key = local.ssh_private_key == "-" ? "" : local.ssh_private_key
127142
}
128143

129-
# copy the secret keys to the host
130144
# copy the secret keys to the host
131145
provisioner "file" {
132146
content = jsonencode(var.keys)
133147
destination = "packed-keys.json"
134148
}
135149

136-
# FIXME: move this to nixos-deploy.sh
137150
# FIXME: move this to nixos-deploy.sh
138151
provisioner "file" {
139152
source = "${path.module}/unpack-keys.sh"
140153
destination = "unpack-keys.sh"
141154
}
142155

143-
# FIXME: move this to nixos-deploy.sh
144156
# FIXME: move this to nixos-deploy.sh
145157
provisioner "file" {
146158
source = "${path.module}/maybe-sudo.sh"
@@ -154,7 +166,6 @@ resource "null_resource" "deploy_nixos" {
154166
]
155167
}
156168

157-
# do the actual deployment
158169
# do the actual deployment
159170
provisioner "local-exec" {
160171
interpreter = concat([
@@ -164,7 +175,7 @@ resource "null_resource" "deploy_nixos" {
164175
"${var.target_user}@${var.target_host}",
165176
var.target_port,
166177
local.build_on_target,
167-
local.ssh_private_key_file,
178+
local.ssh_private_key == "" ? "-" : local.ssh_private_key,
168179
"switch",
169180
],
170181
local.extra_build_args

deploy_nixos/nixos-deploy.sh

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ buildArgs=(
1111
)
1212
profile=/nix/var/nix/profiles/system
1313
# will be set later
14-
controlPath=
1514
sshOpts=(
1615
-o "ControlMaster=auto"
1716
-o "ControlPersist=60"
@@ -32,7 +31,7 @@ outPath="$2"
3231
targetHost="$3"
3332
targetPort="$4"
3433
buildOnTarget="$5"
35-
sshPrivateKeyFile="$6"
34+
sshPrivateKey="$6"
3635
action="$7"
3736
shift 7
3837

@@ -42,8 +41,13 @@ buildArgs+=("$@")
4241

4342
sshOpts+=( -p "${targetPort}" )
4443

45-
if [[ -n "${sshPrivateKeyFile}" && "${sshPrivateKeyFile}" != "-" ]]; then
46-
sshOpts+=( -o "IdentityFile=${sshPrivateKeyFile}" )
44+
workDir=$(mktemp -d)
45+
trap 'rm -rf "$workDir"' EXIT
46+
47+
if [[ -n "${sshPrivateKey}" && "${sshPrivateKey}" != "-" ]]; then
48+
sshPrivateKeyFile="$workDir/ssh_key"
49+
echo "$sshPrivateKey" > "$sshPrivateKeyFile"
50+
sshOpts+=( -o "IdentityFile=${sshPrivateKeyFile}" )
4751
fi
4852

4953
### Functions ###
@@ -62,16 +66,17 @@ targetHostCmd() {
6266
# `ssh` did not properly maintain the array nature of the command line,
6367
# erroneously splitting arguments with internal spaces, even when using `--`.
6468
# Tested with OpenSSH_7.9p1.
69+
#
70+
# shellcheck disable=SC2029
6571
ssh "${sshOpts[@]}" "$targetHost" "./maybe-sudo.sh ${*@Q}"
6672
}
6773

6874
# Setup a temporary ControlPath for this session. This speeds-up the
6975
# operations by not re-creating SSH sessions between each command. At the end
7076
# of the run, the session is forcefully terminated.
7177
setupControlPath() {
72-
controlPath=$(mktemp)
7378
sshOpts+=(
74-
-o "ControlPath=$controlPath"
79+
-o "ControlPath=$workDir/ssh_control"
7580
)
7681
cleanupControlPath() {
7782
local ret=$?
@@ -80,7 +85,7 @@ setupControlPath() {
8085
# Close ssh multiplex-master process gracefully
8186
log "closing persistent ssh-connection"
8287
ssh "${sshOpts[@]}" -O stop "$targetHost"
83-
rm -f "$controlPath"
88+
rm -rf "$workDir"
8489
exit "$ret"
8590
}
8691
trap cleanupControlPath EXIT

deploy_nixos/unpack-keys.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)