Class: YleTf::TfHook
- Inherits:
-
Object
- Object
- YleTf::TfHook
- Defined in:
- lib/yle_tf/tf_hook.rb,
lib/yle_tf/tf_hook/runner.rb
Defined Under Namespace
Classes: Runner
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Class Method Summary collapse
-
.from_config(config, tf_env) ⇒ Object
Returns a ‘TfHook` instance from configuration hash.
-
.from_file(path) ⇒ Object
Returns a ‘Hook` instance from a local file path.
-
.merge_vars(vars, tf_env) ⇒ Object
Returns a hash with env specific vars merged into the default ones.
Instance Method Summary collapse
- #clone_git_repo(config) ⇒ Object
- #create_tmpdir ⇒ Object
- #delete_tmpdir ⇒ Object
- #fetch ⇒ Object
-
#initialize(opts = {}) ⇒ TfHook
constructor
A new instance of TfHook.
- #parse_source_config ⇒ Object
- #run(tf_vars) ⇒ Object
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
29 30 31 |
# File 'lib/yle_tf/tf_hook.rb', line 29 def description @description end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
29 30 31 |
# File 'lib/yle_tf/tf_hook.rb', line 29 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
29 30 31 |
# File 'lib/yle_tf/tf_hook.rb', line 29 def source @source end |
#vars ⇒ Object (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_tmpdir ⇒ Object
74 75 76 |
# File 'lib/yle_tf/tf_hook.rb', line 74 def create_tmpdir @tmpdir = Dir.mktmpdir('tf_hook_') end |
#delete_tmpdir ⇒ Object
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 |
#fetch ⇒ Object
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_config ⇒ Object
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 |