class YleTf::TfHook

Attributes

description[R]
path[R]
source[R]
vars[R]

Public Class Methods

from_config(config, tf_env) click to toggle source

Returns a `TfHook` instance from configuration hash

# 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) click to toggle source

Returns a `Hook` instance from a local file path

# 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) click to toggle source

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

# 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
new(opts = {}) click to toggle source
# 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

Public Instance Methods

clone_git_repo(config) click to toggle source
# 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_tmpdir() click to toggle source
# File lib/yle_tf/tf_hook.rb, line 83
def create_tmpdir
  @tmpdir = Dir.mktmpdir('tf_hook_')
end
delete_tmpdir() click to toggle source
# 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
fetch() click to toggle source
# 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_config() click to toggle source
# 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) click to toggle source
# 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