opswalrus 1.0.12 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'set'
2
+ require_relative 'invocation'
2
3
  require_relative 'ops_file_script_dsl'
3
4
 
4
5
  module OpsWalrus
@@ -8,27 +9,20 @@ module OpsWalrus
8
9
  def self.define_for(ops_file, ruby_script)
9
10
  klass = Class.new(OpsFileScript)
10
11
 
11
- # puts "OpsFileScript.define_for(#{ops_file.to_s}, #{ruby_script.to_s})"
12
-
13
12
  methods_defined = Set.new
14
13
 
15
14
  # define methods for the OpsFile's local_symbol_table: local imports and private lib directory
16
15
  ops_file.local_symbol_table.each do |symbol_name, import_reference|
17
16
  unless methods_defined.include? symbol_name
18
- # puts "defining method for local symbol table entry: #{symbol_name}"
17
+ App.instance.debug "defining method for local symbol table entry: #{symbol_name}"
19
18
  klass.define_method(symbol_name) do |*args, **kwargs, &block|
20
- # puts "resolving local symbol table entry: #{symbol_name}"
19
+ App.instance.debug "resolving local symbol table entry: #{symbol_name}"
21
20
  namespace_or_ops_file = @runtime_env.resolve_import_reference(ops_file, import_reference)
22
- # puts "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"
23
-
24
- case namespace_or_ops_file
25
- when Namespace
26
- namespace_or_ops_file
27
- namespace_or_ops_file._invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
28
- when OpsFile
29
- params_hash = namespace_or_ops_file.build_params_hash(*args, **kwargs)
30
- namespace_or_ops_file.invoke(@runtime_env, params_hash)
31
- end
21
+ App.instance.debug "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"
22
+
23
+ invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
24
+ invocation_context._invoke(*args, **kwargs)
25
+
32
26
  end
33
27
  methods_defined << symbol_name
34
28
  end
@@ -39,23 +33,18 @@ module OpsWalrus
39
33
  sibling_symbol_table_names |= ops_file.dirname.glob("*.ops").map {|ops_file_path| ops_file_path.basename(".ops").to_s } # OpsFiles
40
34
  sibling_symbol_table_names |= ops_file.dirname.glob("*").select(&:directory?).map {|dir_path| dir_path.basename.to_s } # Namespaces
41
35
  # puts "sibling_symbol_table_names=#{sibling_symbol_table_names}"
42
- # puts "methods_defined=#{methods_defined}"
36
+ App.instance.debug "methods_defined=#{methods_defined}"
43
37
  sibling_symbol_table_names.each do |symbol_name|
44
38
  unless methods_defined.include? symbol_name
45
- # puts "defining method for implicit imports: #{symbol_name}"
39
+ App.instance.debug "defining method for implicit imports: #{symbol_name}"
46
40
  klass.define_method(symbol_name) do |*args, **kwargs, &block|
47
- # puts "resolving implicit import: #{symbol_name}"
41
+ App.instance.debug "resolving implicit import: #{symbol_name}"
48
42
  namespace_or_ops_file = @runtime_env.resolve_sibling_symbol(ops_file, symbol_name)
49
- # puts "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"
50
-
51
- case namespace_or_ops_file
52
- when Namespace
53
- namespace_or_ops_file
54
- namespace_or_ops_file._invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
55
- when OpsFile
56
- params_hash = namespace_or_ops_file.build_params_hash(*args, **kwargs)
57
- namespace_or_ops_file.invoke(@runtime_env, params_hash)
58
- end
43
+ App.instance.debug "namespace_or_ops_file=#{namespace_or_ops_file.to_s}"
44
+
45
+ invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
46
+ invocation_context._invoke(*args, **kwargs)
47
+
59
48
  end
60
49
  methods_defined << symbol_name
61
50
  end
@@ -114,10 +114,11 @@ module OpsWalrus
114
114
 
115
115
  module OpsFileScriptDSL
116
116
  def ssh(*args, **kwargs, &block)
