Class: Stax::Cmd::Rds

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/rds.rb

Constant Summary collapse

COLORS =
{
  available: :green,
  'in-sync': :green,
  Complete:  :green,
  Active:    :green,
}

Instance Method Summary collapse

Methods inherited from SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#clustersObject



70
71
72
73
74
75
# File 'lib/stax/mixin/rds.rb', line 70

def clusters
  debug("RDS DB clusters for #{my.stack_name}")
  print_table stack_rds_clusters.map { |c|
    [c.db_cluster_identifier, c.engine, c.engine_version, color(c.status), c.cluster_create_time]
  }
end

#endpointsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stax/mixin/rds.rb', line 97

def endpoints
  stack_rds_clusters.each do |c|
    debug("RDS DB endpoints for cluster #{c.db_cluster_identifier}")
    print_table [
      ['writer', c.endpoint,        c.port, c.hosted_zone_id],
      ['reader', c.reader_endpoint, c.port, c.hosted_zone_id],
    ]
  end

  debug("RDS DB instance endpoints for #{my.stack_name}")
  print_table stack_rds_instances.map { |i|
    [i.db_instance_identifier, i.endpoint&.address, i.endpoint&.port, i.endpoint&.hosted_zone_id]
  }
end

#eventsObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/stax/mixin/rds.rb', line 149

def events
  stack_db_clusters.map(&:physical_resource_id).each do |id|
    debug("Events for cluster #{id}")
    print_rds_events(duration: options[:duration], source_type: 'db-cluster', source_identifier: id)
  end

  stack_db_instances.map(&:physical_resource_id).each do |id|
    debug("Events for instance #{id}")
    print_rds_events(duration: options[:duration], source_type: 'db-instance', source_identifier: id)
  end
end

#failoverObject



126
127
128
129
130
131
132
133
# File 'lib/stax/mixin/rds.rb', line 126

def failover
  stack_rds_clusters.each do |c|
    if yes?("Failover #{c.db_cluster_identifier}?", :yellow)
      resp = Aws::Rds.client.failover_db_cluster(db_cluster_identifier: c.db_cluster_identifier, target_db_instance_identifier: options[:target])
      puts "failing over #{resp.db_cluster.db_cluster_identifier}"
    end
  end
end

#instancesObject



89
90
91
92
93
94
# File 'lib/stax/mixin/rds.rb', line 89

def instances
  debug("RDS DB instances for #{my.stack_name}")
  print_table stack_rds_instances.map { |i|
    [i.db_instance_identifier, i.engine, i.engine_version, color(i.db_instance_status), i.db_instance_class, i.db_subnet_group&.vpc_id, i.availability_zone]
  }
end

#lsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stax/mixin/rds.rb', line 54

def ls
  debug("RDS databases for #{my.stack_name}")
  stack_rds_clusters.map do |c|
    cluster = [ c.db_cluster_identifier, 'cluster', color(c.status), c.engine ]
    instances = c.db_cluster_members.map do |m|
      role = m.is_cluster_writer ? 'writer' : 'reader'
      i = Aws::Rds.instances(filters: [ { name: 'db-instance-id', values: [ m.db_instance_identifier ] } ]).first
      [ '- ' + i.db_instance_identifier, role, color(i.db_instance_status), i.engine, i.availability_zone, i.db_instance_class ]
    end
    [ cluster ] + instances
  end.flatten(1).tap do |list|
    print_table list
  end
end

#membersObject



78
79
80
81
82
83
84
85
86
# File 'lib/stax/mixin/rds.rb', line 78

def members
  stack_rds_clusters.each do |c|
    debug("RDS DB members for cluster #{c.db_cluster_identifier}")
    print_table c.db_cluster_members.map { |m|
      role = m.is_cluster_writer ? 'writer' : 'reader'
      [m.db_instance_identifier, role, m.db_cluster_parameter_group_status]
    }
  end
end

#snapshotsObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/stax/mixin/rds.rb', line 136

def snapshots
  stack_db_clusters.map(&:physical_resource_id).each do |id|
    debug("Snapshots for cluster #{id}")
    Aws::Rds.client.describe_db_cluster_snapshots(db_cluster_identifier: id).map(&:db_cluster_snapshots).flatten.map do |s|
      [ s.db_cluster_snapshot_identifier, s.snapshot_create_time, "#{s.allocated_storage}G", color(s.status), s.snapshot_type ]
    end.tap do |list|
      print_table list
    end
  end
end

#subnetsObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/stax/mixin/rds.rb', line 113

def subnets
  stack_db_subnet_groups.map do |r|
    Aws::Rds.subnet_groups(db_subnet_group_name: r.physical_resource_id)
  end.flatten.each do |g|
    debug("Subnets for group #{g.db_subnet_group_name}")
    print_table g.subnets.map { |s|
      [s&.subnet_availability_zone&.name, s&.subnet_identifier, color(s&.subnet_status)]
    }
  end
end

#write_forwardingObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/stax/mixin/rds.rb', line 164

def write_forwarding
  stack_db_clusters.map(&:physical_resource_id).each do |cluster|
    if options[:enable]
      puts "#{cluster} enabling write-forwarding"
      Aws::Rds.client.modify_db_cluster(db_cluster_identifier: cluster, enable_global_write_forwarding: true)
    elsif options[:disable]
      puts "#{cluster} disabling write-forwarding"
      Aws::Rds.client.modify_db_cluster(db_cluster_identifier: cluster, enable_global_write_forwarding: false)
    else
      print_table Aws::Rds.client.describe_db_clusters(db_cluster_identifier: cluster).db_clusters.map { |c|
        [ c.db_cluster_identifier, c.global_write_forwarding_status || '-' ]
      }
    end
  end
end