Skip to content

Commit

Permalink
Merge pull request #298 from aws/rake-packaging
Browse files Browse the repository at this point in the history
Adds Rakefile task to package
  • Loading branch information
vrr-21 authored Jul 6, 2021
2 parents 93c0149 + 29cab55 commit 3c39d54
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ deployment/
.idea/
.DS_STORE
*.iml
pkg/
vendor-thirdparty/
.bundle/
78 changes: 78 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'rake'
require 'rake/packagetask'
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'rubygems'
require 'yaml'

# Run all units tests in test/
desc "Run unit tests in test/"
Expand Down Expand Up @@ -51,7 +53,83 @@ def getAgentTrackingInfo
end
end

# Packaging into a tar
# we need GNU tar to avoid warning when extracting the content on linux systems
def tar
_tar = `which tar`.chomp
# we must use GNU tar
unless `#{_tar} --version`.include?('GNU')
# probably on a Mac
_tar = `which gtar`.chomp
raise 'The GNU tar utility was not found in this system. Please install GNU tar before trying to run this task.' if _tar.empty?
end
_tar
end

BIN = "bin"
LIB = "lib"
CERTS = "certs"
CONF = "conf"
VENDOR = "vendor"
VERSION_FILE = ".version"
CONFIG_FILE = "#{CONF}/codedeployagent.yml"
FEATURES = "features"

config = YAML.load(File.read(CONFIG_FILE))

def rubygem_folder
ruby_version = RUBY_VERSION
ruby_version_array = ruby_version.split(".")
ruby_version_array[-1] = "0" # 2.6.x will become 2.6.0
ruby_version_array.join(".")
end

pkg = "#{Dir.pwd}/pkg" ## Package where the tar will be generated.

desc "Package files into a tar"
task :package do
# Clean up existing package
FileUtils.rm_rf(pkg)

# Set up directories
bundle_dir = "#{pkg}/#{config[:program_name]}"
FileUtils.mkdir_p bundle_dir
FileUtils.mkdir_p "#{bundle_dir}/opt/#{config[:program_name]}/"
FileUtils.mkdir_p "#{bundle_dir}/opt/#{config[:program_name]}/bin"
FileUtils.mkdir_p "#{bundle_dir}/etc/#{config[:program_name]}/conf"
FileUtils.mkdir_p "#{bundle_dir}/etc/init.d/"

# Copy files
sh "cp -rf #{BIN} #{bundle_dir}/opt/#{config[:program_name]}/"
sh "cp -rf #{LIB} #{bundle_dir}/opt/#{config[:program_name]}/"
sh "cp -f #{CONF}/codedeployagent.yml #{bundle_dir}/etc/#{config[:program_name]}/conf/"
sh "cp -rf #{CERTS} #{bundle_dir}/opt/#{config[:program_name]}/"
sh "cp -rf #{VENDOR} #{bundle_dir}/opt/#{config[:program_name]}/"
sh "cp -rf init.d #{bundle_dir}/etc/"
sh "cp -f LICENSE #{bundle_dir}/opt/#{config[:program_name]}/"

# Vendor folder needs an extra effort, we also need to package the gems installed
gem_lib_folder = "vendor-thirdparty"

rubygemlibs = "#{gem_lib_folder}/ruby/#{rubygem_folder}"
Dir.glob("#{rubygemlibs}/gems/*") do |path|
sh "cp -r #{path} #{bundle_dir}/opt/#{config[:program_name]}/#{VENDOR}/gems"
end
Dir.glob("#{rubygemlibs}/specifications/*") do |path|
sh "cp -r #{path} #{bundle_dir}/opt/#{config[:program_name]}/#{VENDOR}/specifications"
end

sh "sed '/group :test/,$d' Gemfile > #{bundle_dir}/opt/#{config[:program_name]}/Gemfile"
sh "sed '/add_development_dependency/d' codedeploy_agent.gemspec > #{bundle_dir}/opt/#{config[:program_name]}/codedeploy_agent.gemspec"

# Build tar
sh "cd #{bundle_dir} && COPYFILE_DISABLE=true #{tar} --owner=0 --group=0 -cf #{pkg}/#{config[:program_name]}.tar *"
FileUtils.rm_rf("#{bundle_dir}")
end

# Clean up
task :clean do
rm_rf 'deployment'
rm_rf 'pkg'
rm_rf 'vendor-thirdparty'
end

0 comments on commit 3c39d54

Please sign in to comment.