Class: Bosh::AwsCliPlugin::RDS

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

Constant Summary collapse

DEFAULT_RDS_OPTIONS =
{
    :allocated_storage => 5,
    :db_instance_class => "db.m1.small",
    :engine => "mysql",
    :multi_az => true,
    :engine_version => "5.5.40a"
}
DEFAULT_RDS_PROTOCOL =
:tcp
DEFAULT_MYSQL_PORT =
3306

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ RDS

Returns a new instance of RDS.



16
17
18
19
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 16

def initialize(credentials)
  @credentials = credentials
  @aws_provider = AwsProvider.new(@credentials)
end

Instance Method Details

#aws_rdsObject



157
158
159
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 157

def aws_rds
  aws_provider.rds
end

#aws_rds_clientObject



161
162
163
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 161

def aws_rds_client
  aws_provider.rds_client
end

#create_database(name, subnet_ids, vpc_id, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 21

def create_database(name, subnet_ids, vpc_id, options = {})
  create_db_parameter_group('utf8')
  vpc = Bosh::AwsCliPlugin::VPC.find(Bosh::AwsCliPlugin::EC2.new(@credentials), vpc_id)
  create_vpc_db_security_group(vpc, name) if vpc.security_group_by_name(name).nil?
  create_subnet_group(name, subnet_ids) unless subnet_group_exists?(name)

  # symbolize options keys
  options = options.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }

  creation_options = DEFAULT_RDS_OPTIONS.merge(options)
  creation_options[:db_instance_identifier] = name
  creation_options[:db_name]              ||= name
  creation_options[:vpc_security_group_ids] = [vpc.security_group_by_name(name).id]
  creation_options[:db_subnet_group_name]   = name
  creation_options[:db_parameter_group_name]     = 'utf8'
  creation_options[:master_username]      ||= generate_user
  creation_options[:master_user_password] ||= generate_password
  response = aws_rds_client.create_db_instance(creation_options)
  response.data.merge(:master_user_password => creation_options[:master_user_password])
end

#create_db_parameter_group(name) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 93

def create_db_parameter_group(name)
  aws_rds_client.describe_db_parameter_groups(:db_parameter_group_name => name)
rescue AWS::RDS::Errors::DBParameterGroupNotFound
  aws_rds_client.create_db_parameter_group(:db_parameter_group_name => name,
  :db_parameter_group_family => 'mysql5.5', :description => name)
  aws_rds_client.modify_db_parameter_group(:db_parameter_group_name => name,
      :parameters => db_parameter_group_names)
end

#create_subnet_group(name, subnet_ids) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 108

def create_subnet_group(name, subnet_ids)
  aws_rds_client.create_db_subnet_group(
      :db_subnet_group_name => name,
      :db_subnet_group_description => name,
      :subnet_ids => subnet_ids
  )
end

#create_vpc_db_security_group(vpc, name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 128

def create_vpc_db_security_group(vpc, name)
  group_spec = {
      "name" => name,
      "ingress" => [
          {
              "ports" => DEFAULT_MYSQL_PORT,
              "protocol" => DEFAULT_RDS_PROTOCOL,
              "sources" => vpc.cidr_block,
          },
      ],
  }

  vpc.create_security_groups([group_spec])
end

#database(name) ⇒ Object



46
47
48
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 46

def database(name)
  databases.find { |v| v.id == name }
end

#database_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 50

def database_exists?(name)
  !database(name).nil?
end

#database_namesObject



58
59
60
61
62
63
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 58

def database_names
  databases.inject({}) do |memo, db_instance|
    memo[db_instance.id] = db_instance.name
    memo
  end
end

#databasesObject



42
43
44
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 42

def databases
  aws_rds.db_instances
end

#db_parameter_group_namesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 72

def db_parameter_group_names
  charset = 'utf8'
  param_names = %w(character_set_server
                   character_set_client
                   character_set_results
                   character_set_database
                   character_set_connection)

  params = param_names.map{|param_name| {:parameter_name => param_name,
                                :parameter_value => charset,
                                :apply_method => 'immediate'}}

  params << {:parameter_name => 'collation_connection',
                                         :parameter_value => 'utf8_unicode_ci',
                                         :apply_method => 'immediate'}
  params << {:parameter_name => 'collation_server',
                                     :parameter_value => 'utf8_unicode_ci',
                                     :apply_method => 'immediate'}
  params
end

#delete_databasesObject



54
55
56
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 54

def delete_databases
  databases.each { |db| db.delete(skip_final_snapshot: true) unless db.db_instance_status == "deleting" }
end

#delete_db_parameter_group(name) ⇒ Object



102
103
104
105
106
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 102

def delete_db_parameter_group(name)
  aws_rds_client.describe_db_parameter_groups(:db_parameter_group_name => name)
  aws_rds_client.delete_db_parameter_group(:db_parameter_group_name => name)
rescue AWS::RDS::Errors::DBParameterGroupNotFound
end

#delete_security_group(name) ⇒ Object



147
148
149
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 147

def delete_security_group(name)
  aws_rds_client.delete_db_security_group(:db_security_group_name => name)
end

#delete_security_groupsObject



151
152
153
154
155
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 151

def delete_security_groups
  security_group_names.each do |name|
    delete_security_group(name) unless name == "default"
  end
end

#delete_subnet_group(name) ⇒ Object



120
121
122
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 120

def delete_subnet_group(name)
  aws_rds_client.delete_db_subnet_group(:db_subnet_group_name => name)
end

#delete_subnet_groupsObject



124
125
126
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 124

def delete_subnet_groups
  subnet_group_names.each { |name| delete_subnet_group(name) }
end

#security_group_namesObject



143
144
145
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 143

def security_group_names
  aws_rds_client.describe_db_security_groups.data[:db_security_groups].map { |sg| sg[:db_security_group_name] }
end

#subnet_group_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 65

def subnet_group_exists?(name)
  aws_rds_client.describe_db_subnet_groups(:db_subnet_group_name => name)
  return true
rescue AWS::RDS::Errors::DBSubnetGroupNotFoundFault
  return false
end

#subnet_group_namesObject



116
117
118
# File 'lib/bosh_cli_plugin_aws/rds.rb', line 116

def subnet_group_names
  aws_rds_client.describe_db_subnet_groups.data[:db_subnet_groups].map { |sg| sg[:db_subnet_group_name] }
end