Class: BaseProvision

Inherits:
Object
  • Object
show all
Includes:
Executable, ScriptLocator
Defined in:
lib/script_executor/base_provision.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Executable

#execute

Methods included from ScriptLocator

#errors, #evaluate_script_body, #scripts

Constructor Details

#initialize(config_file_name, scripts_file_names) ⇒ BaseProvision

Returns a new instance of BaseProvision.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/script_executor/base_provision.rb', line 13

def initialize(config_file_name, scripts_file_names)
  @terminal = HighLine.new
  @interpolator = TextInterpolator.new

  @env = read_config(config_file_name)

  @script_list = {}

  scripts_file_names.each do |name|
    @script_list.merge!(scripts(name))
  end

  @server_info = env[:node] ? env[:node] : {}

  create_script_methods
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/script_executor/base_provision.rb', line 11

def env
  @env
end

#interpolatorObject (readonly)

Returns the value of attribute interpolator.



11
12
13
# File 'lib/script_executor/base_provision.rb', line 11

def interpolator
  @interpolator
end

#script_listObject (readonly)

Returns the value of attribute script_list.



11
12
13
# File 'lib/script_executor/base_provision.rb', line 11

def script_list
  @script_list
end

#server_infoObject (readonly)

Returns the value of attribute server_info.



11
12
13
# File 'lib/script_executor/base_provision.rb', line 11

def server_info
  @server_info
end

Instance Method Details

#ask_password(message) ⇒ Object



36
37
38
# File 'lib/script_executor/base_provision.rb', line 36

def ask_password(message)
  @terminal.ask(message) {|q| q.echo = '*'}
end

#create_script_methodsObject



50
51
52
53
54
55
56
# File 'lib/script_executor/base_provision.rb', line 50

def create_script_methods
  script_list.keys.each do |name|
    singleton_class.send(:define_method, name.to_sym) do |type, params|
      self.run name.to_s, type, env.merge(ARGV: params.join(' '))
    end
  end
end

#create_thor_methods(parent_class, type = :string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/script_executor/base_provision.rb', line 58

def create_thor_methods(parent_class, type=:string)
  if parent_class.ancestors.collect(&:to_s).include?('Thor')
    provision = self

    provision.script_list.each do |name, value|
      title = value[:comment]

      title = title.nil? ? name : title

      parent_class.send(:desc, name, title) if parent_class.respond_to?(:desc)

      parent_class.send(:define_method, name.to_sym) do |*params|
        provision.send "#{name}".to_sym, type, params
      end
    end
  end
end

#read_config(config_file_name) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/script_executor/base_provision.rb', line 40

def read_config(config_file_name)
  hash = JSON.parse(File.read(config_file_name), :symbolize_names => true)

  result = interpolator.interpolate hash

  puts interpolator.errors if interpolator.errors.size > 0

  result
end

#run(script_name, type = :string, env = {}) ⇒ Object



30
31
32
33
34
# File 'lib/script_executor/base_provision.rb', line 30

def run(script_name, type=:string, env={})
  execute(server_info) do
    evaluate_script_body(script_list[script_name.to_sym][:code], env, type)
  end
end