Class: ForemanMaintain::Utils::Backup

Inherits:
Object
  • Object
show all
Includes:
Concerns::SystemHelpers
Defined in:
lib/foreman_maintain/utils/backup.rb

Constant Summary

Constants included from Concerns::OsFacts

Concerns::OsFacts::FALLBACK_OS_RELEASE_FILE, Concerns::OsFacts::OS_RELEASE_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::SystemHelpers

#check_max_version, #check_min_version, #command_present?, #create_lv_snapshot, #directory_empty?, #execute, #execute!, #execute?, #execute_runner, #execute_with_status, #file_exists?, #file_nonzero?, #find_dir_containing_file, #find_package, #find_symlinks, #foreman_plugin_name, #format_shell_args, #get_lv_info, #get_lv_path, #hammer_package, #hammer_plugin_name, #hostname, included, #package_manager, #package_version, #packages_action, #parse_csv, #parse_json, #proxy_plugin_name, #repository_manager, #ruby_prefix, #server?, #shellescape, #systemd_installed?, #version

Methods included from Concerns::OsFacts

#centos?, #cpu_cores, #deb_major_version, #debian?, #debian_or_ubuntu?, #el7?, #el8?, #el?, #el_major_version, #el_short_name, #facts, #memory, #os_id, #os_id_like_list, #os_name, #os_release_file, #os_version, #os_version_codename, #os_version_id, #rhel?, #ubuntu?, #ubuntu_major_version

Methods included from Concerns::Finders

#check, #detector, #feature, #find_all_scenarios, #find_checks, #find_procedures, #find_scenarios, #procedure

Methods included from Concerns::Logger

#logger

Constructor Details

#initialize(backup_dir) ⇒ Backup

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/foreman_maintain/utils/backup.rb', line 15

def initialize(backup_dir)
  # fpc stands for foreman proxy w/ content
  @backup_dir = backup_dir
  @standard_files = ['config_files.tar.gz']
  @foreman_online_files = ['foreman.dump']
  @foreman_offline_files = ['pgsql_data.tar.gz']
  @katello_online_files = ['candlepin.dump', 'foreman.dump']
  @katello_offline_files = ['pgsql_data.tar.gz']
  if feature(:pulp2)
    @katello_online_files  << 'mongo_dump'
    @katello_offline_files << 'mongo_data.tar.gz'
    @fpc_online_files = ['mongo_dump']
    @fpc_offline_files = ['mongo_data.tar.gz']
  elsif feature(:pulpcore_database)
    @katello_online_files << 'foreman.dump'
    @fpc_online_files = ['pulpcore.dump']
    @fpc_offline_files = ['pgsql_data.tar.gz']
  end
end

Instance Attribute Details

#foreman_offline_filesObject

Returns the value of attribute foreman_offline_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def foreman_offline_files
  @foreman_offline_files
end

#foreman_online_filesObject

Returns the value of attribute foreman_online_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def foreman_online_files
  @foreman_online_files
end

#fpc_offline_filesObject

Returns the value of attribute fpc_offline_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def fpc_offline_files
  @fpc_offline_files
end

#fpc_online_filesObject

Returns the value of attribute fpc_online_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def fpc_online_files
  @fpc_online_files
end

#katello_offline_filesObject

Returns the value of attribute katello_offline_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def katello_offline_files
  @katello_offline_files
end

#katello_online_filesObject

Returns the value of attribute katello_online_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def katello_online_files
  @katello_online_files
end

#standard_filesObject

Returns the value of attribute standard_files.



10
11
12
# File 'lib/foreman_maintain/utils/backup.rb', line 10

def standard_files
  @standard_files
end

Instance Method Details

#check_backupObject



69
70
71
72
73
74
75
76
77
# File 'lib/foreman_maintain/utils/backup.rb', line 69

def check_backup
  if feature(:instance).foreman_proxy_with_content?
    valid_fpc_backup?
  elsif feature(:katello)
    valid_katello_backup?
  else
    valid_foreman_backup?
  end
end

#check_file_existence(existence_map) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/foreman_maintain/utils/backup.rb', line 95

def check_file_existence(existence_map)
  existence_map[:present].each do |file|
    unless file_map[file][:present]
      return false
    end
  end

  existence_map[:absent].each do |file|
    if file_map[file][:present]
      return false
    end
  end

  true
