Skip to content

Commit

Permalink
Refactor Action::Base#write_config
Browse files Browse the repository at this point in the history
  • Loading branch information
tmatilai committed Sep 27, 2013
1 parent ab134ea commit 805e857
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/vagrant-proxyconf/action/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,31 @@ def configure_machine(machine, config)
#
# @param opts [Hash] optional file options
# @option opts [String] :path (#config_path) the path of the configuration file
# @option opts [String] :mode the mode of the file
# @option opts [String] :mode ("0644") the mode of the file
# @option opts [String] :owner ("root:root") the owner (and group) of the file
def write_config(machine, config, opts = {})
tmp = "/tmp/vagrant-proxyconf"
path = opts[:path] || config_path(machine)
logger.debug "Configuration (#{path}):\n#{config}"

temp = Tempfile.new("vagrant")
temp.binmode
temp.write(config)
temp.close

logger.debug "Configuration (#{path}):\n#{config}"
machine.communicate.tap do |comm|
tmp = "/tmp/vagrant-proxyconf"
comm.upload(temp.path, tmp)
mode = opts[:mode] || '0644'
comm.sudo("chmod #{mode} #{tmp}")
comm.sudo("chown root:root #{tmp}")
comm.upload(tempfile(config).path, tmp)
comm.sudo("chmod #{opts[:mode] || '0644'} #{tmp}")
comm.sudo("chown #{opts[:owner] || 'root:root'} #{tmp}")
comm.sudo("mkdir -p #{File.dirname(path)}")
comm.sudo("mv #{tmp} #{path}")
end
end

# @return [Tempfile] a temporary file with the specified content
def tempfile(content)
Tempfile.new("vagrant").tap do |temp|
temp.binmode
temp.write(content)
temp.close
end
end

def cap_name
"#{config_name}_conf".to_sym
end
Expand Down

0 comments on commit 805e857

Please sign in to comment.