hsp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 487664de95ea55c4bca12a7ac0c47081bb5ae91c
4
+ data.tar.gz: a95b87e1db1489398af2c870bfe67b86da9f7918
5
+ SHA512:
6
+ metadata.gz: 159d576a27bef2a42d1d5ac4558c8e7a7f54ea4aa5c162edbcb658b835efacf8b98170dc48c4a7191ba02f9f434b2cb2170dc53c2ba44d3a08ca0458a9e13301
7
+ data.tar.gz: 60fecace861307fc56e6ebf4c3e62a7482fb4ab946cecbdb4c09fcd2a6e35a0477f2a79753e307db4724ee9ab22ddd1fdfaed980859ac1be1d49d30a81e8dc18
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .byebug_history
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at preston.lee@prestonlee.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hsp.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Health Services Platform Agent and Command Line Interface
2
+
3
+ The `hsp` command line utility for interacting with a remote HSPC Marketplace (or compatible) deployment from an on-premise or cloud host. This cross-platform executable provides a:
4
+
5
+ * Command line interface to a Marketplace deployment.
6
+ * "Agent" mode for real-time routing of remote Marketplace commands to a local Docker engine, swarm, or other orchestration agent.
7
+
8
+ ## Installation
9
+
10
+ A recent version of Ruby is required. We recommend the current stable version.
11
+
12
+ ```ruby
13
+ gem install hsp
14
+ ```
15
+
16
+ Built-in help is provided in each respective binary:
17
+
18
+ $ hsp --help
19
+
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## License
28
+
29
+ This software is released under the Apache 2.0 license. Copyright (c) Preston Lee.
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/preston/hsp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hsp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/hsp ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative File.join('..', 'lib', 'hsp')
4
+ Hsp::Cli::Main.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hsp.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hsp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hsp"
8
+ spec.version = Hsp::VERSION
9
+ spec.authors = ["Preston Lee"]
10
+ spec.email = ["preston.lee@prestonlee.com"]
11
+
12
+ spec.summary = %q{Health Services Platform command line utilities.}
13
+ spec.description = %q{Command line utilities for interacting with an account on the public HSPC Marketplace, or any alternative deployment.}
14
+ spec.homepage = "https://github.com/preston/hsp"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "thor", '>= 0.19.4'
24
+ spec.add_dependency "httparty", '>= 0.14.0'
25
+ spec.add_dependency "space_elevator", '>= 0.2.0'
26
+ spec.add_dependency "docker-api", '>= 1.33.2'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "minitest", "~> 5.0"
31
+ end
data/lib/hsp/agent.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'space_elevator'
2
+ require 'eventmachine'
3
+ require 'em-websocket-client'
4
+ require 'jwt'
5
+
6
+ module Hsp
7
+ class Agent
8
+ attr_accessor :marketplace
9
+ attr_accessor :orchestrator
10
+ attr_accessor :platform_id
11
+ attr_accessor :platform_secret
12
+
13
+ def initialize(marketplace, orchestrator, platform_id, platform_secret)
14
+ self.marketplace = marketplace
15
+ self.orchestrator = orchestrator
16
+ self.platform_id = platform_id
17
+ self.platform_secret = platform_secret
18
+ end
19
+
20
+ def run(platform_id, pings = false)
21
+ EventMachine.run do
22
+ url = marketplace.websocket_url
23
+ client = SpaceElevator::Client.new(url) do
24
+ puts 'Disconnected. Exiting...'
25
+ EventMachine.stop_event_loop
26
+ end
27
+ client.connect do |msg|
28
+ case msg['type']
29
+ when 'welcome'
30
+ puts 'The server says "welcome".'
31
+ client.subscribe(channel: 'ChatChannel') do |chat|
32
+ puts "Received Chat Event: #{chat}"
33
+ if chat['type'] == 'confirm_subscription'
34
+ puts "Subscription to #{chat['identifier']['channel']} confirmed!"
35
+ client.publish({channel: 'ChatChannel'}, {subject: 'Hi', text: "What's up, y'all!?!?"})
36
+ end
37
+ end
38
+ client.subscribe(channel: 'PlatformChannel', platform_id: platform_id) do |m|
39
+ puts "Received Platform #{platform_id} Event: #{m}"
40
+ case m['type']
41
+ when 'confirm_subscription'
42
+ # We don't need to dispatch this, I suppose.
43
+ else
44
+ self.orchestrator.dispatch(m)
45
+ end
46
+ end
47
+ when 'ping'
48
+ puts 'The server just pinged us.' if pings
49
+ else
50
+ puts msg
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,65 @@
1
+ require 'thor'
2
+ require 'httparty'
3
+
4
+ module Hsp
5
+ module Cli
6
+ class Builds < Thor
7
+ no_commands do
8
+ def print_builds(json)
9
+ puts "\n#{json['total_results']} total builds found."
10
+ puts "Page #{json['current_page']} of #{json['total_pages']} (Previous: #{json['previous_page'].nil? ? 'N/A' : json['previous_page']}, Next: #{json['next_page'].nil? ? 'N/A' : json['next_page']})"
11
+ puts "ID\t\t\t\t\tName\t\tSecret"
12
+ puts_bar
13
+ json['results'].each do |n|
14
+ puts "#{n['id']}\t#{n['name']}\t#{n['public_key']}"
15
+ end
16
+ puts "\n"
17
+ end
18
+
19
+ def puts_bar
20
+ puts '--------' * 8
21
+ end
22
+ end
23
+
24
+ desc 'list', 'Lists builds of a given service'
25
+ option :service_id, required: true, type: :string
26
+ option :page, type: :numeric
27
+ option :per_page, type: :numeric
28
+ def list
29
+ m = Marketplace.new
30
+ query = {
31
+ page: 1,
32
+ per_page: 10
33
+ }
34
+ query[:page] = options[:page].to_i if options[:page]
35
+ query[:per_page] = options[:per_page].to_i if options[:per_page]
36
+ response = HTTParty.get(m.builds_url(options[:service_id]), query: query, headers: Hsp::Client.headers)
37
+ json = JSON.parse(response.body)
38
+ # puts json
39
+ print_builds(json)
40
+ end
41
+
42
+ desc 'show', 'Print details on a specific build.'
43
+ option :service_id, required: true, type: :string
44
+ option :build_id, required: true, type: :string
45
+ def show
46
+ m = Marketplace.new
47
+ response = HTTParty.get(m.builds_url(options[:service_id], options[:build_id]), headers: Hsp::Client.headers)
48
+ json = JSON.parse(response.body)
49
+ # puts json
50
+ puts ''
51
+ puts json['name']
52
+ puts_bar
53
+ # puts json['description']
54
+ puts "ID: #{json['id']}"
55
+ puts "Service ID: #{json['service_id']}"
56
+ puts "Secret: #{json['public_key']}"
57
+ puts "Created: #{json['created_at']}"
58
+ puts "Updated: #{json['updated_at']}"
59
+ puts "URL: #{json['url']}"
60
+ puts "Path: #{json['path']}"
61
+ puts ''
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,65 @@
1
+ require 'thor'
2
+ require 'httparty'
3
+
4
+ module Hsp
5
+ module Cli
6
+ class Platforms < Thor
7
+ no_commands do
8
+ def print_platforms(json)
9
+ puts "\n#{json['total_results']} total platforms found."
10
+ puts "Page #{json['current_page']} of #{json['total_pages']} (Previous: #{json['previous_page'].nil? ? 'N/A' : json['previous_page']}, Next: #{json['next_page'].nil? ? 'N/A' : json['next_page']})"
11
+ puts "ID\t\t\t\t\tName\t\tSecret"
12
+ puts_bar
13
+ json['results'].each do |n|
14
+ puts "#{n['id']}\t#{n['name']}\t#{n['public_key']}"
15
+ end
16
+ puts "\n"
17
+ end
18
+
19
+ def puts_bar
20
+ puts '--------' * 8
21
+ end
22
+ end
23
+
24
+ desc 'list', 'Lists platforms of a given user'
25
+ option :user_id, required: true, type: :string
26
+ option :page, type: :numeric
27
+ option :per_page, type: :numeric
28
+ def list
29
+ m = Marketplace.new
30
+ query = {
31
+ page: 1,
32
+ per_page: 10
33
+ }
34
+ query[:page] = options[:page].to_i if options[:page]
35
+ query[:per_page] = options[:per_page].to_i if options[:per_page]
36
+ response = HTTParty.get(m.platforms_url(options[:user_id]), query: query, headers: Hsp::Client.headers)
37
+ json = JSON.parse(response.body)
38
+ # puts json
39
+ print_platforms(json)
40
+ end
41
+
42
+ desc 'show', 'Print details on a specific platform.'
43
+ option :user_id, required: true, type: :string
44
+ option :platform_id, required: true, type: :string
45
+ def show
46
+ m = Marketplace.new
47
+ response = HTTParty.get(m.platforms_url(options[:user_id], options[:platform_id]), headers: Hsp::Client.headers)
48
+ json = JSON.parse(response.body)
49
+ # puts json
50
+ puts ''
51
+ puts json['name']
52
+ puts_bar
53
+ # puts json['description']
54
+ puts "ID: #{json['id']}"
55
+ puts "Owner (User ID): #{json['user_id']}"
56
+ puts "Secret: #{json['public_key']}"
57
+ puts "Created: #{json['created_at']}"
58
+ puts "Updated: #{json['updated_at']}"
59
+ puts "URL: #{json['url']}"
60
+ puts "Path: #{json['path']}"
61
+ puts ''
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,63 @@
1
+
2
+ require 'thor'
3
+ require 'httparty'
4
+
5
+ module Hsp
6
+ module Cli
7
+ class Services < Thor
8
+ no_commands do
9
+ def print_services(json)
10
+ puts "\n#{json['total_results']} total services found."
11
+ puts "Page #{json['current_page']} of #{json['total_pages']} (Previous: #{json['previous_page'].nil? ? 'N/A' : json['previous_page']}, Next: #{json['next_page'].nil? ? 'N/A' : json['next_page']})"
12
+ puts "ID\t\t\t\t\tName - Description"
13
+ puts_bar
14
+ json['results'].each do |n|
15
+ puts "#{n['id']}\t#{n['name']} - #{n['description'][0..60]}..."
16
+ end
17
+ puts "\n"
18
+ end
19
+
20
+ def puts_bar
21
+ puts '--------' * 8
22
+ end
23
+ end
24
+
25
+ desc 'list', 'Lists all available services'
26
+ option :page, type: :numeric
27
+ option :per_page, type: :numeric
28
+ def list
29
+ m = Marketplace.new
30
+ query = {
31
+ page: 1,
32
+ per_page: 10
33
+ }
34
+ query[:page] = options[:page].to_i if options[:page]
35
+ query[:per_page] = options[:per_page].to_i if options[:per_page]
36
+ response = HTTParty.get(m.services_url, query: query, headers: Hsp::Client.headers)
37
+ json = JSON.parse(response.body)
38
+ # puts json
39
+ print_services(json)
40
+ end
41
+
42
+ desc 'show', 'Print details on a given service'
43
+ option :service_id, required: true, type: :string
44
+ def show
45
+ m = Marketplace.new
46
+ response = HTTParty.get(m.services_url(options[:service_id]), headers: Hsp::Client.headers)
47
+ json = JSON.parse(response.body)
48
+ # puts json
49
+ puts ''
50
+ puts json['name']
51
+ puts_bar
52
+ puts json['description']
53
+ puts "URI: #{json['uri']}"
54
+ puts "Support URL: #{json['support_url']}"
55
+ puts "License: #{json['license_id']}"
56
+ puts "Created: #{json['created_at']}"
57
+ puts "Updated: #{json['updated_at']}"
58
+ puts "Published: #{json['published_at']}"
59
+ puts ''
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,81 @@
1
+
2
+ require 'thor'
3
+ require 'httparty'
4
+
5
+ module Hsp
6
+ module Cli
7
+ class Users < Thor
8
+ no_commands do
9
+ def print_users(json)
10
+ puts "\n#{json['total_results']} total users found."
11
+ puts "Page #{json['current_page']} of #{json['total_pages']} (Previous: #{json['previous_page'].nil? ? 'N/A' : json['previous_page']}, Next: #{json['next_page'].nil? ? 'N/A' : json['next_page']})"
12
+ puts "ID\t\t\t\t\tName\t\tCreated"
13
+ puts_bar
14
+ json['results'].each do |n|
15
+ puts "#{n['id']}\t#{n['name']}\t#{n['created_at']}"
16
+ end
17
+ puts "\n"
18
+ end
19
+
20
+ def puts_bar
21
+ puts '--------' * 8
22
+ end
23
+ end
24
+
25
+ desc 'list', 'Lists all available users'
26
+ option :page, type: :numeric
27
+ option :per_page, type: :numeric
28
+ def list
29
+ m = Marketplace.new
30
+ query = {
31
+ page: 1,
32
+ per_page: 10
33
+ }
34
+ query[:page] = options[:page].to_i if options[:page]
35
+ query[:per_page] = options[:per_page].to_i if options[:per_page]
36
+ response = HTTParty.get(m.users_url, query: query, headers: Hsp::Client.headers)
37
+ json = JSON.parse(response.body)
38
+ puts json
39
+ print_users(json)
40
+ end
41
+
42
+ desc 'builds', 'Builds of a given users'
43
+ option :user_id, required: true, type: :string
44
+ option :page, type: :numeric
45
+ option :per_page, type: :numeric
46
+ def builds
47
+ m = Marketplace.new
48
+ query = {
49
+ page: 1,
50
+ per_page: 10
51
+ }
52
+ query[:page] = options[:page].to_i if options[:page]
53
+ query[:per_page] = options[:per_page].to_i if options[:per_page]
54
+ response = HTTParty.get(m.builds_url(options[:user_id]), query: query, headers: Hsp::Client.headers)
55
+ json = JSON.parse(response.body)
56
+ # puts json
57
+ print_builds(json)
58
+ end
59
+
60
+ desc 'show', 'Print details on a given user'
61
+ option :user_id, required: true, type: :string
62
+ def show
63
+ m = Marketplace.new
64
+ response = HTTParty.get(m.users_url(options[:user_id]), headers: Hsp::Client.headers)
65
+ json = JSON.parse(response.body)
66
+ # puts json
67
+ puts ''
68
+ puts json['name']
69
+ puts_bar
70
+ puts json['description']
71
+ puts "URI: #{json['uri']}"
72
+ puts "Support URL: #{json['support_url']}"
73
+ puts "License: #{json['license_id']}"
74
+ puts "Created: #{json['created_at']}"
75
+ puts "Updated: #{json['updated_at']}"
76
+ puts "Published: #{json['published_at']}"
77
+ puts ''
78
+ end
79
+ end
80
+ end
81
+ end
data/lib/hsp/cli.rb ADDED
@@ -0,0 +1,54 @@
1
+
2
+ require 'thor'
3
+ require 'httparty'
4
+
5
+ require_relative 'cli/users'
6
+ require_relative 'cli/platforms'
7
+ require_relative 'cli/services'
8
+ require_relative 'cli/builds'
9
+
10
+ module Hsp
11
+ class Client
12
+ def self.headers
13
+ { 'Accept' => 'application/json' }
14
+ end
15
+ end
16
+
17
+ module Cli
18
+ class Main < Thor
19
+ # desc 'login USERNAME URL', 'Logs in with the given to username to the provided URL.'
20
+ # def login(_username, url)
21
+ # puts url
22
+ # end
23
+
24
+ desc 'services SUBCOMMAND ..ARGS', 'Available marketplace services.'
25
+ subcommand 'services', Hsp::Cli::Services
26
+
27
+ desc 'builds SUBCOMMAND ..ARGS', 'Builds of a given service.'
28
+ subcommand 'builds', Hsp::Cli::Services
29
+
30
+ desc 'platforms SUBCOMMAND ..ARGS', 'Register platform(s) for known users.'
31
+ subcommand 'platforms', Hsp::Cli::Platforms
32
+
33
+ desc 'users SUBCOMMAND ..ARGS', 'Marketplace users.'
34
+ subcommand 'users', Hsp::Cli::Users
35
+
36
+ desc 'agent', 'Agent mode for processing commands pushed by a remote marketplace.'
37
+ option :websocket_url, type: :string
38
+ option :rest_url, type: :string
39
+ option :platform_id, required: true, type: :string
40
+ option :platform_secret, required: true, type: :string
41
+ option :show_pings, type: :boolean
42
+ def agent
43
+ m = Marketplace.new
44
+ m.websocket_url = options[:websocket_url] if options[:websocket_url]
45
+ m.rest_url = options[:rest_url] if options[:rest_url]
46
+ response = HTTParty.get(m.services_url(options[:service_id]), headers: Hsp::Client.headers)
47
+ json = JSON.parse(response.body)
48
+ # TODO Don't hardcode the Docker API implementation! For now, though, it's fine.
49
+ agent = Hsp::Agent.new(m, Hsp::Orchestrator::Docker.new, options[:platform_id], options[:platform_secret]) # TODO: Make agnostic to orchestration platform
50
+ agent.run(options[:platform_id], !!options[:show_pings])
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ module Hsp
2
+ class Marketplace
3
+ DEFAULT_REST_URL = 'https://marketplace-server.hspconsortium.org'.freeze
4
+ DEFAULT_WEBSOCKET_URL = 'wss://marketplace-server.hspconsortium.org/websocket'.freeze
5
+ attr_accessor :rest_url
6
+ attr_accessor :websocket_url
7
+
8
+ def initialize(rest_url = DEFAULT_REST_URL, websocket_url = DEFAULT_WEBSOCKET_URL)
9
+ self.rest_url = rest_url
10
+ self.websocket_url = websocket_url
11
+ end
12
+
13
+ def services_url(id = nil)
14
+ rest_url + '/services' + (id.nil? ? '' : "/#{id}")
15
+ end
16
+
17
+ def builds_url(service_id, build_id = nil)
18
+ services_url(service_id) + '/builds' + (build_id.nil? ? '' : "/#{build_id}")
19
+ end
20
+
21
+ def users_url(id = nil)
22
+ rest_url + '/users' + (id.nil? ? '' : "/#{id}")
23
+ end
24
+
25
+ def platforms_url(user_id, platform_id = nil)
26
+ users_url(user_id) + '/platforms' + (platform_id.nil? ? '' : "/#{platform_id}")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,73 @@
1
+ require 'docker'
2
+
3
+ module Hsp
4
+ module Orchestrator
5
+ class Docker
6
+ attr_accessor :url
7
+ def initialize(url = nil)
8
+ self.url = url if url
9
+ end
10
+
11
+ def status
12
+ Docker.info
13
+ end
14
+
15
+ def dispatch(event)
16
+ case event.dig('message', 'resource_type')
17
+ when 'instance'
18
+ dispatch_instance(event)
19
+ else
20
+ puts "Unsupported event type '#{event.dig('resource_type')}' received! It will be ignored."
21
+ end
22
+ end
23
+
24
+ def dispatch_instance(event)
25
+ puts "The server says: #{event.dig('message', 'message')}"
26
+ case event.dig('message', 'event_type')
27
+ when 'created'
28
+ deploy(event.dig('message'))
29
+ end
30
+ end
31
+
32
+ def deploy(message)
33
+ # puts "DEBUG: #{message.dig('model', 'instance', 'build')}"
34
+ repository = message.dig('model', 'instance', 'build', 'container_repository')
35
+ tag = message.dig('model', 'instance', 'build', 'container_tag')
36
+ if repository && tag
37
+ label = "#{repository}:#{tag}"
38
+ puts "Attempting to load '#{label}'..."
39
+ image = ::Docker::Image.create('fromImage' => label)
40
+ if ::Docker::Image.exist?(label)
41
+ puts 'Found! Downloading...'
42
+ image = ::Docker::Image.get(label)
43
+ puts 'Running...'
44
+ declared_ports = image.info['ContainerConfig']['ExposedPorts']
45
+ port_bindings = declared_ports.clone
46
+ port_bindings.each do |k, v| port_bindings[k] = [{}] end
47
+ container = ::Docker::Container.create(
48
+ 'Image' => label,
49
+ 'ExposedPorts' => declared_ports,
50
+ # 'Detach' => true,
51
+ 'HostConfig' => {
52
+ 'PortBindings' => port_bindings
53
+ }
54
+ )
55
+ # require 'byebug'
56
+ # byebug
57
+ c = container.run(nil)
58
+ puts "CONTAINER: #{c.info['Ports']}"
59
+ # TODO Implement logging of event details back to the server side!
60
+ else
61
+ puts "Image does not exist. Are you sure #{label} is correct and accessible?"
62
+ end
63
+ else
64
+ puts 'Both repository and tag must be included in the event!'
65
+ end
66
+ end
67
+
68
+ def undeploy; end
69
+
70
+ def sync_platform(platform_id); end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module Hsp
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hsp.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative "hsp/version"
2
+ require_relative "hsp/marketplace"
3
+ require_relative "hsp/agent"
4
+ require_relative "hsp/cli"
5
+
6
+ require_relative "hsp/orchestrator/docker"
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hsp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Preston Lee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: space_elevator
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: docker-api
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.33.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.33.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.0'
111
+ description: Command line utilities for interacting with an account on the public
112
+ HSPC Marketplace, or any alternative deployment.
113
+ email:
114
+ - preston.lee@prestonlee.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/hsp
127
+ - bin/setup
128
+ - hsp.gemspec
129
+ - lib/hsp.rb
130
+ - lib/hsp/agent.rb
131
+ - lib/hsp/cli.rb
132
+ - lib/hsp/cli/builds.rb
133
+ - lib/hsp/cli/platforms.rb
134
+ - lib/hsp/cli/services.rb
135
+ - lib/hsp/cli/users.rb
136
+ - lib/hsp/marketplace.rb
137
+ - lib/hsp/orchestrator/docker.rb
138
+ - lib/hsp/version.rb
139
+ homepage: https://github.com/preston/hsp
140
+ licenses: []
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.8
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Health Services Platform command line utilities.
162
+ test_files: []