Skip to content

Instantly share code, notes, and snippets.

@3noch
Last active February 22, 2017 19:28
Show Gist options
  • Save 3noch/6fd709fea46ebfd598bb2a725d70cf93 to your computer and use it in GitHub Desktop.
Save 3noch/6fd709fea46ebfd598bb2a725d70cf93 to your computer and use it in GitHub Desktop.
Docker Nix Build Slave
#!/usr/bin/env bash
# Requires: curl, docker, grep
# To clean up:
# * Remove ~/.nix-docker-build-slave.
# * Remove entries from ~/.ssh/config for nix-docker-build-slave.
working_dir="$HOME/.nix-docker-build-slave"
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"
# -- Download SSH credentials for docker machine --
echo ">>> Downloading SSH credentials for the docker machine"
rm -f "$ssh_id_file" "$ssh_id_file.pub"
curl https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa > "$ssh_id_file"
curl https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa.pub > "$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"; 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"
echo "(If you're having trouble using the build slave, try removing this entry and starting over.)"
fi
# -- Start docker machine --
echo ">>> Starting docker machine: $docker_machine_name"
docker run --restart always --name "$docker_machine_name" -d -p 3022:22 lnl7/nix:ssh
# -- Write remote systems confugration --
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment