Class: VagrantPlugins::Saltdeps::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-saltdeps/provisioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, config) ⇒ Provisioner

Returns a new instance of Provisioner.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-saltdeps/provisioner.rb', line 11

def initialize(machine, config)
  super
  @logger          = Log4r::Logger.new("vagrant::provisioner::saltdeps")
  @checkout_path   = config.checkout_path
  @grains_path     = config.grains_path
  @pillars_path    = config.pillars_path
  @merge_pillars   = config.merge_pillars
  @merge_grains    = config.merge_grains
  @merged_path     = config.merged_path
  @deps            = YAML.load_file(config.deps_path)['deps'] || {}
  @current_formula = YAML.load_file(config.deps_path)['name']
  @formula_folders = []
end

Instance Method Details

#checkout_depsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-saltdeps/provisioner.rb', line 32

def checkout_deps
  @deps.each do |name,props|
    uri    = props.fetch('git', 'foobar')
    name   = "#{name}-formula"
    branch = props.fetch('branch','master')

    if File.directory? @checkout_path + "/#{name}"
      g = Git.open @checkout_path + "/#{name}"
    else
      g = Git.clone(uri, name, path: @checkout_path)
    end
    begin
      g.checkout(branch)
      g.pull
    rescue  Git::GitExecuteError => e
      raise GitCheckoutError.new :branch => branch, :message => e.message
    end
  end
end

#clean_up_pathObject



127
128
129
130
131
# File 'lib/vagrant-saltdeps/provisioner.rb', line 127

def clean_up_path
  checkout_path = @checkout_path.split('/')
  base_checkout_path = checkout_path[0..(checkout_path.length-2)].join('/')
  File.expand_path(base_checkout_path, @machine.env.root_path)
end

#cleanupObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-saltdeps/provisioner.rb', line 73

def cleanup
  if File.directory? @checkout_path
    FileUtils.rm_rf(@checkout_path)
  end
  if File.directory? @merged_path
    FileUtils.rm(@merged_path + '/compiled_grains') if File.exists? @merged_path + '/compiled_grains'
    FileUtils.rm(@merged_path + '/compiled_pillars') if File.exists? @merged_path + '/compiled_pillars'
  end
  cleanup_check_path = self.clean_up_path
  cleanup_folders    = @deps.keys
  cleanup_folders   << @current_formula

  cleanup_folders.each do |dep|
    FileUtils.rm_rf(cleanup_check_path + '/' + dep) if File.directory?(cleanup_check_path + '/' + dep)
  end
end

#configure(root_config) ⇒ Object



25
26
27
28
29
30
# File 'lib/vagrant-saltdeps/provisioner.rb', line 25

def configure(root_config)
  checkout_deps
  link_deps(root_config)
  merge_grains
  merge_pillars
end


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-saltdeps/provisioner.rb', line 52

def link_deps(root_config)
  @deps.each do |name, props|
    base_path = @checkout_path + "/#{name}-formula/"
    if File.directory? base_path + "#{name}"
      @formula_folders << {host_path: base_path + "#{name}", guest_path: '/srv/salt'}
    else
      @machine.ui.warn("Not creating shared folder for dependency #{name} because no folder was found at #{base_path + name}")
    end

  end
end

#merge(path, output) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vagrant-saltdeps/provisioner.rb', line 112

def merge(path, output)
  local_path  = File.expand_path(path,  @machine.env.root_path)

  merge_object =  {}
  @formula_folders.each do |folder|
    split_host_path = folder[:host_path].split('/')
    base_dep_path = split_host_path[0..(split_host_path.length-2)].join('/')
    dep_path = File.expand_path(base_dep_path + '/' + path, @machine.env.root_path)
    merge_object.deep_merge!(YAML.load_file(dep_path) || {}) if File.exists?(dep_path)
  end

  merge_object.deep_merge!(File.exists?(local_path)  ? YAML.load_file(local_path) || {} : {})
  File.open(output,  'w') {|f| f.write merge_object.to_yaml }
end

#merge_grainsObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/vagrant-saltdeps/provisioner.rb', line 90

def merge_grains
  output_path = File.expand_path(@merged_path+'/compiled_grains', @machine.env.root_path)
  if @merge_grains
    merge(@grains_path, output_path)
    @machine.config.vm.provisioners.each do |provisioner|
      next unless provisioner.type == :salt
      provisioner.config.grains_config= output_path
    end
  end
end

#merge_pillarsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-saltdeps/provisioner.rb', line 101

def merge_pillars
  output_path = File.expand_path(@merged_path+'/compiled_pillars', @machine.env.root_path)
  if @merge_pillars
    merge(@pillars_path, output_path)
    @machine.config.vm.provisioners.each do |provisioner|
      next unless provisioner.type == :salt
      provisioner.config.pillar(YAML.load_file(output_path) || {})
    end
  end
end

#provisionObject



64
65
66
67
68
69
70
71
# File 'lib/vagrant-saltdeps/provisioner.rb', line 64

def provision
  communicator = @machine.communicate
  @formula_folders.each do |folder|
    communicator.upload(folder[:host_path],folder[:guest_path])
  end
  current_path = File.expand_path(@current_formula, @machine.env.root_path)
  communicator.upload(current_path,'/srv/salt')
end