Class: ImageOptimize::Optimizer

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/app/image_optimizer.rb

Instance Method Summary collapse

Methods included from Command

#debug, #execute

Instance Method Details

#configureObject



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/app/image_optimizer.rb', line 57

def configure

  @log.debug("Config Parameters: #{@args}")

  # check for AWS credentials set
  raise 'Environment variable AWS_SECRET_KEY must be set' unless @args[:aws_secret_key]
  raise 'Environment variable AWS_ACCESS_KEY must be set' unless @args[:aws_access_key]

  # XXX: The fact that we're using two API clients should be hidden
  #      inside a RightScale 'manager' class.
  api_helper = RightScale::ApiClient15.new
  api_helper.log(@log)
  @instance_client = api_helper.get_client(:instance)
  @api_client = api_helper.get_client(:user, @args[:api_user], @args[:api_password])

  if @args[:aws_image_type] == "EBS"  # XXX: use an image bundle factory instead of if-else
    @log.debug("Creating ImageBundleEc2EBS object")
    @image_util =
      ImageBundleEc2EBS.new(
        @instance_client,        # XXX: instead of passing two api handles, just pass a manager class
        @api_client,
        @args[:aws_access_key],
        @args[:aws_secret_key],
        @args[:aws_kernel_id]
      )
  else
    @log.debug("Creating ImageBundleEc2S3 object")

    # check for credentials set in environment
    raise 'Environment variable AWS_S3_IMAGE_BUCKET must be set' unless @args[:aws_s3_image_bucket]
    raise 'Environment variable AWS_ACCOUNT_NUMBER must be set' unless @args[:aws_account_number]

    @image_util =
      ImageBundleEc2S3.new(
        @instance_client,
        @api_client,
        @args[:aws_access_key],
        @args[:aws_secret_key],
        @args[:aws_account_number],
        @args[:aws_s3_key_path],
        @args[:aws_s3_cert_path],
        @args[:aws_s3_image_bucket],
        @args[:aws_s3_bundle_directory],
        @args[:aws_s3_bundle_no_filter],
        @args[:aws_kernel_id]
      )
  end

  @image_util.log(@log)

end

#parse_argsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/app/image_optimizer.rb', line 109

def parse_args
  version = File.open(File.join(File.dirname(__FILE__), "..", "..","VERSION"), "r") { |f| f.read }

  @args = Trollop::options do
    version "image_optimize #{version} (c) 2014 RightScale, Inc."
    banner <<-EOS

Bundle a running VM into a new image that will be used on next launch

Usage:
   image_optimize [options]
where [options] are:
EOS
    opt :verbose, "If set will enable debug logging.  WARNING: will write ec2 creds to log if set!!",
        :long => "--verbose", :default => false

    opt :image_prefix, "Prefix to add to the optimized image name. Helps when searching for your optimized images.",
         :default => "optimized-image", :long => "--prefix", :type => String
    opt :image_description, "Description to add to optimized images",
       :default => "Cached image", :long => "--description", :type => String

    # Mangement platform API Creds
    opt :api_user, "RightScale Dashboard User email. Not needed if API_USER_EMAIL environment variable is set.",
       :default => ENV['API_USER_EMAIL'], :long => "--api-user", :short => "-u", :type => String
    opt :api_password, "RightScale Dashboard User email. Not needed if API_USER_PASSWORD environment variable is set.",
       :default => ENV['API_USER_PASSWORD'], :long => "--api-password", :short => "-p", :type => String
    opt :do_cleanup, "Don't do any cleanup on VM before snapshotting. Useful for debugging.", :long => "--cleanup", :default => true

    # EC2 Creds
    opt :aws_access_key, "EC2 Account Access Key. Not needed if AWS_ACCESS_KEY environment variable is set.",
       :default => ENV['AWS_ACCESS_KEY'], :long => "--aws-access-key", :short => "-k"
    opt :aws_secret_key, "EC2 Account Secret. Not needed if AWS_SECRET_KEY environment variable is set.",
       :default => ENV['AWS_SECRET_KEY'], :long => "--aws-secret-key", :short => "-s"
    opt :aws_account_number, "EC2 Account ID. Not needed if AWS_ACCOUNT_NUMBER environment variable is set. Required for S3 images only.",
       :default => ENV['AWS_ACCOUNT_NUMBER'], :long => "--aws-account-number"

    # EC2 Image type
    opt :aws_image_type, "The type of image to create from this VM. Must be either 'EBS' or 'S3'. ",
       :default => 'EBS', :long => "--aws-image-type", :type => String
    opt :aws_kernel_id, "Kernel to use instead of what the VM is running. i.e. 'aki-fc8f11cc'. For a current list of IDs see http://goo.gl/dOS0mB.",
       :default => nil, :long => "--aws-kernel-id", :type => String

    # EC2 Instance Store Parameters
    opt :aws_s3_key_path, "location to file containing EC2 account key. S3 images only.",
       :default => '/tmp/certs/x509.key', :long => "--aws-s3-key-path"
    opt :aws_s3_cert_path, "location to file containing EC2 account cert. S3 images only.",
       :default => '/tmp/certs/x509.cert', :long => "--aws-s3-cert-path"
    opt :aws_s3_image_bucket, "The bucket name for optimized S3 images (must be url safe). S3 images only",
       :default => "optimized-images", :long => "--aws-s3-image-bucket"
    opt :aws_s3_bundle_directory, "The local directory where the image bundle will be stored before uploading to S3. NOTE: this must have enough free space to hold the image bundle.",
       :default => "/mnt/ephemeral/bundle", :long => "--aws-s3-bundle-directory"
    opt :aws_s3_bundle_no_filter, "If set, will disable the default filtering used by the ec2-bundle-vol command. WARNING: setting this option could leave ssh keys or other secrets on your",
       :default => false, :long => "--aws-s3-bundle-no-filter"
  end
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/app/image_optimizer.rb', line 32

def run
  parse_args
  setup_logging
  configure

  # "cleaning" should wipe user-data and meta-data which will only allow this script
  # to be run once.  Default to false to so we can run this multiple times to make
  # debugging more efficient.
  # XXX: this probably should be done by the manager class
  prepare_image if @args[:do_cleanup]

  # generate unique image name and description for our image
  name = unique_name(@args[:image_prefix])
  description = @args[:image_description]

  # snapshot and register new image
  @log.info "Snapshot instance..."
  @image_util.snapshot_instance(name, description)
  @log.info "Register image..."
  @image_util.register_image(name, description)
  @log.info "Add image to next instance..."
  @image_util.add_image_to_next_instance
  @log.info "SUCCESS!"
end