Class: DatadogBackup::Cli
- Inherits:
-
Object
- Object
- DatadogBackup::Cli
show all
- Includes:
- Options
- Defined in:
- lib/datadog_backup/cli.rb
Instance Method Summary
collapse
Methods included from Options
#action, #backup_dir, #client, #concurrency_limit, #datadog_api_key, #datadog_app_key, #diff_format, #force_restore, #logger, #output_format, #resources
Constructor Details
#initialize(options) ⇒ Cli
Returns a new instance of Cli.
88
89
90
91
|
# File 'lib/datadog_backup/cli.rb', line 88
def initialize(options)
@options = options
initialize_client
end
|
Instance Method Details
#all_diff_futures ⇒ Object
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/datadog_backup/cli.rb', line 11
def all_diff_futures
logger.info("Starting diffs on #{::DatadogBackup::ThreadPool::TPOOL.max_length} threads")
any_resource_instance
.all_file_ids_for_selected_resources
.map do |id|
Concurrent::Promises.future_on(::DatadogBackup::ThreadPool::TPOOL, id) do |id|
[id, getdiff(id)]
end
end
end
|
#any_resource_instance ⇒ Object
22
23
24
|
# File 'lib/datadog_backup/cli.rb', line 22
def any_resource_instance
resource_instances.first
end
|
#backup ⇒ Object
26
27
28
29
30
|
# File 'lib/datadog_backup/cli.rb', line 26
def backup
resource_instances.each(&:purge)
resource_instances.each(&:backup)
any_resource_instance.all_files
end
|
#definitive_resource_instance(id) ⇒ Object
39
40
41
|
# File 'lib/datadog_backup/cli.rb', line 39
def definitive_resource_instance(id)
matching_resource_instance(any_resource_instance.class_from_id(id))
end
|
#diffs ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/datadog_backup/cli.rb', line 43
def diffs
futures = all_diff_futures
::DatadogBackup::ThreadPool.watcher(logger).join
format_diff_output(
Concurrent::Promises
.zip(*futures)
.value!
.compact
)
end
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/datadog_backup/cli.rb', line 69
def format_diff_output(diff_output)
case diff_format
when nil, :color
diff_output.map do |id, diff|
" ---\n id: #{id}\n#{diff}"
end.join("\n")
when :html
'<html><head><style>' +
Diffy::CSS +
'</style></head><body>' +
diff_output.map do |id, diff|
"<br><br> ---<br><strong> id: #{id}</strong><br>" + diff
end.join('<br>') +
'</body></html>'
else
raise 'Unexpected diff_format.'
end
end
|
#getdiff(id) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/datadog_backup/cli.rb', line 55
def getdiff(id)
result = definitive_resource_instance(id).diff(id)
case result
when ''
nil
when "\n"
nil
when '<div class="diff"></div>'
nil
else
result
end
end
|
#initialize_client ⇒ Object
32
33
34
35
36
37
|
# File 'lib/datadog_backup/cli.rb', line 32
def initialize_client
@options[:client] ||= Dogapi::Client.new(
datadog_api_key,
datadog_app_key
)
end
|
#matching_resource_instance(klass) ⇒ Object
93
94
95
|
# File 'lib/datadog_backup/cli.rb', line 93
def matching_resource_instance(klass)
resource_instances.select { |resource_instance| resource_instance.instance_of?(klass) }.first
end
|
#resource_instances ⇒ Object
97
98
99
100
101
|
# File 'lib/datadog_backup/cli.rb', line 97
def resource_instances
@resource_instances ||= resources.map do |resource|
resource.new(@options)
end
end
|
#restore ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/datadog_backup/cli.rb', line 103
def restore
futures = all_diff_futures
watcher = ::DatadogBackup::ThreadPool.watcher(logger)
futures.each do |future|
id, diff = *future.value!
next unless diff
if @options[:force_restore]
definitive_resource_instance(id).restore(id)
else
puts '--------------------------------------------------------------------------------'
puts format_diff_output([id, diff])
puts '(r)estore to Datadog, overwrite local changes and (d)ownload, (s)kip, or (q)uit?'
response = $stdin.gets.chomp
case response
when 'q'
exit
when 'r'
puts "Restoring #{id} to Datadog."
definitive_resource_instance(id).restore(id)
when 'd'
puts "Downloading #{id} from Datadog."
definitive_resource_instance(id).get_and_write_file(id)
when 's'
next
else
puts 'Invalid response, please try again.'
response = $stdin.gets.chomp
end
end
end
watcher.join if watcher.status
end
|
#run! ⇒ Object
138
139
140
141
142
|
# File 'lib/datadog_backup/cli.rb', line 138
def run!
puts(send(action.to_sym))
rescue SystemExit, Interrupt
::DatadogBackup::ThreadPool.shutdown(logger)
end
|