Class: YleTf::TfHook

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf/tf_hook.rb,
lib/yle_tf/tf_hook/runner.rb

Defined Under Namespace

Classes: Runner

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TfHook

Returns a new instance of TfHook.



34
35
36
37
38
39
40
# File 'lib/yle_tf/tf_hook.rb', line 34

def initialize(opts = {})
  @description = opts[:description]
  @source = opts[:source]
  @path = opts[:path]
  @vars = opts[:vars] || {}
  @tmpdir = nil
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



32
33
34
# File 'lib/yle_tf/tf_hook.rb', line 32

def description
  @description
end

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/yle_tf/tf_hook.rb', line 32

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



32
33
34
# File 'lib/yle_tf/tf_hook.rb', line 32

def source
  @source
end

#varsObject (readonly)

Returns the value of attribute vars.



32
33
34
# File 'lib/yle_tf/tf_hook.rb', line 32

def vars
  @vars
end

Class Method Details

.from_config(config, tf_env) ⇒ Object

Returns a ‘TfHook` instance from configuration hash



16
17
18
19
20
21
22
# File 'lib/yle_tf/tf_hook.rb', line 16

def self.from_config(config, tf_env)
  TfHook.new(
    description: config['description'],
    source:      config['source'],
    vars:        merge_vars(config['vars'], tf_env)
  )
end

.from_file(path) ⇒ Object

Returns a ‘Hook` instance from a local file path



25
26
27
28
29
30
# File 'lib/yle_tf/tf_hook.rb', line 25

def self.from_file(path)
  TfHook.new(
    description: File.basename(path),
    path:        path
  )
end

.merge_vars(vars, tf_env) ⇒ Object

Returns a hash with env specific vars merged into the default ones



93
94
95
96
97
# File 'lib/yle_tf/tf_hook.rb', line 93

def self.merge_vars(vars, tf_env)
  vars ||= {}
  defaults = vars['defaults'] || {}
  defaults.merge(vars[tf_env] || {})
end

Instance Method Details

#clone_git_repo(config) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/yle_tf/tf_hook.rb', line 75

def clone_git_repo(config)
  Logger.debug("Cloning hook '#{description}' from #{config[:uri]} (#{config[:ref]})")
  YleTf::System.cmd(
    'git', 'clone', '--quiet', '--depth=1', '--branch', config[:ref],
    '--', config[:uri], config[:dir]
  )
end

#create_tmpdirObject



83
84
85
# File 'lib/yle_tf/tf_hook.rb', line 83

def create_tmpdir
  @tmpdir = Dir.mktmpdir('tf_hook_')
end

#delete_tmpdirObject



87
88
89
90
# File 'lib/yle_tf/tf_hook.rb', line 87

def delete_tmpdir
  FileUtils.rm_rf(@tmpdir, secure: true) if @tmpdir && Dir.exist?(@tmpdir)
  @tmpdir = nil
end

#fetchObject



68
69
70
71
72
73
# File 'lib/yle_tf/tf_hook.rb', line 68

def fetch
  source_config = parse_source_config
  source_config[:dir] = create_tmpdir
  clone_git_repo(source_config)
  @path = File.join(source_config[:dir], source_config[:path])
end

#parse_source_configObject

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/yle_tf/tf_hook.rb', line 57

def parse_source_config
  m = %r{^(?<uri>.+)//(?<path>[^?]+)(\?ref=(?<ref>.*))?$}.match(source)
  raise Error, "Invalid or missing `source` for hook '#{description}'" if !m

  {
    uri:  m[:uri],
    path: m[:path],
    ref:  m[:ref] || 'master'
  }
end

#run(tf_vars) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yle_tf/tf_hook.rb', line 42

def run(tf_vars)
  fetch if !path

  Logger.info("Running hook '#{description}'")
  YleTf::System.cmd(
    path,
    env:      vars.merge(tf_vars),
    progname: File.basename(path),
    stdout:   System::TfHookOutputLogger.new(:info),
    stderr:   System::TfHookOutputLogger.new(:error)
  )
ensure
  delete_tmpdir
end