Class: OpsWalrus::OpsFileScript

Inherits:
Object
  • Object
show all
Includes:
OpsFileScriptDSL
Defined in:
lib/opswalrus/ops_file_script.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpsFileScriptDSL

#current_dir, #debug, #desc, #env, #exit, #import, #inventory, #parse_stdout_and_script_return_value, #read_secret, #reboot, #report_on, #sh, #sh?, #shell, #shell!, #ssh, #ssh_noprep, #warn

Constructor Details

#initialize(ops_file, ruby_script) ⇒ OpsFileScript

Returns a new instance of OpsFileScript.



92
93
94
95
96
97
98
# File 'lib/opswalrus/ops_file_script.rb', line 92

def initialize(ops_file, ruby_script)
  @ops_file = ops_file
  @script = ruby_script
  @runtime_env = nil              # this is set at the very first line of #_invoke
  @params = nil                   # this is set at the very first line of #_invoke
  @runtime_ops_file_path = nil    # this is set at the very first line of #_invoke
end

Instance Attribute Details

#ops_fileObject

Returns the value of attribute ops_file.



90
91
92
# File 'lib/opswalrus/ops_file_script.rb', line 90

def ops_file
  @ops_file
end

Class Method Details

.define_for(ops_file, ruby_script) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opswalrus/ops_file_script.rb', line 10

def self.define_for(ops_file, ruby_script)
  klass = Class.new(OpsFileScript)

  methods_defined = Set.new

  # define methods for the OpsFile's local_symbol_table: local imports and private lib directory
  ops_file.local_symbol_table.each do |symbol_name, import_reference|
    unless methods_defined.include? symbol_name
      App.instance.trace "defining method for local symbol table entry: #{symbol_name}"
      klass.define_method(symbol_name) do |*args, **kwargs, &block|
        App.instance.trace "resolving local symbol table entry: #{symbol_name}"
        namespace_or_ops_file = @runtime_env.resolve_import_reference(ops_file, import_reference)
        App.instance.trace "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"

        invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
        invocation_context._invoke(*args, **kwargs)

      end
      methods_defined << symbol_name
    end
  end

  # define methods for every Namespace or OpsFile within the namespace that the OpsFile resides within
  sibling_symbol_table_names = Set.new
  sibling_symbol_table_names |= ops_file.dirname.glob("*.ops").map {|ops_file_path| ops_file_path.basename(".ops").to_s }   # OpsFiles
  sibling_symbol_table_names |= ops_file.dirname.glob("*").select(&:directory?).map {|dir_path| dir_path.basename.to_s }    # Namespaces
  # App.instance.trace "sibling_symbol_table_names=#{sibling_symbol_table_names}"
  App.instance.trace "methods_defined=#{methods_defined}"
  sibling_symbol_table_names.each do |symbol_name|
    unless methods_defined.include? symbol_name
      App.instance.trace "defining method for implicit imports: #{symbol_name}"
      klass.define_method(symbol_name) do |*args, **kwargs, &block|
        App.instance.trace "resolving implicit import: #{symbol_name}"
        namespace_or_ops_file = @runtime_env.resolve_sibling_symbol(ops_file, symbol_name)
        App.instance.trace "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"

        invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
        invocation_context._invoke(*args, **kwargs)

      end
      methods_defined << symbol_name
    end
  end

  # the evaluation context needs to be a module with all of the following:
  # - OpsFileScriptDSL methods
  # - @runtime_env
  # - @params
  # - #host_proxy_class
  # - #backend
  # - all the dynamically defined methods in the subclass of Invocation
  #
  # Return value is whatever the script returned with one exception:
  # - if the script returns a Hash or an Array, the return value is an EasyNavProxy
  invoke_method_definition = <<~INVOKE_METHOD
    def _invoke(runtime_env, hashlike_params)
      @runtime_env = runtime_env
      @params = hashlike_params.easynav
      @runtime_ops_file_path = __FILE__
      _retval = begin
        #{ruby_script}
      end
      case _retval
      when Hash, Array
        _retval.easynav
      else
        _retval
      end
    end
  INVOKE_METHOD

  invoke_method_line_count_prior_to_ruby_script_from_ops_file = 5
  klass.module_eval(invoke_method_definition, ops_file.ops_file_path.to_s, ops_file.script_line_offset - invoke_method_line_count_prior_to_ruby_script_from_ops_file)

  klass
end

Instance Method Details

#_invoke(runtime_env, hashlike_params) ⇒ Object

The _invoke method is dynamically defined as part of OpsFileScript.define_for



109
110
111
# File 'lib/opswalrus/ops_file_script.rb', line 109

def _invoke(runtime_env, hashlike_params)
  raise "Not implemented in base class."
end

#backendObject



100
101
102
# File 'lib/opswalrus/ops_file_script.rb', line 100

def backend
  @runtime_env.pty
end

#host_proxy_classObject



104
105
106
# File 'lib/opswalrus/ops_file_script.rb', line 104

def host_proxy_class
  @ops_file.host_proxy_class
end

#inspectObject



122
123
124
# File 'lib/opswalrus/ops_file_script.rb', line 122

def inspect
  "OpsFileScript[#{ops_file.ops_file_path}]"
end

#params(*keys, default: nil) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/opswalrus/ops_file_script.rb', line 113

def params(*keys, default: nil)
  keys = keys.map(&:to_s)
  if keys.empty?
    @params
  else
    @params.dig(*keys) || default
  end
end

#to_sObject



126
127
128
# File 'lib/opswalrus/ops_file_script.rb', line 126

def to_s
  @script
end