Method: OpsWalrus::HostProxy.define_host_proxy_class

Defined in:
lib/opswalrus/host.rb

.define_host_proxy_class(ops_file) ⇒ Object



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
# File 'lib/opswalrus/host.rb', line 17

def self.define_host_proxy_class(ops_file)
  klass = Class.new(HostProxy)

  methods_defined = Set.new

  # define methods for every import in the script
  ops_file.local_symbol_table.each do |symbol_name, import_reference|
    unless methods_defined.include? 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 = case import_reference
        # we know we're dealing with a package dependency reference, so we want to run an ops file contained within the bundle directory,
        # therefore, we want to reference the specified ops file with respect to the bundle dir
        when PackageDependencyReference, DynamicPackageImportReference
          RemoteImportInvocationContext.new(@runtime_env, self, namespace_or_ops_file, true, ops_prompt_for_sudo_password: !!ssh_password)

        # we know we're dealing with a directory reference or OpsFile reference outside of the bundle dir, so we want to reference
        # the specified ops file with respect to the root directory, and not with respect to the bundle dir
        when DirectoryReference, OpsFileReference
          RemoteImportInvocationContext.new(@runtime_env, self, namespace_or_ops_file, false, ops_prompt_for_sudo_password: !!ssh_password)
        end

        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 = Set.new
  sibling_symbol_table |= ops_file.dirname.glob("*.ops").map {|ops_file_path| ops_file_path.basename(".ops").to_s }   # OpsFiles
  sibling_symbol_table |= ops_file.dirname.glob("*").select(&:directory?).map {|dir_path| dir_path.basename.to_s }    # Namespaces
  sibling_symbol_table.each do |symbol_name|
    unless methods_defined.include? 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 = RemoteImportInvocationContext.new(@runtime_env, self, namespace_or_ops_file, false, ops_prompt_for_sudo_password: !!ssh_password)
        invocation_context._invoke(*args, **kwargs)
      end
      methods_defined << symbol_name
    end
  end

  klass
end