Class: OpsWalrus::OpsFile
- Inherits:
-
Object
- Object
- OpsWalrus::OpsFile
- Defined in:
- lib/opswalrus/ops_file.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#ops_file_path ⇒ Object
Returns the value of attribute ops_file_path.
-
#yaml ⇒ Object
Returns the value of attribute yaml.
Instance Method Summary collapse
-
#basename ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “info”.
- #build_params_hash(*args, **kwargs) ⇒ Object
-
#dirname ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “/home/david/sync/projects/ops/ops/core/host”.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #host_proxy_class ⇒ Object
- #import_string_to_import_reference(local_name, import_str) ⇒ Object
-
#imports ⇒ Object
returns a map of the form: {“local_symbol” => import_reference, … } import_reference is one of: 1.
-
#initialize(app, ops_file_path) ⇒ OpsFile
constructor
A new instance of OpsFile.
- #invoke(runtime_env, hashlike_params) ⇒ Object
- #lazy_load_file ⇒ Object
-
#local_symbol_table ⇒ Object
symbol table derived from explicit imports and the import for the private lib directory if it exists map of: “symbol_name” => ImportReference.
- #output ⇒ Object
- #package_file ⇒ Object
- #params ⇒ Object
- #parse(script_string) ⇒ Object
-
#private_lib_dir ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “/home/david/sync/projects/ops/ops/core/host/info”.
-
#relative_path_to_app_pwd ⇒ Object
if ops_file_path is /home/david/sync/projects/ops/davidinfra/opswalrus_bundle/pkg_https—github.com-opswalrus-core_version_/file/exists.ops and the @app.pwd is /home/david/sync/projects/ops/davidinfra then this returns opswalrus_bundle/pkg_https—github.com-opswalrus-core_version_/file/exists.ops.
- #resolve_import(symbol_name) ⇒ Object
-
#script ⇒ Object
returns an instance of the class returned by #script_class.
-
#script_class ⇒ Object
returns a subclass of OpsFileScript.
- #script_line_offset ⇒ Object
-
#sibling_directories ⇒ Object
irb(main):073:0> OpsFile.new(“/home/david/sync/projects/ops/example/davidinfra/test.ops”).sibling_directories => [#<Pathname:/home/david/sync/projects/ops/example/davidinfra/caddy>, #<Pathname:/home/david/sync/projects/ops/example/davidinfra/prepare_host>, #<Pathname:/home/david/sync/projects/ops/example/davidinfra/roles>].
- #sibling_ops_files ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(app, ops_file_path) ⇒ OpsFile
Returns a new instance of OpsFile.
14 15 16 17 |
# File 'lib/opswalrus/ops_file.rb', line 14 def initialize(app, ops_file_path) @app = app @ops_file_path = ops_file_path.to_pathname. end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
10 11 12 |
# File 'lib/opswalrus/ops_file.rb', line 10 def app @app end |
#ops_file_path ⇒ Object
Returns the value of attribute ops_file_path.
11 12 13 |
# File 'lib/opswalrus/ops_file.rb', line 11 def ops_file_path @ops_file_path end |
#yaml ⇒ Object
Returns the value of attribute yaml.
12 13 14 |
# File 'lib/opswalrus/ops_file.rb', line 12 def yaml @yaml end |
Instance Method Details
#basename ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “info”
251 252 253 |
# File 'lib/opswalrus/ops_file.rb', line 251 def basename @ops_file_path.basename(".ops") end |
#build_params_hash(*args, **kwargs) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/opswalrus/ops_file.rb', line 182 def build_params_hash(*args, **kwargs) params_hash = {} # if there is only one Hash object in args, treat that as the params hash if args.size == 1 && args.first.is_a?(Hash) tmp_params_hash = args.first.transform_keys(&:to_s) params_hash.merge!(tmp_params_hash) end # if there are the same number of args as there are params, then treat each one as the corresponding param if args.size == params.keys.size tmp_params_hash = params.keys.zip(args).to_h.transform_keys(&:to_s) params_hash.merge!(tmp_params_hash) end # merge in the kwargs as part of the params hash params_hash.merge!(kwargs.transform_keys(&:to_s)) params_hash end |
#dirname ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “/home/david/sync/projects/ops/ops/core/host”
246 247 248 |
# File 'lib/opswalrus/ops_file.rb', line 246 def dirname @ops_file_path.dirname end |
#eql?(other) ⇒ Boolean
23 24 25 |
# File 'lib/opswalrus/ops_file.rb', line 23 def eql?(other) self.class == other.class && self.hash == other.hash end |
#hash ⇒ Object
19 20 21 |
# File 'lib/opswalrus/ops_file.rb', line 19 def hash @ops_file_path.hash end |
#host_proxy_class ⇒ Object
27 28 29 |
# File 'lib/opswalrus/ops_file.rb', line 27 def host_proxy_class @host_proxy_class || (lazy_load_file && @host_proxy_class) end |
#import_string_to_import_reference(local_name, import_str) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/opswalrus/ops_file.rb', line 134 def import_string_to_import_reference(local_name, import_str) if package_reference = package_file&.dependency(import_str) # package dependency reference # in this context, import_str is the local package name documented in the package's dependencies return PackageDependencyReference.new(local_name, package_reference) end import_path = import_str.to_pathname if import_path.absolute? && import_path.exist? # absolute path reference return case when import_path.directory? DirectoryReference.new(local_name, import_path.realpath) when import_path.file? && import_path.extname.downcase == ".ops" OpsFileReference.new(local_name, import_path.realpath) else raise Error, "Unknown import reference for absolute path: #{local_name}: #{import_path}" end end if import_path.relative? # relative path reference rebased_path = dirname.join(import_path) if rebased_path.exist? return case when rebased_path.directory? DirectoryReference.new(local_name, rebased_path.realpath) when rebased_path.file? && rebased_path.extname.downcase == ".ops" OpsFileReference.new(local_name, rebased_path.realpath) else raise Error, "Unknown import reference for relative path: #{local_name}: #{import_path}" end elsif rebased_path.sub_ext(".ops").exist? return OpsFileReference.new(local_name, rebased_path.sub_ext(".ops").realpath) end end package_uri = import_str if package_uri = Git.repo?(package_uri) # ops file has imported an ad-hoc git repo destination_package_path = app.bundler.dynamic_package_path_for_git_package(package_uri) App.instance.trace "DynamicPackageImportReference: #{local_name} -> #{destination_package_path}" return DynamicPackageImportReference.new(local_name, DynamicPackageReference.new(local_name, package_uri, nil)) end raise Error, "Unknown import reference: #{local_name}: #{import_str.inspect}" end |
#imports ⇒ Object
returns a map of the form: {“local_symbol” => import_reference, … } import_reference is one of:
-
a package reference that matches one of the local package names in the dependencies captured in packages.yaml
-
a package reference that resolves to a relative path pointing at a package directory
-
a path that resolves to a directory containing ops files
-
a path that resolves to an ops file
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/opswalrus/ops_file.rb', line 104 def imports @imports ||= begin imports_hash = yaml["imports"] || {} imports_hash.map do |local_name, yaml_import_reference| local_name = local_name.to_s import_reference = case yaml_import_reference # when the imports line says: # imports: # my_package: my_package in String => import_str import_string_to_import_reference(local_name, import_str) # when the imports line says: # imports: # my_package: # url: https://... # version: 2.1 # in Hash => package_defn # url = package_defn["url"] # version = package_defn["version"] # PackageReference.new(local_name, url, version&.to_s) else raise Error, "Unknown import reference: #{local_name}: #{yaml_import_reference.inspect}" end [local_name, import_reference] end.to_h end end |
#invoke(runtime_env, hashlike_params) ⇒ Object
177 178 179 180 |
# File 'lib/opswalrus/ops_file.rb', line 177 def invoke(runtime_env, hashlike_params) # this invokes the dynamically generated _invoke method that is defined at runtime within OpsFileScript.define_for(...) script._invoke(runtime_env, hashlike_params) end |
#lazy_load_file ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/opswalrus/ops_file.rb', line 49 def lazy_load_file App.instance.trace "OpsFile#lazy_load_file for #{@ops_file_path}" yaml, ruby_script = if @ops_file_path.exist? parse(File.read(@ops_file_path)) end || ["", ""] @script_line_offset = yaml.lines.size + 1 # +1 to account for the ... line @yaml = YAML.load(yaml) || {} # post_invariant: @yaml is a Hash @script_class = OpsFileScript.define_for(self, ruby_script) @script = @script_class.new(self, ruby_script) @host_proxy_class = HostProxy.define_host_proxy_class(self) end |
#local_symbol_table ⇒ Object
symbol table derived from explicit imports and the import for the private lib directory if it exists map of: “symbol_name” => ImportReference
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/opswalrus/ops_file.rb', line 205 def local_symbol_table @local_symbol_table ||= begin local_symbol_table = {} local_symbol_table.merge!(imports) # this is the import for the private lib directory if it exists if private_lib_dir.exist? local_symbol_table[basename.to_s] = DirectoryReference.new(basename.to_s, private_lib_dir) end local_symbol_table end end |
#output ⇒ Object
80 81 82 |
# File 'lib/opswalrus/ops_file.rb', line 80 def output yaml["output"] end |
#package_file ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/opswalrus/ops_file.rb', line 84 def package_file return @package_file if @package_file_evaluated @package_file ||= begin ops_file_path = @ops_file_path.realpath ops_file_path.ascend.each do |path| candidate_package_file_path = path.join("package.yaml") return PackageFile.new(candidate_package_file_path) if candidate_package_file_path.exist? end nil end @package_file_evaluated = true @package_file end |
#params ⇒ Object
76 77 78 |
# File 'lib/opswalrus/ops_file.rb', line 76 def params yaml["params"] || {} end |
#parse(script_string) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/opswalrus/ops_file.rb', line 61 def parse(script_string) file_halves = script_string.split(/^\.\.\.$/, 2) case file_halves.count when 0 yaml, ruby_script = "", "" when 1 yaml, ruby_script = "", file_halves.first when 2 yaml, ruby_script = *file_halves else raise Error, "Unexpected number of file sections in #{@ops_file_path}: #{file_halves.inspect}" end [yaml, ruby_script] end |
#private_lib_dir ⇒ Object
“/home/david/sync/projects/ops/ops/core/host/info.ops” => “/home/david/sync/projects/ops/ops/core/host/info”
256 257 258 |
# File 'lib/opswalrus/ops_file.rb', line 256 def private_lib_dir dirname.join(basename) end |
#relative_path_to_app_pwd ⇒ Object
if ops_file_path is /home/david/sync/projects/ops/davidinfra/opswalrus_bundle/pkg_https—github.com-opswalrus-core_version_/file/exists.ops and the @app.pwd is /home/david/sync/projects/ops/davidinfra then this returns opswalrus_bundle/pkg_https—github.com-opswalrus-core_version_/file/exists.ops
241 242 243 |
# File 'lib/opswalrus/ops_file.rb', line 241 def relative_path_to_app_pwd @ops_file_path.relative_path_from(@app.pwd) end |
#resolve_import(symbol_name) ⇒ Object
220 221 222 |
# File 'lib/opswalrus/ops_file.rb', line 220 def resolve_import(symbol_name) local_symbol_table[symbol_name] end |
#script ⇒ Object
returns an instance of the class returned by #script_class
45 46 47 |
# File 'lib/opswalrus/ops_file.rb', line 45 def script @script || (lazy_load_file && @script) end |
#script_class ⇒ Object
returns a subclass of OpsFileScript
40 41 42 |
# File 'lib/opswalrus/ops_file.rb', line 40 def script_class @script_class || (lazy_load_file && @script_class) end |
#script_line_offset ⇒ Object
35 36 37 |
# File 'lib/opswalrus/ops_file.rb', line 35 def script_line_offset @script_line_offset || (lazy_load_file && @script_line_offset) end |
#sibling_directories ⇒ Object
irb(main):073:0> OpsFile.new(“/home/david/sync/projects/ops/example/davidinfra/test.ops”).sibling_directories
> [#<Pathname:/home/david/sync/projects/ops/example/davidinfra/caddy>, #<Pathname:/home/david/sync/projects/ops/example/davidinfra/prepare_host>, #<Pathname:/home/david/sync/projects/ops/example/davidinfra/roles>]
266 267 268 |
# File 'lib/opswalrus/ops_file.rb', line 266 def sibling_directories dirname.glob("*").select(&:directory?) end |
#sibling_ops_files ⇒ Object
260 261 262 |
# File 'lib/opswalrus/ops_file.rb', line 260 def sibling_ops_files dirname.glob("*.ops").map {|path| OpsFile.new(app, path) } end |
#to_s ⇒ Object
270 271 272 |
# File 'lib/opswalrus/ops_file.rb', line 270 def to_s "OpsFile: #{ops_file_path}" end |