Class: Vagrant::Puppet::Scp::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/puppet/scp/provisioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, config) ⇒ Provisioner

Returns a new instance of Provisioner.



11
12
13
14
15
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 11

def initialize(machine, config)
  super

  @logger = Log4r::Logger.new('vagrant::provisioners::puppet_scp')
end

Instance Method Details

#configure(root_config) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 17

def configure(root_config)
  # Calculate the paths we're going to use based on the environment
  root_path = @machine.env.root_path
  @expanded_manifests_path = @config.expanded_manifests_path(root_path)
  @expanded_modules_path = @config.expanded_modules_path(root_path)
  @manifest_file = File.join(@config.manifests_guest_path, @config.manifest_file)
end

#create_guest_pathObject



33
34
35
36
37
38
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 33

def create_guest_path
  @machine.communicate.tap do |comm|
    comm.sudo("mkdir -p #{@config.guest_path}")
    comm.sudo("chown -R #{@machine.ssh_info[:username]} #{@config.guest_path}")
  end
end

#provisionObject



25
26
27
28
29
30
31
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 25

def provision
  create_guest_path
  share_manifests
  share_modules
  verify_binary('puppet')
  run_puppet_apply
end

#recursive_scp(from, to) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 84

def recursive_scp(from, to)
  @machine.communicate.tap do |comm|
    comm.sudo("rm -rf #{to}")
    comm.sudo("mkdir -p #{to}")
    comm.sudo("chown #{@machine.ssh_info[:username]} #{to}")
  end

  Dir.glob("#{from}/**/*") do |path|
    to_path = path.gsub(from.to_s, '') # Remove the local cruft

    if File.directory?(path)
      @machine.communicate.execute("mkdir -p #{to}#{to_path}")
    else
      @machine.communicate.upload(path, "#{to}#{to_path}")
    end
  end
end

#run_puppet_applyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 56

def run_puppet_apply
  options = [config.options].flatten
  options << "--modulepath '#{@config.modules_guest_path}'" if !@config.modules_guest_path.empty?
  options << @manifest_file
  options = options.join(" ")

  # Build up the custom facts if we have any
  facter = ""
  if !config.facter.empty?
    facts = []
    config.facter.each do |key, value|
      facts << "FACTER_#{key}='#{value}'"
    end

    facter = "#{facts.join(" ")} "
  end

  command = "#{facter}puppet apply #{options} || [ $? -eq 2 ]"
  
  @machine.env.ui.info I18n.t("vagrant.provisioners.puppet.running_puppet",
                              :manifest => @config.manifest_file)

  @machine.communicate.sudo(command) do |type, data|
    data.chomp!
    @machine.env.ui.info(data, :prefix => false) if !data.empty?
  end
end

#share_manifestsObject



40
41
42
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 40

def share_manifests
  recursive_scp(@expanded_manifests_path, @config.manifests_guest_path)
end

#share_modulesObject



44
45
46
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 44

def share_modules
  recursive_scp(@expanded_modules_path, @config.modules_guest_path)
end

#verify_binary(binary) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/vagrant/puppet/scp/provisioner.rb', line 48

def verify_binary(binary)
  @machine.communicate.sudo(
    "which #{binary}",
    :error_class => PuppetScpError,
    :error_key => :not_detected,
    :binary => binary)
end