Class: Mccloud::Provisioner::Puppet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Puppet

Returns a new instance of Puppet.



21
22
23
24
25
26
27
28
29
30
# File 'lib/mccloud/provisioner/puppet.rb', line 21

def initialize(env)
  @env=env
  @manifest_file = nil
  @manifests_path = "manifests"
  @module_paths = []
  @pp_path = "/tmp/mccloud-puppet"
  @name="puppet"
  @options = []
  @remote_environment={}
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



10
11
12
# File 'lib/mccloud/provisioner/puppet.rb', line 10

def env
  @env
end

#manifest_fileObject

Returns the value of attribute manifest_file.



12
13
14
# File 'lib/mccloud/provisioner/puppet.rb', line 12

def manifest_file
  @manifest_file
end

#manifests_pathObject

Returns the value of attribute manifests_path.



13
14
15
# File 'lib/mccloud/provisioner/puppet.rb', line 13

def manifests_path
  @manifests_path
end

#module_pathObject

Returns the value of attribute module_path.



14
15
16
# File 'lib/mccloud/provisioner/puppet.rb', line 14

def module_path
  @module_path
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/mccloud/provisioner/puppet.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/mccloud/provisioner/puppet.rb', line 16

def options
  @options
end

#pp_pathObject

Returns the value of attribute pp_path.



15
16
17
# File 'lib/mccloud/provisioner/puppet.rb', line 15

def pp_path
  @pp_path
end

#remote_environmentObject

Returns the value of attribute remote_environment.



17
18
19
# File 'lib/mccloud/provisioner/puppet.rb', line 17

def remote_environment
  @remote_environment
end

#serverObject (readonly)

Returns the value of attribute server.



19
20
21
# File 'lib/mccloud/provisioner/puppet.rb', line 19

def server
  @server
end

Instance Method Details

#cleanupObject



165
166
# File 'lib/mccloud/provisioner/puppet.rb', line 165

def cleanup
end

#erbify(filename) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mccloud/provisioner/puppet.rb', line 136

def erbify(filename)

  # Fill the array to pass to the ERB
  public_ips=Hash.new
  private_ips=Hash.new
  server.provider.vms.each do |name,vm|
    public_ips[name] = vm.public_ip_address
    private_ips[name] = vm.private_ip_address
  end

  data = { :public_ips => public_ips, :private_ips => private_ips}
  vars = ErbBinding.new(data)

  template = File.read(filename)
  erb = ERB.new(template)

  vars_binding = vars.send(:get_binding)
  result=erb.result(vars_binding)
  return result

end

#expanded_manifests_pathObject

Returns the manifests path expanded relative to the root path of the environment.



34
35
36
# File 'lib/mccloud/provisioner/puppet.rb', line 34

def expanded_manifests_path
  Pathname.new(manifests_path).expand_path(env.root_path)
end

#expanded_manifests_pathsObject

Returns the manifests paths as an array of paths expanded relative to the root path.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mccloud/provisioner/puppet.rb', line 55

def expanded_manifests_paths
  return [] if !manifests_path

  # Get all the paths and expand them relative to the root path, returning
  # the array of expanded paths
  paths = manifests_path
  paths = [paths] if !paths.is_a?(Array)
  paths.map do |path|
    Pathname.new(path).expand_path(env.root_path)
  end
end

#expanded_module_pathsObject

Returns the module paths as an array of paths expanded relative to the root path.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mccloud/provisioner/puppet.rb', line 41

def expanded_module_paths
  return [] if !module_path

  # Get all the paths and expand them relative to the root path, returning
  # the array of expanded paths
  paths = module_path
  paths = [paths] if !paths.is_a?(Array)
  paths.map do |path|
    Pathname.new(path).expand_path(env.root_path)
  end
end

#is_erb?(filename) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
# File 'lib/mccloud/provisioner/puppet.rb', line 130

