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.



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

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.



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

def config
  @config
end

#hook_nameObject (readonly)

Returns the value of attribute hook_name.



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

def hook_name
  @hook_name
end

#shellObject (readonly)

Returns the value of attribute shell.



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

def shell
  @shell
end

Instance Method Details

#callObject



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

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



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

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

#display_deprecation_warnings(code) ⇒ Object



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

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



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

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



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

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



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

def hook_path
  config.paths.deploy_hook(hook_name)
end

#ruby_binObject

Ideally we’d use RbConfig.ruby, but that doesn’t work on 1.8.7.



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

def ruby_bin
  File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
end

#syntax_error(file) ⇒ Object



66
67
68
69
# File 'lib/engineyard-serverside/deploy_hook.rb', line 66

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