hive-messages 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +35 -0
  6. data/Rakefile +11 -0
  7. data/hive-messages.gemspec +33 -0
  8. data/lib/hive/messages.rb +46 -0
  9. data/lib/hive/messages/artifact.rb +17 -0
  10. data/lib/hive/messages/base.rb +18 -0
  11. data/lib/hive/messages/configuration.rb +12 -0
  12. data/lib/hive/messages/execution_variables_base.rb +27 -0
  13. data/lib/hive/messages/job.rb +100 -0
  14. data/lib/hive/messages/nil_job.rb +15 -0
  15. data/lib/hive/paths.rb +12 -0
  16. data/lib/hive/paths/artifacts.rb +20 -0
  17. data/lib/hive/paths/jobs.rb +48 -0
  18. data/lib/hive/paths/queues.rb +21 -0
  19. data/lib/hive/representers/artifact_representer.rb +13 -0
  20. data/lib/hive/representers/job_representer.rb +25 -0
  21. data/lib/roar/transport/net_http/request_patch.rb +19 -0
  22. data/lib/virtus/attribute/execution_variables.rb +20 -0
  23. data/spec/fixtures/sample.pem +31 -0
  24. data/spec/fixtures/upload_sample.log +1 -0
  25. data/spec/lib/hive/messages/artifact_spec.rb +50 -0
  26. data/spec/lib/hive/messages/configuration_spec.rb +13 -0
  27. data/spec/lib/hive/messages/execution_variables_base_spec.rb +12 -0
  28. data/spec/lib/hive/messages/job_spec.rb +233 -0
  29. data/spec/lib/hive/messages_spec.rb +30 -0
  30. data/spec/lib/hive/paths/artifacts_spec.rb +22 -0
  31. data/spec/lib/hive/paths/jobs_spec.rb +49 -0
  32. data/spec/lib/hive/paths/queues_spec.rb +35 -0
  33. data/spec/lib/hive/paths_spec.rb +10 -0
  34. data/spec/lib/hive/representers/artifact_representer_spec.rb +49 -0
  35. data/spec/lib/hive/representers/job_representer_spec.rb +96 -0
  36. data/spec/lib/roar/transport/net_http/request_patch_spec.rb +84 -0
  37. data/spec/lib/virtus/attribute/execution_variables_spec.rb +60 -0
  38. data/spec/spec_helper.rb +13 -0
  39. metadata +266 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb04c19fd8b4f3cea97673db617aca170060be64
