foreman_reserve 0.1.6

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: b47f26dc034d5b901b2661b71bb038d7555b864f
4
+ data.tar.gz: 312437ec807d584309fb1cc3eef4e050a7eacc20
5
+ SHA512:
6
+ metadata.gz: 967b9021d5c71dabd6eddf4c8c7ec0d213ee356b63fab9aaadb2d0836a533b19c982babcef6bc45e37359b7603f7a4975c32af3c2c42543ab3cf22584bf796ce
7
+ data.tar.gz: 84457171d2912961de32a5b7802643d7297a29ed84ad35735f115d8c906da740cccba40fba6bf9d65ace5e3f891f1ac91f2b34fe2533206eb1edabdefa19205d
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.2.2"
12
+ gem "jeweler", "~> 1.8.4"
13
+ # gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.9)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ git (1.2.5)
8
+ i18n (0.6.1)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.7.5)
15
+ multi_json (1.3.7)
16
+ rake (10.0.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ shoulda (3.3.2)
20
+ shoulda-context (~> 1.0.1)
21
+ shoulda-matchers (~> 1.4.1)
22
+ shoulda-context (1.0.1)
23
+ shoulda-matchers (1.4.1)
24
+ activesupport (>= 3.0.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.2.2)
31
+ jeweler (~> 1.8.4)
32
+ rdoc (~> 3.12)
33
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Joseph Mitchell Magen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = foreman_reserve
2
+
3
+ This gem will added a route to the API to return the name of available host(s)
4
+
5
+ = Installation:
6
+
7
+ gem 'foreman_reserve', :git => "https://github.com/isratrade/foreman_reserve.git"
8
+
9
+ In order to work, the foreman application must be the following two lines commented out.
10
+
11
+ config/routes.rb
12
+ # match '*a', :to => 'errors#routing'
13
+
14
+ config/routes/v1.rb
15
+ # match '*other', :to => 'home#route_error'
16
+
17
+ = Usage
18
+
19
+ To return 1 host:
20
+ curl -u admin:secret -H 'accept:application/json' http://0.0.0.0:3000/api/hosts_reserve
21
+
22
+ To return multiple hosts, pass a parameter called 'amount'
23
+
24
+ curl -u admin:secret -H 'accept:application/json' http://0.0.0.0:3000/api/hosts_reserve?amount=3
25
+
26
+ Filter using a specific query: (name ~ virt%)
27
+
28
+ curl -u admin:secret -H 'accept:application/json' 'http://0.0.0.0:3000/api/hosts_reserve?query=name%20~%20virt%25'
29
+
30
+ Release a specific host:
31
+
32
+ curl -u admin:secret -H 'accept:application/json' 'http://0.0.0.0:3000/api/hosts_release?host_name=myreservedhost'
33
+
34
+ Release a group of hosts using a query: (name ~ virt%)
35
+
36
+ curl -u admin:secret -H 'accept:application/json' 'http://0.0.0.0:3000/api/hosts_reserve?query=name%20~%20virt%25'
37
+
38
+ == Contributing to foreman_reserve
39
+
40
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
41
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
42
+ * Fork the project.
43
+ * Start a feature/bugfix branch.
44
+ * Commit and push until you are happy with your contribution.
45
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
46
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
47
+
48
+ == Copyright
49
+
50
+ Copyright (c) 2012 Joseph Mitchell Magen. See LICENSE.txt for
51
+ further details.
52
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "foreman_reserve"
18
+ gem.homepage = "http://github.com/isratrade/foreman_reserve"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Plugin engine for Foreman to reserve a host}
21
+ gem.description = %Q{Plugin engine for Foreman to reserve a host}
22
+ gem.email = "jmagen@redhat.com"
23
+ gem.authors = ["Joseph Mitchell Magen"]
24
+ gem.version = '0.1.4'
25
+ end
26
+ #Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ # require 'rcov/rcovtask'
36
+ # Rcov::RcovTask.new do |test|
37
+ # test.libs << 'test'
38
+ # test.pattern = 'test/**/test_*.rb'
39
+ # test.verbose = true
40
+ # test.rcov_opts << '--exclude "gems/*"'
41
+ # end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "foreman_reserve #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.6
@@ -0,0 +1,143 @@
1
+ module Api
2
+ module V1
3
+ class ForemanReserveController < Api::V1::BaseController
4
+ unloadable
5
+
6
+ def get_reserved(query='')
7
+ hosts = User.current.admin? ? Host::Managed : Host::Managed.my_hosts
8
+ hosts.search_for(query)
9
+ hosts.each do |host|
10
+ params = host.info()['parameters']
11
+ if params and
12
+ params.key?('RESERVED') and
13
+ params['RESERVED'] != 'false'
14
+ host
15
+ end
16
+ end
17
+ end
18
+
19
+ def get_free(query='')
20
+ hosts = User.current.admin? ? Host::Managed : Host::Managed.my_hosts
21
+ hosts.search_for(query)
22
+ hosts.each do |host|
23
+ params = host.info()['parameters']
24
+ if params
25
+ and params.has_key('RESERVED')
26
+ and params['RESERVED'] == 'false'
27
+ host
28
+ end
29
+ end
30
+ end
31
+
32
+ def not_found(exception = nil)
33
+ logger.debug "not found: #{exception}" if exception
34
+ respond_to do |format|
35
+ format.html { render "common/404", :status => 404 }
36
+ format.json { head :status => 404}
37
+ format.yaml { head :status => 404}
38
+ end
39
+ true
40
+ end
41
+
42
+ def reserve
43
+ amount = (params[:amount] || 1).to_i
44
+ reason = params[:reason] || 'true'
45
+ ## Lock to avoid reserving the same host twice
46
+ unless File.exists? "/tmp/foreman_reserve.lock"
47
+ File.open("/tmp/foreman_reserve.lock", 'w') {}
48
+ end
49
+ lock = File.new("/tmp/foreman_reserve.lock")
50
+ begin
51
+ lock.flock(File::LOCK_EX)
52
+ potential_hosts = get_free(params[:query])
53
+ return not_found if potential_hosts.empty?
54
+ return not_acceptable if potential_hosts.count < amount
55
+ @hosts = potential_hosts[0..(amount-1)].each do |host|
56
+ logger.error host.class.name
57
+ logger.error host.class.instance_methods(false)
58
+ logger.error Host.class.instance_methods(false)
59
+ host.reserve!(reason)
60
+ end
61
+ ensure
62
+ lock.flock(File::LOCK_UN)
63
+ end
64
+ respond_to do |format|
65
+ format.json {render :json => @hosts }
66
+ format.yaml {render :text => @hosts.to_yaml}
67
+ format.html {not_found }
68
+ end
69
+ end
70
+
71
+ def release
72
+ host_name = (params[:host_name] || '')
73
+ query = params[:query]
74
+ amount = (params[:amount] || 0 ).to_i
75
+ if host_name != ''
76
+ query = "#{query} AND name = #{host_name}"
77
+ end
78
+ reserved_hosts = get_reserved(query)
79
+ return not_found if reserved_hosts.empty?
80
+ if amount != 0
81
+ return not_acceptable if reserved_hosts.count < amount
82
+ @hosts = reserved_hosts[0..(amount-1)].each { |host| host.release! }
83
+ else
84
+ @hosts = reserved_hosts.each { |host| host.release! }
85
+ end
86
+ respond_to do |format|
87
+ format.json {render :json => @hosts.map(&:name) }
88
+ format.yaml {render :text => @hosts.to_yaml}
89
+ format.html {not_found }
90
+ end
91
+ end
92
+
93
+ def show_reserved
94
+ hosts = get_reserved(params[:query])
95
+ return not_found if hosts.empty?
96
+ respond_to do |format|
97
+ format.json {render :json => hosts }
98
+ format.yaml {render :text => hosts.to_yaml}
99
+ format.html {not_found }
100
+ end
101
+ end
102
+
103
+ def show_available
104
+ amount = (params[:amount] || 0).to_i
105
+ hosts = get_free(params[:query])
106
+ return not_found if hosts.empty?
107
+ if amount != 0
108
+ return not_acceptable if hosts.count < amount
109
+ hosts = hosts[0..(amount-1)]
110
+ end
111
+ respond_to do |format|
112
+ format.json {render :json => hosts }
113
+ format.yaml {render :text => hosts.to_yaml}
114
+ format.html {not_found }
115
+ end
116
+ end
117
+
118
+ def update_reason
119
+ amount = (params[:amount] || 0).to_i
120
+ reason = params[:reason] || 'true'
121
+ potential_hosts = get_reserved(params[:query])
122
+ return not_found if potential_hosts.empty?
123
+ if amount != 0
124
+ return not_acceptable if potential_hosts.count < amount
125
+ potential_hosts[0..(amount-1)].each { |host| host.reserve!(reason) }
126
+ else
127
+ potential_hosts.each { |host| host.reserve!(reason) }
128
+ end
129
+ @hosts = get_reserved(params[:query])
130
+ respond_to do |format|
131
+ format.json {render :json => @hosts }
132
+ format.yaml {render :text => @hosts.to_yaml}
133
+ format.html {not_found }
134
+ end
135
+ end
136
+
137
+ def not_acceptable
138
+ head :status => 406
139
+ end
140
+
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,24 @@
1
+ module ForemanReserve
2
+ module HostExtensions
3
+ extend ActiveSupport::Concern
4
+ module InstanceMethods
5
+ def reserve!(reason = 'true')
6
+ param = "RESERVED"
7
+ if p=host_parameters.find_by_name(param)
8
+ p.update_attribute(:value, reason)
9
+ else
10
+ host_parameters.create!(:name => param, :value => reason)
11
+ end
12
+ end
13
+ def release!
14
+ param = "RESERVED"
15
+ if p=host_parameters.find_by_name(param)
16
+ p.update_attribute(:value, "false")
17
+ end
18
+ end
19
+ def as_json(options={})
20
+ super(:methods => [:host_parameters])
21
+ end
22
+ end
23
+ end
24
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ Rails.application.routes.draw do
2
+
3
+ get "api/hosts_reserve" => "api/v1/foreman_reserve#reserve"
4
+ get "api/hosts_release" => "api/v1/foreman_reserve#release"
5
+ get "api/show_available" => "api/v1/foreman_reserve#show_available"
6
+ get "api/show_reserved" => "api/v1/foreman_reserve#show_reserved"
7
+ get "api/update_reserved_reason" => "api/v1/foreman_reserve#update_reason"
8
+
9
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "foreman_reserve"
5
+ s.version = "0.1.6"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Joseph Mitchell Magen", "David Caro"]
9
+ s.date = "2013-02-05"
10
+ s.description = "Plugin engine for Foreman to reserve a host"
11
+ s.email = "jmagen@redhat.com"
12
+ s.extra_rdoc_files = ["LICENSE.txt", "README.rdoc"]
13
+ s.files = [".document", "Gemfile", "Gemfile.lock", "LICENSE.txt",
14
+ "README.rdoc", "Rakefile", "VERSION",
15
+ "app/controllers/foreman_reserve/api/hosts_controller.rb",
16
+ "app/models/foreman_reserve/host_extensions.rb",
17
+ "config/routes.rb", "foreman_reserve.gemspec",
18
+ "lib/foreman_reserve/engine.rb", "lib/foreman_reserve.rb",
19
+ "lib/rails/railties/tasks.rake", "test/helper.rb",
20
+ "test/test_foreman_reserve.rb"]
21
+ s.homepage = "http://github.com/david-caro/foreman_reserve"
22
+ s.licenses = ["MIT"]
23
+ s.require_paths = ["lib"]
24
+ s.rubygems_version = "1.8.10"
25
+ s.summary = "Plugin engine for Foreman to allocate hosts"
26
+
27
+ if s.respond_to? :specification_version then
28
+ s.specification_version = 3
29
+
30
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
31
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
32
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
33
+ s.add_development_dependency(%q<bundler>, ["~> 1.2.2"])
34
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
35
+ else
36
+ s.add_dependency(%q<shoulda>, [">= 0"])
37
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
38
+ s.add_dependency(%q<bundler>, ["~> 1.2.2"])
39
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
40
+ end
41
+ else
42
+ s.add_dependency(%q<shoulda>, [">= 0"])
43
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
44
+ s.add_dependency(%q<bundler>, ["~> 1.2.2"])
45
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ require 'foreman_reserve'
2
+ require 'rails'
3
+ require 'action_controller'
4
+
5
+ module ForemanReserve
6
+ class Engine < ::Rails::Engine
7
+
8
+ config.to_prepare do
9
+ Host::Managed.send :include, ForemanReserve::HostExtensions
10
+ Host.send :include, ForemanReserve::HostExtensions
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ForemanReserve
2
+ require "foreman_reserve/engine"
3
+ end
@@ -0,0 +1,8 @@
1
+ namespace :foreman_reserve do
2
+
3
+ desc "example gem rake task"
4
+ task :report => :environment do
5
+ puts "you just ran the example gem rake task"
6
+ end
7
+
8
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'foreman_reserve'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestForemanReserve < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreman_reserve
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Mitchell Magen
8
+ - David Caro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rdoc
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '3.12'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '3.12'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: 1.2.2
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.2.2
56
+ - !ruby/object:Gem::Dependency
57
+ name: jeweler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 1.8.4
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ description: Plugin engine for Foreman to reserve a host
71
+ email: jmagen@redhat.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.rdoc
77
+ files:
78
+ - .document
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - app/controllers/foreman_reserve/api/hosts_controller.rb
86
+ - app/models/foreman_reserve/host_extensions.rb
87
+ - config/routes.rb
88
+ - foreman_reserve.gemspec
89
+ - lib/foreman_reserve/engine.rb
90
+ - lib/foreman_reserve.rb
91
+ - lib/rails/railties/tasks.rake
92
+ - test/helper.rb
93
+ - test/test_foreman_reserve.rb
94
+ homepage: http://github.com/david-caro/foreman_reserve
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.1.11
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Plugin engine for Foreman to allocate hosts
118
+ test_files: []