diff --git a/lib/mcollective/application/plugin.rb b/lib/mcollective/application/plugin.rb index 84aa5249..49c6879a 100644 --- a/lib/mcollective/application/plugin.rb +++ b/lib/mcollective/application/plugin.rb @@ -298,7 +298,7 @@ def doc_command # Creates the correct package plugin object. def prepare_plugin plugintype = set_plugin_type unless configuration[:plugintype] - configuration[:format] = "ospackage" unless configuration[:format] + configuration[:format] = "forge" unless configuration[:format] PluginPackager.load_packagers plugin_class = PluginPackager[configuration[:plugintype]] @@ -308,7 +308,10 @@ def prepare_plugin configuration[:dependency].map!{|dep| {:name => dep, :version => nil}} end - mcdependency = {:mcname => configuration[:mcname], :mcversion => configuration[:mcversion]} + mcdependency = { + :mcname => configuration[:mcname], + :mcversion => configuration[:mcversion] + } # Deprecation warning for --iteration if configuration[:iteration] diff --git a/lib/mcollective/pluginpackager/agent_definition.rb b/lib/mcollective/pluginpackager/agent_definition.rb index 667c52cb..eee96b05 100644 --- a/lib/mcollective/pluginpackager/agent_definition.rb +++ b/lib/mcollective/pluginpackager/agent_definition.rb @@ -12,7 +12,7 @@ def initialize(configuration, mcdependency, plugintype) @revision = configuration[:revision] || 1 @preinstall = configuration[:preinstall] @postinstall = configuration[:postinstall] - @vendor = configuration[:vendor] || "Puppet Labs" + @vendor = configuration[:vendor] || "Choria.IO User" @dependencies = configuration[:dependency] || [] @target_path = File.expand_path(@path) @metadata, mcversion = PluginPackager.get_metadata(@path, "agent") @@ -36,9 +36,11 @@ def identify_packages # Obtain Agent package files and dependencies. def agent - agent = {:files => [], - :dependencies => @dependencies.clone, - :description => "Agent plugin for #{@metadata[:name]}"} + agent = { + :files => [], + :dependencies => @dependencies.clone, + :description => "Agent plugin for #{@metadata[:name]}" + } agentdir = File.join(@path, "agent") @@ -69,9 +71,11 @@ def client # Obtain common package files and dependencies. def common - common = {:files =>[], - :dependencies => @dependencies.clone, - :description => "Common libraries for #{@metadata[:name]}"} + common = { + :files =>[], + :dependencies => @dependencies.clone, + :description => "Common libraries for #{@metadata[:name]}" + } datadir = File.join(@path, "data", "**") utildir = File.join(@path, "util", "**", "**") diff --git a/lib/mcollective/pluginpackager/debpackage_packager.rb b/lib/mcollective/pluginpackager/debpackage_packager.rb deleted file mode 100644 index be1ec2b5..00000000 --- a/lib/mcollective/pluginpackager/debpackage_packager.rb +++ /dev/null @@ -1,237 +0,0 @@ -module MCollective - module PluginPackager - class DebpackagePackager - require 'erb' - - def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil) - if PluginPackager.command_available?('debuild') - @plugin = plugin - @verbose = verbose - @libdir = pluginpath || '/usr/share/mcollective/plugins/mcollective/' - @signature = signature - @package_name = "#{@plugin.mcname}-#{@plugin.metadata[:name]}" - @keep_artifacts = keep_artifacts - else - raise("Cannot build package. 'debuild' is not present on the system.") - end - end - - # Build process : - # - create buildroot - # - craete buildroot/debian - # - create the relative directories with package contents - # - create install files for each of the plugins that are going to be built - # - create debian build files - # - create tarball - # - create pre and post install files - # - run the build script - # - move packages to cwd - # - clean up - def create_packages - begin - puts "Building packages for #{@package_name} plugin." - - @tmpdir = Dir.mktmpdir('mcollective_packager') - @build_dir = File.join(@tmpdir, "#{@package_name}_#{@plugin.metadata[:version]}") - Dir.mkdir(@build_dir) - - create_debian_dir - @plugin.packagedata.each do |type, data| - prepare_tmpdirs(data) - create_install_file(type, data) - create_pre_and_post_install(type) - end - create_debian_files - create_tar - run_build - move_packages - - puts "Completed building all packages for #{@package_name} plugin." - ensure - if @keep_artifacts - puts 'Keeping build artifacts.' - puts "Build artifacts saved - #{@tmpdir}" - else - puts 'Removing build artifacts.' - cleanup_tmpdirs - end - end - end - - private - - def create_debian_files - ['control', 'Makefile', 'compat', 'rules', 'copyright', 'changelog'].each do |f| - create_file(f) - end - end - - def run_build - FileUtils.cd(@build_dir) do - PluginPackager.execute_verbosely(@verbose) do - if @signature - if @signature.is_a?(String) - PluginPackager.safe_system("debuild --no-lintian -i -k#{@signature}") - else - PluginPackager.safe_system("debuild --no-lintian -i") - end - else - PluginPackager.safe_system("debuild --no-lintian -i -us -uc") - end - end - end - end - - # Creates a string used by the control file to specify dependencies - # Dependencies can be formatted as : - # foo (>= x.x-x) - # foo (>= x.x) - # foo - def build_dependency_string(data) - dependencies = [] - PluginPackager.filter_dependencies('debian', data[:dependencies]).each do |dep| - if dep[:version] && dep[:revision] - dependencies << "#{dep[:name]} (>=#{dep[:version]}-#{dep[:revision]}) | puppet-agent" - elsif dep[:version] - dependencies << "#{dep[:name]} (>=#{dep[:version]}) | puppet-agent" - else - dependencies << "#{dep[:name]} | puppet-agent" - end - end - - if data[:plugindependency] - dependencies << "#{data[:plugindependency][:name]} (= ${binary:Version})" - end - - dependencies.join(', ') - end - - # Creates an install file for each of the packages that are going to be created - # for the plugin - def create_install_file(type, data) - install_file = "#{@package_name}-#{type}" - begin - install_file = File.join(@build_dir, 'debian', "#{install_file}.install") - File.open(install_file, 'w') do |file| - data[:files].each do |f| - extended_filename = File.join(@libdir, File.expand_path(f).gsub(/^#{@plugin.target_path}/, '')) - file.puts "#{extended_filename} #{File.dirname(extended_filename)}" - end - end - rescue Errno::EACCES => e - puts "Could not create install file '#{install_file}'. Permission denied" - raise e - rescue => e - puts "Could not create install file '#{install_file}'." - raise e - end - end - - # Move source package and debs to cwd - def move_packages - begin - files_to_copy = Dir.glob(File.join(@tmpdir, '*.{deb,dsc,diff.gz,orig.tar.gz,changes}')) - FileUtils.cp(files_to_copy, '.') - rescue => e - puts 'Could not copy packages to working directory.' - raise e - end - end - - # Create pre and post install files in $buildroot/debian - # from supplied scripts. - # Note that all packages built for the plugin will invoke - # the same pre and post install scripts. - def create_pre_and_post_install(type) - if @plugin.preinstall - if !File.exists?(@plugin.preinstall) - puts "pre-install script '#{@plugin.preinstall}' not found." - raise(Errno::ENOENT, @plugin.preinstall) - else - FileUtils.cp(@plugin.preinstall, File.join(@build_dir, 'debian', "#{@package_name}-#{type}.preinst")) - end - end - - if @plugin.postinstall - if !File.exists?(@plugin.postinstall) - puts "post-install script '#{@plugin.postinstall}' not found." - raise(Errno::ENOENT, @plugin.postinstall) - else - FileUtils.cp(@plugin.postinstall, File.join(@build_dir, 'debian', "#{@package_name}-#{type}.postinst")) - end - end - end - - # Tar up source - # Expects directory : $mcollective-$agent_$version - # Creates file : $buildroot/$mcollective-$agent_$version.orig.tar.gz - def create_tar - name_and_version = "#{@package_name}_#{@plugin.metadata[:version]}" - tarfile = "#{name_and_version}.orig.tar.gz" - begin - PluginPackager.execute_verbosely(@verbose) do - Dir.chdir(@tmpdir) do - PluginPackager.safe_system("tar -Pcvzf #{File.join(@tmpdir, tarfile)} #{name_and_version}") - end - end - rescue Exception => e - puts "Could not create tarball - #{tarfile}" - raise e - end - end - - def create_file(filename) - begin - file = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', 'debian', "#{filename}.erb")), nil, '-') - File.open(File.join(@build_dir, 'debian', filename), 'w') do |f| - f.puts file.result(binding) - end - rescue => e - puts "Could not create file - '#{filename}'" - raise e - end - end - - # Move files contained in the plugin to the correct directory - # relative to the build root. - def prepare_tmpdirs(data) - data[:files].each do |file| - begin - targetdir = File.join(@build_dir, @libdir, File.dirname(File.expand_path(file)).gsub(/^#{@plugin.target_path}/, "")) - FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir) - FileUtils.cp_r(file, targetdir) - rescue Errno::EACCES => e - puts "Could not create directory '#{targetdir}'. Permission denied" - raise e - rescue Errno::ENOENT => e - puts "Could not copy file '#{file}' to '#{targetdir}'. File does not exist" - raise e - rescue => e - puts 'Could not prepare build directory' - raise e - end - end - end - - # Create the $buildroot/debian directory - def create_debian_dir - deb_dir = File.join(@build_dir, 'debian') - begin - FileUtils.mkdir_p(deb_dir) - rescue => e - puts "Could not create directory '#{deb_dir}'" - raise e - end - end - - def cleanup_tmpdirs - begin - FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) - rescue => e - puts "Could not remove temporary build directory - '#{@tmpdir}'" - raise e - end - end - end - end -end diff --git a/lib/mcollective/pluginpackager/forge_packager.rb b/lib/mcollective/pluginpackager/forge_packager.rb new file mode 100644 index 00000000..827dbcde --- /dev/null +++ b/lib/mcollective/pluginpackager/forge_packager.rb @@ -0,0 +1,246 @@ +require "yaml" + +module MCollective + module PluginPackager + class ForgePackager + 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 + + 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 + return nil + end + + def create_packages + assert_new_enough_puppet + 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 + STDERR.puts("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 + + def version + if Integer(@plugin.revision) > 1 + "%s-%s" % [@plugin.metadata[:version], @plugin.revision] + else + @plugin.metadata[:version] + end + end + + def module_name + "mcollective_%s_%s" % [ + @plugin.plugintype.downcase, + @plugin.metadata[:name].downcase.gsub("-", "_") + ] + end + + def module_file_name + "%s-%s-%s.tar.gz" % [@plugin.vendor, module_name, version] + end + + def dirlist(type) + @plugin.packagedata[type][:files].map do |file| + file.gsub(/^\.\//, "") if File.directory?(file) + end.compact + rescue + [] + end + + def filelist(type) + @plugin.packagedata[type][:files].map do |file| + file.gsub(/^\.\//, "") unless File.directory?(file) + end.compact + rescue + [] + end + + def hierakey(var) + "%s::%s" % [module_name, var] + end + + def module_override_data + YAML.safe_load(File.read(".plugin.yaml")) + rescue + {} + end + + def plugin_hiera_data + { + hierakey(:config_name) => @plugin.metadata[:name].downcase, + hierakey(:common_files) => filelist(:common), + 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 + + def make_module_dirs + ["data", "manifests", "files/mcollective"].each do |dir| + FileUtils.mkdir_p(File.join(@tmpdir, dir)) + end + end + + def copy_additional_files + if File.exist?("puppet") + Dir.glob("puppet/*").each do |file| + FileUtils.cp_r(file, @tmpdir) + end + end + end + + def copy_module_files + @plugin.packagedata.each do |klass, 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 + + 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) + + 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 + + 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 + + def render_template(infile, outfile) + begin + erb = ERB.new(File.read(infile), nil, "-") + File.open(outfile, "w") do |f| + f.puts erb.result(binding) + end + rescue + STDERR.puts("Could not render template %s to %s" % [infile, outfile]) + raise + end + end + + 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.match(" ") + end + + def assert_new_enough_puppet + puppet_bin = which("puppet") + puppet_bin = "/opt/puppetlabs/bin/puppet" if !puppet_bin + if !puppet_bin + raise("Cannot build package. 'puppet' not found on path, and '/opt/puppetlabs/bin/puppet' is not present on the system.") + end + + s = Shell.new("#{puppet_bin} --version") + s.runcommand + actual_version = s.stdout.chomp + required_version = '4.5.1' + + if Util.versioncmp(actual_version, required_version) < 0 + raise("Cannot build package. puppet #{required_version} or greater required. We have #{actual_version}.") + end + end + + def run_build + begin + PluginPackager.execute_verbosely(@verbose) do + Dir.chdir(@tmpdir) do + PluginPackager.safe_system('pdk build --force') + end + end + rescue + STDERR.puts("Build process has failed") + raise + end + end + + def move_package + begin + package_file = File.join(@tmpdir, "pkg", module_file_name) + FileUtils.cp(package_file, ".") + rescue + STDERR.puts("Could not copy package to working directory") + raise + end + end + + def cleanup_tmpdirs + begin + FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) + rescue + STDERR.puts("Could not remove temporary build directory %s" % [@tmpdir]) + raise + end + end + end + end +end diff --git a/lib/mcollective/pluginpackager/modulepackage_packager.rb b/lib/mcollective/pluginpackager/modulepackage_packager.rb deleted file mode 100644 index 113f0ea2..00000000 --- a/lib/mcollective/pluginpackager/modulepackage_packager.rb +++ /dev/null @@ -1,127 +0,0 @@ -module MCollective - module PluginPackager - class ModulepackagePackager - require 'erb' - - def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil) - assert_new_enough_puppet - @plugin = plugin - @package_name = "#{@plugin.mcname}_#{@plugin.metadata[:name]}".gsub(/-/, '_') - @verbose = verbose - @keep_artifacts = keep_artifacts - @module_template = module_template || File.join(File.dirname(__FILE__), 'templates', 'module') - end - - # Build Process : - # - create module directory - # - run 'puppet module build' - # - move generated package back to cwd - def create_packages - begin - puts "Building module for #{@package_name} plugin." - - @tmpdir = Dir.mktmpdir('mcollective_packager') - make_module - run_build - move_package - - puts "Completed building module for #{@package_name} plugin." - ensure - if @keep_artifacts - puts 'Keeping build artifacts' - puts "Build artifacts saved - #{@tmpdir}" - else - cleanup_tmpdirs - end - end - end - - private - - def assert_new_enough_puppet - unless PluginPackager.command_available?('puppet') - raise("Cannot build package. 'puppet' is not present on the system.") - end - - s = Shell.new('puppet --version') - s.runcommand - actual_version = s.stdout.chomp - - required_version = '3.3.0' - if Util.versioncmp(actual_version, required_version) < 0 - raise("Cannot build package. puppet #{required_version} or greater required. We have #{actual_version}.") - end - end - - def make_module - targetdir = File.join(@tmpdir, 'manifests') - FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir) - - # for each subpackage make a subclass - @plugin.packagedata.each do |klass,data| - data[:files].each do |file| - relative_path = File.expand_path(file).gsub(/#{@plugin.target_path}|^\.\//, '') - targetdir = File.join(@tmpdir, 'files', klass.to_s, 'mcollective', File.dirname(relative_path)) - FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir) - FileUtils.cp_r(file, targetdir) - end - - @klass = klass.to_s - render_template('_manifest.pp.erb', File.join(@tmpdir, 'manifests', "#{klass}.pp")) - end - - # render all the templates we have - Dir.glob(File.join(@module_template, '*.erb')).each do |template| - filename = File.basename(template, '.erb') - next if filename =~ /^_/ # starting with underscore makes it private - render_template("#{filename}.erb", File.join(@tmpdir, filename)) - end - end - - def render_template(template, path) - begin - erb = ERB.new(File.read(File.join(@module_template, template)), nil, '-') - File.open(path, 'w') do |f| - f.puts erb.result(binding) - end - rescue => e - puts "Could not render template to path - '#{path}'" - raise e - end - end - - def run_build - begin - PluginPackager.execute_verbosely(@verbose) do - Dir.chdir(@tmpdir) do - PluginPackager.safe_system('puppet module build') - end - end - rescue => e - puts 'Build process has failed' - raise e - end - end - - # Move built package to cwd - def move_package - begin - package_file = File.join(@tmpdir, 'pkg', "#{@plugin.vendor}-#{@package_name}-#{@plugin.metadata[:version]}.tar.gz") - FileUtils.cp(package_file, '.') - rescue => e - puts 'Could not copy package to working directory' - raise e - end - end - - def cleanup_tmpdirs - begin - FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) - rescue => e - puts "Could not remove temporary build directory - '#{@tmpdir}'" - raise e - end - end - end - end -end diff --git a/lib/mcollective/pluginpackager/ospackage_packager.rb b/lib/mcollective/pluginpackager/ospackage_packager.rb deleted file mode 100644 index 5c06a6e3..00000000 --- a/lib/mcollective/pluginpackager/ospackage_packager.rb +++ /dev/null @@ -1,59 +0,0 @@ -module MCollective - module PluginPackager - # MCollective plugin packager general OS implementation. - class OspackagePackager - - attr_accessor :package, :verbose, :packager, :package_type - - # Create packager object with package parameter containing list of files, - # dependencies and package metadata. - def initialize(package, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = false, module_template = nil) - - if File.exists?("/etc/redhat-release") - @packager = PluginPackager["RpmpackagePackager"].new(package, pluginpath, signature, verbose, keep_artifacts, module_template) - @package_type = "RPM" - elsif File.exists?("/etc/debian_version") - @packager = PluginPackager["DebpackagePackager"].new(package, pluginpath, signature, verbose, keep_artifacts, module_template) - @package_type = "Deb" - else - raise "cannot identify operating system." - end - - @package = package - @verbose = verbose - end - - # Hands over package creation to the detected packager implementation - # based on operating system. - def create_packages - @packager.create_packages - end - - # Displays the package metadata and detected files - def package_information - puts - puts "%30s%s" % ["Plugin information : ", @package.metadata[:name]] - puts "%30s%s" % ["-" * 22, "-" * 22] - puts "%30s%s" % ["Plugin Type : ", @package.plugintype.capitalize] - puts "%30s%s" % ["Package Output Format : ", @package_type] - puts "%30s%s" % ["Version : ", @package.metadata[:version]] - puts "%30s%s" % ["Revision : ", @package.revision] - puts "%30s%s" % ["Vendor : ", @package.vendor] - puts "%30s%s" % ["Post Install Script : ", @package.postinstall] if @package.postinstall - puts "%30s%s" % ["Author : ", @package.metadata[:author]] - puts "%30s%s" % ["License : ", @package.metadata[:license]] - puts "%30s%s" % ["URL : ", @package.metadata[:url]] - - if @package.packagedata.size > 0 - @package.packagedata.each_with_index do |values, i| - if i == 0 - puts "%30s%s" % ["Identified Packages : ", values[0]] - else - puts "%30s%s" % [" ", values[0]] - end - end - end - end - end - end -end diff --git a/lib/mcollective/pluginpackager/rpmpackage_packager.rb b/lib/mcollective/pluginpackager/rpmpackage_packager.rb deleted file mode 100644 index 50ba4174..00000000 --- a/lib/mcollective/pluginpackager/rpmpackage_packager.rb +++ /dev/null @@ -1,180 +0,0 @@ -module MCollective - module PluginPackager - class RpmpackagePackager - require 'erb' - - def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil) - if @buildtool = select_command - @plugin = plugin - @package_name = "#{@plugin.mcname}-#{@plugin.metadata[:name]}" - @package_name_and_version = "#{@package_name}-#{@plugin.metadata[:version]}" - @verbose = verbose - @libdir = pluginpath || '/usr/libexec/mcollective/mcollective/' - @signature = signature - @rpmdir = rpmdir - @srpmdir = srpmdir - @keep_artifacts = keep_artifacts - else - raise("Cannot build package. 'rpmbuild' or 'rpmbuild-md5' is not present on the system") - end - end - - # Determine the build tool present on the system - def select_command - if PluginPackager.command_available?('rpmbuild-md5') - return 'rpmbuild-md5' - elsif PluginPackager.command_available?('rpmbuild') - return 'rpmbuild' - else - return nil - end - end - - def rpmdir - `rpm --eval '%_rpmdir'`.chomp - end - - def srpmdir - `rpm --eval '%_srcrpmdir'`.chomp - end - - # Build Process : - # - create temporary buildroot - # - create the spec file - # - create the tarball - # - run the build script - # - move pacakges to cwd - # - clean up - def create_packages - begin - puts "Building packages for #{@package_name} plugin." - - @tmpdir = Dir.mktmpdir('mcollective_packager') - prepare_tmpdirs - - make_spec_file - run_build - move_packages - - puts "Completed building all packages for #{@package_name} plugin." - ensure - if @keep_artifacts - puts 'Keeping build artifacts' - puts "Build artifacts saved - #{@tmpdir}" - else - cleanup_tmpdirs - end - end - end - - private - - def run_build - begin - tarfile = create_tar - PluginPackager.execute_verbosely(@verbose) do - PluginPackager.safe_system("#{@buildtool} -ta#{" --quiet" unless @verbose}#{" --sign" if @signature} #{tarfile}") - end - rescue => e - puts 'Build process has failed' - raise e - end - end - - # Tar up source - # Expects directory $mcollective-$agent-$version - # Creates file : $tmpbuildroot/$mcollective-$agent-$version - def create_tar - tarfile = File.join(@tmpdir, "#{@package_name_and_version}.tgz") - begin - PluginPackager.execute_verbosely(@verbose) do - Dir.chdir(@tmpdir) do - PluginPackager.safe_system("tar -cvzf #{tarfile} #{@package_name_and_version}") - end - end - rescue => e - puts "Could not create tarball - '#{tarfile}'" - raise e - end - tarfile - end - - # Move rpm's and srpm's to cwd - def move_packages - begin - files_to_copy = [] - files_to_copy += Dir.glob(File.join(@rpmdir, 'noarch', "#{@package_name}-*-#{@plugin.metadata[:version]}-#{@plugin.revision}*.noarch.rpm")) - files_to_copy += Dir.glob(File.join(@srpmdir, "#{@package_name}-#{@plugin.metadata[:version]}-#{@plugin.revision}*.src.rpm")) - FileUtils.cp(files_to_copy, '.') - rescue => e - puts 'Could not copy packages to working directory' - raise e - end - end - - # Create the specfile and place as $tmpbuildroot/$mcollective-$agent-$version/$mcollective-$agent-$version.spec - def make_spec_file - spec_file = File.join(@tmpdir, @package_name_and_version, "#{@package_name_and_version}.spec") - begin - spec_template = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', 'redhat', 'rpm_spec.erb')), nil, '-') - File.open(spec_file, 'w') do |f| - f.puts spec_template.result(binding) - end - rescue => e - puts "Could not create specfile - '#{spec_file}'" - raise e - end - end - - # Move files contained in the plugin to the correct directory - # relative to the build root. - def prepare_tmpdirs - plugin_files.each do |file| - begin - targetdir = File.join(@tmpdir, @package_name_and_version, @libdir, File.dirname(File.expand_path(file)).gsub(@plugin.target_path, "")) - FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir) - FileUtils.cp_r(file, targetdir) - rescue Errno::EACCES => e - puts "Could not create directory '#{targetdir}'. Permission denied" - raise e - rescue Errno::ENOENT => e - puts "Could not copy file '#{file}' to '#{targetdir}'. File does not exist" - raise e - rescue => e - puts 'Could not prepare temporary build directory' - raise e - end - end - end - - # Extract all the package files from the plugin's package data hash - def plugin_files - files = [] - @plugin.packagedata.each do |name, data| - files += data[:files].reject{ |f| File.directory?(f) } - end - files - end - - # Extract the package specific files from the file list and omits directories - def package_files(files) - package_files = [] - files.each do |f| - if !File.directory?(f) - package_files << File.join(@libdir, File.expand_path(f).gsub(/#{@plugin.target_path}|\.\//, '')) - end - end - package_files - end - - def cleanup_tmpdirs - begin - FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir) - rescue => e - puts "Could not remove temporary build directory - '#{@tmpdir}'" - raise e - end - end - end - end -end diff --git a/lib/mcollective/pluginpackager/standard_definition.rb b/lib/mcollective/pluginpackager/standard_definition.rb index 3f71e64e..114f0cfd 100644 --- a/lib/mcollective/pluginpackager/standard_definition.rb +++ b/lib/mcollective/pluginpackager/standard_definition.rb @@ -11,7 +11,7 @@ def initialize(configuration, mcdependency, plugintype) @revision = configuration[:revision] || 1 @preinstall = configuration[:preinstall] @postinstall = configuration[:postinstall] - @vendor = configuration[:vendor] || "Puppet Labs" + @vendor = configuration[:vendor] || "Choria.IO User" @dependencies = configuration[:dependency] || [] @target_path = File.expand_path(@path) @metadata, mcversion = PluginPackager.get_metadata(@path, @plugintype) @@ -20,6 +20,7 @@ def initialize(configuration, mcdependency, plugintype) @dependencies << {:name => "#{mcname}-common", :version => @mcversion} @metadata[:name] = (configuration[:pluginname] || @metadata[:name]).downcase.gsub(/\s+|_/, "-") @metadata[:version] = (configuration[:version] || @metadata[:version]) + identify_packages end @@ -33,9 +34,11 @@ def identify_packages # Obtain standard plugin files and dependencies def plugin - plugindata = {:files => [], - :dependencies => @dependencies.clone, - :description => "#{@name} #{@plugintype} plugin for the Marionette Collective."} + plugindata = { + :files => [], + :dependencies => @dependencies.clone, + :description => "#{@name} #{@plugintype} plugin for the Choria Orchestrator." + } plugindir = File.join(@path, @plugintype.to_s) if PluginPackager.check_dir_present plugindir @@ -44,25 +47,33 @@ def plugin return nil end - plugindata[:plugindependency] = {:name => "#{@mcname}-#{@metadata[:name]}-common", - :version => @metadata[:version], - :revision => @revision} if @packagedata[:common] + if @packagedata[:common] + plugindata[:plugindependency] = { + :name => "#{@mcname}-#{@metadata[:name]}-common", + :version => @metadata[:version], + :revision => @revision + } + end + plugindata end # Obtain list of common files def common - common = {:files => [], - :dependencies => @dependencies.clone, - :description => "Common libraries for #{@name} connector plugin"} + common = { + :files => [], + :dependencies => @dependencies.clone, + :description => "Common libraries for #{@name} connector plugin" + } commondir = File.join(@path, "util") if PluginPackager.check_dir_present commondir common[:files] = Dir.glob(File.join(commondir, "*")) + return common - else - return nil end + + nil end end end diff --git a/lib/mcollective/pluginpackager/templates/debian/Makefile.erb b/lib/mcollective/pluginpackager/templates/debian/Makefile.erb deleted file mode 100644 index edf3b4e9..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/Makefile.erb +++ /dev/null @@ -1,7 +0,0 @@ -DESTDIR= - -build: - -clean: - -install: diff --git a/lib/mcollective/pluginpackager/templates/debian/changelog.erb b/lib/mcollective/pluginpackager/templates/debian/changelog.erb deleted file mode 100644 index dfce1d8e..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/changelog.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%= @package_name -%> (<%= @plugin.metadata[:version]-%>-<%= @plugin.revision-%>) unstable; urgency=low - - * Automated release for <%= @package_name -%> by mco plugin packager. - - -- The Marionette Collective <%= Time.new.strftime('%a, %d %b %Y %H:%M:%S %z') %> diff --git a/lib/mcollective/pluginpackager/templates/debian/compat.erb b/lib/mcollective/pluginpackager/templates/debian/compat.erb deleted file mode 100644 index 7f8f011e..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/compat.erb +++ /dev/null @@ -1 +0,0 @@ -7 diff --git a/lib/mcollective/pluginpackager/templates/debian/control.erb b/lib/mcollective/pluginpackager/templates/debian/control.erb deleted file mode 100644 index 75f35fcd..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/control.erb +++ /dev/null @@ -1,15 +0,0 @@ -Source: <%= @package_name %> -Homepage: <%= @plugin.metadata[:url] %> -Section: utils -Priority: extra -Build-Depends: debhelper (>= 7), cdbs, dpatch -Maintainer: <%= @plugin.metadata[:author] %> -Standards-Version: 3.9.1 -<% @plugin.packagedata.each do |type, data| %> -Package: <%= "#{@package_name}-#{type}" %> -Architecture: all -Depends: <%= build_dependency_string(data) %> -Description: <%= @plugin.metadata[:description]%> - <%= data[:description]%> - . -<% end -%> diff --git a/lib/mcollective/pluginpackager/templates/debian/copyright.erb b/lib/mcollective/pluginpackager/templates/debian/copyright.erb deleted file mode 100644 index 06c6c645..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/copyright.erb +++ /dev/null @@ -1,8 +0,0 @@ -This package was generated by the MCollective plugin packager on -<%= Time.now%> - -Upstream Author: - <%= @plugin.metadata[:author] %> <%= @plugin.metadata[:url] %> - -License: - <%= @plugin.metadata[:license] -%> diff --git a/lib/mcollective/pluginpackager/templates/debian/rules.erb b/lib/mcollective/pluginpackager/templates/debian/rules.erb deleted file mode 100644 index 36c26835..00000000 --- a/lib/mcollective/pluginpackager/templates/debian/rules.erb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/make -f - -include /usr/share/cdbs/1/rules/debhelper.mk -include /usr/share/cdbs/1/rules/dpatch.mk -include /usr/share/cdbs/1/class/makefile.mk -DEB_MAKE_INVOKE = $(DEB_MAKE_ENVVARS) make -f debian/Makefile -C $(DEB_BUILDDIR) diff --git a/lib/mcollective/pluginpackager/templates/forge/README.md.erb b/lib/mcollective/pluginpackager/templates/forge/README.md.erb new file mode 100644 index 00000000..b283b898 --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/README.md.erb @@ -0,0 +1,72 @@ +# <%= module_name %> version <%= @plugin.metadata[:version] %> + +#### Table of Contents + +1. [Overview](#overview) +1. [Usage](#usage) +1. [Configuration](#configuration) + +## Overview + +<%= @plugin.metadata[:description] %> + +The <%= module_name %> module is generated automatically, based on the source from <%= @plugin.metadata[:url] %>. +<%- if @plugin.plugintype == "Agent" -%> +<%- + ddl = DDL.new("package", :agent, false) + ddl.instance_eval(File.read(Dir.glob(File.join("**/agent/*.ddl")).first)) +-%> + +Available Actions: + +<%- ddl.entities.keys.sort.each do |action| -%> + * **<%= action %>** - <%= ddl.entities[action][:description] %> +<%- end -%> +<%- end -%> + +## Usage + +You can include this module into your infrastructure as any other module, but as it's designed to work with the [choria mcollective](http://forge.puppet.com/choria/mcollective) module you can configure it via Hiera: + +```yaml +mcollective::plugin_classes: + - <%= module_name %> +``` + +## Configuration + +Server and Client configuration can be added via Hiera and managed through tiers in your site Hiera, they will be merged with any included in this module + +```yaml +<%= module_name %>::config: + example: value +``` + +This will be added to both the `client.cfg` and `server.cfg`, you can likewise configure server and client specific settings using `<%= module_name %>::client_config` and `<%= module_name %>::server_config`. + +These settings will be added to the `/etc/puppetlabs/mcollective/plugin.d/` directory in individual files. + +For a full list of possible configuration settings see the module [source repository documentation](<%= @plugin.metadata[:url] %>). + +## Data Reference + + * `<%= module_name %>::gem_dependencies` - Deep Merged Hash of gem name and version this module depends on + * `<%= module_name %>::manage_gem_dependencies` - disable managing of gem dependencies + * `<%= module_name %>::package_dependencies` - Deep Merged Hash of package name and version this module depends on + * `<%= module_name %>::manage_package_dependencies` - disable managing of packages dependencies + * `<%= module_name %>::class_dependencies` - Array of classes to include when installing this module + * `<%= module_name %>::package_dependencies` - disable managing of class dependencies + * `<%= module_name %>::config` - Deep Merged Hash of common config items for this module + * `<%= module_name %>::server_config` - Deep Merged Hash of config items specific to managed nodes + * `<%= module_name %>::client_config` - Deep Merged Hash of config items specific to client nodes + * `<%= module_name %>::policy_default` - `allow` or `deny` + * `<%= module_name %>::policies` - List of `actionpolicy` policies to deploy with an agent + * `<%= module_name %>::client` - installs client files when true - defaults to `$mcollective::client` + * `<%= module_name %>::server` - installs server files when true - defaults to `$mcollective::server` + * `<%= module_name %>::ensure` - `present` or `absent` + +## Development: + +To contribute to this [Choria.IO](https://choria.io) plugin please visit <%= @plugin.metadata[:url] %>. + +This module was generated using the Choria Plugin Packager based on templates found at the [GitHub Project](https://github.com/choria-io/). diff --git a/lib/mcollective/pluginpackager/templates/forge/data/defaults.yaml.erb b/lib/mcollective/pluginpackager/templates/forge/data/defaults.yaml.erb new file mode 100644 index 00000000..2b2c5da2 --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/data/defaults.yaml.erb @@ -0,0 +1,16 @@ +lookup_options: + <%= module_name %>::gem_dependencies: + merge: + strategy: deep + <%= module_name %>::package_dependencies: + merge: + strategy: deep + <%= module_name %>::config: + merge: + strategy: deep + <%= module_name %>::client_config: + merge: + strategy: deep + <%= module_name %>::server_config: + merge: + strategy: deep diff --git a/lib/mcollective/pluginpackager/templates/forge/data/plugin.yaml.erb b/lib/mcollective/pluginpackager/templates/forge/data/plugin.yaml.erb new file mode 100644 index 00000000..d37442ec --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/data/plugin.yaml.erb @@ -0,0 +1 @@ +<%= YAML.dump(plugin_hiera_data) %> diff --git a/lib/mcollective/pluginpackager/templates/forge/hiera.yaml.erb b/lib/mcollective/pluginpackager/templates/forge/hiera.yaml.erb new file mode 100644 index 00000000..0619108b --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/hiera.yaml.erb @@ -0,0 +1,12 @@ +--- +version: 5 + +defaults: + datadir: "data" + data_hash: yaml_data + +hierarchy: + - name: "plugin" + path: "plugin.yaml" + - name: "defaults" + path: "defaults.yaml" diff --git a/lib/mcollective/pluginpackager/templates/forge/manifests/init.pp.erb b/lib/mcollective/pluginpackager/templates/forge/manifests/init.pp.erb new file mode 100644 index 00000000..d0648f69 --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/manifests/init.pp.erb @@ -0,0 +1,46 @@ +class <%= module_name %> ( + String $config_name, + Array[String] $client_files = [], + Array[String] $client_directories = [], + Array[String] $server_files = [], + Array[String] $server_directories = [], + Array[String] $common_files = [], + Array[String] $common_directories = [], + Boolean $manage_gem_dependencies = true, + Hash $gem_dependencies = {}, + Boolean $manage_package_dependencies = true, + Hash $package_dependencies = {}, + Boolean $manage_class_dependencies = true, + Array[String] $class_dependencies = [], + Mcollective::Policy_action $policy_default = $mcollective::policy_default, + Array[Mcollective::Policy] $policies = [], + Hash $config = {}, + Hash $client_config = {}, + Hash $server_config = {}, + Boolean $client = $mcollective::client, + Boolean $server = $mcollective::server, + Enum["present", "absent"] $ensure = "present" +) { + mcollective::module_plugin{$name: + config_name => $config_name, + client_files => $client_files, + server_files => $server_files, + common_files => $common_files, + client_directories => $client_directories, + server_directories => $server_directories, + common_directories => $common_directories, + gem_dependencies => $gem_dependencies, + manage_gem_dependencies => $manage_gem_dependencies, + package_dependencies => $package_dependencies, + manage_package_dependencies => $manage_package_dependencies, + class_dependencies => $class_dependencies, + policy_default => $policy_default, + policies => $policies, + config => $config, + client_config => $client_config, + server_config => $server_config, + client => $client, + server => $server, + ensure => $ensure + } +} diff --git a/lib/mcollective/pluginpackager/templates/forge/metadata.json.erb b/lib/mcollective/pluginpackager/templates/forge/metadata.json.erb new file mode 100644 index 00000000..0ed30a4e --- /dev/null +++ b/lib/mcollective/pluginpackager/templates/forge/metadata.json.erb @@ -0,0 +1,19 @@ +{ + "name": "<%= @plugin.vendor %>-<%= module_name %>", + "version": "<%= version %>", + "author": "<%= @plugin.metadata[:author] %>", + "license": "<%= @plugin.metadata[:license] %>", + "summary": "<%= @plugin.metadata[:description] %>", + "source": "<%= @plugin.metadata[:url] %>", + "project_page": "<%= @plugin.metadata[:url] %>", + "dependencies": [ + {"name": "choria/mcollective", "version_requirement": ">= 0.9.3 < 2.0.0"} + ], + "requirements": [ + { + "name": "puppet", + "version_requirement": ">= 6.0.0" + } + ] +} + diff --git a/lib/mcollective/pluginpackager/templates/module/Modulefile.erb b/lib/mcollective/pluginpackager/templates/module/Modulefile.erb deleted file mode 100644 index 438bb722..00000000 --- a/lib/mcollective/pluginpackager/templates/module/Modulefile.erb +++ /dev/null @@ -1,5 +0,0 @@ -name '<%= @plugin.vendor %>-<%= @package_name %>' -version '<%= @plugin.metadata[:version] %>' -description '<%= @plugin.metadata[:description] %>' -project_page '<%= @plugin.metadata[:url]%>' -dependency 'puppetlabs/mcollective', '1.x' diff --git a/lib/mcollective/pluginpackager/templates/module/README.md.erb b/lib/mcollective/pluginpackager/templates/module/README.md.erb deleted file mode 100644 index eeb11280..00000000 --- a/lib/mcollective/pluginpackager/templates/module/README.md.erb +++ /dev/null @@ -1,37 +0,0 @@ -# <%= @package_name %> - -#### Table of Contents - -1. [Overview](#overview) -2. [Module Description - What the module does and why it is useful](#module-description) -3. [Setup - The basics of getting started with <%= @package_name %>](#setup) - * [What the <%= @package_name %> module affects](#what-the-<%= @package_name %>-module-affects) - * [Setup requirements](#setup-requirements) -4. [Usage - Configuration options and additional functionality](#usage) -5. [Reference - An under-the-hood peek at what the module is doing and how](#reference) - - -## Overview - -The <%= @package_name %> module is a module that wraps a source release of the -<%= @plugin.metadata[:name] %> mcollective plugin for use with the -[puppetlabs mcollective](http://forge.puppetlabs.com/puppetlabs/mcollective) -module. - -## Module description - -## Usage - -<% @plugin.packagedata.keys.map { |x| x.to_s }.sort.each do |klass| -%> -<%# Don't document common class -%> -<% if klass != "common" -%> -### class <%= @package_name %>::<%= klass %> - -Installs the <%= klass %> component of the <%= @plugin.metadata[:name] %> plugin. - -```puppet -include <%= @package_name%>::<%= klass %> -``` -<% end %> -<% end %> - diff --git a/lib/mcollective/pluginpackager/templates/module/_manifest.pp.erb b/lib/mcollective/pluginpackager/templates/module/_manifest.pp.erb deleted file mode 100644 index bd79335c..00000000 --- a/lib/mcollective/pluginpackager/templates/module/_manifest.pp.erb +++ /dev/null @@ -1,9 +0,0 @@ -# -class <%= @package_name %>::<%= @klass %> { - <% if @plugin.packagedata[:common] && @klass != 'common' %> - include ::<%= @package_name %>::common - <% end %> - mcollective::plugin { '<%= @package_name %>/<%= @klass %>': - source => 'puppet:///modules/<%= @package_name %>/<%= @klass %>', - } -} diff --git a/lib/mcollective/pluginpackager/templates/redhat/rpm_spec.erb b/lib/mcollective/pluginpackager/templates/redhat/rpm_spec.erb deleted file mode 100644 index 06a9a62f..00000000 --- a/lib/mcollective/pluginpackager/templates/redhat/rpm_spec.erb +++ /dev/null @@ -1,63 +0,0 @@ -Name: <%= @package_name %> -Summary: <%= @plugin.metadata[:description] %> -Version: <%= @plugin.metadata[:version] %> -Release: <%= @plugin.revision %>%{?dist} -License: <%= @plugin.metadata[:license]%> -URL: <%= @plugin.metadata[:url]%> -Vendor: <%= @plugin.vendor%> -Packager: <%= @plugin.metadata[:author]%> -BuildArch: noarch -Group: System Tools -BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -Source0: <%= @package_name_and_version%>.tgz - -%description -<%= @plugin.metadata[:description] %> - -%prep -%setup - -%build -<% package_files = plugin_files.map{ |f| File.join(@libdir, File.expand_path(f).gsub(/#{File.expand_path(@plugin.path)}|\.\//, '')) } -%> -<% dirs = package_files.map{ |f| File.dirname(f) }.uniq -%> - -%install -rm -rf %{buildroot} -<% dirs.each do |dir| -%> -%{__install} -d -m0755 %{buildroot}<%= dir%> -<% end -%> -<% package_files.each do |file| -%> -%{__install} -m0644 -v <%= (file[0].chr == '/') ? file[1..file.size-1]: file%> %{buildroot}<%=file %> -<% end -%> - -<% @plugin.packagedata.each do |type, data| %> -%package <%= type %> -Summary: <%= @plugin.metadata[:description] %> -<% if data[:plugindependency] %> -Requires: <%= data[:plugindependency][:name] -%> = <%= data[:plugindependency][:version]%>-<%= data[:plugindependency][:revision]%>%{?dist} -<% end -%> -<% PluginPackager.filter_dependencies('redhat', data[:dependencies]).each do |dep|-%> -Requires: <%= dep[:name] -%> <%= ">= #{dep[:version]}" if dep[:version]%><%="-#{dep[:revision]}" if dep[:revision]%> -<% end -%> -%description <%= type %> -<%= data[:description] %> - -%files <%= type %> -%defattr(-, root, root, -) -<% package_files(data[:files]).each do |file| -%> -<%= file %> -<% end -%> -<% end -%> - -<% if @plugin.preinstall -%> -%pre -<%= @plugin.preinstall %> -<% end -%> -<% if @plugin.postinstall -%> -%post -<%= @plugin.postinstall%> -<% end -%> - -%changelog -* <%= Time.now.strftime("%a %b %d %Y") -%> <%= @plugin.metadata[:author]%> - <%= @plugin.metadata[:version]%>-<%= @plugin.revision %> -- Built Package <%= @plugin.metadata[:name] -%> diff --git a/spec/unit/mcollective/packagers/debpackage_packager_spec.rb b/spec/unit/mcollective/packagers/debpackage_packager_spec.rb deleted file mode 100644 index c87f06a4..00000000 --- a/spec/unit/mcollective/packagers/debpackage_packager_spec.rb +++ /dev/null @@ -1,332 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' -require 'mcollective/pluginpackager/debpackage_packager' - -module MCollective - module PluginPackager - describe DebpackagePackager, :unless => MCollective::Util.windows? do - - let(:plugin) do - plugin = mock - plugin.stubs(:mcname).returns('mcollective') - plugin.stubs(:metadata).returns({ :name => 'rspec', :version => '1.0'}) - plugin.stubs(:target_path).returns('/rspec') - plugin - end - - let(:packager) do - p = DebpackagePackager.new(plugin) - p.instance_variable_set(:@plugin, plugin) - p.instance_variable_set(:@tmpdir, 'rspec_tmp') - p.instance_variable_set(:@build_dir, 'rspec_build') - p.instance_variable_set(:@libdir, 'rspec_libdir') - p - end - - let(:data) do - {:files => ['/rspec/agent/file.rb', '/rspec/agent/file.ddl', '/rspec/application/file.rb'], - :dependencies => [{:name => 'dep1', :version => nil, :revision => nil}, - {:name => 'dep2', :version => '1.1', :revision => nil}, - {:name => 'dep3', :version => '1.1', :revision => 2}], - :plugindependency => {:name => 'mcollective-rspec-common'}} - end - - before :each do - PluginPackager.stubs(:command_available?).returns(true) - end - - describe '#initialize' do - it 'should set the instance variables' do - new_packager = DebpackagePackager.new(plugin) - new_packager.instance_variable_get(:@plugin).should == plugin - new_packager.instance_variable_get(:@verbose).should == false - new_packager.instance_variable_get(:@libdir).should == '/usr/share/mcollective/plugins/mcollective/' - new_packager.instance_variable_get(:@signature).should == nil - new_packager.instance_variable_get(:@package_name).should == 'mcollective-rspec' - end - - it 'should fail if debuild is not present on the system' do - PluginPackager.stubs(:command_available?).with('debuild').returns(false) - expect{ - DebpackagePackager.new(plugin) - }.to raise_error("Cannot build package. 'debuild' is not present on the system.") - end - end - - describe '#create_packages' do - it 'should run through the complete build process' do - build_dir = 'mc-tmp/mcollective-rspec_1.0' - tmpdir = 'mc-tmp' - packager.stubs(:puts) - Dir.expects(:mktmpdir).with('mcollective_packager').returns(tmpdir) - Dir.expects(:mkdir).with(build_dir) - packager.expects(:create_debian_dir) - plugin.stubs(:packagedata).returns({:agent => data}) - packager.expects(:prepare_tmpdirs).with(data) - packager.expects(:create_install_file).with(:agent, data) - packager.expects(:create_pre_and_post_install).with(:agent) - packager.expects(:create_debian_files) - packager.expects(:create_tar) - packager.expects(:run_build) - packager.expects(:move_packages) - packager.expects(:cleanup_tmpdirs) - packager.create_packages - end - - it 'should clean up tmpdirs if keep_artifacts is false' do - packager.stubs(:puts) - Dir.stubs(:mktmpdir).raises('error') - packager.expects(:cleanup_tmpdirs) - expect{ - packager.create_packages - }.to raise_error('error') - end - - it 'should keep the build artifacts if keep_artifacts is true' do - packager.instance_variable_set(:@keep_artifacts, true) - packager.stubs(:puts) - Dir.stubs(:mktmpdir).raises('error') - packager.expects(:cleanup_tmpdirs).never - expect{ - packager.create_packages - }.to raise_error('error') - end - end - - describe '#create_debian_files' do - it 'should create all the debian build files' do - ['control', 'Makefile', 'compat', 'rules', 'copyright', 'changelog'].each do |f| - packager.expects(:create_file).with(f) - end - packager.send(:create_debian_files) - end - end - - describe '#run_build' do - it 'should build the packages' do - FileUtils.expects(:cd).with('rspec_build').yields - PluginPackager.stubs(:do_quietly).with(false).yields - PluginPackager.expects(:safe_system).with('debuild --no-lintian -i -us -uc') - packager.send(:run_build) - end - - it 'should build the package and sign it with the set signature' do - packager.instance_variable_set(:@signature, '0x1234') - FileUtils.expects(:cd).with('rspec_build').yields - PluginPackager.stubs(:do_quietly).with(false).yields - PluginPackager.expects(:safe_system).with('debuild --no-lintian -i -k0x1234') - packager.send(:run_build) - end - - - it 'should sign with the exported gpg key' do - packager.instance_variable_set(:@signature, true) - FileUtils.expects(:cd).with('rspec_build').yields - PluginPackager.stubs(:do_quietly).with(false).yields - PluginPackager.expects(:safe_system).with('debuild --no-lintian -i') - packager.send(:run_build) - end - end - - describe '#build_dependency_string' do - it 'should create the correct dependency string' do - PluginPackager.expects(:filter_dependencies).with('debian', data[:dependencies]).returns(data[:dependencies]) - result = packager.send(:build_dependency_string, data) - result.should == 'dep1 | puppet-agent, dep2 (>=1.1) | puppet-agent, dep3 (>=1.1-2) | puppet-agent, mcollective-rspec-common (= ${binary:Version})' - end - end - - describe '#create_install_file' do - it 'should create the .install file in the correct location' do - file = mock - file.expects(:puts).with('rspec_libdir/agent/file.rb rspec_libdir/agent') - file.expects(:puts).with('rspec_libdir/agent/file.ddl rspec_libdir/agent') - file.expects(:puts).with('rspec_libdir/application/file.rb rspec_libdir/application') - File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').yields(file) - packager.send(:create_install_file, :agent, data) - end - - it 'should write a message and raise an error if we do not have permission' do - File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').raises(Errno::EACCES) - packager.expects(:puts).with("Could not create install file 'rspec_build/debian/mcollective-rspec-agent.install'. Permission denied") - expect{ - packager.send(:create_install_file, :agent, data) - }.to raise_error(Errno::EACCES) - end - - it 'should write a message and raise an error if we cannot create the install file' do - File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').raises('error') - packager.expects(:puts).with("Could not create install file 'rspec_build/debian/mcollective-rspec-agent.install'.") - expect{ - packager.send(:create_install_file, :agent, data) - }.to raise_error('error') - end - end - - describe '#move_packages' do - it 'should move the source package and debs to the cdw' do - files = ['rspec.deb', 'rspec.diff.gz', 'rspec.orig.tar.gz', 'rspec.changes'] - Dir.stubs(:glob).returns(files) - FileUtils.expects(:cp).with(files, '.') - packager.send(:move_packages) - end - - it 'should log an error and raise an error if the files cannot be moved' do - files = ['rspec.deb', 'rspec.diff.gz', 'rspec.orig.tar.gz', 'rspec.changes'] - Dir.stubs(:glob).returns(files) - FileUtils.expects(:cp).with(files, '.').raises('error') - packager.expects(:puts).with('Could not copy packages to working directory.') - expect{ - packager.send(:move_packages) - }.to raise_error('error') - end - end - - describe '#create_pre_and_post_install' do - it 'should create the pre-install file' do - plugin.stubs(:preinstall).returns('rspec-preinstall') - plugin.stubs(:postinstall).returns(nil) - File.expects(:exists?).with('rspec-preinstall').returns(true) - FileUtils.expects(:cp).with('rspec-preinstall', 'rspec_build/debian/mcollective-rspec-agent.preinst') - packager.send(:create_pre_and_post_install, :agent) - end - - it 'should create the post-install file' do - plugin.stubs(:preinstall).returns(nil) - plugin.stubs(:postinstall).returns('rspec-postinstall') - File.expects(:exists?).with('rspec-postinstall').returns(true) - FileUtils.expects(:cp).with('rspec-postinstall', 'rspec_build/debian/mcollective-rspec-agent.postinst') - packager.send(:create_pre_and_post_install, :agent) - end - - it 'should fail if a pre-install script is defined but the file does not exist' do - plugin.stubs(:preinstall).returns('rspec-preinstall') - File.expects(:exists?).with('rspec-preinstall').returns(false) - packager.expects(:puts).with("pre-install script 'rspec-preinstall' not found.") - expect{ - packager.send(:create_pre_and_post_install, :agent) - }.to raise_error(Errno::ENOENT, 'No such file or directory - rspec-preinstall') - end - - it 'should fail if a post-install script is defined but the file does not exist' do - plugin.stubs(:preinstall).returns(nil) - plugin.stubs(:postinstall).returns('rspec-postinstall') - File.expects(:exists?).with('rspec-postinstall').returns(false) - packager.expects(:puts).with("post-install script 'rspec-postinstall' not found.") - expect{ - packager.send(:create_pre_and_post_install, :agent) - }.to raise_error(Errno::ENOENT, 'No such file or directory - rspec-postinstall') - end - end - - describe '#create_tar' do - it 'should create the tarball' do - PluginPackager.stubs(:do_quietly?).yields - Dir.stubs(:chdir).with('rspec_tmp').yields - PluginPackager.expects(:safe_system).with('tar -Pcvzf rspec_tmp/mcollective-rspec_1.0.orig.tar.gz mcollective-rspec_1.0') - packager.send(:create_tar) - end - - it 'should log an error and raise an exception if it cannot create the tarball' do - PluginPackager.stubs(:do_quietly?).yields - Dir.stubs(:chdir).with('rspec_tmp').yields - PluginPackager.expects(:safe_system).raises('error') - packager.expects(:puts).with("Could not create tarball - mcollective-rspec_1.0.orig.tar.gz") - expect{ - packager.send(:create_tar) - }.to raise_error('error') - end - end - - describe '#create_file' do - it 'should create the named file in the build dir' do - file = mock - erb_content = mock - File.stubs(:read).returns('<%= "file content" %>') - ERB.expects(:new).with('<%= "file content" %>', nil, '-').returns(erb_content) - File.stubs(:open).with('rspec_build/debian/rspec', 'w').yields(file) - erb_content.expects(:result).returns('file content') - file.expects(:puts).with('file content') - packager.send(:create_file, 'rspec') - end - - it 'should log an error and raise if it cannot create the file' do - File.stubs(:read).returns('<%= "file content" %>') - ERB.stubs(:new).with('<%= "file content" %>', nil, '-').raises('error') - packager.expects(:puts).with("Could not create file - 'rspec'") - expect{ - packager.send(:create_file, 'rspec') - }.to raise_error('error') - end - end - - describe '#prepare_tmpdirs' do - it 'should create the target directories and copy the files' do - FileUtils.expects(:mkdir_p).with('rspec_build/rspec_libdir/agent').twice - FileUtils.expects(:mkdir_p).with('rspec_build/rspec_libdir/application') - FileUtils.expects(:cp_r).with('/rspec/agent/file.rb', 'rspec_build/rspec_libdir/agent') - FileUtils.expects(:cp_r).with('/rspec/agent/file.ddl', 'rspec_build/rspec_libdir/agent') - FileUtils.expects(:cp_r).with('/rspec/application/file.rb', 'rspec_build/rspec_libdir/application') - packager.send(:prepare_tmpdirs, data) - end - - it 'should log an error and raise if permissions means the dir cannot be created' do - FileUtils.stubs(:mkdir_p).with('rspec_build/rspec_libdir/agent').raises(Errno::EACCES) - packager.expects(:puts).with("Could not create directory 'rspec_build/rspec_libdir/agent'. Permission denied") - expect{ - packager.send(:prepare_tmpdirs, data) - }.to raise_error(Errno::EACCES) - end - - it 'should log an error and raise if the file does not exist' do - FileUtils.stubs(:mkdir_p) - FileUtils.expects(:cp_r).with('/rspec/agent/file.rb', 'rspec_build/rspec_libdir/agent').raises(Errno::ENOENT) - packager.expects(:puts).with("Could not copy file '/rspec/agent/file.rb' to 'rspec_build/rspec_libdir/agent'. File does not exist") - expect{ - packager.send(:prepare_tmpdirs, data) - }.to raise_error(Errno::ENOENT) - end - - it 'should log an error and raise for any other exception' do - File.stubs(:expand_path).raises('error') - packager.expects(:puts).with('Could not prepare build directory') - expect{ - packager.send(:prepare_tmpdirs, data) - }.to raise_error('error') - end - end - - describe '#create_debian_dir' do - it 'should create the debian dir in the build dir' do - FileUtils.expects(:mkdir_p).with('rspec_build/debian') - packager.send(:create_debian_dir) - end - - it 'should log an error and raise an exception if the dir cannot be created' do - FileUtils.expects(:mkdir_p).with('rspec_build/debian').raises('error') - packager.expects(:puts).with("Could not create directory 'rspec_build/debian'") - expect{ - packager.send(:create_debian_dir) - }.to raise_error('error') - end - end - - describe '#cleanup_tmpdirs' do - it 'should remove the temporary build directory' do - File.stubs(:directory?).with('rspec_tmp').returns(true) - FileUtils.expects(:rm_r).with('rspec_tmp') - packager.send(:cleanup_tmpdirs) - end - - it 'should log an error and raise an exception if the directory could not be removed' do - File.stubs(:directory?).with('rspec_tmp').returns(true) - FileUtils.expects(:rm_r).with('rspec_tmp').raises('error') - packager.expects(:puts).with("Could not remove temporary build directory - 'rspec_tmp'") - expect{ - packager.send(:cleanup_tmpdirs) - }.to raise_error('error') - end - end - end - end -end diff --git a/spec/unit/mcollective/packagers/modulepackage_packager_spec.rb b/spec/unit/mcollective/packagers/modulepackage_packager_spec.rb deleted file mode 100644 index 6edbdae2..00000000 --- a/spec/unit/mcollective/packagers/modulepackage_packager_spec.rb +++ /dev/null @@ -1,214 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' -require 'mcollective/pluginpackager/modulepackage_packager' - -module MCollective - module PluginPackager - describe ModulepackagePackager, :unless => MCollective::Util.windows? do - let(:data) do - {:agent => {:files => ['./agent/rspec.rb']}, - :common => {:files => ['./agent/rspec.ddl']}, - :client => {:files => ['./application/rspec.rb']}} - end - - let(:plugin) do - p = mock - p.stubs(:mcname).returns('mcollective') - p.stubs(:metadata).returns({ - :version => '1.0', - :name => 'rspec', - :description => 'An rspec.', - :url => 'http://example.com/rspec/', - }) - p.stubs(:revision).returns(1) - p.stubs(:target_path).returns('rspec_build') - p.stubs(:vendor).returns('puppetlabs') - p.stubs(:packagedata).returns(data) - p - end - - let(:packager) do - p = ModulepackagePackager.new(plugin) - p.instance_variable_set(:@tmpdir, 'rspec_tmp') - p - end - - before :each do - ModulepackagePackager.any_instance.stubs(:assert_new_enough_puppet) - @packager = packager - end - - describe '#initialize' do - it 'should set the instance variables' do - new_packager = ModulepackagePackager.new(plugin) - new_packager.instance_variable_get(:@plugin).should == plugin - new_packager.instance_variable_get(:@package_name).should == 'mcollective_rspec' - new_packager.instance_variable_get(:@verbose).should == false - new_packager.instance_variable_get(:@keep_artifacts).should == nil - end - - it 'should fail if no build command is present' do - ModulepackagePackager.any_instance.stubs(:assert_new_enough_puppet).raises("Cannot build package. 'puppet' is not present on the system") - expect{ - new_packager = ModulepackagePackager.new(plugin) - }.to raise_error("Cannot build package. 'puppet' is not present on the system") - end - end - - describe '#create_packages' do - before :each do - @packager.stubs(:puts) - end - - it 'should run through the complete build process' do - Dir.expects(:mktmpdir).with('mcollective_packager').returns('rspec_tmp') - @packager.expects(:make_module) - @packager.expects(:run_build) - @packager.expects(:move_package) - @packager.expects(:cleanup_tmpdirs) - @packager.create_packages - end - - it 'should clean up tmpdirs if keep_artifacts is false' do - Dir.stubs(:mktmpdir).raises('error') - @packager.expects(:cleanup_tmpdirs) - expect{ - packager.create_packages - }.to raise_error('error') - end - - it 'should keep the build artifacts if keep_artifacts is true' do - @packager.instance_variable_set(:@keep_artifacts, true) - Dir.stubs(:mktmpdir).raises('error') - @packager.expects(:cleanup_tmpdirs).never - expect{ - @packager.create_packages - }.to raise_error('error') - end - end - - describe '#assert_new_enough_puppet' do - before :each do - ModulepackagePackager.any_instance.unstub(:assert_new_enough_puppet) - end - - let(:shell) do - s = mock - s.stubs(:runcommand) - s - end - - it 'should ensure we have a puppet' do - PluginPackager.expects(:command_available?).with('puppet').returns(false) - expect { - packager.send(:assert_new_enough_puppet) - }.to raise_error('Cannot build package. \'puppet\' is not present on the system.') - end - - it 'should find the version of puppet' do - PluginPackager.stubs(:command_available?).with('puppet').returns(true) - shell.expects(:stdout).returns("3.3.0\n") - Shell.stubs(:new).with('puppet --version').returns(shell) - packager.send(:assert_new_enough_puppet) - end - - it 'should assert the version of puppet is >= 3.3.0' do - PluginPackager.expects(:command_available?).with('puppet').returns(true) - Shell.stubs(:new).with('puppet --version').returns(shell) - shell.stubs(:stdout).returns("3.2.0\n") - expect { - packager.send(:assert_new_enough_puppet) - }.to raise_error('Cannot build package. puppet 3.3.0 or greater required. We have 3.2.0.') - end - end - - describe '#make_module' do - it 'should copy the package content to the tmp build dir' do - file = mock() - - ['./agent/rspec.rb', './agent/rspec.ddl', './application/rspec.rb'].each do |m| - File.stubs(:expand_path).with(m).returns(m) - end - - File.stubs(:directory?).with('rspec_tmp/manifests').returns(false, true) - File.stubs(:directory?).with('rspec_tmp/files/agent/mcollective/agent').returns(false, true) - File.stubs(:directory?).with('rspec_tmp/files/common/mcollective/agent').returns(false, true) - File.stubs(:directory?).with('rspec_tmp/files/client/mcollective/application').returns(false) - FileUtils.expects(:mkdir_p).with('rspec_tmp/manifests') - FileUtils.expects(:mkdir_p).with('rspec_tmp/files/common/mcollective/agent') - FileUtils.expects(:mkdir_p).with('rspec_tmp/files/agent/mcollective/agent') - FileUtils.expects(:mkdir_p).with('rspec_tmp/files/client/mcollective/application') - FileUtils.expects(:cp_r).with('./agent/rspec.rb', 'rspec_tmp/files/agent/mcollective/agent') - FileUtils.expects(:cp_r).with('./agent/rspec.ddl', 'rspec_tmp/files/common/mcollective/agent') - FileUtils.expects(:cp_r).with('./application/rspec.rb', 'rspec_tmp/files/client/mcollective/application') - File.stubs(:open).with('rspec_tmp/manifests/common.pp', 'w').yields(file) - File.stubs(:open).with('rspec_tmp/manifests/agent.pp', 'w').yields(file) - File.stubs(:open).with('rspec_tmp/manifests/client.pp', 'w').yields(file) - File.stubs(:open).with('rspec_tmp/Modulefile', 'w').yields(file) - File.stubs(:open).with('rspec_tmp/README.md', 'w').yields(file) - file.expects(:puts).times(5) - @packager.send(:make_module) - end - end - - describe '#run_build' do - before :each do - Dir.stubs(:chdir).yields - end - - it 'should build the packages' do - PluginPackager.expects(:execute_verbosely).with(false).yields - PluginPackager.expects(:safe_system).with('puppet module build') - @packager.send(:run_build) - end - - it 'should not build silently if verbose is false' do - @packager.instance_variable_set(:@verbose, true) - PluginPackager.expects(:execute_verbosely).with(true).yields - PluginPackager.expects(:safe_system).with('puppet module build') - @packager.send(:run_build) - end - - it 'should write a message and raise an exception if the build fails' do - Dir.stubs(:chdir).raises('error') - @packager.expects(:puts).with('Build process has failed') - expect{ - @packager.send(:run_build) - }.to raise_error('error') - end - end - - describe '#move_package' do - it 'should copy all the build artifacts to the cwd' do - File.stubs(:join).with('rspec_tmp', 'pkg', 'puppetlabs-mcollective_rspec-1.0.tar.gz').returns('1.tar.gz') - FileUtils.expects(:cp).with('1.tar.gz', '.') - @packager.send(:move_package) - end - - it 'should write a message and raise an exception if the artifacts cannot be copied' do - FileUtils.stubs(:cp).raises('error') - @packager.expects(:puts).with('Could not copy package to working directory') - expect{ - @packager.send(:move_package) - }.to raise_error('error') - end - end - - describe '#cleanup_tmpdirs' do - it 'should remove the tmp dirs' do - File.stubs(:directory?).with('rspec_tmp').returns(true) - FileUtils.expects(:rm_r).with('rspec_tmp') - @packager.send(:cleanup_tmpdirs) - end - - it 'should write a message and raise an exception if it cannot remove the tmp dirs' do - File.stubs(:directory?).raises('error') - @packager.expects(:puts).with("Could not remove temporary build directory - 'rspec_tmp'") - expect{ - @packager.send(:cleanup_tmpdirs) - }.to raise_error('error') - end - end - end - end -end diff --git a/spec/unit/mcollective/packagers/ospackage_spec.rb b/spec/unit/mcollective/packagers/ospackage_spec.rb deleted file mode 100644 index 82752434..00000000 --- a/spec/unit/mcollective/packagers/ospackage_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env rspec - -require 'spec_helper' - -MCollective::PluginManager.clear -require 'mcollective/pluginpackager/ospackage_packager' - -module MCollective - module PluginPackager - describe "#initialize" do - - before :all do - @packager = mock() - @packager.stubs(:new) - end - - it "should correctly set members and create the correct packager on redhat" do - File.expects(:exists?).with("/etc/redhat-release").returns(true) - PluginPackager.expects(:[]).with("RpmpackagePackager").returns(@packager) - ospackager = OspackagePackager.new("package") - ospackager.package_type.should == "RPM" - end - - it "should correctly set members and create the correct packager on debian" do - File.expects(:exists?).with("/etc/redhat-release").returns(false) - File.expects(:exists?).with("/etc/debian_version").returns(true) - PluginPackager.expects(:[]).with("DebpackagePackager").returns(@packager) - ospackager = OspackagePackager.new("package") - ospackager.package_type.should == "Deb" - end - - it "should raise an exception if the os can't be identified" do - File.expects(:exists?).with("/etc/redhat-release").returns(false) - File.expects(:exists?).with("/etc/debian_version").returns(false) - expect{ - OspackagePackager.new("package") - }.to raise_error(RuntimeError) - end - end - - describe "#create_packages" do - before :all do - @packager = mock - @packager.stubs(:new).returns(@packager) - end - - it "should call a packagers create_packages class" do - File.expects(:exists?).with("/etc/redhat-release").returns(true) - PluginPackager.expects(:[]).with("RpmpackagePackager").returns(@packager) - @packager.expects(:create_packages) - ospackager = OspackagePackager.new("package") - ospackager.class.should == OspackagePackager - ospackager.create_packages - end - end - end -end diff --git a/spec/unit/mcollective/packagers/rpmpackage_packager_spec.rb b/spec/unit/mcollective/packagers/rpmpackage_packager_spec.rb deleted file mode 100644 index e96dcd02..00000000 --- a/spec/unit/mcollective/packagers/rpmpackage_packager_spec.rb +++ /dev/null @@ -1,283 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' -require 'mcollective/pluginpackager/rpmpackage_packager' - -module MCollective - module PluginPackager - describe RpmpackagePackager, :unless => MCollective::Util.windows? do - let(:plugin) do - p = mock - p.stubs(:mcname).returns('mcollective') - p.stubs(:metadata).returns({:version => '1.0', :name => 'rspec'}) - p.stubs(:revision).returns(1) - p.stubs(:target_path).returns('rspec_build') - p - end - - let(:packager) do - p = RpmpackagePackager.new(plugin) - p.instance_variable_set(:@tmpdir, 'rspec_tmp') - p - end - - let(:data) do - {:agent => {:files => ['agent/rspec.rb', 'agent/rspec.ddl']}, - :client => {:files => ['application/rspec.rb']}} - end - - before :each do - RpmpackagePackager.any_instance.stubs(:select_command).returns('rpmbuild') - RpmpackagePackager.any_instance.stubs(:rpmdir).returns('rspec_rpm') - RpmpackagePackager.any_instance.stubs(:srpmdir).returns('rspec_srpm') - @packager = packager - end - - describe '#initialize' do - it 'should set the instance variables' do - new_packager = RpmpackagePackager.new(plugin) - new_packager.instance_variable_get(:@plugin).should == plugin - new_packager.instance_variable_get(:@package_name).should == 'mcollective-rspec' - new_packager.instance_variable_get(:@package_name_and_version).should == 'mcollective-rspec-1.0' - new_packager.instance_variable_get(:@verbose).should == false - new_packager.instance_variable_get(:@libdir).should == '/usr/libexec/mcollective/mcollective/' - new_packager.instance_variable_get(:@signature).should == nil - new_packager.instance_variable_get(:@rpmdir).should == 'rspec_rpm' - new_packager.instance_variable_get(:@srpmdir).should == 'rspec_srpm' - new_packager.instance_variable_get(:@keep_artifacts).should == nil - end - - it 'should fail if no build command is present' do - RpmpackagePackager.any_instance.stubs(:select_command).returns(nil) - expect{ - new_packager = RpmpackagePackager.new(plugin) - }.to raise_error("Cannot build package. 'rpmbuild' or 'rpmbuild-md5' is not present on the system") - end - end - - describe '#select_command' do - it 'should return the command string for rpmbuild if its present' do - RpmpackagePackager.any_instance.unstub(:select_command) - PluginPackager.stubs(:command_available?).with('rpmbuild-md5').returns(false) - PluginPackager.stubs(:command_available?).with('rpmbuild').returns(true) - @packager.select_command.should == 'rpmbuild' - end - - it 'should return the command string for rpmbuild-md5 if its present' do - RpmpackagePackager.any_instance.unstub(:select_command) - PluginPackager.stubs(:command_available?).with('rpmbuild-md5').returns(true) - @packager.select_command.should == 'rpmbuild-md5' - end - - it 'should return nil if neither are present' do - RpmpackagePackager.any_instance.unstub(:select_command) - PluginPackager.stubs(:command_available?).with('rpmbuild-md5').returns(false) - PluginPackager.stubs(:command_available?).with('rpmbuild').returns(false) - @packager.select_command.should == nil - end - end - - describe '#create_packages' do - before :each do - @packager.stubs(:puts) - end - - it 'should run through the complete build process' do - Dir.expects(:mktmpdir).with('mcollective_packager').returns('rspec_tmp') - @packager.expects(:prepare_tmpdirs) - @packager.expects(:make_spec_file) - @packager.expects(:run_build) - @packager.expects(:move_packages) - @packager.expects(:cleanup_tmpdirs) - @packager.create_packages - end - - it 'should clean up tmpdirs if keep_artifacts is false' do - Dir.stubs(:mktmpdir).raises('error') - @packager.expects(:cleanup_tmpdirs) - expect{ - packager.create_packages - }.to raise_error('error') - end - - it 'should keep the build artifacts if keep_artifacts is true' do - @packager.instance_variable_set(:@keep_artifacts, true) - Dir.stubs(:mktmpdir).raises('error') - @packager.expects(:cleanup_tmpdirs).never - expect{ - @packager.create_packages - }.to raise_error('error') - end - end - - describe '#run_build' do - it 'should build the packages' do - @packager.expects(:create_tar).returns('rspec.tgz') - PluginPackager.expects(:execute_verbosely).with(false).yields - PluginPackager.expects(:safe_system).with('rpmbuild -ta --quiet rspec.tgz') - @packager.send(:run_build) - end - - it 'should sign the packages if a signature is defined' do - @packager.instance_variable_set(:@signature, true) - @packager.expects(:create_tar).returns('rspec.tgz') - PluginPackager.expects(:execute_verbosely).with(false).yields - PluginPackager.expects(:safe_system).with('rpmbuild -ta --quiet --sign rspec.tgz') - @packager.send(:run_build) - end - - it 'should not build silently if verbose is false' do - @packager.instance_variable_set(:@verbose, true) - @packager.expects(:create_tar).returns('rspec.tgz') - PluginPackager.expects(:execute_verbosely).with(true).yields - PluginPackager.expects(:safe_system).with('rpmbuild -ta rspec.tgz') - @packager.send(:run_build) - end - - it 'should write a message and raise an exception if the build fails' do - @packager.stubs(:create_tar).raises('error') - @packager.expects(:puts).with('Build process has failed') - expect{ - @packager.send(:run_build) - }.to raise_error('error') - end - end - - describe '#create_tar' do - it 'should create the tarball' do - PluginPackager.expects(:execute_verbosely).with(false).yields - Dir.expects(:chdir).with('rspec_tmp').yields - PluginPackager.expects(:safe_system).with('tar -cvzf rspec_tmp/mcollective-rspec-1.0.tgz mcollective-rspec-1.0') - @packager.send(:create_tar) - end - - it 'should write a message and raise an exception if the tarball cannot be built' do - PluginPackager.expects(:execute_verbosely).with(false).raises('error') - @packager.expects(:puts).with("Could not create tarball - 'rspec_tmp/mcollective-rspec-1.0.tgz'") - expect{ - @packager.send(:create_tar) - }.to raise_error('error') - end - end - - describe '#move_packages' do - it 'should copy all the build artifacts to the cwd' do - File.stubs(:join).with('rspec_rpm', 'noarch', 'mcollective-rspec-*-1.0-1*.noarch.rpm').returns('rspec_rpm/noarch/mcollective-rspec-*-1.0-1.noarch.rpm') - File.stubs(:join).with('rspec_srpm', 'mcollective-rspec-1.0-1*.src.rpm').returns('rspec_srpm/mcollective-rspec-1.0-1*.src.rpm') - Dir.stubs(:glob).with('rspec_rpm/noarch/mcollective-rspec-*-1.0-1.noarch.rpm').returns(['1.rpm', '2.rpm']) - Dir.stubs(:glob).with('rspec_srpm/mcollective-rspec-1.0-1*.src.rpm').returns(['1.src.rpm']) - FileUtils.expects(:cp).with(['1.rpm', '2.rpm', '1.src.rpm'], '.') - @packager.send(:move_packages) - end - - it 'should write a message and raise an exception if the artifacts cannot be copied' do - Dir.stubs(:glob).raises('error') - @packager.expects(:puts).with('Could not copy packages to working directory') - expect{ - @packager.send(:move_packages) - }.to raise_error('error') - end - end - - describe '#make_spec_file' do - it 'should create a specfile from the erb template' do - erb = mock - file = mock - erb.stubs(:result).returns('<% rspec =%>') - File.stubs(:dirname).returns('rspec_template') - File.expects(:read).with('rspec_template/templates/redhat/rpm_spec.erb').returns(erb) - ERB.expects(:new).with(erb, nil, '-').returns(erb) - File.expects(:open).with('rspec_tmp/mcollective-rspec-1.0/mcollective-rspec-1.0.spec', 'w').yields(file) - file.expects(:puts).with('<% rspec =%>') - @packager.send(:make_spec_file) - end - - it 'should write a message and raise an exception if it could not create the specfile' do - ERB.stubs(:new).raises('error') - @packager.expects(:puts).with("Could not create specfile - 'rspec_tmp/mcollective-rspec-1.0/mcollective-rspec-1.0.spec'") - expect{ - @packager.send(:make_spec_file) - }.to raise_error('error') - end - end - - describe '#prepare_tmpdirs' do - before :each do - pluginfiles = ['/agent/rspec.rb', '/agent/rspec.ddl', '/application/rspec.rb'] - @packager.stubs(:plugin_files).returns(pluginfiles) - end - - it 'should copy the package content to the tmp build dir' do - File.stubs(:directory?).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent').returns(false, true) - File.stubs(:directory?).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/application').returns(false) - FileUtils.expects(:mkdir_p).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent') - FileUtils.expects(:mkdir_p).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/application') - FileUtils.expects(:cp_r).with('/agent/rspec.rb', 'rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent') - FileUtils.expects(:cp_r).with('/agent/rspec.ddl', 'rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent') - FileUtils.expects(:cp_r).with('/application/rspec.rb', 'rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/application') - @packager.send(:prepare_tmpdirs) - end - - it 'should fail if we do not have permission to create the directory' do - File.stubs(:directory?).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent').raises(Errno::EACCES) - @packager.expects(:puts) - expect{ - @packager.send(:prepare_tmpdirs) - }.to raise_error(Errno::EACCES) - end - - it 'should fail if the files we are trying to copy do not exist' do - File.stubs(:directory?).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent').raises(Errno::ENOENT) - @packager.expects(:puts) - expect{ - @packager.send(:prepare_tmpdirs) - }.to raise_error(Errno::ENOENT) - - end - - it 'should write a message an error if the something else goes wrong and raise an exception' do - File.stubs(:directory?).with('rspec_tmp/mcollective-rspec-1.0/usr/libexec/mcollective/mcollective/agent').raises('error') - @packager.expects(:puts) - expect{ - @packager.send(:prepare_tmpdirs) - }.to raise_error('error') - end - end - - describe '#plugin_files' do - it 'should return the package files in a single array' do - files = ['agent/rspec.rb', 'agent/rspec.ddl', 'application/rspec.rb'] - plugin.stubs(:packagedata).returns(data) - result = @packager.send(:plugin_files) - (files | result).should == files - end - end - - describe '#package_files' do - it 'should return the package files and ommit the directories' do - files = ['/rspec', '/rspec/1.rb', '/rspec/2.rb'] - File.expects(:directory?).with('/rspec').returns(true) - File.expects(:directory?).with('/rspec/1.rb').returns(false) - File.expects(:directory?).with('/rspec/2.rb').returns(false) - @packager.send(:package_files, files).should == ['/usr/libexec/mcollective/mcollective/rspec/1.rb', - '/usr/libexec/mcollective/mcollective/rspec/2.rb'] - end - end - - describe '#cleanup_tmpdirs' do - it 'should remove the tmp dirs' do - File.stubs(:directory?).with('rspec_tmp').returns(true) - FileUtils.expects(:rm_r).with('rspec_tmp') - @packager.send(:cleanup_tmpdirs) - end - - it 'should write a message and raise an exception if it cannot remove the tmp dirs' do - File.stubs(:directory?).raises('error') - @packager.expects(:puts).with("Could not remove temporary build directory - 'rspec_tmp'") - expect{ - @packager.send(:cleanup_tmpdirs) - }.to raise_error('error') - end - end - end - end -end