Skip to content
Browse files

Refactoring of Rakefile and all tasks + added new versioning based on…

… SemVer specs
  • Loading branch information...
1 parent 9245a50 commit 301a065b40a6cdd12b3e331d40b63a25c76e1f26 @dieterdemeyer committed
View
201 Rakefile
@@ -1,204 +1,11 @@
require 'rubygems'
-require 'rake/clean'
-require 'rspec/core/rake_task'
+require 'rake'
-$:.unshift(File.join(File.dirname(__FILE__), 'build'))
-require 'rpm_packager'
-require 'deb_packager'
+task_dir = File.expand_path("../tasks", __FILE__)
-CLEAN.include("")
-CLOBBER.include("target")
+FileList["#{task_dir}/**/*.rake"].each { |fn| load fn }
-desc "Default task prints the possible targets."
+desc "Default task prints the available targets."
task :default do
sh %{rake -T}
end
-
-desc "Run puppet module RSpec tests."
-RSpec::Core::RakeTask.new(:spec) do |t|
- t.rspec_opts = ["--format", "doc", "--color"]
- t.fail_on_error = false
- t.pattern = 'spec/**/*_spec.rb'
-end
-
-desc "Run puppet module acceptance tests."
-task :acceptance do
- puts "Running acceptance tests..."
- # cucumber ? cucumber-puppet
-end
-
-desc "Check puppet module syntax."
-task :syntax do
- begin
- require 'puppet/face'
- rescue LoadError
- fail 'Cannot load puppet/face, are you sure you have Puppet 2.7?'
- end
-
- def validate_manifest(file)
- begin
- Puppet::Face[:parser, '0.0.1'].validate(file)
- rescue Puppet::Error => error
- puts error.message
- end
- end
-
- puts "Checking puppet module syntax..."
- FileList['**/*.pp'].each do |manifest|
- puts "Evaluating syntax for #{manifest}"
- validate_manifest manifest
- end
-end
-
-desc "Check puppet module code style."
-task :style do
- begin
- require 'puppet-lint'
- rescue LoadError
- fail 'Cannot load puppet-lint, did you install it?'
- end
-
- puts "Checking puppet module code style..."
- linter = PuppetLint.new
- linter.configuration.log_format = '%{path}:%{linenumber}:%{check}:%{KIND}:%{message}'
- linter.configuration.send("disable_80chars")
-
- FileList['**/*.pp'].each do |puppet_file|
- puts "Evaluating code style for #{puppet_file}"
- linter.file = puppet_file
- linter.run
- end
- fail if linter.errors?
-end
-
-# TODO: Reevaluate this when/if it becomes available in Puppet Faces
-desc "Create puppet module documentation."
-task :doc do
- output_dir = "target/doc"
- manifest_dir = "target/manifests"
-
- if File.directory?(output_dir)
- FileUtils.rm_r output_dir
- end
- if File.directory?(manifest_dir)
- FileUtils.rm_r manifest_dir
- end
- FileUtils.mkdir_p manifest_dir
-
- puts "Generating puppet module documentation..."
- FileUtils.mkdir_p("target/manifests")
- sh %{puppet doc --mode rdoc --manifestdir target/manifests/ --modulepath ../ --outputdir target/doc}
-
- work_dir = File.dirname(__FILE__)
- parent_dir = File.dirname(work_dir)
-
- if File.exists? "#{work_dir}/target/doc/files/#{work_dir}"
- FileUtils.mv "#{work_dir}/target/doc/files/#{work_dir}", "#{work_dir}/target/doc/files"
- end
-
- FileList['target/doc/**/*.html'].egrep(%r(#{parent_dir})) do |fn,line,match|
- text = File.read(fn)
- replace = text.gsub(%r(#{parent_dir}), "")
- File.open(fn, "w") { |file| file.puts replace }
- end
-
- FileList['target/doc/files/**/*_pp.html'].egrep(/rdoc-style\.css/) do |fn,line,match|
- depth_in_doc = fn.split(/\//).length - 3
- original_string = /[\.\/]*rdoc-style.css/
- replacement_string = '../' * depth_in_doc + 'rdoc-style.css'
- text = File.read(fn)
- replace = text.gsub(original_string, replacement_string)
- File.open(fn, "w") { |file| file.puts replace }
- end
-end
-
-desc "Create RPM package from puppet module."
-task :rpm do
- puts "Creating RPM package from puppet module..."
- module_name = ENV["JOB_NAME"].split('-')[1]
-
- rpm_packager = RpmPackager.new
- output = rpm_packager.build(module_name)
- puts output
-end
-
-desc "Create DEB package from puppet module."
-task :deb do
- puts "Creating DEB package from puppet module..."
- module_name = ENV["JOB_NAME"].split('-')[1]
-
- deb_packager = DebPackager.new
- output = deb_packager.build(module_name)
- puts output
-end
-
-desc "Create a puppet module, compatible with Puppet Forge."
-task :build do
- begin
- require 'puppet/face'
- rescue LoadError
- fail 'Cannot load puppet/face, are you sure you have Puppet 2.7?'
- end
-
- puts "Creating puppet module for Puppet Forge..."
- # puppet module build / upload to forge
- #Puppet::Face[:module, '1.0.0'].build()
-end
-
-desc "Create a version tag for the current commit."
-task :tag, [:version] do |t,args|
- puts "Tagging version #{args.version}"
- # git tag
- # deal with ChangeLog
-end
-
-desc "Create a puppet module release for the provided version."
-task :release, [:version] => [:tag] do |t,args|
- puts "Releasing version #{args.version}"
- # checkout tag / build rpm/deb/forge package
-end
-
-namespace "jenkins" do
- begin
- require 'ci/reporter/rake/rspec'
- require 'ci/reporter/rake/cucumber'
- rescue LoadError
- fail 'Cannot load ci_reporter, did you install it?'
- end
-
- SPEC_REPORTS_PATH = "target/reports/spec/"
- ACCEPTANCE_REPORTS_PATH = "target/reports/acceptance/"
-
- desc "Run Jenkins compatible Rspec tests."
- task :spec_tests => ["ci:setup:rspec"] do
- ENV["CI_REPORTS"] = SPEC_REPORTS_PATH
- FileUtils.mkdir_p(SPEC_REPORTS_PATH)
-
- Rake::Task[:spec].invoke
- end
-
- desc "Run Jenkins compatible acceptance tests."
- task :acceptance_tests => ["ci:setup:cucumber"] do
- ENV["CI_REPORTS"] = ACCEPTANCE_REPORTS_PATH
- FileUtils.mkdir_p(ACCEPTANCE_REPORTS_PATH)
-
- Rake::Task[:acceptance].invoke
- end
-
- desc "Archive job configuration in YAML format."
- task :archive_job_configuration do
- dist_dir = "target/dist"
-
- module_name = ENV["JOB_NAME"]
- git_commit = ENV["GIT_COMMIT"]
-
- if !git_commit.nil? and !git_commit.empty?
- puts "Saving #{module_name}.yaml file"
- FileUtils.mkdir_p(dist_dir)
- open("target/dist/#{module_name}.yaml", "w") { |file|
- file.puts "module_name: #{module_name}"
- file.puts "git_commit: #{git_commit}"
- }
- end
- end
-end
View
12 metadata.json
@@ -1,12 +0,0 @@
-/*
-+-----------------------------------------------------------------------+
-| |
-| ==> DO NOT EDIT THIS FILE! <== |
-| |
-| You should edit the `Modulefile` and run `puppet-module build` |
-| to generate the `metadata.json` file for your releases. |
-| |
-+-----------------------------------------------------------------------+
-*/
-
-{}
View
8 tasks/Gemfile
@@ -0,0 +1,8 @@
+source :rubygems
+
+gem 'rake'
+gem 'rspec'
+gem 'rspec-puppet'
+gem 'ci_reporter'
+gem 'puppet-lint'
+gem 'fpm', '<=0.3.11'
View
16 tasks/env.rb
@@ -0,0 +1,16 @@
+# This file contains the environment configuration shared by all
+# tasks. It should be required at the top of each *.rake file with:
+#
+# require File.expand_path('../../env', __FILE__)
+
+# Sync all stdout/stderr to play nice with buildtools
+# running the rake tasks.
+$stdout.sync = true
+$stderr.sync = true
+
+# Module root directory
+MODULE_ROOT_DIR = Rake.original_dir
+
+# Basedir for all output.
+RESULTS = ENV['results'] || 'target'
+
View
6 tasks/rake/clean.rake
@@ -0,0 +1,6 @@
+require 'rake/clean'
+
+require File.expand_path('../../env', __FILE__)
+
+CLEAN.include("")
+CLOBBER.include("#{RESULTS}")
View
14 tasks/rake/deb.rake
@@ -0,0 +1,14 @@
+require File.expand_path('../../env', __FILE__)
+
+$:.unshift(File.join(File.dirname(__FILE__), 'lib', 'packaging'))
+require 'deb_packager'
+
+desc "Create DEB package from puppet module."
+task :deb do
+ puts "Creating DEB package from puppet module..."
+ module_name = ENV['JOB_NAME'].split('-')[1]
+
+ deb_packager = DebPackager.new
+ output = deb_packager.build(module_name)
+ puts output
+end
View
47 tasks/rake/doc.rake
@@ -0,0 +1,47 @@
+require File.expand_path('../../env', __FILE__)
+
+task :doc_prep do
+ output_dir = "#{RESULTS}/doc"
+ manifest_dir = "#{RESULTS}/manifests"
+
+ if File.directory?(output_dir)
+ FileUtils.rm_r output_dir
+ end
+ if File.directory?(manifest_dir)
+ FileUtils.rm_r manifest_dir
+ end
+ FileUtils.mkdir_p manifest_dir
+end
+
+# TODO: Revisit this when/if it becomes available in Puppet Faces
+desc "Create puppet module documentation."
+task :doc => [:doc_prep] do
+ puts "Generating puppet module documentation..."
+
+ output_dir = "#{RESULTS}/doc"
+ manifest_dir = "#{RESULTS}/manifests"
+
+ sh %{puppet doc --mode rdoc --manifestdir #{manifest_dir} --modulepath ../ --outputdir #{output_dir}}
+
+ work_dir = File.dirname(__FILE__)
+ parent_dir = File.dirname(work_dir)
+
+ if File.directory? "#{output_dir}/files/#{work_dir}"
+ FileUtils.mv "#{output_dir}/files/#{work_dir}", "#{output_dir}/files"
+ end
+
+ FileList["#{output_dir}/**/*.html"].each do |fn|
+ text = File.read(fn)
+ replace = text.gsub(%r(#{parent_dir}), "")
+ File.open(fn, "w") { |file| file.puts replace }
+ end
+
+ FileList["#{output_dir}/files/**/*_pp.html"].each do |fn|
+ depth_below_doc = fn.split(/\//).length - 3
+ original_string = /[\.\/]*rdoc-style.css/
+ replacement_string = '../' * depth_below_doc + 'rdoc-style.css'
+ text = File.read(fn)
+ replace = text.gsub(original_string, replacement_string)
+ File.open(fn, "w") { |file| file.puts replace }
+ end
+end
View
46 tasks/rake/jenkins.rake
@@ -0,0 +1,46 @@
+require File.expand_path('../../env', __FILE__)
+
+namespace "jenkins" do
+ begin
+ require 'ci/reporter/rake/rspec'
+ require 'ci/reporter/rake/cucumber'
+ rescue LoadError
+ fail 'Cannot load ci_reporter, did you install it?'
+ end
+
+ SPEC_REPORTS_PATH = "#{RESULTS}/reports/spec/"
+ ACCEPTANCE_REPORTS_PATH = "#{RESULTS}/reports/acceptance/"
+
+ desc "Run Jenkins compatible Rspec tests."
+ task :spec_tests => ["ci:setup:rspec"] do
+ ENV['CI_REPORTS'] = SPEC_REPORTS_PATH
+ FileUtils.mkdir_p(SPEC_REPORTS_PATH)
+
+ Rake::Task[:spec].invoke
+ end
+
+ desc "Run Jenkins compatible acceptance tests."
+ task :acceptance_tests => ["ci:setup:cucumber"] do
+ ENV['CI_REPORTS'] = ACCEPTANCE_REPORTS_PATH
+ FileUtils.mkdir_p(ACCEPTANCE_REPORTS_PATH)
+
+ Rake::Task[:acceptance].invoke
+ end
+
+ desc "Archive job configuration in YAML format."
+ task :archive_job_configuration do
+ dist_dir = "#{RESULTS}/dist"
+
+ module_name = ENV['JOB_NAME']
+ git_commit = ENV['GIT_COMMIT']
+
+ if !git_commit.nil? and !git_commit.empty?
+ puts "Saving #{module_name}.yaml file"
+ FileUtils.mkdir_p(dist_dir)
+ open("#{dist_dir}/#{module_name}.yaml", "w") { |file|
+ file.puts "module_name: #{module_name}"
+ file.puts "git_commit: #{git_commit}"
+ }
+ end
+ end
+end
View
18 tasks/rake/lib/modulefile_reader.rb
@@ -0,0 +1,18 @@
+require File.expand_path('../../../env', __FILE__)
+
+class ModulefileReader
+
+ def initialize()
+ filename = File.join(MODULE_ROOT_DIR, 'Modulefile')
+ @module_file = File.read(filename)
+ end
+
+ def version
+ @module_file.each_line do |line|
+ if line.match(/version/)
+ return line.split(' ')[1].gsub("'", "")
+ end
+ end
+ end
+
+end
View
61 tasks/rake/lib/packaging/base_packager.rb
@@ -0,0 +1,61 @@
+gem 'fpm', '<=0.3.11'
+require 'fpm'
+require 'fpm/program'
+require 'pp'
+
+$:.unshift(File.join(File.dirname(__FILE__), '..'))
+require 'modulefile_reader'
+
+class BasePackager
+
+ def initialize(package_type)
+ self.validate_environment
+
+ @basedirectory = ENV['WORKSPACE']
+ @package_version = ModulefileReader.new.version
+ @semver_version = @package_version + "+build." + ENV['BUILD_NUMBER'] + "." + ENV['GIT_COMMIT'][0,10]
+ @release = "1"
+ @package_type = package_type
+
+ case package_type
+ when "rpm"
+ @first_delimiter, @second_delimiter, @architecture = "-", ".", "noarch"
+ when "deb"
+ @first_delimiter, @second_delimiter, @architecture = "_", "_", "all"
+ end
+ end
+
+ def validate_environment()
+ if ENV['WORKSPACE'].nil?
+ fail("Environment variable WORKSPACE has not been set.")
+ end
+ if ENV['BUILD_NUMBER'].nil?
+ ENV["BUILD_NUMBER"] = "0"
+ end
+ if ENV['GIT_COMMIT'].nil?
+ ENV['GIT_COMMIT'] = "54b0c58c7ce9f2a8b551351102ee0938"[0,10]
+ end
+ end
+
+ def build(module_name)
+ package_name = "cegeka-puppet-#{module_name}"
+ destination_file = "#{package_name}#{@first_delimiter}#{@semver_version}-#{@release}#{@second_delimiter}#{@architecture}.#{@package_type}"
+ destination_folder = "#{@basedirectory}/#{module_name}/#{RESULTS}/dist"
+ url = "https://github.com/cegeka/puppet-#{module_name}"
+ description = "Puppet module: #{module_name} by Cegeka\nModule #{module_name} description goes here."
+
+ static_arguments = ["-t", @package_type, "-s", "dir", "-x", ".git", "-x", ".gitignore", "-x", "build", "-x", "Rakefile", "-a", @architecture, "-m", "Cegeka <computing@cegeka.be>", "--prefix", "/etc/puppet/modules"]
+ var_arguments = ["-n", package_name, "-v", @semver_version, "--iteration", @release, "--url", url, "--description", description, "-C", @basedirectory, module_name]
+ arguments = static_arguments + var_arguments
+
+ tmpdir = Dir.mktmpdir
+ Dir.chdir tmpdir
+ FileUtils.mkpath destination_folder
+ packagebuild = FPM::Program.new
+ ret = packagebuild.run(arguments)
+ FileUtils.mv("#{tmpdir}/#{destination_file}","#{destination_folder}/#{destination_file}")
+ FileUtils.remove_entry_secure(tmpdir)
+ return "Created #{destination_folder}/#{destination_file}"
+ end
+
+end
View
11 tasks/rake/lib/packaging/deb_packager.rb
@@ -0,0 +1,11 @@
+require 'base_packager'
+
+class DebPackager < BasePackager
+
+ PACKAGETYPE = "deb"
+
+ def initialize()
+ super(PACKAGETYPE)
+ end
+
+end
View
11 tasks/rake/lib/packaging/rpm_packager.rb
@@ -0,0 +1,11 @@
+require 'base_packager'
+
+class RpmPackager < BasePackager
+
+ PACKAGETYPE = "rpm"
+
+ def initialize()
+ super(PACKAGETYPE)
+ end
+
+end
View
14 tasks/rake/rpm.rake
@@ -0,0 +1,14 @@
+require File.expand_path('../../env', __FILE__)
+
+$:.unshift(File.join(File.dirname(__FILE__), 'lib', 'packaging'))
+require 'rpm_packager'
+
+desc "Create RPM package from puppet module."
+task :rpm do
+ puts "Creating RPM package from puppet module..."
+ module_name = ENV['JOB_NAME'].split('-')[1]
+
+ rpm_packager = RpmPackager.new
+ output = rpm_packager.build(module_name)
+ puts output
+end
View
10 tasks/rake/spec.rake
@@ -0,0 +1,10 @@
+require 'rspec/core/rake_task'
+
+require File.expand_path('../../env', __FILE__)
+
+desc "Run puppet module RSpec tests"
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = ["--format", "doc", "--color"]
+ t.fail_on_error = false
+ t.pattern = 'spec/**/*_spec.rb'
+end
View
27 tasks/rake/style.rake
@@ -0,0 +1,27 @@
+require File.expand_path('../../env', __FILE__)
+
+desc "Check puppet module code style."
+task :style do
+ begin
+ require 'puppet-lint'
+ rescue LoadError
+ fail 'Cannot load puppet-lint, did you install it?'
+ end
+
+ puts "Checking puppet module code style..."
+
+ success = true
+
+ linter = PuppetLint.new
+ linter.configuration.log_format =
+ '%{path}:%{linenumber}:%{check}:%{KIND}:%{message}'
+
+ FileList['**/*.pp'].each do |puppet_file|
+ puts "Evaluating code style for #{puppet_file}"
+ linter.file = puppet_file
+ linter.run
+ success = false if linter.errors?
+ end
+
+ abort "Checking puppet module code style FAILED" if success.is_a?(FalseClass)
+end
View
26 tasks/rake/syntax.rake
@@ -0,0 +1,26 @@
+require File.expand_path('../../env', __FILE__)
+
+desc "Check puppet module syntax."
+task :syntax do
+ begin
+ require 'puppet/face'
+ rescue LoadError
+ fail 'Cannot load puppet/face, are you sure you have Puppet 2.7?'
+ end
+
+ puts "Checking puppet module syntax..."
+
+ success = true
+
+ FileList['**/*.pp'].each do |manifest|
+ puts "Evaluating syntax for #{manifest}"
+ begin
+ Puppet::Face[:parser, '0.0.1'].validate(manifest)
+ rescue Puppet::Error => error
+ puts error.message
+ success = false
+ end
+ end
+
+ abort "Checking puppet module syntax FAILED" if success.is_a?(FalseClass)
+end

0 comments on commit 301a065

Please sign in to comment.
Something went wrong with that request. Please try again.