~awstools-dev/ubuntu/maverick/ec2-ami-tools/maverick

« back to all changes in this revision

Viewing changes to lib/ec2/amitools/unbundleparameters.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-10-14 08:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20081014083525-c0n69wr7r7aqfb8w
Tags: 1.3-26357-0ubuntu2
* New upstream version.
* Update the debian copyright file.
* Added quilt patch system to make it easier to maintain. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008 Amazon.com, Inc. or its affiliates.  All Rights
 
2
# Reserved.  Licensed under the Amazon Software License (the
 
3
# "License").  You may not use this file except in compliance with the
 
4
# License. A copy of the License is located at
 
5
# http://aws.amazon.com/asl or in the "license" file accompanying this
 
6
# file.  This file is distributed on an "AS IS" BASIS, WITHOUT
 
7
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 
8
# the License for the specific language governing permissions and
 
9
# limitations under the License.
 
10
 
 
11
require 'optparse'
 
12
require 'ec2/amitools/version'
 
13
 
 
14
class UnbundleParameters < OptionParser
 
15
  
 
16
  MANIFEST_DESCRIPTION    = "The path to the AMI manifest file."
 
17
  USER_PK_PATH_DESCRIPTION= "The path to the user's PEM encoded private key."
 
18
  SOURCE_DESCRIPTION      = 'The directory containing bundled AMI parts to unbundle. Defaults to ".".'
 
19
  DESTINATION_DESCRIPTION = 'The directory to unbundle the AMI into. Defaults to the ".".'
 
20
  DEBUG_DESCRIPTION       = "Print debug messages."
 
21
  HELP_DESCRIPTION        = "Display this help message and exit."
 
22
  VERSION_DESCRIPTION     = "Display the version and copyright notice and then exit."
 
23
  
 
24
  attr_accessor :manifest,
 
25
                :user_pk_path,
 
26
                :source,
 
27
                :destination,
 
28
                :show_help,
 
29
                :debug
 
30
  
 
31
  class Error < RuntimeError  
 
32
    class MissingMandatory < Error
 
33
      def initialize( name )
 
34
        super( "missing mandatory parameter: #{name}" )
 
35
      end
 
36
    end
 
37
    
 
38
    class InvalidValue < Error
 
39
      def initialize( name, value )
 
40
        super( "#{name} value invalid: #{value.to_s}" )
 
41
      end
 
42
    end
 
43
  end
 
44
  
 
45
  def initialize( argv, name )
 
46
    super( argv )
 
47
    
 
48
    self.banner = "Usage: #{name} PARAMETERS"
 
49
    
 
50
    #
 
51
    # Mandatory parameters.
 
52
    #
 
53
    separator( "" )
 
54
    separator( "MANDATORY PARAMETERS" )
 
55
    
 
56
    on( '-m', '--manifest PATH', String, MANIFEST_DESCRIPTION ) do |manifest|
 
57
      unless ( File::exist?( manifest ) and File::file?( manifest ) )
 
58
        raise Error::InvalidValue.new( "--manifest", manifest )
 
59
      end
 
60
      @manifest = manifest
 
61
    end
 
62
    
 
63
    on( '-k', '--privatekey PATH', String, USER_PK_PATH_DESCRIPTION ) do |p|
 
64
      unless File:: exist?( p ) and File::file?( p )
 
65
        raise Error::InvalidValue.new( '--privatekey', p )
 
66
      end
 
67
      @user_pk_path = p
 
68
    end
 
69
    
 
70
    #
 
71
    # Optional parameters.
 
72
    #
 
73
    self.separator( "" )
 
74
    self.separator( "OPTIONAL PARAMETERS" )
 
75
    
 
76
    on( '-s', '--source DIRECTORY', String, SOURCE_DESCRIPTION ) do |directory|
 
77
      unless File::exist?( directory ) and File::directory?( directory )
 
78
        raise Error::InvalidValue.new( '--source', directory ) 
 
79
      end
 
80
      @source = directory
 
81
    end  
 
82
    
 
83
    on( '-d', '--destination DIRECTORY', String, DESTINATION_DESCRIPTION ) do |directory|
 
84
      unless File::exist?( directory ) and File::directory?( directory )
 
85
        raise Error::InvalidValue.new( '--destination', directory ) 
 
86
      end
 
87
      @destination = directory
 
88
    end
 
89
    
 
90
    on( '--debug', DEBUG_DESCRIPTION ) do
 
91
      @debug = true
 
92
    end
 
93
    
 
94
    on( '-h', '--help', HELP_DESCRIPTION ) do
 
95
      @show_help = true
 
96
    end
 
97
    
 
98
    on( '--version', VERSION_DESCRIPTION ) do
 
99
      puts version_copyright_string()
 
100
      exit
 
101
    end
 
102
 
 
103
    #
 
104
    # Parse the command line parameters.
 
105
    #
 
106
    parse!( argv )
 
107
    
 
108
    unless @show_help
 
109
      #
 
110
      # Verify mandatory parameters.
 
111
      #
 
112
      raise Error::MissingMandatory.new( '--manifest' ) unless @manifest
 
113
      raise Error::MissingMandatory.new( '--privatekey' ) unless @user_pk_path
 
114
      
 
115
      #
 
116
      # Set defaults for optional parameters.
 
117
      #
 
118
      @source =  Dir::pwd() unless @source
 
119
      @destination =  Dir::pwd() unless @destination
 
120
    end
 
121
  end
 
122
end