def is_erb?(filename)
  result=File.extname(filename) == ".erb"
  puts result
  return result
end

#prepareObject



158
159
160
161
162
163
# File 'lib/mccloud/provisioner/puppet.rb', line 158

def prepare
  share_manifests
  share_modules
  share_manifest
  set_module_paths
end

#run(server) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/mccloud/provisioner/puppet.rb', line 168

def run(server)
  @server=server
  if @manifest_file.nil?
    env.ui.info "No specific manifestfile specified defaulting to site.pp in the manifest dir"
  end

  env.logger.info "Starting puppet run"
  server.execute("mkdir -p '#{@pp_path}'")
  server.execute("chmod 700 '#{@pp_path}'")
  prepare

  env.ui.info "Running puppet"

  pre_options=[]
  unless @remote_environment.empty?
    @remote_environment.each do |key,value|
      pre_options << "#{key.to_s}=\"#{value}\""
    end
  end

  puppet_options=[ "apply"]
  @options.each do |o|
    puppet_options << o
  end


  unless @module_paths.empty?
    puppet_options << "--modulepath=#{@module_paths.values.join(':')}"
  end

  manifestdir="#{File.join(@pp_path,'manifests-0')}"
  puppet_options << "--manifestdir=#{manifestdir}"

  if @manifest_file.nil?
    puppet_options << File.join(manifestdir,"site.pp")
  else
    if is_erb?(@manifest_file)
      puppet_options << File.join(@pp_path,File.basename(@manifest_file,".erb"))
    else
      puppet_options << File.join(@pp_path,File.basename(@manifest_file))
    end
  end

  server.sudo("#{pre_options.join(" ")} puppet #{puppet_options.join(' ')}")
end

#set_module_pathsObject



90
91
92
93
94
95
# File 'lib/mccloud/provisioner/puppet.rb', line 90

def set_module_paths
  @module_paths = {}
  expanded_module_paths.each_with_index do |path, i|
    @module_paths[path] = File.join(@pp_path, "modules-#{i}")
  end
end

#share_manifestObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mccloud/provisioner/puppet.rb', line 97

def share_manifest
  if @manifest_file.nil?
    # Nothing to do here
    return
  end

  full_path=Pathname.new(File.join(manifests_path,@manifest_file)).expand_path(env.root_path).to_s

  # These are the default
  dest=File.join(@pp_path,File.basename(@manifest_file))
  src=full_path

  # ERB file
  if is_erb?(full_path)
    env.ui.info "Interpreting ERB puppet manifest"

    # Correct the src path
    temp_file = Tempfile.new("puppet_erb")
    src=temp_file.path
    result=erbify(full_path)
    File.open(temp_file,'w') { |f| f.write(result)}

    # Correct the dest path
    dest=File.join(@pp_path,File.basename(@manifest_file,".erb"))

    # Immediately unlink it
    #temp_file.close(true)
  end

  env.ui.info "Synching manifest #{src} -> #{dest}"
  server.transfer(src,dest)
end

#share_manifestsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mccloud/provisioner/puppet.rb', line 78

def share_manifests
  i=0
  expanded_manifests_paths.each do |path|
    remote_path=File.join(pp_path,"manifests-#{i}")
    env.ui.info "Sharing manifest dir #{path}"
    server.execute("test -d '#{remote_path}' || mkdir -p '#{remote_path}'")
    server.share_sync(path,remote_path,{:mute => false})
    server.execute("chmod 700 '#{remote_path}'")
    i=i+1
  end
end

#share_modulesObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/mccloud/provisioner/puppet.rb', line 67

def share_modules
  i=0
  expanded_module_paths.each do |path|
    remote_path=File.join(pp_path,"modules-#{i}")
    env.ui.info "Sharing module dir #{path}"
    server.share_sync(path,remote_path,{:mute => false})
    server.execute("chmod 700 '#{remote_path}'")
    i=i+1
  end
end