Class: ChefBackup::Runner

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Helpers
Defined in:
lib/chef_backup/runner.rb

Overview

ChefBackup::Runner class initializes the strategy and runs the action

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_PG_OPTIONS, Helpers::SERVER_ADD_ONS

Instance Method Summary collapse

Methods included from Helpers

#addon_install_dir, #all_services, #backend?, #base_config_dir, #base_install_dir, #chpst, #cleanup, #config_base, #ctl_command, #database_name, #disabled_services, #enabled_addons, #enabled_services, #ensure_file!, #frontend?, #ha?, #log, #marketplace?, #online?, #pg_options, #pgsql, #project_name, #reconfigure_add_ons, #reconfigure_marketplace, #restart_add_ons, #restart_chef_server, #running_filepath, #service_config, #service_enabled?, #shell_out, #shell_out!, #shell_timeout, #standalone?, #start_chef_server, #start_service, #stop_chef_server, #stop_service, #strategy, #tier?, #tmp_dir, #topology, #version_from_manifest_file

Constructor Details

#initialize(running_config) ⇒ ChefBackup::Runner

Parameters:

  • running_config (Hash)

    A hash of the private-chef-running.json or the CLI args for a restore



16
17
18
19
# File 'lib/chef_backup/runner.rb', line 16

def initialize(running_config)
  ChefBackup::Config.config = running_config
  ChefBackup::Logger.logger(service_config["backup"]["logfile"] || nil)
end

Instance Method Details

#backupTrueClass, FalseClass

Returns Execute Chef Server backup.

Returns:

  • (TrueClass, FalseClass)

    Execute Chef Server backup



24
25
26
27
# File 'lib/chef_backup/runner.rb', line 24

def backup
  @backup ||= ChefBackup::Strategy.backup(backup_strategy)
  @backup.backup
end

#backup_nameString

Returns The backup name from the restore param.

Returns:

  • (String)

    The backup name from the restore param



103
104
105
106
107
108
109
# File 'lib/chef_backup/runner.rb', line 103

def backup_name
  if tarball?
    Pathname.new(restore_param).basename.sub_ext("").to_s
  elsif ebs_snapshot?
    restore_param
  end
end

#backup_strategyString

Returns String name of the configured backup strategy.

Returns:

  • (String)

    String name of the configured backup strategy



47
48
49
# File 'lib/chef_backup/runner.rb', line 47

def backup_strategy
  service_config["backup"]["strategy"]
end

#configChefBackup::Config

Returns:



32
33
34
# File 'lib/chef_backup/runner.rb', line 32

def config
  ChefBackup::Config.config
end

#ebs_snapshot?TrueClass, FalseClass

Returns Is the restore_param an EBS Snapshot ID?.

Returns:

  • (TrueClass, FalseClass)

    Is the restore_param an EBS Snapshot ID?



86
87
88
# File 'lib/chef_backup/runner.rb', line 86

def ebs_snapshot?
  restore_param =~ /^snap-\h{8}$/
end

#manifestHash

Returns A parsed copy of the manifest.json in the backup tarball.

Returns:

  • (Hash)

    A parsed copy of the manifest.json in the backup tarball



133
134
135
136
137
138
139
# File 'lib/chef_backup/runner.rb', line 133

def manifest
  @manifest ||= begin
    file = "#{restore_directory}/manifest.json"
    ensure_file!(file, InvalidTarball, "No manifest found in tarball")
    JSON.parse(File.read(file))
  end
end

#restoreTrueClass, FalseClass

Returns Execute Chef Server restore.

Returns:

  • (TrueClass, FalseClass)

    Execute Chef Server restore



39
40
41
42
# File 'lib/chef_backup/runner.rb', line 39

def restore
  @restore ||= ChefBackup::Strategy.restore(restore_strategy, restore_param)
  @restore.restore
end

#restore_directoryString

Sets the restore_dir in ChefBackup::Config and ensures the directory exists and is cleaned.

Returns:

  • (String)

    A path to the restore directory



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef_backup/runner.rb', line 117

def restore_directory
  config["restore_dir"] ||= begin
    dir_name = File.join(tmp_dir, backup_name)
    if File.directory?(dir_name)
      # clean restore directory if it exists
      FileUtils.rm_r(Dir.glob("#{dir_name}/*"))
    else
      FileUtils.mkdir_p(dir_name)
    end
    dir_name
  end
end

#restore_paramString

Returns String of the restore parameter. eg: EBS snapshot ID or a path to a tarball.

Returns:

  • (String)

    String of the restore parameter. eg: EBS snapshot ID or a path to a tarball



55
56
57
# File 'lib/chef_backup/runner.rb', line 55

def restore_param
  config["restore_param"]
end

#restore_strategyString

Returns A path to backup tarball or EBS snapshot ID.

Returns:

  • (String)

    A path to backup tarball or EBS snapshot ID



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef_backup/runner.rb', line 62

def restore_strategy
  @restore_strategy ||= begin
    if tarball?
      unpack_tarball
      manifest["strategy"]
    elsif ebs_snapshot?
      "ebs"
    else
      raise InvalidStrategy, "#{restore_param} is not a valid backup"
    end
  end
end

#tarball?TrueClass, FalseClass

Returns Is the restore_param is a tarball?.

Returns:

  • (TrueClass, FalseClass)

    Is the restore_param is a tarball?



78
79
80
81
# File 'lib/chef_backup/runner.rb', line 78

def tarball?
  file = Pathname.new(File.expand_path(restore_param))
  file.exist? && file.extname == ".tgz"
end

#unpack_tarballTrueClass, FalseClass

Returns Expands tarball into restore directory.

Returns:

  • (TrueClass, FalseClass)

    Expands tarball into restore directory



93
94
95
96
97
98
# File 'lib/chef_backup/runner.rb', line 93

def unpack_tarball
  file = File.expand_path(restore_param)
  ensure_file!(file, InvalidTarball, "#{file} not found")
  log "Expanding tarball: #{file}"
  shell_out!("tar zxf #{file} -C #{restore_directory}")
end