Commit 2ec4fb01 authored by Volker Hilsheimer's avatar Volker Hilsheimer
Browse files

Cleanup: get rid of reverse mutagen mode

It's never been implemented for all platform, is completely untested,
useless for cloud machines, and overly complex. With the current version
of mutagen, we also don't need to create the directory structure on the
guest anymore, that shaves a few more seconds from creating a machine.

We can assume that mutagen is on the host, or that the configuration
doesn't use mutagen.
parent 0571dae1
param(
[string]$reverse,
[string]$role,
[string]$boxname,
[string]$user,
[string]$mutagen_host_ip,
[string[]]$alpha,
[string[]]$beta
)
function Download-Mutagen {
param(
$ARCH,
$VERSION,
$PATH
)
$PLATFORM="windows"
$FILE="mutagen_${PLATFORM}_${ARCH}_v${VERSION}.tar.gz"
$URL="https://github.com/mutagen-io/mutagen/releases/download/v${VERSION}/${FILE}"
Write-Host "Downloading '$URL'"
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
$webClient = new-object net.webclient
$webClient.DownloadFile($URL, $PATH) | out-null
}
function Install-Mutagen {
param(
[string]$Version,
[string]$InstallPath
)
if (!(Test-Path $InstallPath)) {
New-Item -ItemType Directory -Force -Path $InstallPath | Out-Null
}
cd $InstallPath
if (!(Test-Path "mutagen.tar.gz")) {
Download-Mutagen -Arch amd64 -Version $Version -Path "${InstallPath}\mutagen.tar.gz"
}
tar -xzf mutagen.tar.gz
Remove-Item mutagen.tar.gz
Write-Host "Starting mutagen daemon"
c:\mutagen\mutagen daemon register
c:\mutagen\mutagen daemon start
}
function Mount-Paths {
param(
[string[]]$Alphas,
[string[]]$Betas
)
begin {
if ($Alphas.count -ne $Betas.count) {
throw "Alphas and Betas need to have the same number of entries!"
}
if ($reverse -eq "true") {
ssh-keyscan $mutagen_host_ip >> c:\Users\vagrant\.ssh\known_hosts
}
}
process{
if ($reverse -eq "true") {
c:\mutagen\mutagen sync terminate minicoin | Out-Null
}
for ($i = 0; $i -lt $Alphas.count; $i++) {
$a = $Alphas[$i]
$b = $Betas[$i]
if (!(Test-Path $b)) {
New-Item -ItemType Directory -Force -Path $b | Out-Null
}
if ($reverse -eq "true") {
Write-Host "Mapping as" $user "from" $a "on" $mutagen_host_ip "to" $b
echo yes | c:\mutagen\mutagen sync create --sync-mode one-way-replica --ignore-vcs --name minicoin ${user}@${mutagen_host_ip}:$a $b
}
}
}
end {
if ($reverse -eq "true") {
Write-Host "Established mutagen sync points:"
c:\mutagen\mutagen sync list
}
}
}
if ($reverse -eq "true") {
Install-Mutagen -InstallPath "$env:SystemDrive\mutagen" -Version "0.13.1"
}
Mount-Paths -Alphas $alpha.split(",") -Betas $beta.split(",")
require 'socket'
require 'open3'
$HOST_HAS_MUTAGEN = nil
def mutagen_guest_to_host(box, name)
key_file = "#{$PWD}/.vagrant/machines/#{name}/mutagen"
authorized_keys = "#{$HOME}/.ssh/authorized_keys"
box.trigger.before [:up, :provision] do |trigger|
trigger.name = "Generating mutagen key pair and authorizing guest"
trigger.ruby do |e,m|
if !File.file?(key_file)
`ssh-keygen -f #{key_file} -C mutagen@#{name} -q -N \"\"`
public_key = File.read("#{key_file}.pub")
open(authorized_keys, 'a') do |file|
file.puts "#{public_key}"
end
File.chmod(0600, authorized_keys)
end
end
end
box.trigger.before [:destroy] do |trigger|
trigger.name = "Removing mutagen key pair and de-authorising guest"
trigger.ruby do |e,m|
File.open("#{authorized_keys}.new", 'w') do |out|
out.chmod(File.stat(authorized_keys).mode)
File.foreach(authorized_keys) do |line|
out.puts line unless line =~ /mutagen@#{name}/
end
end
File.rename("#{authorized_keys}.new", authorized_keys)
File.delete(key_file) if File.exist?(key_file)
File.delete("#{key_file}.pub") if File.exist?("#{key_file}.pub")
end
end
if ["up", "provision", "reload"].include? ARGV[0]
# needs to be id_rsa, mutagen doesn't allow specifying an ssh identity file
if box.vm.guest == :windows
dest = "C:\\Users\\vagrant\\.ssh\\id_rsa"
else
dest = "~/.ssh/id_rsa"
end
box.vm.provision "mutagen(reverse):key upload",
type: :file,
source: key_file,
destination: dest
end
end
def mutagen_provision(box, name, role_params, machine)
paths = role_params["paths"]
role_params.delete("paths")
# autodetect if mutagen is locally installed; if not, try to install it on the guest
if role_params['reverse'].nil?
if $HOST_HAS_MUTAGEN.nil?
begin
`mutagen version`
$HOST_HAS_MUTAGEN = true
rescue
$HOST_HAS_MUTAGEN = false
end
end
role_params["reverse"] = !$HOST_HAS_MUTAGEN
end
if role_params["reverse"] == true
address = Socket.ip_address_list.detect{|intf| intf.ipv4_private? }
ip_address = address.ip_address
role_params["mutagen_host_ip"] = ip_address
end
alphas = []
betas = []
sessions = {}
......@@ -90,17 +19,10 @@ def mutagen_provision(box, name, role_params, machine)
paths.each do |alpha, beta|
alphas << alpha
if box.vm.guest == :windows
if role_params["reverse"] == true
beta.gsub!("~", "#{box.minicoin.guest_homes}\\vagrant")
beta.gsub!($HOME, "#{box.minicoin.guest_homes}\\vagrant")
beta.gsub!("/", "\\")
beta.gsub!(/^\\$/, "C:\\")
else
beta.gsub!("~", "#{box.minicoin.guest_homes}/vagrant")
beta.gsub!($HOME, "#{box.minicoin.guest_homes}/vagrant")
beta.gsub!("\\", "/")
beta.gsub!(/^\//, "C:/")
end
beta.gsub!("~", "#{box.minicoin.guest_homes}/vagrant")
beta.gsub!($HOME, "#{box.minicoin.guest_homes}/vagrant")
beta.gsub!("\\", "/")
beta.gsub!(/^\//, "C:/")
else
beta.gsub!("~", "#{box.minicoin.guest_homes}/vagrant")
beta.gsub!($HOME, "#{box.minicoin.guest_homes}/vagrant")
......@@ -113,18 +35,14 @@ def mutagen_provision(box, name, role_params, machine)
role_params["alpha"] = alphas
role_params["beta"] = betas
if role_params["reverse"] == true
mutagen_guest_to_host(box, name, alphas, betas, role_params["ignores"])
else
sessions.each do |alpha, beta|
ignores = [ role_params["ignores"] || [] ].flatten
options = [ role_params["options"] || [ "--ignore-vcs" ] ].flatten
ignores.each do |ignore|
options << "--ignore #{ignore}"
end
box.vm.synced_folder alpha, beta,
type: :mutagen,
mount_options: options
sessions.each do |alpha, beta|
ignores = [ role_params["ignores"] || [] ].flatten
options = [ role_params["options"] || [ "--ignore-vcs" ] ].flatten
ignores.each do |ignore|
options << "--ignore #{ignore}"
end
box.vm.synced_folder alpha, beta,
type: :mutagen,
mount_options: options
end
end
. /opt/minicoin/util/parse-opts.sh $HOME "$@"
if [ "$PARAM_reverse" == "true" ]
then
platform=$(uname)
if [[ $(uname) =~ "Darwin" ]]
then
sudo -H -u vagrant brew install mutagen-io/mutagen/mutagen
else
echo "Mutagen not implemented"
exit 1
fi
USERNAME="${POSITIONAL[2]}"
HOST=$(echo $SSH_CONNECTION | cut -f 1 -d ' ')
ssh-keyscan -H "$HOST" >> /Users/vagrant/.ssh/known_hosts
sudo -H -u vagrant mutagen daemon register
sudo -H -u vagrant mutagen daemon start
sudo -H -u vagrant mutagen sync terminate minicoin 2> /dev/null
fi
for ((i=0;i<${#PARAM_alpha[@]};++i))
do
beta=${PARAM_beta[i]}
sudo -H -u vagrant mkdir -p $beta
if [ "$PARAM_reverse" == "true" ]
then
sudo -H -u vagrant mutagen sync create --sync-mode one-way-replica --ignore-vcs --name minicoin ${USERNAME}@${HOST}:${PARAM_alpha[i]} $beta
fi
done
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment