Class: MCollective::PluginPackager::ForgePackager

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/pluginpackager/forge_packager.rb

Instance Method Summary collapse

Constructor Details

#initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil) ⇒ ForgePackager

Returns a new instance of ForgePackager.



6
7
8
9
10
11
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 6

def initialize(plugin, pluginpath=nil, signature=nil, verbose=false, keep_artifacts=nil, module_template=nil)
  @plugin = plugin
  @verbose = verbose
  @keep_artifacts = keep_artifacts
  @module_template = module_template || File.join(File.dirname(__FILE__), "templates", "forge")
end

Instance Method Details

#assert_new_enough_pdkObject



215
216
217
218
219
220
221
222
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 215

def assert_new_enough_pdk
  s = Shell.new("#{pdk_path} --version")
  s.runcommand
  actual_version = s.stdout.chomp
  required_version = "1.12.0"

  raise("Cannot build package. pdk #{required_version} or greater required.  We have #{actual_version}.") if Util.versioncmp(actual_version, required_version) < 0
end

#cleanup_tmpdirsObject



243
244
245
246
247
248
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 243

def cleanup_tmpdirs
  FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
rescue
  warn("Could not remove temporary build directory %s" % [@tmpdir])
  raise
end

#copy_additional_filesObject



126
127
128
129
130
131
132
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 126

def copy_additional_files
  if File.exist?("puppet")
    Dir.glob("puppet/*").each do |file|
      FileUtils.cp_r(file, @tmpdir)
    end
  end
end

#copy_module_filesObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 134

def copy_module_files
  @plugin.packagedata.each_value do |data|
    data[:files].each do |file|
      clean_dest_file = file.gsub("./lib/mcollective", "")
      dest_dir = File.expand_path(File.join(@tmpdir, "files", "mcollective", File.dirname(clean_dest_file)))

      FileUtils.mkdir_p(dest_dir) unless File.directory?(dest_dir)
      FileUtils.cp(file, dest_dir) if File.file?(file)
    end
  end
end

#create_packagesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 24

def create_packages
  assert_new_enough_pdk
  validate_environment

  begin
    puts("Building Choria module %s" % module_name)

    @tmpdir = Dir.mktmpdir("mcollective_packager")

    make_module_dirs
    copy_module_files
    generate_agent_json_ddls
    render_templates
    copy_additional_files
    run_build
    move_package

    puts("Completed building module for %s" % module_name)
  rescue
    warn("Failed to build plugin module: %s: %s" % [$!.class, $!.to_s])
  ensure
    if @keep_artifacts
      puts("Keeping build artifacts")
      puts("Build artifacts saved in %s" % @tmpdir)
    else
      cleanup_tmpdirs
    end
  end
end

#dirlist(type) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 73

