Class: BiblioTech::CommandGenerator

Inherits:
Object
  • Object
show all
Includes:
Logging, Caliph::CommandLineDSL
Defined in:
lib/bibliotech/command_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

log, log_level, logger, logger=, null_logger

Constructor Details

#initialize(config) ⇒ CommandGenerator

Returns a new instance of CommandGenerator.



16
17
18
# File 'lib/bibliotech/command_generator.rb', line 16

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/bibliotech/command_generator.rb', line 14

def config
  @config
end

Instance Method Details

#createObject

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/bibliotech/command_generator.rb', line 114

def create()
  raise NotImplementedError
end

#deleteObject

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/bibliotech/command_generator.rb', line 111

def delete()
  raise NotImplementedError
end

#export(options = nil) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/bibliotech/command_generator.rb', line 20

def export(options = nil)
  options = config.merge(options || {})
  command = cmd
  command = Builders::Export.for(options).go(command)
  Builders::FileOutput.for(options).go(command).tap do |cmd|
    log.info{ cmd.command }
  end
end

#fetch(remote, filename, options = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bibliotech/command_generator.rb', line 54

def fetch(remote, filename, options = nil)
  options = config.merge(options || {})
  local_path = options.local_file(filename)
  cmd("mkdir") do |cmd|
    cmd.options << "-p" #ok Mac OS X doesn't have --parents in its mkdir
    cmd.options << File::dirname(local_path)
  end & cmd("scp") do |cmd|
    options.optionally{ cmd.options << "-i #{options.id_file(remote)}" }
    cmd.options << options.remote_file(remote, filename)
    cmd.options << local_path
  end.tap do |cmd|
    log.info{ cmd.command }
  end
end

#import(opts = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bibliotech/command_generator.rb', line 29

def import(opts = nil)
  opts ||= {}
  options = config.merge(opts)
  unless File.exists?(options.backup_file)
    filename = options.backup_file
    opts[:backups] = opts.fetch(:backups){ opts.fetch("backups", {})}
    opts[:filename] = filename

    options = config.merge(opts)

    if File.exists?(options.backup_file)
      log.warn { "Actually restoring from #{options.backup_file} - this behavior is deprecated. In future, use explicit path."}
    else
      log.fatal{ "Cannot restore from database from missing file #{filename} !"}
      raise "Missing #{filename}"
    end
  end

  command = cmd()
  command = Builders::Import.for(options).go(command)
  Builders::FileInput.for(options).go(command).tap do |cmd|
    log.info{ cmd.command }
  end
end

#push(remote, filename, options = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/bibliotech/command_generator.rb', line 69

def push(remote, filename, options = nil)
  options = config.merge(options || {})
  cmd("scp") do |cmd|
    cmd.options << options.local_file(filename)
    cmd.options << options.remote_file(remote, filename)
  end.tap do |cmd|
    log.info{ cmd.command }
  end
end

#remote_cli(remote, *command_options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bibliotech/command_generator.rb', line 79

def remote_cli(remote, *command_options)
  options = {}
  if command_options.last.is_a? Hash
    options = command_options.pop
  end
  options = config.merge(options)
  command_on_remote = cmd("cd") do |cmd|
    cmd.options << options.root_dir_on(remote)
  end & cmd("bundle", "exec", "bibliotech", *command_options)
  cmd("ssh") do |cmd|
    cmd.options << "-n" #because we're not going to be doing any input
    options.optionally{ cmd.options << "-i #{options.id_file(remote)}" }
    options.optionally{ cmd.options << "-l #{options.remote_user(remote)}" }

    cmd.options << options.remote_host(remote)

    options.optionally{ cmd.options << "-p #{options.remote_port(remote)}" } #ok


    options.optionally do
      options.ssh_options(remote).each do |opt|
        cmd.options << "-o #{opt}"
      end
    end
  end - escaped_command(command_on_remote).tap do |cmd|
    log.info{ cmd.command }
  end
end

#wipeObject

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/bibliotech/command_generator.rb', line 108

def wipe()
  raise NotImplementedError
end