117
- hosts = inventory(*args, **kwargs).map {|host| host_proxy_class.new(host) }
117
+ runtime_env = @runtime_env
118
+
119
+ hosts = inventory(*args, **kwargs).map {|host| host_proxy_class.new(runtime_env, host) }
118
120
  sshkit_hosts = hosts.map(&:sshkit_host)
119
121
  sshkit_host_to_ops_host_map = sshkit_hosts.zip(hosts).to_h
120
- runtime_env = @runtime_env
121
122
  local_host = self
122
123
  # bootstrap_shell_script = BootstrapLinuxHostShellScript
123
124
  # on sshkit_hosts do |sshkit_host|
@@ -37,6 +37,17 @@ module OpsWalrus
37
37
  local_name
38
38
  end
39
39
 
40
+ def to_s
41
+ "PackageReference(local_name=#{@local_name}, package_uri=#{@package_uri}, version=#{@version})"
42
+ end
43
+
44
+ def summary
45
+ if version
46
+ "#{package_uri}:#{version}"
47
+ else
48
+ package_uri
49
+ end
50
+ end
40
51
  end
41
52
 
42
53
  # these are dynamic package references defined at runtime when an OpsFile's imports are being evaluated.
@@ -23,6 +23,12 @@ module OpsWalrus
23
23
  super(local_name)
24
24
  @package_reference = package_reference
25
25
  end
26
+ def to_s
27
+ "PackageDependencyReference(local_name=#{@local_name}, package_reference=#{@package_reference})"
28
+ end
29
+ def summary
30
+ "#{local_name}: #{package_reference.summary}"
31
+ end
26
32
  end
27
33
 
28
34
  class DirectoryReference < ImportReference
@@ -31,6 +37,12 @@ module OpsWalrus
31
37
  super(local_name)
32
38
  @dirname = dirname
33
39
  end
40
+ def to_s
41
+ "DirectoryReference(local_name=#{@local_name}, dirname=#{@dirname})"
42
+ end
43
+ def summary
44
+ "#{local_name}: #{dirname}"
45
+ end
34
46
  end
35
47
 
36
48
  class DynamicPackageImportReference < ImportReference
@@ -39,6 +51,12 @@ module OpsWalrus
39
51
  super(local_name)
40
52
  @package_reference = package_reference
41
53
  end
54
+ def to_s
55
+ "DynamicPackageImportReference(local_name=#{@local_name}, package_reference=#{@package_reference})"
56
+ end
57
+ def summary
58
+ "#{local_name}: #{package_reference.summary}"
59
+ end
42
60
  end
43
61
 
44
62
  class OpsFileReference < ImportReference
@@ -47,6 +65,12 @@ module OpsWalrus
47
65
  super(local_name)
48
66
  @ops_file_path = ops_file_path
49
67
  end
68
+ def to_s
69
+ "DirectoryReference(local_name=#{@local_name}, ops_file_path=#{@ops_file_path})"
70
+ end
71
+ def summary
72
+ "#{local_name}: #{ops_file_path}"
73
+ end
50
74
  end
51
75
 
52
76
  # Namespace is really just a Map of symbol_name -> (Namespace | OpsFile) pairs
@@ -82,32 +106,31 @@ module OpsWalrus
82
106
  @symbol_table[symbol_name.to_s]
83
107
  end
84
108
 
85
- # if this namespace contains an OpsFile of the same name as the namespace, e.g. pkg/install/install.ops, then this
86
- # method invokes the OpsFile of that same name and returns the result;
87
- # otherwise we return this namespace object
88
- def _invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs, &block)
89
- resolved_symbol = resolve_symbol(@dirname.basename)
90
- if resolved_symbol.is_a? OpsFile
91
- params_hash = resolved_symbol.build_params_hash(*args, **kwargs)
92
- resolved_symbol.invoke(runtime_env, params_hash)
93
- else
94
- self
95
- end
96
- end
97
-
98
- def method_missing(name, *args, **kwargs, &block)
99
- # puts "method_missing: #{name}"
100
- # puts caller
101
- resolved_symbol = resolve_symbol(name)
102
- case resolved_symbol
103
- when Namespace
104
- resolved_symbol
105
- resolved_symbol._invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
106
- when OpsFile
107
- params_hash = resolved_symbol.build_params_hash(*args, **kwargs)
108
- resolved_symbol.invoke(runtime_env, params_hash)
109
- end
110
- end
109
+ # # if this namespace contains an OpsFile of the same name as the namespace, e.g. pkg/install/install.ops, then this
110
+ # # method invokes the OpsFile of that same name and returns the result;
111
+ # # otherwise we return this namespace object
112
+ # def _invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs, &block)
113
+ # resolved_symbol = resolve_symbol(@dirname.basename)
114
+ # if resolved_symbol.is_a? OpsFile
115
+ # params_hash = resolved_symbol.build_params_hash(*args, **kwargs)
116
+ # resolved_symbol.invoke(runtime_env, params_hash)
117
+ # else
118
+ # self
119
+ # end
120
+ # end
121
+
122
+ # def method_missing(name, *args, **kwargs, &block)
123
+ # # puts "method_missing: #{name}"
124
+ # # puts caller
125
+ # resolved_symbol = resolve_symbol(name)
126
+ # case resolved_symbol
127
+ # when Namespace
128
+ # resolved_symbol._invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
129
+ # when OpsFile
130
+ # params_hash = resolved_symbol.build_params_hash(*args, **kwargs)
131
+ # resolved_symbol.invoke(runtime_env, params_hash)
132
+ # end
133
+ # end
111
134
  end
112
135
 
113
136
  # the assumption is that we have a bundle directory with all the packages in it
@@ -179,6 +202,10 @@ module OpsWalrus
179
202
  path_map
180
203
  end
181
204
 
205
+ def includes_path?(path)
206
+ !!@path_map[path]
207
+ end
208
+
182
209
  def dynamically_add_new_package_dir(new_package_dir)
183
210
  # patch the symbol resolution (namespace) tree
184
211
  dir_basename = new_package_dir.basename
@@ -199,19 +226,22 @@ module OpsWalrus
199
226
 
200
227
  # returns a Namespace or OpsFile
201
228
  def resolve_symbol(origin_ops_file, symbol_name)
202
- lookup_namespace(origin_ops_file)&.resolve_symbol(symbol_name)
229
+ resolved_namespace_or_ops_file = lookup_namespace(origin_ops_file)&.resolve_symbol(symbol_name)
230
+ App.instance.debug("LoadPath#resolve_symbol(#{origin_ops_file}, #{symbol_name}) -> #{resolved_namespace_or_ops_file}")
231
+ resolved_namespace_or_ops_file
203
232
  end
204
233
 
205
234
  # returns a Namespace | OpsFile
206
235
  def resolve_import_reference(origin_ops_file, import_reference)
207
- case import_reference
236
+ resolved_namespace_or_ops_file = case import_reference
208
237
  when PackageDependencyReference
209
238
  # puts "root namespace: #{@root_namespace.symbol_table}"
210
- @root_namespace.resolve_symbol(import_reference.package_reference.import_resolution_dirname) # returns the Namespace associated with the bundled package dirname (i.e. the local name)
239
+ @root_namespace.resolve_symbol(import_reference.package_reference.import_resolution_dirname) # returns the Namespace associated with the bundled package import_resolution_dirname (i.e. the local name)
211
240
  when DynamicPackageImportReference
212
241
  dynamic_package_reference = import_reference.package_reference
213
242
  @dynamic_package_additions_memo[dynamic_package_reference] ||= begin
214
243
  # puts "Downloading dynamic package: #{dynamic_package_reference.inspect}"
244
+ App.instance.debug("Downloading dynamic package: #{dynamic_package_reference}")
215
245
  dynamically_added_package_dir = @runtime_env.app.bundler.download_git_package(dynamic_package_reference.package_uri, dynamic_package_reference.version)
216
246
  dynamically_add_new_package_dir(dynamically_added_package_dir)
217
247
  dynamically_added_package_dir
