gapinc-cobbler 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cobbler.rb ADDED
@@ -0,0 +1,35 @@
1
+ #
2
+ # cobbler.rb - Cobbler module declaration.
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+
23
+ require 'active_support/inflector'
24
+ require 'cobbler/base'
25
+ require 'cobbler/distro'
26
+ require 'cobbler/image'
27
+ require 'cobbler/profile'
28
+ require 'cobbler/repo'
29
+ require 'cobbler/system'
30
+
31
+ module Cobbler
32
+
33
+ VERSION = '2.0.0'
34
+
35
+ end
@@ -0,0 +1,128 @@
1
+ #
2
+ # base.rb
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+ #
23
+
24
+ require 'cobbler/common/debug'
25
+ require 'cobbler/connection/handling'
26
+ require 'cobbler/connection/common'
27
+ require 'cobbler/common/lifecycle'
28
+ require 'cobbler/common/finders'
29
+
30
+ # +Base+ represents a type of item on the Cobbler server.
31
+ #
32
+ # Child classes can define fields that will be retrieved from Cobbler by
33
+ # using the +cobbler_field+ method. For example:
34
+ #
35
+ # class System < Base
36
+ # cobbler_lifecycle :find_all => 'get_systems'
37
+ # cobbler_field :name
38
+ # cobbler_collection :owners, :type => 'String', :packing => :hash
39
+ # end
40
+ #
41
+ # declares a class named System that contains two fields and a class-level
42
+ # method.
43
+ #
44
+ # The first field, "name", is a simple property. It will be retrieved from
45
+ # the value "name" in the remote definition for a system, identifyed by the
46
+ # +:owner+ argument.
47
+ #
48
+ # The second field, "owners", is similarly retrieved from a property also
49
+ # named "owners" in the remote definition. However, this property is a
50
+ # collection: in this case, it is an array of definitions itself. The
51
+ # +:type+ argument identifies what the +local+ class type is that will be
52
+ # used to represent each element in the collection.
53
+ #
54
+ # A +cobbler_collection+ is packed in one of two ways: either as an array
55
+ # of values or as a hash of keys and associated values. These are defined by
56
+ # the +:packing+ argument with the values +Array+ and +Hash+, respectively.
57
+ #
58
+ # The +cobbler_lifecycle+ method allows for declaring different methods for
59
+ # retrieving remote instances of the class. +cobbler_lifecycle+ also declares
60
+ # automatically the various API methods if they aren't overwritten.
61
+ # These methods are (defaults are shown for an item called Model):
62
+ #
63
+ # +find_one+ - to find a single instance (get_model)
64
+ # +find_all+ - to find all instances (get_models)
65
+ # +remove+ - to remove an instance (remove_model)
66
+ # +handle+ - to obtain the handle for this item (get_model_handle)
67
+ # +save+ - to store an item (save_model)
68
+ # +new+ - to create a new item (new_model)
69
+ # +modify+ - to modify an existing model (modfiy_model)
70
+ #
71
+ module Cobbler
72
+ class Base
73
+
74
+ def initialize(defs = {},new_record = true)
75
+ if new_record
76
+ @user_definitions = defs
77
+ else
78
+ @definitions = defs
79
+ end
80
+ end
81
+
82
+ include Cobbler::Common::Debug
83
+ include Cobbler::Connection::Handling
84
+ include Cobbler::Connection::Common
85
+
86
+ include Cobbler::Common::Lifecycle
87
+ include Cobbler::Common::Finders
88
+
89
+ # Save an item on the remote cobbler server
90
+ # This will first lookup if the item already exists on the remote server
91
+ # and use its handle store the attributes. Otherwise a new item is created.
92
+ def save
93
+ unless [ :handle, :new, :modify, :save ].all?{|method| api_methods[method] }
94
+ raise "Not all necessary api methods are defined to process this action!"
95
+ end
96
+ entry = self.class.find_one(name)
97
+ self.class.in_transaction(true) do |token|
98
+ if entry
99
+ entryid = self.class.make_call(api_methods[:handle],name,token)
100
+ else
101
+ entryid = self.class.make_call(api_methods[:new],token)
102
+ self.class.make_call(api_methods[:modify],entryid,'name', name, token)
103
+ end
104
+
105
+ cobbler_record_fields.each do |field|
106
+ field_s = field.to_s
107
+ if !locked_fields.include?(field) && user_definitions.has_key?(field_s)
108
+ self.class.make_call(api_methods[:modify],entryid,field_s, user_definitions[field_s], token)
109
+ end
110
+ end
111
+
112
+ cobbler_collections_store_callbacks.each do |callback|
113
+ send(callback,entryid,token)
114
+ end
115
+
116
+ self.class.make_call(api_methods[:save],entryid,token)
117
+ end
118
+ end
119
+
120
+ # delete the item on the cobbler server
121
+ def remove
122
+ raise "Not all necessary api methods are defined to process this action!" unless api_methods[:remove]
123
+ self.class.in_transaction(true) do |token|
124
+ self.class.make_call(api_methods[:remove],name,token)
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,59 @@
1
+ #
2
+ # debug.rb
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+ #
23
+
24
+ # +Debug+ provides a basic debugging infrastructure.
25
+ module Cobbler
26
+ module Common
27
+ module Debug
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+
32
+ def debug(msg)
33
+ self.class.debug(msg)
34
+ end
35
+
36
+ module ClassMethods
37
+ def debug_enabled
38
+ @debug_enabled ||= false
39
+ end
40
+
41
+ def debug_enabled=(enable)
42
+ @debug_enabled = enable
43
+ end
44
+
45
+ def output=(output)
46
+ @output = output
47
+ end
48
+
49
+ def output
50
+ @output ||= STDOUT
51
+ end
52
+
53
+ def debug(msg)
54
+ output.puts msg if @debug_enabled
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,55 @@
1
+ #
2
+ # finders.rb
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+ #
23
+
24
+ # +Finders+ provides the basic 2 finder methods to query a cobbler server
25
+ #
26
+ # +find_one+ to fetch exactly one item based on its name
27
+ # +find+ to find all items, takes a block to work with the fetched items
28
+ module Cobbler
29
+ module Common
30
+ module Finders
31
+ def self.included(base)
32
+ base.extend(ClassMethods)
33
+ end
34
+
35
+ module ClassMethods
36
+ def find(&block)
37
+ raise "No idea how to fetch a list of myself, as no find_all method is defined" unless api_methods[:find_all]
38
+ result = []
39
+ in_transaction { make_call(api_methods[:find_all]) }.to_a.each do |record|
40
+ c_record = new(record,false)
41
+ result << c_record
42
+ yield(c_record) if block_given?
43
+ end
44
+ return result
45
+ end
46
+
47
+ def find_one(name)
48
+ raise "No idea how to fetch myself, as no find_one method is defined" unless api_methods[:find_one]
49
+ properties = in_transaction { make_call(api_methods[:find_one],name) }
50
+ valid_properties?(properties) ? new(properties,false) : nil
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,163 @@
1
+ #
2
+ # lifecycle.rb
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+
23
+ # +Lifecycle+ implements a default set of functionality that a cobbler item
24
+ # can have.
25
+ # Usually the +Lifecycle+ methods are used to define query functions and define
26
+ # which fields of a cobbler item is exposed through the api.
27
+ module Cobbler
28
+ module Common
29
+ module Lifecycle
30
+ def self.included(base)
31
+ base.extend ClassMethods
32
+ end
33
+
34
+ def definitions
35
+ @definitions ||= {}
36
+ end
37
+
38
+ def user_definitions
39
+ @user_definitions ||= {}
40
+ end
41
+
42
+ def locked_fields
43
+ self.class.locked_fields
44
+ end
45
+
46
+ def cobbler_record_fields
47
+ self.class.cobbler_record_fields
48
+ end
49
+ def cobbler_collections_store_callbacks
50
+ self.class.cobbler_collections_store_callbacks
51
+ end
52
+
53
+ def api_methods
54
+ self.class.api_methods
55
+ end
56
+
57
+ module ClassMethods
58
+ def api_methods
59
+ return @api_methods if @api_methods
60
+ model_name = self.name.gsub(/.*::/,'').underscore
61
+ @api_methods = {
62
+ :find_all => "get_#{model_name.pluralize}",
63
+ :find_one => "get_#{model_name}",
64
+ :handle => "get_#{model_name}_handle",
65
+ :remove => "remove_#{model_name}",
66
+ :save => "save_#{model_name}",
67
+ :new => "new_#{model_name}",
68
+ :modify => "modify_#{model_name}"
69
+ }
70
+ end
71
+
72
+ def cobbler_record_fields
73
+ @cobbler_record_fields ||= []
74
+ end
75
+
76
+ def locked_fields
77
+ @locked_fields ||= []
78
+ end
79
+
80
+ def cobbler_collections_store_callbacks
81
+ @cobbler_collections_store_callbacks ||= []
82
+ end
83
+
84
+ # Define/adjust all necessary lookup methods for a usual
85
+ # cobbler item.
86
+ #
87
+ def cobbler_lifecycle(lookup_methods={})
88
+ api_methods.merge!(lookup_methods)
89
+ end
90
+
91
+ # Allows for dynamically declaring fields that will come from
92
+ # Cobbler.
93
+ #
94
+ def cobbler_field(field,options={})
95
+ # name is always locked and findable as this is a special field
96
+ if field == :name
97
+ options[:locked] = true if options[:locked] || options[:locked].nil?
98
+ options[:findable] = api_methods[:find_one] if options[:findable] || options[:findable].nil?
99
+ end
100
+ options.each do |key,value|
101
+ case key
102
+ when :findable then
103
+ if value
104
+ module_eval <<-"MEND"
105
+ def self.find_by_#{field}(value)
106
+ properties = in_transaction{ make_call('#{value}',value) }
107
+ valid_properties?(properties) ? new(properties,false) : nil
108
+ end
109
+ MEND
110
+ end
111
+ when :locked then
112
+ locked_fields << field if value
113
+ end
114
+ end
115
+
116
+ module_eval("def #{field}() user_definitions['#{field}'] || definitions['#{field}']; end")
117
+ module_eval("def #{field}=(val) user_definitions['#{field}'] = val; end")
118
+
119
+ cobbler_record_fields << field
120
+ end
121
+
122
+ # Declare many fields at once.
123
+ def cobbler_fields(*fields)
124
+ fields.to_a.each {|field| cobbler_field field }
125
+ end
126
+
127
+ # Allows a field to be defined as a collection of objects. The type for that
128
+ # other class must be provided.
129
+ #
130
+ def cobbler_collection(field, options={})
131
+ classname = options[:type] || 'String'
132
+ packing = options[:packing] ? options[:packing].to_s.classify : 'Array'
133
+
134
+ packing_code = {
135
+ 'Array' => "(definitions['#{field}']||[]).each{|value| new_value << #{classname}.new(value) }",
136
+ 'Hash' => "(definitions['#{field}']||{}).each{|key,value| new_value[key] = #{classname}.new(value) }"
137
+ }
138
+
139
+ cobbler_collections_store_callbacks << options[:store] if options[:store]
140
+ # unless we have a seperate store callback we store collections normally
141
+ cobbler_record_fields << field unless options[:store]
142
+
143
+ module_eval <<-"MEND"
144
+ def #{field}
145
+ if !user_definitions['#{field}'] && !definitions['#{field}'].is_a?(#{packing})
146
+ new_value = #{packing}.new
147
+ #{packing_code[packing]}
148
+ definitions['#{field}'] = new_value
149
+ end
150
+ user_definitions['#{field}'] ||= definitions['#{field}']
151
+ # return always the user_definitions as we might do operations on these objects, e.g. <<
152
+ user_definitions['#{field}']
153
+ end
154
+
155
+ def #{field}=(value)
156
+ user_definitions['#{field}'] = value
157
+ end
158
+ MEND
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,79 @@
1
+ #
2
+ # common.rb
3
+ #
4
+ # Copyright (C) 2008,2009 Red Hat, Inc.
5
+ # Written by Darryl L. Pierce <dpierce@redhat.com>
6
+ # Extended 2012 by duritong <peter.meier@immerda.ch>
7
+ #
8
+ # This file is part of rubygem-cobbler.
9
+ #
10
+ # rubygem-cobbler is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published
12
+ # by the Free Software Foundation, either version 2.1 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # rubygem-cobbler is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
22
+
23
+ # +Common+ provides common methods of the cobbler server exposed through the
24
+ # api.
25
+ module Cobbler
26
+ module Connection
27
+ module Common
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+
32
+ module ClassMethods
33
+ # tests a connections
34
+ def test_connection
35
+ !in_transaction do
36
+ result = login
37
+ logout if result
38
+ result
39
+ end.nil?
40
+ end
41
+
42
+ # start a sync on the cobbler server
43
+ def sync
44
+ in_transaction(true) do |token|
45
+ make_call('sync',token)
46
+ end
47
+ end
48
+
49
+ # get all events (for a certain user)
50
+ def events(for_user='')
51
+ in_transaction do
52
+ make_call('get_events',for_user)
53
+ end
54
+ end
55
+
56
+ # get the log for a certain event
57
+ def event_log(event_id)
58
+ in_transaction do
59
+ make_call('get_event_log',event_id)
60
+ end
61
+ end
62
+
63
+ # import a tree into cobbler
64
+ def import(path,name,arch,additional_options={})
65
+ in_transaction(true) do |token|
66
+ make_call('background_import',{'path' => path ,'name' => name , 'arch' => arch}.merge(additional_options),token)
67
+ end
68
+ end
69
+
70
+ # start syncing the following repositories.
71
+ def reposync(repos=[],tries=3)
72
+ in_transaction(true) do |token|
73
+ make_call('background_reposync',{'repos' => repos, 'tries' => tries},token)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end