Class: Hugo::Aws::Rds

Inherits:
Object
  • Object
show all
Defined in:
lib/hugo/aws/rds.rb

Constant Summary collapse

ACCESS_KEY =
ENV['AMAZON_ACCESS_KEY_ID']
SECRET_KEY =
ENV['AMAZON_SECRET_ACCESS_KEY']
DEFAULT_SIZE =
5
INSTANCE_CLASS =
"db.m1.small"
ZONE =
"us-east-1c"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rds

Returns a new instance of Rds.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hugo/aws/rds.rb', line 17

def initialize(options={})
  # create instance
  
  @server = options[:server] || options["DBInstanceIdentifier"]
  @db = options[:name] #|| options["DBName"]
  @user = options[:user] || options["MasterUsername"]
  @password = options[:password] if options[:password]
  @size = options[:size] || options["AllocatedStorage"] || DEFAULT_SIZE
  @instance_class = options[:instance_class] || options["DBInstanceClass"] || INSTANCE_CLASS
  @zone = options[:zone] || options["AvailabilityZone"] || ZONE
  @status = options["DBInstanceStatus"] || "pending"
  @create_time = options["InstanceCreateTime"] || nil
  if options["Endpoint"] and options["Endpoint"]["Address"]
    @uri = options["Endpoint"]["Address"] || nil 
  end
  @db_security_group = options[:db_security_group] || "default"
  if options["DBSecurityGroups"] and options["DBSecurityGroups"]["DBSecurityGroup"]
    @db_security_group = options["DBSecurityGroups"]["DBSecurityGroup"]["DBSecurityGroupName"] 
  end
  @aws_access_key_id = options[:aws_access_key_id] || ACCESS_KEY
  @aws_secret_access_key = options[:aws_secret_access_key] || SECRET_KEY
  
end

Instance Attribute Details

#aws_access_key_idObject

Returns the value of attribute aws_access_key_id.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def aws_access_key_id
  @aws_access_key_id
end

#aws_secret_access_keyObject

Returns the value of attribute aws_secret_access_key.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def aws_secret_access_key
  @aws_secret_access_key
end

#create_timeObject

Returns the value of attribute create_time.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def create_time
  @create_time
end

#dbObject

Returns the value of attribute db.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def db
  @db
end

#db_security_groupObject

Returns the value of attribute db_security_group.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def db_security_group
  @db_security_group
end

#instance_classObject

Returns the value of attribute instance_class.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def instance_class
  @instance_class
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def password
  @password
end

#serverObject

Returns the value of attribute server.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def server
  @server
end

#sizeObject

Returns the value of attribute size.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def size
  @size
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def status
  @status
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def uri
  @uri
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def user
  @user
end

#zoneObject

Returns the value of attribute zone.



11
12
13
# File 'lib/hugo/aws/rds.rb', line 11

def zone
  @zone
end

Class Method Details

.all(aws_access_key_id, aws_secret_access_key) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hugo/aws/rds.rb', line 65

def self.all(aws_access_key_id, aws_secret_access_key)
  @rds = AWS::RDS::Base.new(:access_key_id => aws_access_key_id, :secret_access_key => aws_secret_access_key)
  instances = @rds.describe_db_instances.DescribeDBInstancesResult.DBInstances.DBInstance

  if instances.kind_of?(Array)
    #instances.map { |i| self.get_from_aws(i) }
    instances.map { |i| self.new(i) }
  else
     #self.get_from_aws(instances)      
     [ self.new(instances) ]
  end
end

.find(instance, aws_access_key_id, aws_secret_access_key) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hugo/aws/rds.rb', line 78

def self.find(instance, aws_access_key_id, aws_secret_access_key)
  # find instance
  @rds = AWS::RDS::Base.new(:access_key_id => aws_access_key_id, :secret_access_key => aws_secret_access_key)
  @rds_instance = @rds.describe_db_instances(:db_instance_identifier => instance)
  instance_desc = @rds_instance.DescribeDBInstancesResult.DBInstances.DBInstance  
  # initialize Hugo::Rds Object with instance hash
  self.new(instance_desc)
rescue
  # AWS Can't find db instance called ????
  nil
end

.find_or_create(options) ⇒ Object



90
91
92
93
94
95
# File 'lib/hugo/aws/rds.rb', line 90

def self.find_or_create(options)
  rds = self.find(options[:server], options[:aws_access_key_id], options[:aws_secret_access_key]) || self.new(options).create
  rds.db = options[:name]
  rds.password = options[:password]
  rds
end

Instance Method Details

#authorize_security_group(db_sec, ec2_sec, owner_id) ⇒ Object



97
98
99
100
# File 'lib/hugo/aws/rds.rb', line 97

def authorize_security_group(db_sec, ec2_sec, owner_id)
  #@rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
  rds.authorize_db_security_group(:db_security_group_name => db_sec, :ec2_security_group_name => ec2_sec, :ec2_security_group_owner_id => owner_id)
end

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hugo/aws/rds.rb', line 41

def create
  
  rds.create_db_instance(
    :db_instance_identifier => self.server,
    :allocated_storage => self.size,
    :db_instance_class => self.instance_class,
    :engine => "MySQL5.1",
    :master_username => self.user,
    :master_user_password => self.password,
    :db_name => self.db,
    :availability_zone => self.zone
    #, :db_security_groups => @db_security_group
    ) unless self.create_time
  self
end

#destroyObject



61
62
63
# File 'lib/hugo/aws/rds.rb', line 61

def destroy
  rds.delete_db_instance(:db_instance_identifier => self.server, :skip_final_snapshot => true)
end

#destroy_db_security_group(name) ⇒ Object



113
114
115
# File 'lib/hugo/aws/rds.rb', line 113

def destroy_db_security_group(name)
  rds.delete_db_security_group(:db_security_group_name => name)
end

#find_or_create_db_security_group(name, description) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/hugo/aws/rds.rb', line 102

def find_or_create_db_security_group(name, description)
  #@rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
  begin
    @security_groups = rds.describe_db_security_groups(:db_security_group_name => name)
  rescue
    @security_groups = rds.create_db_security_group(:db_security_group_name => name, :db_security_group_description => description)
  end
  #puts @security_groups
  @security_groups
end

#saveObject



57
58
59
# File 'lib/hugo/aws/rds.rb', line 57

def save
  create 
end