class Bixby::CommandSpec

Describes a Command execution request for the Agent

Attributes

args[RW]
bundle[RW]
command[RW]
digest[RW]
env[RW]
group[RW]
repo[RW]
stdin[RW]
user[RW]

Public Class Methods

new(params = nil) click to toggle source

Create new CommandSpec

@param [Hash] params Hash of attributes to initialize with

# 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

Public Instance Methods

bundle_dir() click to toggle source

resolve the given bundle

# File lib/bixby-common/command_spec.rb, line 49
def bundle_dir
  File.expand_path(File.join(Bixby.repo_path, self.relative_path))
end
bundle_exists?() click to toggle source

Check if the bundle described by this CommandSpec exists

@return [Boolean]

# File lib/bixby-common/command_spec.rb, line 70
def bundle_exists?
  File.exists? self.bundle_dir
end
command_exists?() click to toggle source

Check if the command file exists

@return [Boolean]

# File lib/bixby-common/command_spec.rb, line 84
def command_exists?
  File.exists? self.command_file
end
command_file() click to toggle source

Absolute command filename

@return [String]

# File lib/bixby-common/command_spec.rb, line 77
def command_file
  path("bin", @command)
end
config_file()
Alias for: manifest_file
digest_file() click to toggle source

Bundle digest filename

@return [String] path to bundle digest file

# File lib/bixby-common/command_spec.rb, line 112
def digest_file
  path("digest")
end
load_bundle_manifest() click to toggle source

Retrieve the bundle manifest

@return [Hash]

# 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
Also aliased as: load_manifest
load_config()
Alias for: manifest
load_digest() click to toggle source

Retrieve the bundle digest

@return [Hash]

# 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
load_manifest()
manifest() click to toggle source

Retrieve the command’s Manifest, loading it from disk if necessary If no Manifest is available, returns an empty hash

@return [Hash]

# 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
Also aliased as: load_config
manifest_file() click to toggle source

Command manifest filename

@return [String]

# File lib/bixby-common/command_spec.rb, line 91
def manifest_file
  command_file + ".json"
end
Also aliased as: config_file
path(*relative) click to toggle source

Create and return an absolute pathname pointing to the given file

@param [String] *relative

@return [String]

# File lib/bixby-common/command_spec.rb, line 144
def path(*relative)
  File.join(self.bundle_dir, *relative)
end
relative_path() click to toggle source

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

@return [String]

# File lib/bixby-common/command_spec.rb, line 63
def relative_path
  File.join(@repo, @bundle)
end
to_s() click to toggle source

Convert object to String, useful for debugging

@return [String]

# 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() click to toggle source

Update the digest hashes for this bundle

# 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) click to toggle source

Validate the existence of this Command on the local system and compare digest to local version

@param [String] expected_digest @return [Boolean] returns true if available, else raises error @raise [BundleNotFound] If bundle doesn’t exist or digest does not match @raise [CommandNotFound] If command doesn’t exist

# 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