@@ -222,6 +252,8 @@ module OpsWalrus
222
252
  when OpsFileReference
223
253
  @path_map[import_reference.ops_file_path]
224
254
  end
255
+ App.instance.debug("LoadPath#resolve_import_reference(#{origin_ops_file}, #{import_reference}) -> #{resolved_namespace_or_ops_file}")
256
+ resolved_namespace_or_ops_file
225
257
  end
226
258
  end
227
259
 
@@ -316,23 +348,41 @@ module OpsWalrus
316
348
  ops_file.invoke(self, params_hash)
317
349
  end
318
350
 
319
- # returns a Namespace or OpsFile
351
+ def find_load_path_that_includes_path(path)
352
+ load_path = [@bundle_load_path, @app_load_path].find {|load_path| load_path.includes_path?(path) }
353
+ raise SymbolResolutionError, "No load path includes the path: #{path}" unless load_path
354
+ load_path
355
+ end
356
+
357
+ # returns a Namespace | OpsFile
320
358
  def resolve_sibling_symbol(origin_ops_file, symbol_name)
321
- @app_load_path.resolve_symbol(origin_ops_file, symbol_name)
359
+ # if the origin_ops_file's file path is contained within a Bundler::BUNDLE_DIR directory, then we want to consult the @bundle_load_path
360
+ # otherwise, we want to consult the @app_load_path
361
+ load_path = find_load_path_that_includes_path(origin_ops_file.ops_file_path)
362
+ namespace_or_ops_file = load_path.resolve_symbol(origin_ops_file, symbol_name)
363
+ raise SymbolResolutionError, "Symbol '#{symbol_name}' not in load path for #{origin_ops_file.ops_file_path}" unless namespace_or_ops_file
364
+ namespace_or_ops_file
322
365
  end
323
366
 
324
367
  # returns a Namespace | OpsFile
325
368
  def resolve_import_reference(origin_ops_file, import_reference)
326
- case import_reference
369
+ load_path = case import_reference
327
370
 
328
- # we know we're dealing with a package dependency reference, so we want to do the lookup in the bundle load path, where package dependencies live
371
+ # We know we're dealing with a package dependency reference, so we want to do the lookup in the bundle load path, where package dependencies live.
372
+ # Package references are guaranteed to live in the bundle dir
329
373
  when PackageDependencyReference, DynamicPackageImportReference
330
- @bundle_load_path.resolve_import_reference(origin_ops_file, import_reference)
331
-
332
- # we know we're dealing with a directory reference or OpsFile reference outside of the bundle dir, so we want to do the lookup in the app load path
333
- when DirectoryReference, OpsFileReference
334
- @app_load_path.resolve_import_reference(origin_ops_file, import_reference)
374
+ @bundle_load_path
375
+ when DirectoryReference
376
+ find_load_path_that_includes_path(import_reference.dirname)
377
+ when OpsFileReference
378
+ find_load_path_that_includes_path(import_reference.ops_file_path)
335
379
  end
380
+
381
+ namespace_or_ops_file = load_path.resolve_import_reference(origin_ops_file, import_reference)
382
+
383
+ raise SymbolResolutionError, "Import reference '#{import_reference.summary}' not in load path for #{origin_ops_file.ops_file_path}" unless namespace_or_ops_file
384
+
385
+ namespace_or_ops_file
336
386
  end
337
387
 
338
388
  end
@@ -1,3 +1,3 @@
1
1
  module OpsWalrus
2
- VERSION = "1.0.12"
2
+ VERSION = "1.0.14"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opswalrus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.12
4
+ version: 1.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Ellis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-21 00:00:00.000000000 Z
11
+ date: 2023-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: citrus
@@ -132,6 +132,7 @@ files:
132
132
  - lib/opswalrus/bootstrap.sh
133
133
  - lib/opswalrus/bundler.rb
134
134
  - lib/opswalrus/cli.rb
135
+ - lib/opswalrus/errors.rb
135
136
  - lib/opswalrus/git.rb
136
137
  - lib/opswalrus/host.rb
137
138
  - lib/opswalrus/hosts_file.rb