Class: Unbundler

Inherits:
AMITool show all
Defined in:
lib/ec2/amitools/unbundle.rb

Constant Summary

Constants inherited from AMITool

AMITool::BACKOFF_PERIOD, AMITool::MAX_TRIES, AMITool::PROMPT_TIMEOUT

Instance Method Summary collapse

Methods inherited from AMITool

#get_parameters, #handle_early_exit_parameters, #interactive?, #interactive_prompt, #retry_s3, #run, #warn_confirm

Instance Method Details

#get_manualObject

——————————————————————————# Overrides ——————————————————————————#



88
89
90
# File 'lib/ec2/amitools/unbundle.rb', line 88

def get_manual()
  UNBUNDLE_MANUAL
end

#get_nameObject



92
93
94
# File 'lib/ec2/amitools/unbundle.rb', line 92

def get_name()
  UNBUNDLE_NAME
end

#main(p) ⇒ Object



96
97
98
# File 'lib/ec2/amitools/unbundle.rb', line 96

def main(p)
  unbundle(p)
end

#unbundle(p) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ec2/amitools/unbundle.rb', line 30

def unbundle(p)
  begin
    manifest_path = p.manifest_path
    src_dir = p.source
    dst_dir = p.destination
    
    digest_pipe = File::join( '/tmp', "ec2-unbundle-image-digest-pipe" )
    File::delete( digest_pipe ) if File::exist?( digest_pipe )
    unless system( "mkfifo #{digest_pipe}" )
      raise "error creating named pipe #{digest_pipe}"
    end
    
    # Load manifest and the user's private key.
    manifest = ManifestV3.new(File.open( manifest_path ) { |f| f.read() })
    pk = Crypto::loadprivkey( p.user_pk_path )
    
    # Extract key and IV from XML manifest.
    key = pk.private_decrypt(Format::hex2bin( manifest.user_encrypted_key))
    iv = pk.private_decrypt(Format::hex2bin( manifest.user_encrypted_iv))
    
    # Create a string of space separated part paths.
    part_files = manifest.parts.collect do |part| 
      File::join( src_dir, part.filename ) 
    end.join( ' ' )
    
    # Join, decrypt, decompress and untar.
    untar = EC2::Platform::Current::Tar::Command.new.extract.chdir(dst_dir)
    pipeline = EC2::Platform::Current::Pipeline.new('image-unbundle-pipeline', @debug)
    pipeline.concat([
                     ['cat', "openssl sha1 < #{digest_pipe} & cat #{part_files}"],
                     ['decrypt', "openssl enc -d -aes-128-cbc -K #{key} -iv #{iv}"],
                     ['gunzip', "gunzip"],
                     ['tee', "tee #{digest_pipe}"],
                     ['untar', untar.expand]
                    ])
    digest = nil
    begin
      digest = pipeline.execute.split(/\s+/).last.strip
    rescue EC2::Platform::Current::Pipeline::ExecutionError => e
      $stderr.puts e.message
    end
    
    # Verify digest.
    unless manifest.digest == digest
      raise "invalid digest, expected #{manifest.digest} received #{digest}"
    end
    
    puts "Unbundle complete."
    return 0
  ensure
    File::delete( digest_pipe ) if (digest_pipe && File::exist?( digest_pipe ))
  end
end