Skip to content

Commit

Permalink
Merge pull request #5 from oyelekci/cluster00
Browse files Browse the repository at this point in the history
Add db cluster support
  • Loading branch information
KashifSaadat authored Apr 8, 2020
2 parents 49c27bf + df4381b commit dff4379
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
26 changes: 26 additions & 0 deletions lib/rds_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def db_instances
@rds_client.describe_db_instances.db_instances
end

def db_clusters
@rds_client.describe_db_clusters.db_clusters
end

def db_tags(db_arn)
@rds_client.list_tags_for_resource(
resource_name: db_arn
Expand Down Expand Up @@ -41,4 +45,26 @@ def stop_db_instance(db_name, db_status, db_schedule, schedule_type)
@logger.warn "DB Instance '#{db_name}' is not in a running state, not taking action (status: #{db_status}, #{schedule_type} schedule: '#{db_schedule}')"
end
end

def start_db_cluster(db_cluster_identifier, db_status, db_schedule, schedule_type)
if db_status.eql? 'stopped'
@logger.info "Starting DB Cluster '#{db_cluster_identifier}' (#{schedule_type} schedule: '#{db_schedule}', dry_run: #{@dry_run})"
@rds_client.start_db_cluster(db_cluster_identifier: db_cluster_identifier) unless @dry_run
elsif db_status.eql? 'available'
@logger.info "DB Cluster '#{db_cluster_identifier}' is currently available (#{schedule_type} schedule: '#{db_schedule}')"
else
@logger.warn "DB Cluster '#{db_cluster_identifier}' is not in a stopped state, not taking action (status: #{db_status}, #{schedule_type} schedule: '#{db_schedule}')"
end
end

def stop_db_cluster(db_cluster_identifier, db_status, db_schedule, schedule_type)
if %w[stopping stopped].include?(db_status)
@logger.info "DB Cluster '#{db_cluster_identifier}' is currently stopped (status: #{db_status}, #{schedule_type} schedule: '#{db_schedule}')"
elsif db_status.eql? 'available'
@logger.info "Stopping DB Cluster '#{db_cluster_identifier}' (#{schedule_type} schedule: '#{db_schedule}', dry_run: #{@dry_run})"
@rds_client.stop_db_cluster(db_cluster_identifier: db_cluster_identifier) unless @dry_run
else
@logger.warn "DB Cluster '#{db_cluster_identifier}' is not in a running state, not taking action (status: #{db_status}, #{schedule_type} schedule: '#{db_schedule}')"
end
end
end
14 changes: 10 additions & 4 deletions lib/rds_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def execute
break
end

process_schedule(rds.db_instance_identifier, rds.db_instance_status, db_schedule, downtime_schedule)
process_schedule(rds.db_cluster_identifier, rds.db_instance_identifier, rds.db_instance_status, db_schedule, downtime_schedule)
end

break if @run_once
Expand All @@ -48,7 +48,7 @@ def execute
end
end

def process_schedule(db_name, db_status, db_schedule, downtime_schedule)
def process_schedule(db_cluster_identifier, db_name, db_status, db_schedule, downtime_schedule)
if db_schedule
begin
parsed_schedule = @time_parser.parse_schedule(db_schedule)
Expand All @@ -58,9 +58,15 @@ def process_schedule(db_name, db_status, db_schedule, downtime_schedule)
schedule_type = downtime_schedule ? 'downtime' : 'uptime'
begin
if (@time_parser.schedule_active?(parsed_schedule) && !downtime_schedule) || (!@time_parser.schedule_active?(parsed_schedule) && downtime_schedule)
@rds_client.start_db_instance(db_name, db_status, db_schedule, schedule_type)
else
if db_cluster_identifier.nil?
@rds_client.start_db_instance(db_name, db_status, db_schedule, schedule_type)
else
@rds_client.start_db_cluster(db_cluster_identifier, db_status, db_schedule, schedule_type)
end
elsif db_cluster_identifier.nil?
@rds_client.stop_db_instance(db_name, db_status, db_schedule, schedule_type)
else
@rds_client.stop_db_cluster(db_cluster_identifier, db_status, db_schedule, schedule_type)
end
rescue TimeScheduleParser::TimezoneInvalid => e
@logger.error "Error processing Time Schedule for DB Instance '#{db_name}': #{e.message}"
Expand Down

0 comments on commit dff4379

Please sign in to comment.