-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
80 lines (67 loc) · 1.65 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'aws-sdk'
require 'dotenv'
require 'pp'
def create_snapshot
begin
resp = $redshift.create_cluster_snapshot({
snapshot_identifier: ENV["CLUSTEER_NAME"] + "-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S-%Z")}",
cluster_identifier: ENV["CLUSTEER_NAME"]
})
pp resp
rescue => e
pp e
end
end
def restore_cluster
begin
resp = $redshift.describe_cluster_snapshots({
cluster_identifier: ENV["CLUSTEER_NAME"],
start_time: Time.now.strftime("%Y-%m-%d")
})
if resp.snapshots.empty?
raise "Can't get snapshots."
end
snapshots = resp.snapshots.select{|e| !e.snapshot_identifier.start_with?("rs:")}
snapshots.sort_by{ |x| x.snapshot_create_time }.reverse!
resp = $redshift.restore_from_cluster_snapshot({
cluster_identifier: ENV["ANALYSIS_CLUSTER_NAME"],
snapshot_cluster_identifier: ENV["CLUSTEER_NAME"],
snapshot_identifier: snapshots[0].snapshot_identifier
})
pp resp
rescue => e
pp e
end
end
def delete_cluster
begin
resp = $redshift.delete_cluster({
cluster_identifier: ENV["ANALYSIS_CLUSTER_NAME"],
skip_final_cluster_snapshot: true
})
pp resp
rescue => e
pp e
end
end
# Main
begin
Dotenv.load
# Credentials
$redshift = Aws::Redshift::Client.new(
region: ENV["AWS_REGION"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
)
if ARGV[0] == "create_snapshot" then
create_snapshot
elsif ARGV[0] == "restore_cluster" then
restore_cluster
elsif ARGV[0] == "delete_cluster" then
delete_cluster
else
raise "Illegal argument."
end
rescue => e
pp e
end