Class: Bixby::CommandSpec
- Inherits:
-
Object
- Object
- Bixby::CommandSpec
- Defined in:
- lib/bixby-common/command_spec.rb
Overview
Describes a Command execution request for the Agent
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#bundle ⇒ Object
Returns the value of attribute bundle.
-
#command ⇒ Object
Returns the value of attribute command.
-
#digest ⇒ Object
Returns the value of attribute digest.
-
#env ⇒ Object
Returns the value of attribute env.
-
#group ⇒ Object
Returns the value of attribute group.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#bundle_dir ⇒ Object
resolve the given bundle.
-
#bundle_exists? ⇒ Boolean
Check if the bundle described by this CommandSpec exists.
-
#command_exists? ⇒ Boolean
Check if the command file exists.
-
#command_file ⇒ String
Absolute command filename.
-
#digest_file ⇒ String
Bundle digest filename.
-
#initialize(params = nil) ⇒ CommandSpec
constructor
Create new CommandSpec.
-
#load_bundle_manifest ⇒ Hash
(also: #load_manifest)
Retrieve the bundle manifest.
-
#load_digest ⇒ Hash
Retrieve the bundle digest.
-
#manifest ⇒ Hash
(also: #load_config)
Retrieve the command’s Manifest, loading it from disk if necessary If no Manifest is available, returns an empty hash.
-
#manifest_file ⇒ String
(also: #config_file)
Command manifest filename.
-
#path(*relative) ⇒ String
Create and return an absolute pathname pointing to the given file.
-
#relative_path ⇒ String
Return the relative path to the bundle (inside the repository).
-
#to_s ⇒ String
Convert object to String, useful for debugging.
-
#update_digest ⇒ Object
Update the digest hashes for this bundle.
-
#validate(expected_digest) ⇒ Boolean
Validate the existence of this Command on the local system and compare digest to local version.
Methods included from Hashify
Methods included from Jsonify
Constructor Details
#initialize(params = nil) ⇒ CommandSpec
Create new CommandSpec
19 20 21 22 23 24 25 |
# File 'lib/bixby-common/command_spec.rb', line 19 def initialize(params = nil) return if params.nil? or params.empty? params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" } digest = load_digest() @digest = digest["digest"] if digest end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def args @args end |
#bundle ⇒ Object
Returns the value of attribute bundle.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def bundle @bundle end |
#command ⇒ Object
Returns the value of attribute command.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def command @command end |
#digest ⇒ Object
Returns the value of attribute digest.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def digest @digest end |
#env ⇒ Object
Returns the value of attribute env.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def env @env end |
#group ⇒ Object
Returns the value of attribute group.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def group @group end |
#repo ⇒ Object
Returns the value of attribute repo.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def repo @repo end |
#stdin ⇒ Object
Returns the value of attribute stdin.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def stdin @stdin end |
#user ⇒ Object
Returns the value of attribute user.
13 14 15 |
# File 'lib/bixby-common/command_spec.rb', line 13 def user @user end |
Instance Method Details
#bundle_dir ⇒ Object
resolve the given bundle
49 50 51 |
# File 'lib/bixby-common/command_spec.rb', line 49 def bundle_dir File.(File.join(Bixby.repo_path, self.relative_path)) end |
#bundle_exists? ⇒ Boolean
Check if the bundle described by this CommandSpec exists
70 71 72 |
# File 'lib/bixby-common/command_spec.rb', line 70 def bundle_exists? File.exists? self.bundle_dir end |
#command_exists? ⇒ Boolean
Check if the command file exists
84 85 86 |
# File 'lib/bixby-common/command_spec.rb', line 84 def command_exists? File.exists? self.command_file end |
#command_file ⇒ String
Absolute command filename
77 78 79 |
# File 'lib/bixby-common/command_spec.rb', line 77 def command_file path("bin", @command) end |
#digest_file ⇒ String
Bundle digest filename
112 113 114 |
# File 'lib/bixby-common/command_spec.rb', line 112 def digest_file path("digest") end |
#load_bundle_manifest ⇒ Hash Also known as: load_manifest
Retrieve the bundle manifest
130 131 132 133 134 135 136 |
# File 'lib/bixby-common/command_spec.rb', line 130 def load_bundle_manifest begin return MultiJson.load(File.read(path("manifest.json"))) rescue => ex end nil end |
#load_digest ⇒ Hash
Retrieve the bundle digest
119 120 121 122 123 124 125 |
# File 'lib/bixby-common/command_spec.rb', line 119 def load_digest begin return MultiJson.load(File.read(digest_file)) rescue => ex end nil end |
#manifest ⇒ Hash Also known as: load_config
Retrieve the command’s Manifest, loading it from disk if necessary If no Manifest is available, returns an empty hash
100 101 102 103 104 105 106 |
# File 'lib/bixby-common/command_spec.rb', line 100 def manifest if File.exists?(manifest_file) && File.readable?(manifest_file) then MultiJson.load(File.read(manifest_file)) else {} end end |
#manifest_file ⇒ String Also known as: config_file
Command manifest filename
91 92 93 |
# File 'lib/bixby-common/command_spec.rb', line 91 def manifest_file command_file + ".json" end |
#path(*relative) ⇒ String
Create and return an absolute pathname pointing to the given file
144 145 146 |
# File 'lib/bixby-common/command_spec.rb', line 144 def path(*relative) File.join(self.bundle_dir, *relative) end |
#relative_path ⇒ String
Return the relative path to the bundle (inside the repository)
e.g., if Bixby.repo_path = /opt/bixby/repo then a relative path would
look like:
vendor/system/monitoring
or
megacorp/sysops/scripts
63 64 65 |
# File 'lib/bixby-common/command_spec.rb', line 63 def relative_path File.join(@repo, @bundle) end |
#to_s ⇒ String
Convert object to String, useful for debugging
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/bixby-common/command_spec.rb', line 173 def to_s # :nocov: s = [] s << "CommandSpec:#{self.object_id}" s << " digest: #{self.digest}" s << " repo: #{self.repo}" s << " bundle: #{self.bundle}" s << " command: #{self.command}" s << " args: #{self.args}" s << " user: #{self.user}" s << " group: #{self.group}" s << " env: " + (self.env.nil?() ? "" : MultiJson.dump(self.env)) s << " stdin: " + Debug.pretty_str(stdin) s.join("\n") end |
#update_digest ⇒ Object
Update the digest hashes for this bundle
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/bixby-common/command_spec.rb', line 149 def update_digest path = self.bundle_dir sha = Digest::SHA2.new bundle_sha = Digest::SHA2.new digests = [] Dir.glob("#{path}/**/*").sort.each do |f| next if File.directory?(f) || File.basename(f) == "digest" || f =~ /^#{path}\/test/ bundle_sha.file(f) sha.reset() digests << { :file => f.gsub(/#{path}\//, ''), :digest => sha.file(f).hexdigest() } end @digest = { :digest => bundle_sha.hexdigest(), :files => digests } File.open(path+"/digest", 'w'){ |f| f.write(MultiJson.dump(@digest, :pretty => true, :adapter => :json_gem) + "\n") } end |
#validate(expected_digest) ⇒ Boolean
Validate the existence of this Command on the local system and compare digest to local version
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bixby-common/command_spec.rb', line 34 def validate(expected_digest) if not bundle_exists? then raise BundleNotFound.new("repo = #{@repo}; bundle = #{@bundle}") end if not command_exists? then raise CommandNotFound.new("repo = #{@repo}; bundle = #{@bundle}; command = #{@command}") end if self.digest != expected_digest then raise BundleNotFound, "digest does not match ('#{self.digest}' != '#{expected_digest}')", caller end return true end |