Class: EY::Serverside::DeployHook

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-serverside/deploy_hook.rb

Defined Under Namespace

Classes: CallbackContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, shell, hook_name) ⇒ DeployHook

Returns a new instance of DeployHook.



8
9
10
# File 'lib/engineyard-serverside/deploy_hook.rb', line 8

def initialize(config, shell, hook_name)
  @config, @shell, @hook_name = config, shell, hook_name
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/engineyard-serverside/deploy_hook.rb', line 6

def config
  @config
end

#hook_nameObject (readonly)

Returns the value of attribute hook_name.



6
7
8
# File 'lib/engineyard-serverside/deploy_hook.rb', line 6

def hook_name
  @hook_name
end

#shellObject (readonly)

Returns the value of attribute shell.



6
7
8
# File 'lib/engineyard-serverside/deploy_hook.rb', line 6

def shell
  @shell
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/engineyard-serverside/deploy_hook.rb', line 20

def call
  if hook_path.exist?
    Dir.chdir(config.paths.active_release.to_s) do
      if desc = syntax_error(hook_path)
        hook_name = hook_path.basename
        abort "*** [Error] Invalid Ruby syntax in hook: #{hook_name} ***\n*** #{desc.chomp} ***"
      else
        eval_hook(hook_path.read)
      end
    end
  end
end

#callback_contextObject



16
17
18
# File 'lib/engineyard-serverside/deploy_hook.rb', line 16

def callback_context
  @context ||= CallbackContext.new(config, shell, hook_path)
end

#display_deprecation_warnings(code) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/engineyard-serverside/deploy_hook.rb', line 41

def display_deprecation_warnings(code)
  if code =~ /@configuration/
    shell.warning("Use of `@configuration` in deploy hooks is deprecated.\nPlease use `config`, which provides access to the same object.\n\tin #{hook_path}")
  end
  if code =~ /@node/
    shell.warning("Use of `@node` in deploy hooks is deprecated.\nPlease use `config.node`, which provides access to the same object.\n\tin #{hook_path}")
  end
end

#display_hook_error(exception, code, hook_path) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/engineyard-serverside/deploy_hook.rb', line 50

def display_hook_error(exception, code, hook_path)
  shell.fatal <<-ERROR
Exception raised in deploy hook #{hook_path}.

#{exception.class}: #{exception.to_s}

Please fix this error before retrying.
  ERROR
end

#eval_hook(code) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/engineyard-serverside/deploy_hook.rb', line 33

def eval_hook(code)
  display_deprecation_warnings(code)
  callback_context.instance_eval(code)
rescue Exception => exception
  display_hook_error(exception, code, hook_path)
  raise exception
end

#hook_pathObject



12
13
14
# File 'lib/engineyard-serverside/deploy_hook.rb', line 12

def hook_path
  config.paths.deploy_hook(hook_name)
end

#syntax_error(file) ⇒ Object



60
61
62
63
# File 'lib/engineyard-serverside/deploy_hook.rb', line 60

def syntax_error(file)
  output = `ruby -c #{file} 2>&1`
  output unless output =~ /Syntax OK/
end