Class: Bixby::CommandSpec

Inherits:
Object
  • Object
show all
Includes:
Hashify, Jsonify
Defined in:
lib/bixby-common/command_spec.rb

Overview

Describes a Command execution request for the Agent

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hashify

#to_hash

Methods included from Jsonify

included, #to_json

Constructor Details

#initialize(params = nil) ⇒ CommandSpec

Create new CommandSpec

Parameters:

  • params (Hash) (defaults to: nil)

    Hash of attributes to initialize with



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

#argsObject

Returns the value of attribute args.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def args
  @args
end

#bundleObject

Returns the value of attribute bundle.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def bundle
  @bundle
end

#commandObject

Returns the value of attribute command.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def command
  @command
end

#digestObject

Returns the value of attribute digest.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def digest
  @digest
end

#envObject

Returns the value of attribute env.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def env
  @env
end

#groupObject

Returns the value of attribute group.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def group
  @group
end

#repoObject

Returns the value of attribute repo.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def repo
  @repo
end

#stdinObject

Returns the value of attribute stdin.



13
14
15
# File 'lib/bixby-common/command_spec.rb', line 13

def stdin
  @stdin
end

#userObject

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_dirObject

resolve the given bundle



49
50
51
# 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?Boolean

Check if the bundle described by this CommandSpec exists

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


84
85
86
# File 'lib/bixby-common/command_spec.rb', line 84

def command_exists?
  File.exists? self.command_file
end

#command_fileString

Absolute command filename

Returns:

  • (String)


77
78
79
# File 'lib/bixby-common/command_spec.rb', line 77

def command_file
  path("bin", @command)
end

#digest_fileString

Bundle digest filename

Returns:

  • (String)

    path to bundle digest file



112
113
114
# File 'lib/bixby-common/command_spec.rb', line 112

def digest_file
  path("digest")
end

#load_bundle_manifestHash Also known as: load_manifest

Retrieve the bundle manifest

Returns:

  • (Hash)


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_digestHash

Retrieve the bundle digest

Returns:

  • (Hash)


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

#manifestHash 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

Returns:

  • (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_fileString Also known as: config_file

Command manifest filename

Returns:

  • (String)


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

Parameters:

  • *relative (String)

Returns:

  • (String)


144
145
146
# File 'lib/bixby-common/command_spec.rb', line 144

def path(*relative)
  File.join(self.bundle_dir, *relative)
end

#relative_pathString

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

Returns:

  • (String)


63
64
65
# File 'lib/bixby-common/command_spec.rb', line 63

def relative_path
  File.join(@repo, @bundle)
end

#to_sString

Convert object to String, useful for debugging

Returns:

  • (String)


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_digestObject

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

Parameters:

  • expected_digest (String)

Returns:

  • (Boolean)

    returns true if available, else raises error

Raises:



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