Class: Harrison::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/harrison/base.rb

Direct Known Subclasses

Deploy, Package

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg_opts = [], opts = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/harrison/base.rb', line 6

def initialize(arg_opts=[], opts={})
  # Config helpers for Harrisonfile.
  self.class.option_helper(:user)
  self.class.option_helper(:env)

  @arg_opts = arg_opts
  @arg_opts << [ :debug, "Output debug messages.", :type => :boolean, :default => false ]
  @arg_opts << [ :env, "Environment to package for or deploy to. This can be examined in your Harrisonfile to calculate target hosts.", :type => :string ]

  @options = opts
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/harrison/base.rb', line 4

def options
  @options
end

Class Method Details

.option_helper(option) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/harrison/base.rb', line 18

def self.option_helper(option)
  send :define_method, option do
    @options[option]
  end

  send :define_method, "#{option}=" do |val|
    @options[option] = val
  end
end

Instance Method Details

#closeObject



88
89
90
# File 'lib/harrison/base.rb', line 88

def close
  ssh.close if @ssh
end

#download(remote_path, local_path) ⇒ Object



80
81
82
# File 'lib/harrison/base.rb', line 80

def download(remote_path, local_path)
  ssh.download(remote_path, local_path)
end

#exec(cmd) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/harrison/base.rb', line 35

def exec(cmd)
  result = `#{cmd}`

  if ($?.success? && result)
    result.strip
  else
    throw :failure, true
  end
end

#parse(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/harrison/base.rb', line 55

def parse(args)
  opt_parser = Trollop::Parser.new

  @arg_opts.each do |arg_opt|
    opt_parser.opt(*arg_opt)
  end

  @options.merge!(Trollop::with_standard_exception_handling(opt_parser) do
    opt_parser.parse(args)
  end)

  Harrison.send(:remove_const, "DEBUG") if Harrison.const_defined?("DEBUG")
  Harrison.const_set("DEBUG", @options[:debug])
end

#remote_exec(cmd) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/harrison/base.rb', line 45

def remote_exec(cmd)
  result = ssh.exec(cmd)

  if result
    result.strip
  else
    throw :failure, true
  end
end

#run(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/harrison/base.rb', line 70

def run(&block)
  if block_given?
    # If called with a block, convert it to a proc and store.
    @run_block = block
  else
    # Otherwise, invoke the previously stored block with self.
    @run_block && @run_block.call(self)
  end
end

#upload(local_path, remote_path) ⇒ Object



84
85
86
# File 'lib/harrison/base.rb', line 84

def upload(local_path, remote_path)
  ssh.upload(local_path, remote_path)
end