-
Notifications
You must be signed in to change notification settings - Fork 40
/
start-docker-nix-build-slave
executable file
·70 lines (57 loc) · 2.36 KB
/
start-docker-nix-build-slave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Requires: curl, docker, grep
working_dir_name=".nix-docker-build-slave"
working_dir="$HOME/$working_dir_name"
mkdir -p "$working_dir"
ssh_id_file="$working_dir/insecure_rsa"
remote_sys_conf="$working_dir/remote-systems.conf"
ssh_config="$HOME/.ssh/config"
docker_machine_name="nix-docker-build-slave"
# -- Display info and troubleshooting tips --
echo "## Use Docker Container as Nix Build Slave"
echo "##"
echo "## Usage: source $0"
echo "##"
echo "## Note: if you experience issues, you can clean up the build slave artifacts and start over by:"
echo "## 1. rm -r ~/$working_dir_name"
echo "## 2. Delete entry in ~/.ssh/config for"
echo "## Host \"$docker_machine_name\""
echo "## 3. Delete the docker container named"
echo "## $docker_machine_name"
echo
# -- Download SSH credentials for docker container --
echo ">>> Downloading SSH credentials for the docker container"
rm -f "$ssh_id_file" "$ssh_id_file.pub"
curl -fsSL https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa -o "$ssh_id_file"
curl -fsSL https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa.pub -o "$ssh_id_file.pub"
chmod 600 "$ssh_id_file"
# -- Set up SSH configuration --
[ -f "$ssh_config" ] || touch "$ssh_config"
if ! grep "$docker_machine_name" "$HOME/.ssh/config" > /dev/null; then
echo ">>> Adding an entry to $ssh_config for $docker_machine_name"
cat >> "$ssh_config" <<CONF
Host "$docker_machine_name"
User root
HostName 127.0.0.1
Port 3022
IdentityFile "$ssh_id_file"
CONF
else
echo ">>> SSH configuration already contains an entry for $docker_machine_name in $ssh_config"
fi
# -- Start docker container --
echo ">>> Starting docker container: $docker_machine_name"
echo " (This may fail if the container was already created.)"
docker run --restart always --name "$docker_machine_name" -d -p 3022:22 lnl7/nix:ssh
# -- Write remote systems configuration --
echo ">>> Writing remote systems configuration to $remote_sys_conf"
rm -f "$remote_sys_conf"
cat > "$remote_sys_conf" <<CONF
$docker_machine_name x86_64-linux $ssh_id_file 1
CONF
# -- Test connection --
echo ">>> Running SSH test"
ssh "$docker_machine_name" echo "SSH connection is working." || echo "SSH connection failed."
# -- Export environment --
echo ">>> Setting \$NIX_REMOTE_SYSTEMS to use $remote_sys_conf"
export NIX_REMOTE_SYSTEMS="$remote_sys_conf"