Module: Morpheus::Cli::BackupsHelper

Included in:
BackupJobsCommand, BackupRestoresCommand, BackupResultsCommand, BackupsCommand
Defined in:
lib/morpheus/cli/mixins/backups_helper.rb

Overview

Provides common methods for backups management

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 6

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#backup_job_list_keyObject



71
72
73
74
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 71

def backup_job_list_key
  # 'backupJobs'
  'jobs'
end

#backup_job_object_keyObject



66
67
68
69
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 66

def backup_job_object_key
  # 'backupJob'
  'job'
end

#backup_jobs_interfaceObject



15
16
17
18
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 15

def backup_jobs_interface
  raise "#{self.class} has not defined @backup_jobs_interface" if @backup_jobs_interface.nil?
  @backup_jobs_interface
end

#backup_list_keyObject



24
25
26
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 24

def backup_list_key
  'backups'
end

#backup_object_keyObject



20
21
22
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 20

def backup_object_key
  'backup'
end

#backup_restore_column_definitionsObject



164
165
166
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 164

def backup_restore_column_definitions()
  backup_restore_list_column_definitions()
end

#backup_restore_list_column_definitionsObject

Backup Restores



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 150

def backup_restore_list_column_definitions()
  {
    "ID" => 'id',
    "Backup" => lambda {|it| it['backup']['name'] rescue '' },
    "Backup Result ID" => lambda {|it| it['backupResultId'] rescue '' },
    "Target" => lambda {|it| it['instance']['name'] rescue '' },
    "Status" => lambda {|it| format_backup_result_status(it) },
    #"Duration" => lambda {|it| format_duration(it['startDate'], it['endDate']) },
    "Duration" => lambda {|it| format_duration_milliseconds(it['durationMillis']) if it['durationMillis'].to_i > 0 },
    "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
    "End Date" => lambda {|it| format_local_dt(it['endDate']) },
  }
end

#backup_result_column_definitionsObject



129
130
131
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 129

def backup_result_column_definitions()
  backup_result_list_column_definitions()
end

#backup_result_list_column_definitionsObject

Backup Results



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 116

def backup_result_list_column_definitions()
  {
    "ID" => 'id',
    "Backup" => lambda {|it| it['backup']['name'] rescue '' },
    "Status" => lambda {|it| format_backup_result_status(it) },
    #"Duration" => lambda {|it| format_duration(it['startDate'], it['endDate']) },
    "Duration" => lambda {|it| format_duration_milliseconds(it['durationMillis']) if it['durationMillis'].to_i > 0 },
    "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
    "End Date" => lambda {|it| format_local_dt(it['endDate']) },
    "Size" => lambda {|it| it['sizeInMb'].to_i != 0 ? format_bytes(it['sizeInMb'], 'MB') : '' },
  }
end

#backups_interfaceObject



10
11
12
13
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 10

def backups_interface
  raise "#{self.class} has not defined @backups_interface" if @backups_interface.nil?
  @backups_interface
end

#find_backup_by_id(id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 36

def find_backup_by_id(id)
  begin
    json_response = backups_interface.get(id.to_i)
    return json_response[backup_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Backup not found by id '#{id}'"
    else
      raise e
    end
  end
end

#find_backup_by_name(name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 49

def find_backup_by_name(name)
  json_response = backups_interface.list({name: name.to_s})
  backups = json_response[backup_list_key]
  if backups.empty?
    print_red_alert "Backup not found by name '#{name}'"
    return nil
  elsif backups.size > 1
    print_red_alert "#{backups.size} backups found by name '#{name}'"
    puts_error as_pretty_table(backups, [:id, :name], {color:red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return backups[0]
  end
end

#find_backup_by_name_or_id(val) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 28

def find_backup_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_backup_by_id(val)
  else
    return find_backup_by_name(val)
  end
end

#find_backup_job_by_id(id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 84

def find_backup_job_by_id(id)
  begin
    json_response = backup_jobs_interface.get(id.to_i)
    return json_response[backup_job_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Backup job not found by id '#{id}'"
    else
      raise e
    end
  end
end

#find_backup_job_by_name(name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 97

def find_backup_job_by_name(name)
  json_response = backup_jobs_interface.list({name: name.to_s})
  backup_jobs = json_response[backup_job_list_key]
  if backup_jobs.empty?
    print_red_alert "Backup job not found by name '#{name}'"
    return nil
  elsif backup_jobs.size > 1
    print_red_alert "#{backup_jobs.size} backup jobs found by name '#{name}'"
    puts_error as_pretty_table(backup_jobs, [:id, :name], {color:red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return backup_jobs[0]
  end
end

#find_backup_job_by_name_or_id(val) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 76

def find_backup_job_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_backup_job_by_id(val)
  else
    return find_backup_job_by_name(val)
  end
end

#format_backup_restore_status(backup_restore, return_color = cyan) ⇒ Object



168
169
170
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 168

def format_backup_restore_status(backup_restore, return_color=cyan)
  format_backup_result_status(backup_restore, return_color)
end

#format_backup_result_status(backup_result, return_color = cyan) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/morpheus/cli/mixins/backups_helper.rb', line 133

def format_backup_result_status(backup_result, return_color=cyan)
  out = ""
  status_string = backup_result['status'].to_s.upcase
  if status_string == 'SUCCEEDED' || status_string == 'SUCCESS'
    out <<  "#{green}#{status_string.upcase}#{return_color}"
  elsif status_string == 'FAILED'
    out <<  "#{red}#{status_string.upcase}#{return_color}"
  elsif status_string
    out <<  "#{cyan}#{status_string.upcase}#{return_color}"
  else
    out <<  ""
  end
  out
end