end

#dumps_for_hybrid_db_setup(dbs_hash) ⇒ Object

rubocop:disable Metrics/MethodLength



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/foreman_maintain/utils/backup.rb', line 248

def dumps_for_hybrid_db_setup(dbs_hash)
  present = []
  absent = []
  dbs_hash.each do |data_file, dbs|
    dbs.each do |db|
      feature_label = db == 'mongo' ? db : "#{db}_database"
      dump_file = "#{db}_dump"
      if feature(feature_label.to_sym).local?
        present |= [data_file]
        absent << dump_file.to_sym
      else
        present << dump_file.to_sym
      end
    end
    absent |= [data_file] unless present.include?(data_file)
  end
  [present, absent]
end

#file_mapObject

rubocop:enable Metrics/MethodLength



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/foreman_maintain/utils/backup.rb', line 36

def file_map
  @file_map ||= {
    :mongo_data => map_file(@backup_dir, 'mongo_data.tar.gz'),
    :pgsql_data => map_file(@backup_dir, 'pgsql_data.tar.gz'),
    :pulp_data => map_file(@backup_dir, 'pulp_data.tar'),
    :foreman_dump => map_file(@backup_dir, 'foreman.dump'),
    :candlepin_dump => map_file(@backup_dir, 'candlepin.dump'),
    :mongo_dump => map_file(@backup_dir, 'mongo_dump'),
    :config_files => map_file(@backup_dir, 'config_files.tar.gz'),
    :pg_globals => map_file(@backup_dir, 'pg_globals.dump'),
    :metadata => map_file(@backup_dir, 'metadata.yml'),
    :pulpcore_dump => map_file(@backup_dir, 'pulpcore.dump')
  }
end

#foreman_logical_backup?Boolean

Returns:

  • (Boolean)


242
243
244
245
# File 'lib/foreman_maintain/utils/backup.rb', line 242

def foreman_logical_backup?
  check_file_existence(:present => [:pgsql_data, :foreman_dump],
                       :absent => [:candlepin_dump, :mongo_data, :mongo_dump, :pulpcore_dump])
end

#foreman_online_backup?Boolean

Returns:

  • (Boolean)


236
237
238
239
240
# File 'lib/foreman_maintain/utils/backup.rb', line 236

def foreman_online_backup?
  check_file_existence(:present => [:foreman_dump],
                       :absent => [:candlepin_dump, :pgsql_data,
                                   :mongo_data, :mongo_dump, :pulpcore_dump])
end

#foreman_standard_backup?Boolean

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/foreman_maintain/utils/backup.rb', line 230

def foreman_standard_backup?
  check_file_existence(:present => [:pgsql_data],
                       :absent => [:candlepin_dump, :foreman_dump, :pulpcore_dump,
                                   :mongo_data, :mongo_dump])
end

#fpc_hybrid_db_backup?Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
# File 'lib/foreman_maintain/utils/backup.rb', line 221

def fpc_hybrid_db_backup?
  all_dbs = { :pgsql_data => [], :mongo_data => [] }
  all_dbs[:pgsql_data] << 'pulpcore' if feature(:pulpcore_database)
  all_dbs[:mongo_data] << 'mongo' if feature(:mongo)
  present, absent = dumps_for_hybrid_db_setup(all_dbs)
  absent.concat [:candlepin_dump, :foreman_dump]
  check_file_existence(:present => present, :absent => absent)
end

#fpc_logical_backup?Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/foreman_maintain/utils/backup.rb', line 204

def fpc_logical_backup?
  present = []
  absent = [:candlepin_dump, :foreman_dump]
  if feature(:pulpcore_database) && !feature(:pulp2)
    present.concat [:pulpcore_dump, :pgsql_data]
    absent.concat [:mongo_dump, :mongo_data]
  elsif feature(:pulp2) && feature(:pulpcore_database)
    present.concat [:mongo_dump, :mongo_data, :pulpcore_dump, :pgsql_data]
  elsif feature(:pulp2)
    present.concat [:mongo_dump, :mongo_data]
    absent.concat [:pulpcore_dump, :pgsql_data]
  else
    return false
  end
  check_file_existence(:present => present, :absent => absent)
