athlete 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Athlete
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tobi Knaup
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,130 @@
1
+ require 'uri'
2
+
3
+ module Marathon
4
+ class Client
5
+ include HTTParty
6
+
7
+ headers(
8
+ 'Content-Type' => 'application/json',
9
+ 'Accept' => 'application/json'
10
+ )
11
+
12
+ query_string_normalizer proc { |query| MultiJson.dump(query) }
13
+ maintain_method_across_redirects
14
+ default_timeout 5
15
+
16
+ EDITABLE_APP_ATTRIBUTES = [
17
+ :cmd, :constraints, :container, :cpus, :env, :executor, :id, :instances,
18
+ :mem, :ports, :uris]
19
+
20
+ def initialize(host = nil, user = nil, pass = nil, proxy = nil)
21
+ @host = host || ENV['MARATHON_HOST'] || 'http://localhost:8080'
22
+ @default_options = {}
23
+
24
+ if user && pass
25
+ @default_options[:basic_auth] = {:username => user, :password => pass}
26
+ end
27
+
28
+ if proxy
29
+ @default_options[:http_proxyaddr] = proxy[:addr]
30
+ @default_options[:http_proxyport] = proxy[:port]
31
+ @default_options[:http_proxyuser] = proxy[:user] if proxy[:user]
32
+ @default_options[:http_proxypass] = proxy[:pass] if proxy[:pass]
33
+ end
34
+ end
35
+
36
+ def list
37
+ wrap_request(:get, '/v2/apps')
38
+ end
39
+
40
+ def find(id)
41
+ wrap_request(:get, "/v2/apps/#{id}")
42
+ end
43
+
44
+ def list_tasks(id)
45
+ wrap_request(:get, URI.escape("/v2/apps/#{id}/tasks"))
46
+ end
47
+
48
+ def search(id = nil, cmd = nil)
49
+ params = {}
50
+ params[:id] = id unless id.nil?
51
+ params[:cmd] = cmd unless cmd.nil?
52
+
53
+ wrap_request(:get, "/v2/apps?#{query_params(params)}")
54
+ end
55
+
56
+ def deployments
57
+ wrap_request(:get, "/v2/deployments")
58
+ end
59
+
60
+ def find_deployment_by_name(name)
61
+ wrap_request(:get, "/v2/deployments").parsed_response.find do |deployment|
62
+ deployment['affectedApps'].include?("/#{name}")
63
+ end
64
+ end
65
+
66
+ def endpoints(id = nil)
67
+ if id.nil?
68
+ url = "/v2/tasks"
69
+ else
70
+ url = "/v2/apps/#{id}/tasks"
71
+ end
72
+
73
+ wrap_request(:get, url)
74
+ end
75
+
76
+ def start(id, opts)
77
+ body = opts.dup
78
+ body[:id] = id
79
+ wrap_request(:post, '/v2/apps/', :body => body)
80
+ end
81
+
82
+ def update(id, opts)
83
+ body = opts.dup
84
+ body[:id] = id
85
+ wrap_request(:put, "/v2/apps/#{id}", :body => body)
86
+ end
87
+
88
+ def scale(id, num_instances)
89
+ # Fetch current state and update only the 'instances' attribute. Since the
90
+ # API only supports PUT, the full representation of the app must be
91
+ # supplied to update even just a single attribute.
92
+ app = wrap_request(:get, "/v2/apps/#{id}").parsed_response['app']
93
+ app.select! {|k, v| EDITABLE_APP_ATTRIBUTES.include?(k)}
94
+
95
+ app[:instances] = num_instances
96
+ wrap_request(:put, "/v2/apps/#{id}", :body => app)
97
+ end
98
+
99
+ def kill(id)
100
+ wrap_request(:delete, "/v2/apps/#{id}")
101
+ end
102
+
103
+ def kill_tasks(appId, params = {})
104
+ if params[:task_id].nil?
105
+ wrap_request(:delete, "/v2/apps/#{appId}/tasks?#{query_params(params)}")
106
+ else
107
+ query = params.clone
108
+ task_id = query[:task_id]
109
+ query.delete(:task_id)
110
+
111
+ wrap_request(:delete, "/v2/apps/#{appId}/tasks/#{task_id}?#{query_params(query)}")
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def wrap_request(method, url, options = {})
118
+ options = @default_options.merge(options)
119
+ http = self.class.send(method, @host + url, options)
120
+ Marathon::Response.new(http)
121
+ rescue => e
122
+ Marathon::Response.error(e.message)
123
+ end
124
+
125
+ def query_params(hash)
126
+ hash = hash.select { |k,v| !v.nil? }
127
+ URI.escape(hash.map { |k,v| "#{k}=#{v}" }.join('&'))
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,54 @@
1
+ module Marathon
2
+ class Response
3
+
4
+ # TODO make this attr_reader and set the error some other way
5
+ attr_accessor :error
6
+
7
+ def initialize(http)
8
+ @http = http
9
+ @error = error_message_from_response
10
+ end
11
+
12
+ def success?
13
+ @http && @http.success?
14
+ end
15
+
16
+ def error?
17
+ !success?
18
+ end
19
+
20
+ def parsed_response
21
+ @http && @http.parsed_response
22
+ end
23
+
24
+ def self.error(message)
25
+ error = new(nil)
26
+ error.error = message
27
+ error
28
+ end
29
+
30
+ def internal_response
31
+ @http.response
32
+ end
33
+
34
+ def code
35
+ @http.code
36
+ end
37
+
38
+ def to_s
39
+ if success?
40
+ "OK"
41
+ else
42
+ "ERROR: #{error}"
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def error_message_from_response
49
+ return if success?
50
+ return if @http.nil?
51
+ @http.body
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: athlete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andy Sykes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.19'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.19'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.13'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.10'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.10'
83
+ description: A deployment tool for building Docker containers for Marathon and Mesos
84
+ email:
85
+ - github@tinycat.co.uk
86
+ executables:
87
+ - athlete
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".ruby-gemset"
93
+ - ".ruby-version"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - athlete.gemspec
99
+ - bin/athlete
100
+ - lib/athlete.rb
101
+ - lib/athlete/build.rb
102
+ - lib/athlete/cli.rb
103
+ - lib/athlete/deployment.rb
104
+ - lib/athlete/logging.rb
105
+ - lib/athlete/utils.rb
106
+ - lib/athlete/version.rb
107
+ - lib/marathon/LICENSE.txt
108
+ - lib/marathon/client.rb
109
+ - lib/marathon/response.rb
110
+ homepage: https://github.com/forward3d/athlete
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: A deployment tool for Marathon and Mesos
134
+ test_files: []