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, #concurrency_limit, #diff_format, #force_restore, #output_format, #resources
Constructor Details
#initialize(options) ⇒ Cli
Returns a new instance of Cli.
80
81
82
|
# File 'lib/datadog_backup/cli.rb', line 80
def initialize(options)
@options = options
end
|
Instance Method Details
#all_diff_futures ⇒ Object
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/datadog_backup/cli.rb', line 10
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
21
22
23
|
# File 'lib/datadog_backup/cli.rb', line 21
def any_resource_instance
resource_instances.first
end
|
#backup ⇒ Object
25
26
27
28
29
|
# File 'lib/datadog_backup/cli.rb', line 25
def backup
resource_instances.each(&:purge)
resource_instances.each(&:backup)
any_resource_instance.all_files
end
|
#definitive_resource_instance(id) ⇒ Object
31
32
33
|
# File 'lib/datadog_backup/cli.rb', line 31
def definitive_resource_instance(id)
matching_resource_instance(any_resource_instance.class_from_id(id))
end
|
#diffs ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/datadog_backup/cli.rb', line 35
def diffs
futures = all_diff_futures
::DatadogBackup::ThreadPool.watcher.join
format_diff_output(
Concurrent::Promises
.zip(*futures)
.value!
.compact
)
end
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/datadog_backup/cli.rb', line 61
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
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/datadog_backup/cli.rb', line 47
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
|
#matching_resource_instance(klass) ⇒ Object
84
85
86
|
# File 'lib/datadog_backup/cli.rb', line 84
def matching_resource_instance(klass)
resource_instances.select { |resource_instance| resource_instance.instance_of?(klass) }.first
end
|
#resource_instances ⇒ Object
88
89
90
91
92
|
# File 'lib/datadog_backup/cli.rb', line 88
def resource_instances
@resource_instances ||= resources.map do |resource|
resource.new(@options)
end
end
|
#restore ⇒ Object
94
95
96
97
98
99
100
101
102
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
|
# File 'lib/datadog_backup/cli.rb', line 94
def restore
futures = all_diff_futures
watcher = ::DatadogBackup::ThreadPool.watcher
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
129
130
131
132
133
|
# File 'lib/datadog_backup/cli.rb', line 129
def run!
puts(send(action.to_sym))
rescue SystemExit, Interrupt
::DatadogBackup::ThreadPool.shutdown
end
|