4
+ data.tar.gz: 90c9b2c9d5498fe5332ac27b8118e0d83c4acd52
5
+ SHA512:
6
+ metadata.gz: bbdf1f7d54486cf1248bfa065e63a180500a0f25bc27987ff8e1c8c33954db4702f44c0d38658e87552134e6fe11a2e9fcca7bc4ed7fdb3315313df47d25f5cc
7
+ data.tar.gz: 1a36c08c3f9bf278d250e6db514e3067ebd3275823667260c4d5062374b1b72d394b74e24eaad373cec16971cfe2c45717a7c530cb6ecdc615949d4ba3591c23
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
24
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 BBC
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # Hive::Messages
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hive-messages'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hive-messages
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/hive-messages/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
30
+
31
+ ## License
32
+
33
+ Simple Stats Store is available to everyone under the terms of the MIT open source licence. Take a look at the LICENSE file in the code.
34
+
35
+ Copyright (c) 2015 BBC
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/version_task'
3
+
4
+ Rake::VersionTask.new do |task|
5
+ task.with_git_tag = true
6
+ end
7
+
8
+ desc "Open an irb session preloaded with this library"
9
+ task :console do
10
+ sh "irb -rubygems -I lib -r hive/messages.rb"
11
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "hive-messages"
7
+ spec.version = '1.0.1'
8
+ spec.authors = ["David Buckhurst", "Paul Carey"]
9
+ spec.email = ["david.buckhurst@bbc.co.uk"]
10
+ spec.summary = %q{Hive communication library.}
11
+ spec.description = %q{Hive Messages, communications between hive components.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "virtus", "~> 1.0"
21
+ spec.add_dependency "roar", "~> 1.0"
22
+ spec.add_dependency "activemodel", "~> 4.2"
23
+ spec.add_dependency "activesupport", "~> 4.2"
24
+ spec.add_dependency "multipart-post", "~> 2.0"
25
+ spec.add_dependency "mimemagic", "~> 0.3"
26
+ spec.add_dependency "multi_json", "~> 1.11"
27
+
28
+ spec.add_development_dependency "rspec", "~> 2.14"
29
+ spec.add_development_dependency "bundler", "~> 1.6"
30
+ spec.add_development_dependency "rake", "~> 10.4"
31
+ spec.add_development_dependency "shoulda-matchers", "~> 2.8"
32
+ spec.add_development_dependency "webmock", "~> 1.21"
33
+ end
@@ -0,0 +1,46 @@
1
+ require "virtus"
2
+ require "roar/json"
3
+ require "openssl"
4
+ require "active_model"
5
+
6
+ require "active_support/version"
7
+
8
+ if ActiveSupport.version >= Gem::Version.new("4.1")
9
+ require "active_support/core_ext/object/json"
10
+ else
11
+ require "active_support/core_ext/object/to_json"
12
+ end
13
+
14
+ require "roar/transport/net_http/request_patch"
15
+
16
+ require "hive/paths"
17
+ require "hive/messages/configuration"
18
+
19
+ require "hive/representers/job_representer"
20
+ require "hive/representers/artifact_representer"
21
+
22
+
23
+ require "hive/messages/base"
24
+ require "hive/messages/execution_variables_base"
25
+ require "hive/messages/job"
26
+ require "hive/messages/artifact"
27
+
28
+ module Hive
29
+ module Messages
30
+
31
+ class << self
32
+ attr_accessor :configuration
33
+
34
+ def root
35
+ File.expand_path '../../..', __FILE__
36
+ end
37
+
38
+ def configure
39
+
40
+ self.configuration = Configuration.new
41
+ yield(configuration)
42
+ Hive::Paths.base = self.configuration.base_path
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+
2
+ module Hive
3
+ module Messages
4
+ class Artifact < Hive::Messages::Base
5
+ include Hive::Representers::ArtifactRepresenter
6
+
7
+
8
+ attribute :artifact_id, Integer
9
+ attribute :job_id, Integer
10
+ attribute :asset_file_name, String
11
+ attribute :asset_content_type, String
12
+ attribute :asset_file_size, Integer
13
+
14
+ validates :artifact_id, :job_id, :asset_file_name, :asset_content_type, :asset_file_size, presence: true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'virtus/attribute/execution_variables'
2
+ require 'roar/json'
3
+ require 'roar/client'
4
+ require 'net/http/post/multipart'
5
+ require 'mimemagic'
6
+ require 'pathname'
7
+
8
+ module Hive
9
+ module Messages
10
+ class Base
11
+ include Virtus.model
12
+ include ::ActiveModel::Validations
13
+ include Roar::JSON
14
+ include Roar::Client
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module Hive
2
+ module Messages
3
+
4
+ class Configuration
5
+ include Virtus.model
6
+
7
+ attribute :base_path, String
8
+ attribute :pem_file, String
9
+ attribute :ssl_verify_mode, Integer, default: OpenSSL::SSL::VERIFY_PEER
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module Hive
3
+ module Messages
4
+ class ExecutionVariablesBase
5
+ include Virtus.model
6
+ include ActiveModel::Validations
7
+
8
+ attribute :job_id, Integer
9
+ attribute :version, String
10
+ attribute :queue_name, String
11
+
12
+ validates :job_id, :version, :queue_name, presence: true
13
+
14
+ def self.model_name
15
+ ActiveModel::Name.new(self, nil, "ExecutionVariables")
16
+ end
17
+
18
+ # def to_s
19
+ # require "pry"; binding.pry
20
+ # end
21
+
22
+ # def to_json(*args)
23
+ # require "pry"; binding.pry
24
+ # end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,100 @@
1
+ require 'hive/messages/nil_job'
2
+
3
+ module Hive
4
+ module Messages
5
+ class Job < Hive::Messages::Base
6
+ include Hive::Representers::JobRepresenter
7
+
8
+ attribute :command, String
9
+ attribute :job_id, Integer
10
+ attribute :repository, String
11
+ attribute :execution_directory, String
12
+ attribute :target, Hash
13
+ attribute :execution_variables, ExecutionVariables
14
+ attribute :reservation_details, Hash
15
+ attribute :device_id, Integer
16
+ attribute :running_count, Integer
17
+ attribute :failed_count, Integer
18
+ attribute :errored_count, Integer
19
+ attribute :passed_count, Integer
20
+ attribute :state, String
21
+ attribute :result, String
22
+ attribute :exit_value, Integer
23
+ attribute :message, String
24
+ attribute :result_details, String
25
+
26
+ validates :command, :job_id, presence: true
27
+
28
+ class << self
29
+
30
+ def reserve(queues, reservation_details)
31
+ job = self.new
32
+ job.reservation_details = reservation_details
33
+ begin
34
+ job.patch(uri: Hive::Paths::Queues.job_reservation_url(queues), as: "application/json")
35
+ rescue => e
36
+ Hive::Messages::NilJob.new(e)
37
+ end
38
+ end
39
+ end
40
+
41
+ def prepare(device_id)
42
+ self.device_id = device_id
43
+ self.patch(uri: Hive::Paths::Jobs.prepare_url(self.job_id), as: "application/json")
44
+ end
45
+
46
+ def start
47
+ self.patch(uri: Hive::Paths::Jobs.start_url(self.job_id), as: "application/json")
48
+ end
49
+
50
+ def end(exit_value)
51
+ self.exit_value = exit_value
52
+ self.patch(uri: Hive::Paths::Jobs.end_url(self.job_id), as: "application/json")
53
+ end
54
+
55
+ def update_results(details)
56
+ self.result_details = details[:result_details]
57
+ counts = details.slice(:running_count, :failed_count, :errored_count, :passed_count)
58
+ counts.each_pair do |count_key, count_value|
59
+ self.send("#{count_key}=", count_value)
60
+ end
61
+ self.patch(uri: Hive::Paths::Jobs.update_results_url(self.job_id), as: "application/json")
62
+ end
63
+
64
+ def report_artifact(artifact_path)
65
+ url = URI.parse(Hive::Paths::Artifacts.create_url(self.job_id))
66
+
67
+ basename = Pathname.new(artifact_path).basename.to_s
68
+
69
+ mime = MimeMagic.by_path(artifact_path)
70
+ mime_type = mime ? mime.type : 'text/plain'
71
+
72
+ net_http_args = {:use_ssl => url.instance_of?(URI::HTTPS)}
73
+
74
+ if Hive::Messages.configuration.pem_file
75
+ pem = File.read(Hive::Messages.configuration.pem_file)
76
+ net_http_args[:cert] = OpenSSL::X509::Certificate.new(pem)
77
+ net_http_args[:key] = OpenSSL::PKey::RSA.new(pem)
78
+ net_http_args[:verify_mode] = Hive::Messages.configuration.ssl_verify_mode
79
+ end
80
+
81
+ File.open(artifact_path) do |artifact|
82
+ req = Net::HTTP::Post::Multipart.new url.path, "data" => UploadIO.new(artifact, mime_type, basename)
83
+ res = Net::HTTP.start(url.host, url.port, net_http_args) do |http|
84
+ http.request(req)
85
+ end
86
+ Hive::Messages::Artifact.new.from_json(res.body)
87
+ end
88
+ end
89
+
90
+ def complete
91
+ self.patch(uri: Hive::Paths::Jobs.complete_url(self.job_id), as: "application/json")
92
+ end
93
+
94
+ def error(message)
95
+ self.message = message
96
+ self.patch(uri: Hive::Paths::Jobs.error_url(self.job_id), as: "application/json")
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,15 @@
1
+ module Hive
2
+ module Messages
3
+ class NilJob
4
+ attr_accessor :exception
5
+
6
+ def initialize(exception)
7
+ @message = exception
8
+ end
9
+
10
+ def nil?
11
+ true
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require "hive/paths/queues"
2
+ require "hive/paths/jobs"
3
+ require "hive/paths/artifacts"
4
+
5
+ module Hive
6
+ module Paths
7
+
8
+ class << self
9
+ attr_accessor :base
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module Hive
2
+ module Paths
3
+
4
+ class Artifacts
5
+
6
+ class << self
7
+
8
+ def create_url(job_id)
9
+ "#{Hive::Paths::Jobs.job_base(job_id)}/artifacts"
10
+ end
11
+
12
+ private
13
+
14
+ def queues_base
15
+ "#{Hive::Paths.base}/api/queues"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ module Hive
2
+ module Paths
3
+
4
+ class Jobs
5
+
6
+ class << self
7
+
8
+ def prepare_url(job_id)
9
+ "#{job_base(job_id)}/prepare"
10
+ end
11
+
12
+ def start_url(job_id)
13
+ "#{job_base(job_id)}/start"
14
+ end
15
+
16
+ def end_url(job_id)
17
+ "#{job_base(job_id)}/end"
18
+ end
19
+
20
+ def update_results_url(job_id)
21
+ "#{job_base(job_id)}/update_results"
22
+ end
23
+
24
+ def report_artifacts_url(job_id)
25
+ "#{job_base(job_id)}/report_artifacts"
26
+ end
27
+
28
+ def complete_url(job_id)
29
+ "#{job_base(job_id)}/complete"
30
+ end
31
+
32
+ def error_url(job_id)
33
+ "#{job_base(job_id)}/error"
34
+ end
35
+
36
+ def job_base(job_id)
37
+ "#{jobs_base}/#{job_id.to_s}"
38
+ end
39
+
40
+ private
41
+
42
+ def jobs_base
43
+ "#{Hive::Paths.base}/api/jobs"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end