weavr 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
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
+
19
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'gorillib-model', git: 'git@github.com:infochimps-platform/gorillib-model.git', branch: 'master'
6
+
7
+ group :development do
8
+ gem 'rake', '~> 10.3.2'
9
+ gem 'rspec', '~> 2.14.0'
10
+ gem 'simplecov', '~> 0.8.2'
11
+ end
@@ -0,0 +1,95 @@
1
+ # License for Weavr
2
+
3
+ The code is __Copyright (c) 2014 Infochimps, Inc__
4
+
5
+ This code is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10
+
11
+ __________________________________________________________________________
12
+
13
+ # Apache License
14
+
15
+
16
+ Apache License
17
+ Version 2.0, January 2004
18
+ http://www.apache.org/licenses/
19
+
20
+ _TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION_
21
+
22
+ ## 1. Definitions.
23
+
24
+ * **License** shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
25
+
26
+ * **Licensor** shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
27
+
28
+ * **Legal Entity** shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
29
+
30
+ * **You** (or **Your**) shall mean an individual or Legal Entity exercising permissions granted by this License.
31
+
32
+ * **Source** form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
33
+
34
+ * **Object** form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
35
+
36
+ * **Work** shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
37
+
38
+ * **Derivative Works** shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
39
+
40
+ * **Contribution** shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
41
+
42
+ * **Contributor** shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
43
+
44
+ ## 2. Grant of Copyright License.
45
+
46
+ Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
47
+
48
+ ## 3. Grant of Patent License.
49
+
50
+ Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
51
+
52
+ ## 4. Redistribution.
53
+
54
+ You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
55
+
56
+ - (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
57
+ - (b) You must cause any modified files to carry prominent notices stating that You changed the files; and
58
+ - (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
59
+ - (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
60
+
61
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
62
+
63
+ ## 5. Submission of Contributions.
64
+
65
+ Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
66
+
67
+ ## 6. Trademarks.
68
+
69
+ This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
70
+
71
+ ## 7. Disclaimer of Warranty.
72
+
73
+ Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
74
+
75
+ ## 8. Limitation of Liability.
76
+
77
+ In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
78
+
79
+ ## 9. Accepting Warranty or Additional Liability.
80
+
81
+ While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
82
+
83
+ _END OF TERMS AND CONDITIONS_
84
+
85
+ ## APPENDIX: How to apply the Apache License to your work.
86
+
87
+ To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets `[]` replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
88
+
89
+ > Copyright [yyyy] [name of copyright owner]
90
+ >
91
+ > Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
92
+ >
93
+ > http://www.apache.org/licenses/LICENSE-2.0
94
+ >
95
+ > Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
@@ -0,0 +1,101 @@
1
+ # Weavr
2
+
3
+ A Ruby client for Apache Ambari [REST client](https://github.com/apache/ambari/blob/trunk/ambari-server/docs/api/v1/index.md)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'weavr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install weavr
18
+
19
+ ## Usage
20
+
21
+ Configure the singleton client:
22
+
23
+ ```ruby
24
+ Weavr.configure(host: 'ambari.web.host',
25
+ port: 8080,
26
+ username: 'barnaby',
27
+ password: 'jones',
28
+ log_level: 'debug',
29
+ log_format: ->(sev, t, prog, msg){ "%s - %s - %s\n", [t, sev, msg] })
30
+ ```
31
+
32
+ Create a new cluster:
33
+
34
+ ```ruby
35
+ hwx = Weavr.create_cluster 'hwx'
36
+ ```
37
+
38
+ Retrieve an existing cluster:
39
+
40
+ ```ruby
41
+ hwx = Weavr.cluster 'hwx'
42
+ ```
43
+
44
+ Add hosts to the cluster. They must already exist and have Ambari agent properly configured:
45
+
46
+ ```ruby
47
+ hwx.add_hosts('hadoop0', 'hadoop1', 'hadoop2')
48
+ ```
49
+
50
+ Add services to the cluster:
51
+
52
+ ```ruby
53
+ hwx.add_services('HDFS', 'YARN', 'ZOOKEEPER')
54
+ ```
55
+
56
+ Add components to the services:
57
+
58
+ ```ruby
59
+ hwx.add_components('HDFS', ['NAMENODE', 'DATANODE', HDFS_CLIENT'])
60
+ ```
61
+
62
+ Assign components to hosts:
63
+
64
+ ```ruby
65
+ hwx.assign_host_components('hadoop0' => ['NAMENODE'],
66
+ 'hadoop1' => ['DATANODE', 'HDFS_CLIENT'],
67
+ 'hadoop2' => ['DATANODE', 'HDFS_CLIENT'])
68
+ ```
69
+
70
+ Create config files:
71
+
72
+ ```ruby
73
+ hwx.create_config('hdfs-site', 'version2', 'some.java.prop' => 'value')
74
+ ```
75
+
76
+ Persist the cluster for the web api:
77
+
78
+ ```ruby
79
+ hwx.persist
80
+ ```
81
+
82
+ Access collections of objects by name:
83
+
84
+ ```ruby
85
+ hdfs = hwx.services['HDFS']
86
+ nn = hdfs.components['NAMENODE']
87
+ ```
88
+
89
+ All objects can be refreshed to repopulate parameters:
90
+
91
+ ```ruby
92
+ hwx.refresh!
93
+ hwx.services['HDFS'].refresh!
94
+ hwx.services['HDFS'].components['NAMENODE'].refresh!
95
+ ```
96
+
97
+ Adjust the repositories used to install components:
98
+
99
+ ```ruby
100
+ Weavr.configure_repository(version: '2.1', os: 'redhat6', repo: 'HDP-2.1', url: 'http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.1.3.0/')
101
+ ```
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Run RSpec code examples with simplecov'
8
+ task :cov do
9
+ ENV['WEAVR_COV'] = 'true'
10
+ Rake::Task[:spec].invoke
11
+ end
12
+
13
+ task default: [:spec]
@@ -0,0 +1,87 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'gorillib/builder'
5
+ require 'gorillib/model/type/extended'
6
+ require 'logger'
7
+
8
+ require 'weavr/connection'
9
+ require 'weavr/error'
10
+ require 'weavr/resource'
11
+ require 'weavr/resource/class_map'
12
+ require 'weavr/response'
13
+ require 'weavr/version'
14
+
15
+ module Weavr
16
+ extend self
17
+
18
+ def default_configuration
19
+ {
20
+ username: 'admin',
21
+ password: 'admin',
22
+ host: 'localhost',
23
+ port: 8080,
24
+ log_level: 'info',
25
+ log_format: ->(sev, t, prog, msg){ "[%-5s] %s\n" % [sev.downcase, msg] }
26
+ }
27
+ end
28
+
29
+ def default_logger(level, formatter)
30
+ logger = Logger.new(STDOUT)
31
+ logger.level = Logger.const_get(level.to_s.upcase)
32
+ logger.formatter = formatter
33
+ logger
34
+ end
35
+
36
+ def configure(overrides = {})
37
+ options = default_configuration.merge(overrides)
38
+ set_log options[:logger] || default_logger(options[:log_level], options[:log_format])
39
+ set_connection Connection.new(options)
40
+ end
41
+
42
+ def set_connection http
43
+ @connection ||= http
44
+ end
45
+
46
+ def connection
47
+ return @connection if @connection
48
+ configure
49
+ end
50
+
51
+ def set_log device
52
+ @log ||= device
53
+ end
54
+
55
+ def log
56
+ @log
57
+ end
58
+
59
+ # Should probably be Cluster.find
60
+ def cluster name
61
+ Cluster.receive connection.resource(:get, "clusters/#{name}")
62
+ end
63
+
64
+ def clusters
65
+ Collection.of(Cluster).receive connection.resource(:get, 'clusters')
66
+ end
67
+
68
+ # Should probably be Cluster.create
69
+ def create_cluster(name, options = {})
70
+ cluster = Cluster.receive(cluster_name: name, href: File.join('clusters', name))
71
+ cluster.create options
72
+ end
73
+
74
+ def stacks
75
+ Collection.of(Stack).receive connection.resource(:get, 'stacks2')
76
+ end
77
+
78
+ def configure_repository(params = {})
79
+ stack = stacks.items.first.refresh!
80
+ version = stack.versions[params[:version]].refresh!
81
+ os = version.operatingSystems[params[:os]].refresh!
82
+ repo = os.repositories[params[:repo]].refresh!
83
+ repo.update_base_url params[:url]
84
+ repo.refresh!
85
+ end
86
+
87
+ end
@@ -0,0 +1,40 @@
1
+ module Weavr
2
+ class Connection
3
+
4
+ attr_reader :host, :port, :http
5
+
6
+ def initialize(options = {})
7
+ @host = options[:host]
8
+ @port = options[:port]
9
+ establish_connection(options[:username], options[:password])
10
+ end
11
+
12
+ def establish_connection(user, password)
13
+ @http ||= Faraday.new(url: base_url, headers: request_headers) do |conn|
14
+ conn.request :basic_auth, user, password
15
+ conn.response :json
16
+ conn.response :logger, Weavr.log
17
+ conn.adapter Faraday.default_adapter
18
+ end
19
+ end
20
+
21
+ def base_url
22
+ "http://#{host}:#{port}/api/v1"
23
+ end
24
+
25
+ def request_headers
26
+ { 'X-Requested-By' => "Weavr v#{Weavr::VERSION}" }
27
+ end
28
+
29
+ def resource(action, path, data = {})
30
+ resp = http.send(action, path) do |req|
31
+ Weavr.log.info "payload: #{data}" if data
32
+ req.body = data.to_json if data
33
+ end
34
+ Weavr.log.info "body: #{resp.body}"
35
+ Response.handle(resp.status, resp.headers, resp.body)
36
+ rescue Faraday::ConnectionFailed
37
+ raise ConnectionError.new "Could not reach Ambari API at #{host}:#{port}"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module Weavr
2
+ Error = Class.new Exception
3
+ ConnectionError = Class.new Error
4
+ RequestError = Class.new Error
5
+ end
@@ -0,0 +1,53 @@
1
+ module Weavr
2
+ class Resource
3
+ include Gorillib::Builder
4
+
5
+ field :href, String
6
+
7
+ class << self
8
+ def receive(params, &blk)
9
+ super extract_class_params(params, &blk)
10
+ end
11
+
12
+ def extract_class_params params
13
+ params.merge(params[label] || {})
14
+ end
15
+
16
+ def label
17
+ name.demodulize.pluralize
18
+ end
19
+
20
+ def predefine_class name
21
+ Weavr.const_set(name, Class.new(self))
22
+ child_resources << name
23
+ end
24
+
25
+ def child_resources
26
+ @resources ||= []
27
+ end
28
+
29
+ def load_definitions!
30
+ child_resources.each do |name|
31
+ require File.expand_path("../resource/#{name.underscore}", __FILE__)
32
+ end
33
+ end
34
+ end
35
+
36
+ def receive! params
37
+ super self.class.extract_class_params(params)
38
+ end
39
+
40
+ def connection
41
+ Weavr.connection
42
+ end
43
+
44
+ def resource_action(action, data = {})
45
+ connection.resource(action, href, data)
46
+ end
47
+
48
+ def refresh!
49
+ receive! resource_action(:get)
50
+ self
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ # Declare all classes ahead of time to avoid dependency problems
2
+ %w[ Cluster Collection Component Configuration Host
3
+ HostRole OperatingSystem Repository Request
4
+ Service ServiceComponent Stack StackConfiguration
5
+ StackService StackVersion Task
6
+ ].each{ |resource| Weavr::Resource.predefine_class resource }
7
+
8
+ Weavr::Resource.load_definitions!
9
+
10
+
@@ -0,0 +1,64 @@
1
+ module Weavr
2
+ class Cluster < Resource
3
+ field :cluster_id, Integer
4
+ field :cluster_name, String
5
+ field :version, String
6
+ field :desired_configs, Hash # Fix
7
+ collection :requests, Request, key_method: 'id'
8
+ collection :services, Service, key_method: 'service_name'
9
+ collection :hosts, Host, key_method: 'host_name'
10
+ field :configurations, Array, of: Configuration
11
+
12
+ def create options
13
+ resource_action(:post, self.class.label => { version: 'HDP-2.1' }.merge(options))
14
+ self
15
+ end
16
+
17
+ def add_hosts(*names)
18
+ names.each do |name|
19
+ host = Host.receive(href: File.join(href, 'hosts', name))
20
+ host.create
21
+ end
22
+ refresh!
23
+ end
24
+
25
+ def add_services(*names)
26
+ names.each do |name|
27
+ srvc = Service.receive(service_name: name, href: File.join(href, 'services'))
28
+ srvc.create
29
+ end
30
+ refresh!
31
+ end
32
+
33
+ def add_components(srvc, names)
34
+ names.each do |name|
35
+ comp = Component.receive(component_name: name, href: File.join(href, 'services', srvc, 'components', name))
36
+ comp.create
37
+ end
38
+ refresh!
39
+ end
40
+
41
+ def assign_host_components mapping
42
+ mapping.each_pair do |host, comps|
43
+ comps.each do |comp|
44
+ role = HostRole.receive(href: File.join(href, 'hosts', host, 'host_components', comp))
45
+ role.create
46
+ end
47
+ end
48
+ end
49
+
50
+ def create_config(type, version, properties)
51
+ resource_action(:put, self.class.label => { desired_config: { type: type, tag: version, properties: properties } })
52
+ refresh!
53
+ end
54
+
55
+ def delete
56
+ resource_action(:delete)
57
+ end
58
+
59
+ # Weird persist method for the browser
60
+ def persist
61
+ connection.resource(:post, 'persist', 'CLUSTER_CURRENT_STATUS' => { 'clusterState' => 'CLUSTER_STARTED_5' }.to_json)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,10 @@
1
+ module Weavr
2
+ class Collection < Resource
3
+ def self.of kind
4
+ coll = Class.new(self) do
5
+ field :items, Array, of: kind
6
+ end
7
+ Weavr.const_set("#{kind.to_s.demodulize}Collection", coll)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Weavr
2
+ class Component < Resource
3
+ def self.label() 'ServiceComponentInfo' ; end
4
+
5
+ field :component_name, String
6
+ field :service_name, String
7
+ field :category, String
8
+ field :cluster_name, String
9
+ field :state, String
10
+ collection :host_components, HostRole, key_method: 'component_name'
11
+
12
+ def create
13
+ resource_action(:post)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module Weavr
2
+ class Configuration < Resource
3
+ def self.label() 'Config' ; end
4
+
5
+ field :cluster_name, String
6
+ field :tag, String
7
+ field :type, String
8
+ field :properties, Hash
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module Weavr
2
+ class Host < Resource
3
+ field :cluster_name, String
4
+ field :host_name, String
5
+ field :cpu_count, Integer
6
+ field :disk_info, Array, of: Hash # Fix me
7
+ field :host_health_report, String
8
+ field :host_status, String
9
+ field :ip, IpAddress
10
+ field :last_agent_env, Hash # Fix me
11
+ field :last_heartbeat_time, Integer # Needs to be date
12
+ field :last_registration_time, Integer # Needs to be date
13
+ field :maintenance_state, String
14
+ field :os_arch, String
15
+ field :os_type, String
16
+ field :ph_cpu_count, Integer
17
+ field :public_host_name, String
18
+ field :rack_info, String
19
+ field :total_mem, Integer
20
+ field :desired_configs, Hash # Fix me
21
+ collection :host_components, HostRole, key_method: 'component_name'
22
+
23
+ def create
24
+ resource_action(:post)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module Weavr
2
+ class HostRole < Resource
3
+ field :cluster_name, String
4
+ field :component_name, String
5
+ field :host_name, String
6
+ field :desired_admin_state, String
7
+ field :desired_stack_id, String
8
+ field :desired_state, String
9
+ field :maintenance_state, String
10
+ field :stack_id, String
11
+ field :stale_configs, :boolean
12
+ field :state, String
13
+ field :actual_configs, Hash # Fix
14
+ field :host, Hash # Fix
15
+ field :metrics, Hash # Fix
16
+ field :processes, Array # Fix
17
+ field :component, Array, of: Component
18
+
19
+ def create
20
+ resource_action(:post)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ module Weavr
2
+ class OperatingSystem < Resource
3
+ field :os_type, String
4
+ field :stack_name, String
5
+ field :stack_version, String
6
+ collection :repositories, Repository, key_method: 'repo_id'
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Weavr
2
+ class Repository < Resource
3
+ field :base_url, String
4
+ field :default_base_url, String
5
+ field :latest_base_url, String
6
+ field :mirrors_list, Whatever # Fix
7
+ field :os_type, String
8
+ field :repo_id, String
9
+ field :repo_name, String
10
+ field :stack_name, String
11
+ field :stack_vesion, String
12
+
13
+ def update_base_url url
14
+ resource_action(:put, self.class.label => { base_url: url })
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Weavr
2
+ class Request < Resource
3
+ field :cluster_name, String
4
+ field :id, Integer
5
+ field :aborted_task_count, Integer
6
+ field :completed_task_count, Integer
7
+ field :create_time, Integer # Needs to be Date
8
+ field :end_time, Integer # Needs to be Date
9
+ field :failed_task_count, Integer
10
+ field :inputs, Whatever # Fix
11
+ field :progress_percent, Float
12
+ field :queued_task_count, Integer
13
+ field :request_context, String
14
+ field :request_schedule, Whatever # Fix
15
+ field :request_status, String
16
+ field :resource_filter, Array
17
+ field :start_time, Integer # Needs to be Date
18
+ field :task_count, Integer
19
+ field :timed_out_task_count, Integer
20
+ field :type, String
21
+ collection :tasks, Task, key_method: 'id'
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Weavr
2
+ class Service < Resource
3
+ def self.label() 'ServiceInfo' ; end
4
+
5
+ field :cluster_name, String
6
+ field :service_name, String
7
+ field :maintenance_state, String
8
+ field :state, String
9
+ collection :components, Component, key_method: 'component_name'
10
+
11
+ def create
12
+ resource_action(:post, self.class.label => { service_name: service_name })
13
+ end
14
+
15
+ def to_state state
16
+ res = resource_action(:put, RequestInfo: { context: "Transition to #{state}" }, Body: { ServiceInfo: { state: state } })
17
+ Request.receive(res || { })
18
+ end
19
+
20
+ def install
21
+ to_state'INSTALLED'
22
+ end
23
+
24
+ def stop
25
+ to_state 'INSTALLED'
26
+ end
27
+
28
+ def start
29
+ to_state 'STARTED'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ module Weavr
2
+ class ServiceComponent < Resource
3
+ def self.label() 'StackServiceComponents' ; end
4
+ field :cardinality, Integer
5
+ field :component_category, String
6
+ field :component_name, String
7
+ field :is_client, :boolean
8
+ field :is_master, :boolean
9
+ field :service_name, String
10
+ field :stack_name, String
11
+ field :stack_version, String
12
+ field :dependencies, Array # Fix
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module Weavr
2
+ class Stack < Resource
3
+ field :stack_name, String
4
+ collection :versions, StackVersion, key_method: 'stack_version'
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Weavr
2
+ class StackConfiguration < Resource
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Weavr
2
+ class StackService < Resource
3
+ field :service_name, String
4
+ field :stack_name, String
5
+ field :stack_version, String
6
+ field :comments, String
7
+ field :config_types, Array, of: String
8
+ field :service_version, String
9
+ field :user_name, String
10
+ collection :service_components, ServiceComponent, key_method: 'component_name'
11
+ collection :configurations, StackConfiguration, key_method: 'property_name'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Weavr
2
+ class StackVersion < Resource
3
+ def self.label() 'Versions' ; end
4
+
5
+ field :stack_name, String
6
+ field :stack_version, String
7
+ field :active, :boolean
8
+ field :min_upgrade_version, String
9
+ field :parent_stack_version, String
10
+ collection :stackServices, StackService, key_method: 'service_name'
11
+ collection :operatingSystems, OperatingSystem, key_method: 'os_type'
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module Weavr
2
+ class Task < Resource
3
+ field :cluster_name, String
4
+ field :attempt_cnt, Integer
5
+ field :id, Integer
6
+ field :request_id, Integer
7
+ field :command, String
8
+ field :command_detail, String
9
+ field :exit_code, Integer
10
+ field :host_name, String
11
+ field :request_id, String
12
+ field :role, String
13
+ field :stage_id, String
14
+ field :start_time, Integer # Needs to be Date
15
+ field :status, String
16
+ field :stdout, String
17
+ field :structured_out, String
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ module Weavr
2
+ class Response
3
+ include Gorillib::Model
4
+
5
+ field :status, Integer
6
+ field :headers, Hash
7
+ field :body, Hash
8
+ field :errors, Array, default: []
9
+
10
+ def self.handle(status, headers, body)
11
+ self.receive(status: status, headers: headers, body: body).validate!
12
+ end
13
+
14
+ # Really wish there was validations for these
15
+ def receive_status param
16
+ case param
17
+ when 200
18
+ @status = param
19
+ when 400
20
+ errors << { status: 400 }
21
+ when 404
22
+ errors << { status: 404 }
23
+ when 500
24
+ errors << { status: 500 }
25
+ end
26
+ end
27
+
28
+ # def receive_headers param
29
+ # end
30
+
31
+ # def receive_body param
32
+ # end
33
+
34
+ def validate!
35
+ raise RequestError.new error_message unless errors.empty?
36
+ body
37
+ end
38
+
39
+ def error_message
40
+ body['message']
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Weavr
2
+ VERSION = '0.0.4'
3
+ end
@@ -0,0 +1,9 @@
1
+ if ENV['WEAVR_COV']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_group 'Specs', 'spec/'
5
+ add_group 'Library', 'lib/'
6
+ end
7
+ end
8
+
9
+ require 'weavr'
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe Weavr do
4
+
5
+ def clear_variables mod
6
+ mod.instance_variables.each do |ivar|
7
+ mod.instance_variable_set(ivar, nil)
8
+ end
9
+ end
10
+
11
+ subject(:weavr) { described_class }
12
+ subject(:collection_resource){ Weavr::Collection }
13
+ subject(:cluster_resource) { Weavr::Cluster }
14
+
15
+ after(:each) do
16
+ clear_variables described_class
17
+ end
18
+
19
+ context '.configure' do
20
+ it 'sets the log' do
21
+ weavr.configure
22
+ expect(weavr.log).to_not be_nil
23
+ end
24
+
25
+ it 'sets the connection' do
26
+ weavr.configure
27
+ expect(weavr.connection).to_not be_nil
28
+ end
29
+
30
+ it 'allows overrides' do
31
+ weavr.configure(log_level: 'debug', host: 'foo')
32
+ expect(weavr.log.level).to eq(Logger::DEBUG)
33
+ expect(weavr.connection.host).to eq('foo')
34
+ end
35
+ end
36
+
37
+ context '.connection' do
38
+ it 'returns the connection if set' do
39
+ weavr.set_connection :conn
40
+ expect(weavr.connection).to eq(:conn)
41
+ end
42
+
43
+ it 'configures a default connection if unset' do
44
+ weavr.should_receive(:configure)
45
+ weavr.connection
46
+ end
47
+ end
48
+
49
+ context '.log' do
50
+ it 'returns the log if set' do
51
+ weavr.set_log :device
52
+ expect(weavr.log).to eq(:device)
53
+ end
54
+ end
55
+
56
+ context '.cluster' do
57
+ it 'returns the named cluster' do
58
+ weavr.connection.should_receive(:resource).with(:get, 'clusters/foo').and_return(cluster_name: 'foo')
59
+ expect(weavr.cluster('foo').cluster_name).to eq('foo')
60
+ end
61
+ end
62
+
63
+ context '.clusters' do
64
+ it 'returns a collection of clusters' do
65
+ weavr.connection.should_receive(:resource).with(:get, 'clusters').and_return(items: [{ cluster_name: 'foo' }])
66
+ clusters = weavr.clusters
67
+ expect(clusters).to be_a(collection_resource)
68
+ expect(clusters.items.one?{ |c| c.cluster_name == 'foo' }).to be_true
69
+ end
70
+ end
71
+
72
+ context '.create_cluster' do
73
+ it 'creates a new cluster' do
74
+ weavr.connection.should_receive(:resource).with(:post, 'clusters/foo', an_instance_of(Hash)).and_return(cluster_name: 'foo')
75
+ cluster = weavr.create_cluster('foo')
76
+ expect(cluster).to be_a(cluster_resource)
77
+ expect(cluster.cluster_name).to eq('foo')
78
+ end
79
+ end
80
+
81
+ context '.stacks' do
82
+ it 'returns a collection of stacks' do
83
+ weavr.connection.should_receive(:resource).with(:get, 'stacks2').and_return(items: [{ stack_name: 'hdp' }])
84
+ stacks = weavr.stacks
85
+ expect(stacks).to be_a(collection_resource)
86
+ expect(stacks.items.one?{ |c| c.stack_name == 'hdp' }).to be_true
87
+ end
88
+ end
89
+
90
+ context '.configure_repository' do
91
+ end
92
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'weavr/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'weavr'
7
+ gem.version = Weavr::VERSION
8
+ gem.authors = ['Travis Dempsey']
9
+ gem.email = ['dempsey.travis@gmail.com']
10
+ gem.licenses = ['Apache 2.0']
11
+ gem.homepage = 'https://github.com/infochimps-labs/weavr.git'
12
+ gem.summary = 'A Ruby library for Apache Ambari REST client'
13
+ gem.description = <<-DESC.gsub(/^ {4}/, '').chomp
14
+ Weavr
15
+ DESC
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(/^bin/){ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(/^(test|spec|features)/)
20
+ gem.require_paths = ['lib']
21
+
22
+ gem.add_development_dependency('bundler', '~> 1.6.2')
23
+
24
+ gem.add_dependency('activesupport', '~> 4.1.4')
25
+ gem.add_dependency('faraday', '~> 0.9.0')
26
+ gem.add_dependency('faraday_middleware', '~> 0.9.1')
27
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weavr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Dempsey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 4.1.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 4.1.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday_middleware
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.1
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.1
78
+ description: Weavr
79
+ email:
80
+ - dempsey.travis@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.md
88
+ - README.md
89
+ - Rakefile
90
+ - lib/weavr.rb
91
+ - lib/weavr/connection.rb
92
+ - lib/weavr/error.rb
93
+ - lib/weavr/resource.rb
94
+ - lib/weavr/resource/class_map.rb
95
+ - lib/weavr/resource/cluster.rb
96
+ - lib/weavr/resource/collection.rb
97
+ - lib/weavr/resource/component.rb
98
+ - lib/weavr/resource/configuration.rb
99
+ - lib/weavr/resource/host.rb
100
+ - lib/weavr/resource/host_role.rb
101
+ - lib/weavr/resource/operating_system.rb
102
+ - lib/weavr/resource/repository.rb
103
+ - lib/weavr/resource/request.rb
104
+ - lib/weavr/resource/service.rb
105
+ - lib/weavr/resource/service_component.rb
106
+ - lib/weavr/resource/stack.rb
107
+ - lib/weavr/resource/stack_configuration.rb
108
+ - lib/weavr/resource/stack_service.rb
109
+ - lib/weavr/resource/stack_version.rb
110
+ - lib/weavr/resource/task.rb
111
+ - lib/weavr/response.rb
112
+ - lib/weavr/version.rb
113
+ - spec/spec_helper.rb
114
+ - spec/weavr_spec.rb
115
+ - weavr.gemspec
116
+ homepage: https://github.com/infochimps-labs/weavr.git
117
+ licenses:
118
+ - Apache 2.0
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ segments:
130
+ - 0
131
+ hash: 1235392214138228814
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ segments:
139
+ - 0
140
+ hash: 1235392214138228814
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.23
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: A Ruby library for Apache Ambari REST client
147
+ test_files:
148
+ - spec/spec_helper.rb
149
+ - spec/weavr_spec.rb