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



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

#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

#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

Instance Method Details

#bundle_dirObject

resolve the given bundle



48
49
50
# File 'lib/bixby-common/command_spec.rb', line 48

def bundle_dir
  File.expand_path(File.join(Bixby.repo_path, self.relative_path))
end

#bundle_exists?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


74
75
76
# File 'lib/bixby-common/command_spec.rb', line 74

def command_exists?
  File.exists? self.command_file
end

#command_fileObject



70
71
72
# File 'lib/bixby-common/command_spec.rb', line 70

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

#config_fileObject



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

def config_file
  command_file + ".json"
end

#digest_fileObject



90
91
92
# File 'lib/bixby-common/command_spec.rb', line 90

def digest_file
  path("digest")
end

#load_configObject



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_digestObject



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_manifestObject



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

Parameters:

  • *relative (String)

Returns:

  • (String)


115
116
117
# File 'lib/bixby-common/command_spec.rb', line 115

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)


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

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

#to_sString

Convert object to String, useful for debugging

Returns:

  • (String)


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_digestObject



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

Parameters:

  • expected_digest (String)

Returns:

  • (Boolean)

    returns true if available, else raises error

Raises:



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