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.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
Instance Method Summary collapse
-
#bundle_dir ⇒ Object
resolve the given bundle.
- #bundle_exists? ⇒ Boolean
- #command_exists? ⇒ Boolean
- #command_file ⇒ Object
- #config_file ⇒ Object
- #digest_file ⇒ Object
-
#initialize(params = nil) ⇒ CommandSpec
constructor
Create new CommandSpec.
- #load_config ⇒ Object
- #load_digest ⇒ Object
- #load_manifest ⇒ Object
-
#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
-
#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
18 19 20 21 22 23 24 |
# File 'lib/bixby-common/command_spec.rb', line 18 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 |
#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 |
Instance Method Details
#bundle_dir ⇒ Object
resolve the given bundle
48 49 50 |
# File 'lib/bixby-common/command_spec.rb', line 48 def bundle_dir File.(File.join(Bixby.repo_path, self.relative_path)) end |
#bundle_exists? ⇒ Boolean
66 67 68 |
# File 'lib/bixby-common/command_spec.rb', line 66 def bundle_exists? File.exists? self.bundle_dir end |
#command_exists? ⇒ Boolean
74 75 76 |
# File 'lib/bixby-common/command_spec.rb', line 74 def command_exists? File.exists? self.command_file end |
#command_file ⇒ Object
70 71 72 |
# File 'lib/bixby-common/command_spec.rb', line 70 def command_file path("bin", @command) end |
#config_file ⇒ Object
78 79 80 |
# File 'lib/bixby-common/command_spec.rb', line 78 def config_file command_file + ".json" end |
#digest_file ⇒ Object
90 91 92 |
# File 'lib/bixby-common/command_spec.rb', line 90 def digest_file path("digest") end |
#load_config ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/bixby-common/command_spec.rb', line 82 def load_config if File.exists? config_file then MultiJson.load(File.read(config_file)) else {} end end |
#load_digest ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/bixby-common/command_spec.rb', line 94 def load_digest begin return MultiJson.load(File.read(digest_file)) rescue => ex end nil end |
#load_manifest ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/bixby-common/command_spec.rb', line 102 def load_manifest begin return MultiJson.load(File.read(path("manifest.json"))) rescue => ex end nil end |
#path(*relative) ⇒ String
Create and return an absolute pathname pointing to the given file
115 116 117 |
# File 'lib/bixby-common/command_spec.rb', line 115 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
62 63 64 |
# File 'lib/bixby-common/command_spec.rb', line 62 def relative_path File.join(@repo, @bundle) end |
#to_s ⇒ String
Convert object to String, useful for debugging
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/bixby-common/command_spec.rb', line 143 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 << " env: " + MultiJson.dump(self.env) s << " stdin: " + Debug.pretty_str(stdin) s.join("\n") end |
#update_digest ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bixby-common/command_spec.rb', line 119 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
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bixby-common/command_spec.rb', line 33 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 |