end

#fpc_online_backup?Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/foreman_maintain/utils/backup.rb', line 187

def fpc_online_backup?
  present = []
  absent = [:mongo_data, :pgsql_data, :candlepin_dump, :foreman_dump]
  if feature(:pulpcore_database) && !feature(:pulp2)
    present.concat [:pulpcore_dump]
    absent.concat [:mongo_dump]
  elsif feature(:pulp2) && feature(:pulpcore_database)
    present.concat [:mongo_dump, :pulpcore_dump]
  elsif feature(:pulp2)
    present.concat [:mongo_dump]
    absent.concat [:pulpcore_dump]
  else
    return false
  end
  check_file_existence(:present => present, :absent => absent)
end

#fpc_standard_backup?Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/foreman_maintain/utils/backup.rb', line 169

def fpc_standard_backup?
  present = []
  absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump, :mongo_dump]
  if feature(:pulpcore_database) && !feature(:pulp2)
    present.concat [:pgsql_data]
    absent.concat [:mongo_data]
  elsif feature(:pulp2) && feature(:pulpcore_database)
    present.concat [:mongo_data, :pgsql_data]
  elsif feature(:pulp2)
    present.concat [:mongo_data]
    absent.concat [:pgsql_data]
  else
    return false
  end
  check_file_existence(:present => present,
                       :absent => absent)
end

#incremental?Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/foreman_maintain/utils/backup.rb', line 334

def incremental?
  !!.fetch('incremental', false)
end

#katello_hybrid_db_backup?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
# File 'lib/foreman_maintain/utils/backup.rb', line 161

def katello_hybrid_db_backup?
  all_dbs = { :pgsql_data => %w[candlepin foreman], :mongo_data => [] }
  all_dbs[:pgsql_data] << 'pulpcore' if feature(:pulpcore_database)
  all_dbs[:mongo_data] << 'mongo' if feature(:mongo)
  present, absent = dumps_for_hybrid_db_setup(all_dbs)
  check_file_existence(:present => present, :absent => absent)
end

#katello_logical_backup?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/foreman_maintain/utils/backup.rb', line 143

def katello_logical_backup?
  present = [:pgsql_data, :candlepin_dump, :foreman_dump]
  absent = []
  if feature(:pulpcore_database) && !feature(:pulp2)
    present.concat [:pulpcore_dump]
    absent.concat [:mongo_dump, :mongo_data]
  elsif feature(:pulp2) && feature(:pulpcore_database)
    present.concat [:mongo_dump, :mongo_data, :pulpcore_dump]
  elsif feature(:pulp2)
    present.concat [:mongo_dump, :mongo_data]
    absent.concat [:pulpcore_dump]
  else
    return false
  end
  check_file_existence(:present => present,
                       :absent => absent)
end

#katello_online_backup?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/foreman_maintain/utils/backup.rb', line 125

def katello_online_backup?
  present = [:candlepin_dump, :foreman_dump]
  absent = [:mongo_data, :pgsql_data]
  if feature(:pulpcore_database) && !feature(:pulp2)
    present.concat [:pulpcore_dump]
    absent.concat [:mongo_dump]
  elsif feature(:pulp2) && feature(:pulpcore_database)
    present.concat [:mongo_dump, :pulpcore_dump]
  elsif feature(:pulp2)
    present.concat [:mongo_dump]
    absent.concat [:pulpcore_dump]
  else
    return false
  end
  check_file_existence(:present => present,
                       :absent => absent)
end

#katello_standard_backup?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/foreman_maintain/utils/backup.rb', line 111

def katello_standard_backup?
  present = [:pgsql_data]
  absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump, :mongo_dump]
  if feature(:pulpcore_database) && !feature(:pulp2)
    absent.concat [:mongo_data]
  elsif feature(:pulp2)
    present.concat [:mongo_data]
  else
    return false
  end
  check_file_existence(:present => present,
                       :absent => absent)
end

#map_file(backup_dir, filename) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/foreman_maintain/utils/backup.rb', line 51

def map_file(backup_dir, filename)
  file_path = File.join(backup_dir, filename)
  present = File.exist?(file_path)
  {
    :present => present,
    :path => file_path
  }
