class ChefBackup::Runner

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

Public Class Methods

new(running_config) click to toggle source

@param running_config [Hash] A hash of the private-chef-running.json

or the CLI args for a restore

@return [ChefBackup::Runner]

# 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

Public Instance Methods

backup() click to toggle source

@return [TrueClass, FalseClass] Execute Chef Server backup

# File lib/chef_backup/runner.rb, line 24
def backup
  @backup ||= ChefBackup::Strategy.backup(backup_strategy)
  @backup.backup
end
backup_name() click to toggle source

@return [String] The backup name from the restore param

# File lib/chef_backup/runner.rb, line 101
def backup_name
  if tarball?
    Pathname.new(restore_param).basename.sub_ext("").to_s
  elsif ebs_snapshot?
    restore_param
  end
end
backup_strategy() click to toggle source

@return [String] String name of the configured backup strategy

# File lib/chef_backup/runner.rb, line 47
def backup_strategy
  service_config["backup"]["strategy"]
end
config() click to toggle source

@return [ChefBackup::Config]

# File lib/chef_backup/runner.rb, line 32
def config
  ChefBackup::Config.config
end
ebs_snapshot?() click to toggle source

@return [TrueClass, FalseClass] Is the restore_param an EBS Snapshot ID?

# File lib/chef_backup/runner.rb, line 84
def ebs_snapshot?
  restore_param =~ /^snap-\h{8}$/
end
manifest() click to toggle source

@return [Hash] A parsed copy of the manifest.json in the backup tarball

# File lib/chef_backup/runner.rb, line 131
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
restore() click to toggle source

@return [TrueClass, FalseClass] Execute Chef Server restore

# File lib/chef_backup/runner.rb, line 39
def restore
  @restore ||= ChefBackup::Strategy.restore(restore_strategy, restore_param)
  @restore.restore
end
restore_directory() click to toggle source

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

@return [String] A path to the restore directory

# File lib/chef_backup/runner.rb, line 115
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_param() click to toggle source

@return [String] String of the restore parameter. eg: EBS snapshot ID

or a path to a tarball
# File lib/chef_backup/runner.rb, line 55
def restore_param
  config["restore_param"]
end
restore_strategy() click to toggle source

@return [String] A path to backup tarball or EBS snapshot ID

# File lib/chef_backup/runner.rb, line 62
def restore_strategy
  @restore_strategy ||= if tarball?
                          unpack_tarball
                          manifest["strategy"]
                        elsif ebs_snapshot?
                          "ebs"
                        else
                          raise InvalidStrategy, "#{restore_param} is not a valid backup"
                        end
end
tarball?() click to toggle source

@return [TrueClass, FalseClass] Is the restore_param is a tarball?

# File lib/chef_backup/runner.rb, line 76
def tarball?
  file = Pathname.new(File.expand_path(restore_param))
  file.exist? && file.extname == ".tgz"
end
unpack_tarball() click to toggle source

@return [TrueClass, FalseClass] Expands tarball into restore directory

# File lib/chef_backup/runner.rb, line 91
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