Skip to content

Commit

Permalink
Merge pull request #288 from simaishi/post_compression
Browse files Browse the repository at this point in the history
Add post compression support
  • Loading branch information
jrafanie authored Oct 4, 2018
2 parents 5dbfc1c + be9f805 commit f1412c2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Below are instructions on installing and configuring a virtual machine to genera
yum install ruby
yum install ruby-devel
yum install zlib-devel
yum install zip unzip

gem install trollop
gem install fog
Expand Down
4 changes: 4 additions & 0 deletions scripts/spec/target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
expect(described_class.new("openstack").file_extension).to eql "qc2"
end

it "#compression_type" do
expect(described_class.new("openstack").compression_type).to eql nil
end

it "#sort" do
targets = [described_class.new("vsphere"), described_class.new("openstack")]
expect(targets.sort.collect(&:name)).to eql %w(openstack vsphere)
Expand Down
24 changes: 14 additions & 10 deletions scripts/target.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Build
class Target
ImagefactoryMetadata = Struct.new(:imagefactory_type, :ova_format, :file_extension)
ImagefactoryMetadata = Struct.new(:imagefactory_type, :ova_format, :file_extension, :compression_type)

TYPES = {
'vsphere' => ImagefactoryMetadata.new('vsphere', 'vsphere', 'ova'),
'ovirt' => ImagefactoryMetadata.new('rhevm', 'rhevm', 'ova'),
'openstack' => ImagefactoryMetadata.new('openstack-kvm', nil, 'qc2'),
'hyperv' => ImagefactoryMetadata.new('hyperv', nil, 'vhd'),
'azure' => ImagefactoryMetadata.new('hyperv', nil, 'vhd'),
'vagrant' => ImagefactoryMetadata.new('vsphere', 'vagrant-virtualbox', 'box'),
'libvirt' => ImagefactoryMetadata.new('openstack-kvm', nil, 'qc2'),
'gce' => ImagefactoryMetadata.new('gce', nil, 'tar.gz'),
'ec2' => ImagefactoryMetadata.new('ec2', nil, 'vhd'),
'vsphere' => ImagefactoryMetadata.new('vsphere', 'vsphere', 'ova', nil),
'ovirt' => ImagefactoryMetadata.new('rhevm', 'rhevm', 'ova', nil),
'openstack' => ImagefactoryMetadata.new('openstack-kvm', nil, 'qc2', nil),
'hyperv' => ImagefactoryMetadata.new('hyperv', nil, 'vhd', nil),
'azure' => ImagefactoryMetadata.new('hyperv', nil, 'vhd', nil),
'vagrant' => ImagefactoryMetadata.new('vsphere', 'vagrant-virtualbox', 'box', nil),
'libvirt' => ImagefactoryMetadata.new('openstack-kvm', nil, 'qc2', nil),
'gce' => ImagefactoryMetadata.new('gce', nil, 'tar.gz', nil),
'ec2' => ImagefactoryMetadata.new('ec2', nil, 'vhd', nil),
}

attr_reader :name
Expand Down Expand Up @@ -41,6 +41,10 @@ def file_extension
TYPES.fetch(name).file_extension
end

def compression_type
TYPES.fetch(name).compression_type
end

def <=>(other)
name <=> other.name
end
Expand Down
28 changes: 23 additions & 5 deletions scripts/vmbuild.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def verify_run(output)
targets.sort.reverse.each do |target|
imgfac_target = target.imagefactory_type
ova_format = target.ova_format
compression = target.compression_type
$log.info "Building for #{target}:"

tdl_name = target.name == "azure" ? "base_azure.tdl" : "base.tdl"
Expand Down Expand Up @@ -144,12 +145,29 @@ def verify_run(output)
temp_file_uuid << uuid
end
$log.info "Built #{target} with final UUID: #{uuid}"
source = STORAGE_DIR.join("#{uuid}.body")

FileUtils.mkdir_p(destination_directory)
file_name = "#{name}-#{target}-#{build_label}-#{timestamp}-#{manageiq_checkout.commit_sha}.#{target.file_extension}"
destination = destination_directory.join(file_name)
$log.info `mv #{source} #{destination}`

Dir.chdir(STORAGE_DIR) do
FileUtils.mv("#{uuid}.body", file_name)

case compression
when 'gzip'
destination = destination.sub_ext(destination.extname + '.gz')
$log.info "Compressing #{file_name} to #{destination}"
$log.info `gzip -c #{file_name} > #{destination}`
FileUtils.rm_f(file_name)
when 'zip'
destination = destination.sub_ext('.zip')
$log.info "Compressing #{file_name} to #{destination}"
$log.info `zip -m -j #{destination} #{file_name}`
else
$log.info "Moving #{file_name} to #{destination}"
FileUtils.mv(file_name, destination)
end
end

if !File.exist?(destination)
$log.warn "Cannot find the target file #{destination}"
Expand All @@ -158,8 +176,8 @@ def verify_run(output)
if cli_options[:fileshare] && FILE_SERVER && File.size(destination)
$log.info "Creating File server #{FILE_SERVER} directory #{file_rdu_dir} ..."
$log.info `ssh #{FILE_SERVER_ACCOUNT}@#{FILE_SERVER} mkdir -p #{file_rdu_dir}`
$log.info "Copying file #{file_name} to #{FILE_SERVER}:#{file_rdu_dir}/ ..."
$log.info `scp #{destination} #{FILE_SERVER_ACCOUNT}@#{FILE_SERVER}:#{file_rdu_dir.join(file_name)}`
$log.info "Copying file #{destination} to #{FILE_SERVER}:#{file_rdu_dir}/ ..."
$log.info `scp #{destination} #{FILE_SERVER_ACCOUNT}@#{FILE_SERVER}:#{file_rdu_dir}`
end
end

Expand All @@ -172,7 +190,7 @@ def verify_run(output)
$log.info `qemu-img convert -f qcow2 -O qcow2 -o compat=0.10 -c #{STORAGE_DIR.join("#{target_image_uuid}.body")} #{source}`
$log.info `mv #{source} #{destination}`
if cli_options[:fileshare] && FILE_SERVER && File.size(destination)
$log.info `scp #{destination} #{FILE_SERVER_ACCOUNT}@#{FILE_SERVER}:#{file_rdu_dir.join(file_name)}`
$log.info `scp #{destination} #{FILE_SERVER_ACCOUNT}@#{FILE_SERVER}:#{file_rdu_dir}`
end
end

Expand Down

0 comments on commit f1412c2

Please sign in to comment.