end

#metadataObject



310
311
312
313
314
315
316
# File 'lib/foreman_maintain/utils/backup.rb', line 310

def 
  if file_map[:metadata][:present]
    YAML.load_file(file_map[:metadata][:path])
  else
    {}
  end
end

#online_backup?Boolean

Returns:

  • (Boolean)


338
339
340
# File 'lib/foreman_maintain/utils/backup.rb', line 338

def online_backup?
  !!.fetch('online', false)
end

#present_filesObject



60
61
62
63
# File 'lib/foreman_maintain/utils/backup.rb', line 60

def present_files
  present_files = file_map.select { |_k, v| v[:present] }
  present_files.values.map { |f| File.basename(f[:path]) }
end

#pulp_tar_split?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/foreman_maintain/utils/backup.rb', line 318

def pulp_tar_split?
  File.exist?(File.join(@backup_dir, 'pulp_data.part0002'))
end

#sql_dump_files_exist?Boolean

Returns:

  • (Boolean)


328
329
330
331
332
# File 'lib/foreman_maintain/utils/backup.rb', line 328

def sql_dump_files_exist?
  file_map[:foreman_dump][:present] ||
    file_map[:candlepin_dump][:present] ||
    (feature(:pulpcore_database) && file_map[:pulpcore_dump][:present])
end

#tar_backups_exist?Boolean

Returns:

  • (Boolean)


322
323
324
325
326
# File 'lib/foreman_maintain/utils/backup.rb', line 322

def tar_backups_exist?
  file_map[:mongo_data][:present] ||
    file_map[:pulp_data][:present] ||
    file_map[:pgsql_data][:present]
end

#valid_backup?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/foreman_maintain/utils/backup.rb', line 65

def valid_backup?
  file_map[:config_files][:present] && check_backup
end

#valid_foreman_backup?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/foreman_maintain/utils/backup.rb', line 91

def valid_foreman_backup?
  foreman_standard_backup? || foreman_online_backup? || foreman_logical_backup?
end

#valid_fpc_backup?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/foreman_maintain/utils/backup.rb', line 79

def valid_fpc_backup?
  fpc_online_backup? || fpc_standard_backup? || fpc_logical_backup? || \
    # fpc can have setup where mongo or pulpcore db is external but not both
    fpc_hybrid_db_backup?
end

#valid_katello_backup?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/foreman_maintain/utils/backup.rb', line 85

def valid_katello_backup?
  katello_online_backup? || katello_standard_backup? || katello_logical_backup? || \
    # Katello can have setup where some of dbs are external but not all
    katello_hybrid_db_backup?
end

#validate_hostname?Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/foreman_maintain/utils/backup.rb', line 268

def validate_hostname?
  # make sure that the system hostname is the same as the backup
   = .fetch('hostname', nil)
  if 
     == hostname
  else
    config_tarball = file_map[:config_files][:path]
    tar_cmd = "tar zxf #{config_tarball} etc/httpd/conf/httpd.conf --to-stdout --occurrence=1"
    status, httpd_config = execute_with_status(tar_cmd)

    # Incremental backups sometimes don't include httpd.conf. Since a "base" backup
    # is restored before an incremental, we can assume that the hostname is checked
    # during the base backup restore
    if status == 0
      match = httpd_config.match(/\s*ServerName\s+"*([^ "]+)"*\s*$/)
      match ? match[1] == hostname : false
    else
      true
    end
  end
end

#validate_interfacesObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/foreman_maintain/utils/backup.rb', line 290

def validate_interfaces
  # I wanted to do `Socket.getifaddrs.map(&:name).uniq`,
  # but this has to work with Ruby 2.0, and Socket.getifaddrs is 2.1+
  errors = {}
  system_interfaces = Dir.entries('/sys/class/net') - ['.', '..']

  proxy_config = .fetch('proxy_config', {})

  %w[dns dhcp].each do |feature|
    next unless proxy_config.fetch(feature, false)

    wanted_interface = proxy_config.fetch("#{feature}_interface", 'lo')
    unless system_interfaces.include?(wanted_interface)
      errors[feature] = { 'configured' => wanted_interface, 'available' => system_interfaces }
    end
  end

  return errors
end