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.



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

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.



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

def description
  @description
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

#varsObject (readonly)

Returns the value of attribute vars.



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

def vars
  @vars
end

Class Method Details

.from_config(config, tf_env) ⇒ Object

Returns a ‘TfHook` instance from configuration hash



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

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



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

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



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

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



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

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

#create_tmpdirObject



74
75
76
# File 'lib/yle_tf/tf_hook.rb', line 74

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

#delete_tmpdirObject



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

def delete_tmpdir
  FileUtils.rm_r(@tmpdir) if @tmpdir
  @tmpdir = nil
end

#fetchObject



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

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:



48
49
50
51
52
53
54
55
56
57
# File 'lib/yle_tf/tf_hook.rb', line 48

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



39
40
41
42
43
44
45
46
# File 'lib/yle_tf/tf_hook.rb', line 39

def run(tf_vars)
  fetch if !path

  Logger.info("Running hook '#{description}'...")
  YleTf::System.cmd(path, env: vars.merge(tf_vars))
ensure
  delete_tmpdir
end