Class: Crew::Context

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/crew/context.rb,
lib/crew/context/dsl.rb,
lib/crew/context/ssh.rb,
lib/crew/context/local.rb,
lib/crew/context/fusion.rb,
lib/crew/context/vagrant.rb

Defined Under Namespace

Modules: Fusion, Local, SSH, Vagrant Classes: DSL, FailedCommandError, TestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#assert, #escape, #poll, #retryable

Constructor Details

#initialize(home, name, file = nil, &blk) ⇒ Context

Returns a new instance of Context.



42
43
44
45
46
47
48
# File 'lib/crew/context.rb', line 42

def initialize(home, name, file = nil, &blk)
  @home = home
  @name = name
  @hints = Set.new
  @shell_count = 0
  load(file, &blk)
end

Instance Attribute Details

#adapter_nameObject

Returns the value of attribute adapter_name.



39
40
41
# File 'lib/crew/context.rb', line 39

def adapter_name
  @adapter_name
end

#hintsObject (readonly)

Returns the value of attribute hints.



40
41
42
# File 'lib/crew/context.rb', line 40

def hints
  @hints
end

#homeObject (readonly)

Returns the value of attribute home.



40
41
42
# File 'lib/crew/context.rb', line 40

def home
  @home
end

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/crew/context.rb', line 40

def name
  @name
end

#optsObject

Returns the value of attribute opts.



39
40
41
# File 'lib/crew/context.rb', line 39

def opts
  @opts
end

#sourceObject

Returns the value of attribute source.



39
40
41
# File 'lib/crew/context.rb', line 39

def source
  @source
end

Instance Method Details

#enter_sudo_passwordObject



58
59
60
61
62
63
64
# File 'lib/crew/context.rb', line 58

def enter_sudo_password
  unless defined?(@sudo_password)
    puts "Getting your sudo password (just this once):"
    @sudo_password = ENV['SUDO_PASSWORD'] || STDIN.noecho(&:gets).chomp
  end
  nil
end

#hint(h) ⇒ Object



107
108
109
# File 'lib/crew/context.rb', line 107

def hint(h)
  @hints << h.to_s
end

#listObject



117
118
119
120
121
# File 'lib/crew/context.rb', line 117

def list
  @home.each_task do |task|
    yield task
  end
end

#load(file = nil, &blk) ⇒ Object



50
51
52
# File 'lib/crew/context.rb', line 50

def load(file = nil, &blk)
  DSL.new(self).load(file, &blk)
end

#loggerObject



54
55
56
# File 'lib/crew/context.rb', line 54

def logger
  @home.logger
end

#registerObject



127
128
129
130
131
132
# File 'lib/crew/context.rb', line 127

def register
  @started ||= begin
    start
    true
  end
end

#run(name, *hints, &blk) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/crew/context.rb', line 66

def run(name, *hints, &blk)
  with_callbacks do
    task = Task.new(@home, name) { run &blk }
    task.default_hints = hints
    task.run!
  end
end

#run_callbacks(type) ⇒ Object



93
94
95
96
97
# File 'lib/crew/context.rb', line 93

def run_callbacks(type)
  home.callbacks[type].each do |before|
    run before.source_location.join(':'), &before
  end
end

#sh(cmd, opts = {}) ⇒ Object



111
112
113
114
115
# File 'lib/crew/context.rb', line 111

def sh(cmd, opts = {})
  out, err, code = sh_with_code(cmd, opts)
  assert code.zero?, "Command `#{cmd}' with out: #{out.inspect} err: #{err.inspect} code=#{code}"
  out
end

#startObject



134
135
# File 'lib/crew/context.rb', line 134

def start
end

#start_shellObject



99
100
101
# File 'lib/crew/context.rb', line 99

def start_shell
  run_callbacks(:before)
end

#stop_shellObject



103
104
105
# File 'lib/crew/context.rb', line 103

def stop_shell
  run_callbacks(:after)
end

#task(name) ⇒ Object



123
124
125
# File 'lib/crew/context.rb', line 123

def task(name)
  @home.find_and_add_task(name)
end

#with_callbacksObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/crew/context.rb', line 74

def with_callbacks
  out = nil
  begin
    @shell_count += 1
    if @shell_count == 1
      logger.context @name do
        start_shell
        out = yield
      end
    else
      out = yield
    end
  ensure
    @shell_count -= 1
    stop_shell if @shell_count == 0
  end
  out
end