2016-05-29 03:17:40 +00:00
|
|
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
|
|
|
require_relative "../../../../lib/vagrant/util/tempfile"
|
2014-05-15 02:22:48 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestNixos
|
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
include Vagrant::Util
|
|
|
|
|
|
|
|
def self.change_host_name(machine, name)
|
|
|
|
# upload the config file
|
2014-05-22 16:35:12 +00:00
|
|
|
hostname_module = TemplateRenderer.render("guests/nixos/hostname", name: name)
|
2014-05-15 02:22:48 +00:00
|
|
|
upload(machine, hostname_module, "/etc/nixos/vagrant-hostname.nix")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Upload a file.
|
|
|
|
def self.upload(machine, content, remote_path)
|
|
|
|
remote_temp = mktemp(machine)
|
2016-05-29 03:17:40 +00:00
|
|
|
|
|
|
|
Tempfile.create("nixos-change-host-name") do |f|
|
|
|
|
f.write(content)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
machine.communicate.upload(f.path, "#{remote_temp}")
|
|
|
|
end
|
|
|
|
|
2014-05-15 02:22:48 +00:00
|
|
|
machine.communicate.sudo("mv #{remote_temp} #{remote_path}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a temp file.
|
|
|
|
def self.mktemp(machine)
|
|
|
|
path = nil
|
|
|
|
|
|
|
|
machine.communicate.execute("mktemp --suffix -vagrant-upload") do |type, result|
|
|
|
|
path = result.chomp if type == :stdout
|
|
|
|
end
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|