Skip to content

Commit 5761c05

Browse files
authored
deploy_nixos: add ssh_private_key (#37)
Sometimes it's useful to pass the SSH key content directly.
1 parent 5f5a040 commit 5761c05

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

deploy_nixos/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ see also:
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 |
110110
| ssh\_agent | Whether to use an SSH agent | `bool` | `true` | no |
111+
| ssh\_private\_key | Content of private key used to connect to the target\_host. Ignored if empty. | `string` | `""` | no |
111112
| ssh\_private\_key\_file | Path to private key used to connect to the target\_host. Ignored if `-` or empty. | `string` | `"-"` | no |
112113
| target\_host | DNS host to deploy to | `any` | n/a | yes |
113114
| target\_port | SSH port used to connect to the target\_host | `number` | `22` | no |

deploy_nixos/main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ variable "target_port" {
1313
default = 22
1414
}
1515

16+
variable "ssh_private_key" {
17+
description = "Content of private key used to connect to the target_host. Ignored if empty."
18+
default = ""
19+
}
20+
1621
variable "ssh_private_key_file" {
1722
description = "Path to private key used to connect to the target_host. Ignored if `-` or empty."
1823
default = "-"
@@ -95,6 +100,7 @@ locals {
95100
var.extra_build_args,
96101
)
97102
ssh_private_key_file = var.ssh_private_key_file == "" ? "-" : var.ssh_private_key_file
103+
ssh_private_key = local.ssh_private_key_file == "-" ? null : file(local.ssh_private_key_file)
98104
build_on_target = data.external.nixos-instantiate.result["currentSystem"] != var.target_system ? true : tobool(var.build_on_target)
99105
}
100106

@@ -123,7 +129,7 @@ resource "null_resource" "deploy_nixos" {
123129
user = var.target_user
124130
agent = var.ssh_agent
125131
timeout = "100s"
126-
private_key = local.ssh_private_key_file != "-" ? file(var.ssh_private_key_file) : null
132+
private_key = local.ssh_private_key
127133
}
128134

129135
# copy the secret keys to the host
@@ -164,7 +170,7 @@ resource "null_resource" "deploy_nixos" {
164170
"${var.target_user}@${var.target_host}",
165171
var.target_port,
166172
local.build_on_target,
167-
local.ssh_private_key_file,
173+
local.ssh_private_key,
168174
"switch",
169175
],
170176
local.extra_build_args

deploy_nixos/nixos-deploy.sh

Lines changed: 12 additions & 7 deletions
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}" ]]; 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

0 commit comments

Comments
 (0)