Class: OpsWalrus::HostProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/opswalrus/host.rb

Overview

the subclasses of HostProxy will define methods that handle method dispatch via HostProxyOpsFileInvocationBuilder objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_env, host) ⇒ HostProxy

Returns a new instance of HostProxy.



73
74
75
76
# File 'lib/opswalrus/host.rb', line 73

def initialize(runtime_env, host)
  @_host = host
  @runtime_env = runtime_env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



135
136
137
# File 'lib/opswalrus/host.rb', line 135

def method_missing(name, ...)
  @_host.send(name, ...)
end

Instance Attribute Details

#_hostObject

Returns the value of attribute _host.



71
72
73
# File 'lib/opswalrus/host.rb', line 71

def _host
  @_host
end

Class Method Details

.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

Instance Method Details

#_bootstrap_host(print_report = true) ⇒ Object

returns [stdout, stderr, exit_status]



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/opswalrus/host.rb', line 91

def _bootstrap_host(print_report = true)
  # copy over bootstrap shell script
  # io = StringIO.new(bootstrap_shell_script)
  io = File.open(__FILE__.to_pathname.dirname.join("bootstrap.sh"))
  upload_success = @_host.upload(io, "tmpopsbootstrap.sh")
  io.close
  raise Error, "Unable to upload bootstrap shell script to remote host #{to_s} (alias=#{self.alias})" unless upload_success
  @_host.execute(:chmod, "755", "tmpopsbootstrap.sh")
  sshkit_cmd = @_host.execute_cmd(:sh, "tmpopsbootstrap.sh")

  stdout, stderr, exit_status = [sshkit_cmd.full_stdout, sshkit_cmd.full_stderr, sshkit_cmd.exit_status]

  if print_report && App.instance.info?
    if exit_status == 0
      report = "Bootstrap success - #{to_s} (alias=#{self.alias})"
      stdout_report = stdout.lines.last(3).map {|line| "        #{line}" }.join()
      report << "\n    stdout:\n#{stdout_report}" unless stdout_report.empty?
      puts report
    else
      stdout_report = stdout.lines.last(3).map {|line| "        #{line}" }.join()
      stderr_report = stderr.lines.last(3).map {|line| "        #{line}" }.join()
      report = "Bootstrap failure - #{to_s} (alias=#{self.alias})"
      report << "\n    stdout:\n#{stdout_report}" unless stdout_report.empty?
      report << "\n    stderr:\n#{stderr_report}" unless stderr_report.empty?
      puts report
    end
  end

  [stdout, stderr, exit_status]
ensure
  @_host.execute(:rm, "-f", "tmpopsbootstrap.sh") rescue nil
end

#_invoke_remote(ops_file, *args, **kwargs) ⇒ Object



84
85
86
87
88
# File 'lib/opswalrus/host.rb', line 84

def _invoke_remote(ops_file, *args, **kwargs)
  App.instance.debug "Remove invoke #{ops_file.relative_path_to_app_pwd} on host `#{to_s}` with args:\n  #{args.inspect}\nkwargs:\n  #{kwargs.inspect}"

  RemoteInvocation.new(self, ops_file, ops_prompt_for_sudo_password: !!ssh_password).invoke(*args, **kwargs)
end

#_zip_copy_and_run_ops_bundle(local_host, block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/opswalrus/host.rb', line 124

def _zip_copy_and_run_ops_bundle(local_host, block)
  @_host._initialize_session

  # we run the block in the context of the host proxy object, s.t. `self` within the block evaluates to the host proxy object (i.e. self)
  retval = instance_exec(local_host, &block)    # local_host is passed as the argument to the block

  @_host._cleanup_session

  retval
end

#to_sObject

the subclasses of this class will define methods that handle method dispatch via RemoteImportInvocationContext objects



80
81
82
# File 'lib/opswalrus/host.rb', line 80

def to_s
  @_host.to_s
end