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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hugo/aws/rds.rb', line 14

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
  
end

Instance Attribute Details

#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

.allObject



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

def self.all
  @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_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) ⇒ Object



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

def self.find(instance)
  # find instance
  @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_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



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

def self.find_or_create(options)
  rds = self.find(options[:server]) || 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



99
100
101
102
# File 'lib/hugo/aws/rds.rb', line 99

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



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

def create
  @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)      
  
  # TODO Create DB Security Group wheh Amazon Bug Fixed! -tnw
  # if @db_security_group
  #   find_or_create_db_security_group(@db_security_group, @db_security_group)
  # end
  
  @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



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

def destroy
  @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)      
  @rds.delete_db_instance(:db_instance_identifier => self.server, :skip_final_snapshot => true)
end

#destroy_db_security_group(name) ⇒ Object



115
116
117
118
# File 'lib/hugo/aws/rds.rb', line 115

def destroy_db_security_group(name)
  @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
  @rds.delete_db_security_group(:db_security_group_name => name)
end

#find_or_create_db_security_group(name, description) ⇒ Object



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

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



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

def save
  create 
end