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.



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

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.



30
31
32
# File 'lib/yle_tf/tf_hook.rb', line 30

def description
  @description
end

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/yle_tf/tf_hook.rb', line 30

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



30
31
32
# File 'lib/yle_tf/tf_hook.rb', line 30

def source
  @source
end

#varsObject (readonly)

Returns the value of attribute vars.



30
31
32
# File 'lib/yle_tf/tf_hook.rb', line 30

def vars
  @vars
end

Class Method Details

.from_config(config, tf_env) ⇒ Object

Returns a ‘TfHook` instance from configuration hash



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

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



23
24
25
26
27
28
# File 'lib/yle_tf/tf_hook.rb', line 23

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



91
92
93
94
95
# File 'lib/yle_tf/tf_hook.rb', line 91

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



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

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



81
82
83
# File 'lib/yle_tf/tf_hook.rb', line 81

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

#delete_tmpdirObject



85
86
87
88
# File 'lib/yle_tf/tf_hook.rb', line 85

def delete_tmpdir
  FileUtils.rm_r(@tmpdir) if @tmpdir && Dir.exist?(@tmpdir)
  @tmpdir = nil
end

#fetchObject



66
67
68
69
70
71
# File 'lib/yle_tf/tf_hook.rb', line 66

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:



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

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



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

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