def dirlist(type)
  @plugin.packagedata[type][:files].map do |file|
    file.gsub(/^\.\//, "") if File.directory?(file)
  end.compact
rescue
  []
end

#executablelist(type) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 89

def executablelist(type)
  @plugin.packagedata[type][:executable_files].map do |file|
    file.gsub(/^\.\//, "") unless File.directory?(file)
  end.compact.uniq
rescue
  []
end

#filelist(type) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 81

def filelist(type)
  @plugin.packagedata[type][:files].map do |file|
    file.gsub(/^\.\//, "") unless File.directory?(file)
  end.compact.uniq
rescue
  []
end

#generate_agent_json_ddlsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 146

def generate_agent_json_ddls
  agent_dir = File.expand_path(File.join(@tmpdir, "files", "mcollective", "agent"))

  if File.directory?(agent_dir)
    Dir.glob(File.join(agent_dir, "*.ddl")) do |file|
      agent_name = File.basename(file, ".ddl")
      json_file = File.join(agent_dir, "%s.json" % agent_name)

      if File.exist?(json_file)
        Log.warn("JSON DDL %s already exist, not regenerating from the %s" % [json_file, agent_name])
        next
      end

      ddl = DDL.new(agent_name, :agent, false)
      ddl.instance_eval(File.read(file))

      data = {
        "$schema" => "https://choria.io/schemas/mcorpc/ddl/v1/agent.json",
        "metadata" => ddl.meta,
        "actions" => []
      }

      ddl.actions.sort.each do |action|
        data["actions"] << ddl.action_interface(action)
      end

      File.open(json_file, "w") do |jddl|
        jddl.print(JSON.pretty_generate(data))
      end

      @plugin.packagedata[:common][:files] << "agent/%s.json" % agent_name
    end
  end
end

#hierakey(var) ⇒ Object



97
98
99
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 97

def hierakey(var)
  "%s::%s" % [module_name, var]
end

#make_module_dirsObject



120
121
122
123
124
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 120

def make_module_dirs
  ["data", "manifests", "files/mcollective"].each do |dir|
    FileUtils.mkdir_p(File.join(@tmpdir, dir))
  end
end

#module_file_nameObject



69
70
71
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 69

def module_file_name
  "%s-%s-%s.tar.gz" % [@plugin.vendor, module_name, version]
end

#module_nameObject



62
63
64
65
66
67
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 62

def module_name
  "mcollective_%s_%s" % [
    @plugin.plugintype.downcase,
    @plugin.[:name].downcase.gsub("-", "_")
  ]
end

#module_override_dataObject



101
102
103
104
105
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 101

def module_override_data
  YAML.safe_load(File.read(".plugin.yaml"))
rescue
  {}
end

#move_packageObject



235
236
237
238
239
240
241
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 235

def move_package
  package_file = File.join(@tmpdir, "pkg", module_file_name)
  FileUtils.cp(package_file, ".")
rescue
  warn("Could not copy package to working directory")
  raise
end

#pdk_pathObject



208
209
210
211
212
213
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 208

def pdk_path
  pdk_bin = which("pdk")
  pdk_bin ||= "/opt/puppetlabs/pdk/bin/pdk"

  pdk_bin
end

#plugin_hiera_dataObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 107

def plugin_hiera_data
  {
    hierakey(:config_name) => @plugin.[:name].downcase,
    hierakey(:common_files) => filelist(:common),
    hierakey(:executable_files) => executablelist(:agent),
    hierakey(:common_directories) => dirlist(:common),
    hierakey(:server_files) => filelist(:agent),
    hierakey(:server_directories) => dirlist(:agent),
    hierakey(:client_files) => filelist(:client),
    hierakey(:client_directories) => dirlist(:client)
  }.merge(module_override_data)
end

#render_template(infile, outfile) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 193

def render_template(infile, outfile)
  erb = ERB.new(File.read(infile), 0, "-")
  File.open(outfile, "w") do |f|
    f.puts erb.result(binding)
  end
rescue
  warn("Could not render template %s to %s" % [infile, outfile])
  raise
end

#render_templatesObject



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 181

def render_templates
  templates = Dir.chdir(@module_template) do |_path|
    Dir.glob("**/*.erb")
  end

  templates.each do |template|
    infile = File.join(@module_template, template)
    outfile = File.join(@tmpdir, template.gsub(/\.erb$/, ""))
    render_template(infile, outfile)
  end
end

#run_buildObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 224

def run_build
  PluginPackager.execute_verbosely(@verbose) do
    Dir.chdir(@tmpdir) do
      PluginPackager.safe_system("#{pdk_path} build --force")
    end
  end
rescue
  warn("Build process has failed")
  raise
end

#validate_environmentObject



203
204
205
206
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 203

def validate_environment
  raise("Supplying a vendor is required, please use --vendor") if @plugin.vendor == "Puppet Labs"
  raise("Vendor names may not have a space in them, please specify a valid vendor using --vendor") if @plugin.vendor.include?(" ")
end

#versionObject



54
55
56
57
58
59
60
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 54

def version
  if Integer(@plugin.revision) > 1
    "%s-%s" % [@plugin.[:version], @plugin.revision]
  else
    @plugin.[:version]
  end
end

#which(cmd) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/mcollective/pluginpackager/forge_packager.rb', line 13

def which(cmd)
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end