Class: Understudy::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/understudy/cli.rb

Instance Method Summary collapse

Instance Method Details

#perform(job) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/understudy/cli.rb', line 11

def perform(job)
  ENV['PATH'] += ":/etc/understudy"
  ENV['TMPDIR']='/tmp/understudy/'

  log = Logger.new(STDOUT)
  log.level = options[:verbose] ? Logger::DEBUG : Logger::INFO
  log.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end

  job_name = job

  config_file = "/etc/understudy/#{job_name}.conf"

  log.error "Cannot find '#{config_file}'" unless File.exist? config_file

  data_dir = "/tmp/understudy-#{job_name}"

  FileUtils.mkdir_p data_dir

  lockfile = File.open( "#{data_dir}/LOCK", File::RDWR|File::CREAT, 0644 )
  unless lockfile.flock File::LOCK_EX|File::LOCK_NB
    log.error "Job #{job_name} is already running"
  end
  lockfile.puts $$
  lockfile.flush

  config = OpenStruct.new

  # Any options that aren't our options go to rdiff-backup
  our_options = %w{source dest command}

  rdiff_args = []

  File.read( config_file ).split( /\n/ ).each do |line|
    line.sub! /#.*$/, ''
    line.sub! /^\s+/, ''
    line.sub! /\s+$/, ''
    next if line.empty?

    line =~ /^([-\w]+)(\s+=\s+(.*))?$/ or raise "Strange line in config: #{line}"
    key = $1
    val = $3
    if our_options.member? key
      config.send "#{key}=".to_sym, val
    else
      rdiff_args.push "--#{key}" + ( val ? "=#{val}" : "" )
    end
  end

  log.error "Missing 'dest' config option" unless config.dest
  log.error "Missing 'source' config option" unless config.source

  rdiff_data = "#{config.dest}/rdiff-backup-data"
  exists = File.directory? rdiff_data

  if exists && options["first-time"]
    log.error "Cannot force first time, destination '#{config.dest}' already exists"
    return 1
  elsif !exists && !options["first-time"]
    log.error "Destination '#{config.dest}' does not appear to exist, try again with --first-time"
    return 1
  end

  run config.command if config.command

  command = []

  command << "--terminal-verbosity=5" if options[:verbose]
  command << "--print-statistics"

  command += rdiff_args

  command << config.source
  command << config.dest

  run command, log
end