Class: CloudMaker::S3Archiver

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_maker/s3_archiver.rb

Constant Summary collapse

KEY_PREFIX =

Public: All archive keys will be prefixed with KEY_PREFIX/

"cloud-maker"
INSTANCE_YAML =
'instance.yaml'
CLOUD_CONFIG_YAML =
'cloud_config.yaml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3Archiver

Public: Creates a new S3 Archiver instance

options - S3 configuration options

:aws_access_key_id     - (required) The AWS access key
:aws_secret_access_key - (required) The AWS secret
:bucket_name           - (required) The bucket for the archiver to access
:instance_id           - (required) The AWS instance ID the archive describes

Returns a new CloudMaker::S3Archiver instance Raises RuntimeError if any of the required options are not specified

Raises:

  • (RuntimeError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloud_maker/s3_archiver.rb', line 30

def initialize(options)
  required_keys = [:aws_access_key_id, :aws_secret_access_key, :instance_id, :bucket_name]
  unless (required_keys - options.keys).empty?
    raise RuntimeError.new("Instantiated #{self.class} without required attributes: #{required_keys - options.keys}.")
  end

  self.instance_id = options[:instance_id]
  self.aws_access_key_id = options[:aws_access_key_id]
  self.aws_secret_access_key = options[:aws_secret_access_key]

  self.bucket = AWS::S3.new(
    :access_key_id => self.aws_access_key_id,
    :secret_access_key => self.aws_secret_access_key
  ).buckets[options[:bucket_name]]

  raise RuntimeError.new("The S3 bucket #{options[:bucket_name]} does not exist.") unless self.bucket.exists?
end

Instance Attribute Details

#aws_access_key_idObject

Public: Gets/Sets the AWS secret.



7
8
9
# File 'lib/cloud_maker/s3_archiver.rb', line 7

def aws_access_key_id
  @aws_access_key_id
end

#aws_secret_access_keyObject

Public: Gets/Sets the AWS access key.



5
6
7
# File 'lib/cloud_maker/s3_archiver.rb', line 5

def aws_secret_access_key
  @aws_secret_access_key
end

#bucketObject

Internal: Gets/Sets the bucket object used for storing/loading archives.



11
12
13
# File 'lib/cloud_maker/s3_archiver.rb', line 11

def bucket
  @bucket
end

#instance_idObject

Public: Gets/Sets the EC2 instance ID string.



9
10
11
# File 'lib/cloud_maker/s3_archiver.rb', line 9

def instance_id
  @instance_id
end

Instance Method Details

#cloud_config_yaml_keyObject

Internal: Returns the key for the cloud config yaml file



85
86
87
# File 'lib/cloud_maker/s3_archiver.rb', line 85

def cloud_config_yaml_key
  self.prefix_key('cloud_config.yaml')
end

#instance_yaml_keyObject

Internal: Returns the key for the instance yaml file



80
81
82
# File 'lib/cloud_maker/s3_archiver.rb', line 80

def instance_yaml_key
  self.prefix_key('instance.yaml')
end

#load_archiveObject

Public: Retrieves a previously created archive from S3

Returns the content of the archive.



66
67
68
69
70
71
72
# File 'lib/cloud_maker/s3_archiver.rb', line 66

def load_archive
  {
    :user_data => self.bucket.objects[self.user_data_key].read,
    :cloud_config => YAML::load(self.bucket.objects[self.cloud_config_yaml_key].read),
    :instance => YAML::load(self.bucket.objects[self.instance_yaml_key].read)
  }
end

#prefix_key(key) ⇒ Object

Public: Returns the key that the archive will be stored under



91
92
93
94
95
96
97
# File 'lib/cloud_maker/s3_archiver.rb', line 91

def prefix_key(key)
  if self.instance_id
    [KEY_PREFIX, self.instance_id, key].join('/')
  else
    raise RuntimeError.new("Attempted to generate a key name without an instance id.")
  end
end

#store_archive(cloud_maker_config, properties) ⇒ Object

Public: Generates an archive with all information relevant to an instance launch and stores it to S3.

cloud_maker_config - The CloudMaker::Config the instance was launched with properties - A Hash describing the properties of the launched instance

Returns nothing.



55
56
57
58
59
60
61
# File 'lib/cloud_maker/s3_archiver.rb', line 55

def store_archive(cloud_maker_config, properties)
  userdata = cloud_maker_config.to_user_data
  self.bucket.objects.create(self.user_data_key, :data => userdata)
  self.bucket.objects.create(self.instance_yaml_key, :data => properties.to_yaml)
  self.bucket.objects.create(self.cloud_config_yaml_key, :data => cloud_maker_config.to_hash.to_yaml)
  true
end

#user_data_keyObject

Internal: Returns the key for the user_data file



75
76
77
# File 'lib/cloud_maker/s3_archiver.rb', line 75

def user_data_key
  self.prefix_key('user_data')
end