Class BoxGrinder::BasePlugin
In: lib/boxgrinder-build/plugins/base-plugin.rb
lib/boxgrinder-build/plugins/base-plugin.rb
Parent: Object

Methods

Included Modules

Plugins Plugins

Attributes

deliverables  [R] 
deliverables  [R] 
plugin_info  [R] 
plugin_info  [R] 

Public Class methods

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 37
37:     def initialize
38:       @plugin_config = {}
39: 
40:       @deliverables = AStruct.new
41:       @supported_oses = AStruct.new
42:       @supported_platforms = []
43:       @target_deliverables = AStruct.new
44:       @dir = AStruct.new
45:     end

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 37
37:     def initialize
38:       @plugin_config = {}
39: 
40:       @deliverables = AStruct.new
41:       @supported_oses = AStruct.new
42:       @supported_platforms = []
43:       @target_deliverables = AStruct.new
44:       @dir = AStruct.new
45:     end

Public Instance methods

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 138
138:     def current_platform
139:       platform = :raw
140: 
141:       if @previous_plugin_info[:type] == :platform
142:         platform = @previous_plugin_info[:name]
143:       end unless @previous_plugin_info.nil?
144: 
145:       platform.to_s
146:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 138
138:     def current_platform
139:       platform = :raw
140: 
141:       if @previous_plugin_info[:type] == :platform
142:         platform = @previous_plugin_info[:name]
143:       end unless @previous_plugin_info.nil?
144: 
145:       platform.to_s
146:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 201
201:     def deliverables
202:       @target_deliverables
203:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 201
201:     def deliverables
202:       @target_deliverables
203:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 184
184:     def deliverables_exists?
185:       raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
186: 
187:       return false if deliverables.empty?
188: 
189:       exists = true
190: 
191:       deliverables.each_value do |file|
192:         unless File.exists?(file)
193:           exists = false
194:           break
195:         end
196:       end
197: 
198:       exists
199:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 184
184:     def deliverables_exists?
185:       raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
186: 
187:       return false if deliverables.empty?
188: 
189:       exists = true
190: 
191:       deliverables.each_value do |file|
192:         unless File.exists?(file)
193:           exists = false
194:           break
195:         end
196:       end
197: 
198:       exists
199:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 156
156:     def execute(args = nil)
157:       raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
158:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 156
156:     def execute(args = nil)
157:       raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
158:     end

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 47
47:     def init(config, appliance_config, info, options = {})
48:       @config = config
49:       @appliance_config = appliance_config
50:       @options = options
51:       @plugin_info = info
52: 
53:       # Optional options :)
54:       @type = options[:type] || @plugin_info[:name]
55:       @previous_plugin = options[:previous_plugin]
56:       @log = options[:log] || LogHelper.new
57:       @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
58:       @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)
59: 
60:       @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
61:       @dir.tmp = "#{@dir.base}/tmp"
62: 
63:       if @previous_plugin
64:         @previous_deliverables = @previous_plugin.deliverables
65:         @previous_plugin_info = @previous_plugin.plugin_info
66:       else
67:         @previous_deliverables = AStruct.new
68:       end
69: 
70:       # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
71:       read_plugin_config
72:       merge_plugin_config
73: 
74:       # Indicate whether deliverables of select plugin should be moved to final destination or not.
75:       # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
76:       @move_deliverables = true
77: 
78:       # The plugin is initialized now. We can do some fancy stuff with it.
79:       @initialized = true
80: 
81:       self
82:     end

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 47
47:     def init(config, appliance_config, info, options = {})
48:       @config = config
49:       @appliance_config = appliance_config
50:       @options = options
51:       @plugin_info = info
52: 
53:       # Optional options :)
54:       @type = options[:type] || @plugin_info[:name]
55:       @previous_plugin = options[:previous_plugin]
56:       @log = options[:log] || LogHelper.new
57:       @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
58:       @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)
59: 
60:       @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
61:       @dir.tmp = "#{@dir.base}/tmp"
62: 
63:       if @previous_plugin
64:         @previous_deliverables = @previous_plugin.deliverables
65:         @previous_plugin_info = @previous_plugin.plugin_info
66:       else
67:         @previous_deliverables = AStruct.new
68:       end
69: 
70:       # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
71:       read_plugin_config
72:       merge_plugin_config
73: 
74:       # Indicate whether deliverables of select plugin should be moved to final destination or not.
75:       # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
76:       @move_deliverables = true
77: 
78:       # The plugin is initialized now. We can do some fancy stuff with it.
79:       @initialized = true
80: 
81:       self
82:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 121
121:     def is_supported_os?
122:       return true if @supported_oses.empty?
123:       return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
124:       true
125:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 121
121:     def is_supported_os?
122:       return true if @supported_oses.empty?
123:       return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
124:       true
125:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 115
115:     def is_supported_platform?
116:       return true if @supported_platforms.empty?
117:       return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
118:       true
119:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 115
115:     def is_supported_platform?
116:       return true if @supported_platforms.empty?
117:       return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
118:       true
119:     end

This merges the plugin config with configuration provided in command line

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 220
220:     def merge_plugin_config
221:       config =
222:           case @plugin_info[:type]
223:             when :os
224:               @config.os_config
225:             when :platform
226:               @config.platform_config
227:             when :delivery
228:               @config.delivery_config
229:           end
230: 
231:       @plugin_config.merge!(config)
232:     end

This merges the plugin config with configuration provided in command line

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 220
220:     def merge_plugin_config
221:       config =
222:           case @plugin_info[:type]
223:             when :os
224:               @config.os_config
225:             when :platform
226:               @config.platform_config
227:             when :delivery
228:               @config.delivery_config
229:           end
230: 
231:       @plugin_config.merge!(config)
232:     end

This reads the plugin config from file

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 214
214:     def read_plugin_config
215:       return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
216:       @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
217:     end

This reads the plugin config from file

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 214
214:     def read_plugin_config
215:       return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
216:       @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
217:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 93
 93:     def register_deliverable(deliverable)
 94:       raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
 95:       raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)
 96: 
 97:       deliverable.each do |name, path|
 98:         @deliverables[name] = "#{@dir.tmp}/#{path}"
 99:         @target_deliverables[name] = "#{@dir.base}/#{path}"
100:       end
101:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 93
 93:     def register_deliverable(deliverable)
 94:       raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
 95:       raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)
 96: 
 97:       deliverable.each do |name, path|
 98:         @deliverables[name] = "#{@dir.tmp}/#{path}"
 99:         @target_deliverables[name] = "#{@dir.base}/#{path}"
100:       end
101:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 103
103:     def register_supported_os(name, versions)
104:       raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
105: 
106:       @supported_oses[name] = AStruct.new if @supported_oses[name].nil?
107:       @supported_oses[name] = versions
108:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 103
103:     def register_supported_os(name, versions)
104:       raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
105: 
106:       @supported_oses[name] = AStruct.new if @supported_oses[name].nil?
107:       @supported_oses[name] = versions
108:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 110
110:     def register_supported_platform(name)
111:       raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
112:       @supported_platforms << name
113:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 110
110:     def register_supported_platform(name)
111:       raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
112:       @supported_platforms << name
113:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 160
160:     def run(param = nil)
161:       unless is_supported_os?
162:         raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following operating systems: #{supported_oses}. Your appliance contains #{@appliance_config.os.name} #{@appliance_config.os.version} operating system which is not supported by this plugin, sorry."
163:       end
164: 
165:       unless is_supported_platform?
166:         raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following platforms: #{@supported_platforms.join(', ')}. You selected #{@previous_plugin_info[:name]} platform which is not supported by this plugin, sorry."
167:       end
168: 
169:       FileUtils.rm_rf @dir.tmp
170:       FileUtils.mkdir_p @dir.tmp
171: 
172:       param.nil? ? execute : execute(param)
173: 
174:       # TODO execute post commands for platform plugins here?
175: 
176:       @deliverables.each do |name, path|
177:         @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
178:         FileUtils.mv(path, @target_deliverables[name])
179:       end if @move_deliverables
180: 
181:       FileUtils.rm_rf @dir.tmp
182:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 160
160:     def run(param = nil)
161:       unless is_supported_os?
162:         raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following operating systems: #{supported_oses}. Your appliance contains #{@appliance_config.os.name} #{@appliance_config.os.version} operating system which is not supported by this plugin, sorry."
163:       end
164: 
165:       unless is_supported_platform?
166:         raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following platforms: #{@supported_platforms.join(', ')}. You selected #{@previous_plugin_info[:name]} platform which is not supported by this plugin, sorry."
167:       end
168: 
169:       FileUtils.rm_rf @dir.tmp
170:       FileUtils.mkdir_p @dir.tmp
171: 
172:       param.nil? ? execute : execute(param)
173: 
174:       # TODO execute post commands for platform plugins here?
175: 
176:       @deliverables.each do |name, path|
177:         @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
178:         FileUtils.mv(path, @target_deliverables[name])
179:       end if @move_deliverables
180: 
181:       FileUtils.rm_rf @dir.tmp
182:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 205
205:     def set_default_config_value(key, default_value=nil, &blk)
206:       if block_given? && !(@plugin_config[key].nil?)
207:         @plugin_config[key] = yield(key, default_value, @plugin_config[key])
208:       else
209:         @plugin_config[key] ||= default_value
210:       end
211:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 205
205:     def set_default_config_value(key, default_value=nil, &blk)
206:       if block_given? && !(@plugin_config[key].nil?)
207:         @plugin_config[key] = yield(key, default_value, @plugin_config[key])
208:       else
209:         @plugin_config[key] ||= default_value
210:       end
211:     end

Validation helper method.

Execute the validation only for selected plugin type. TODO make this prettier somehow? Maybe moving validation to a class?

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 88
88:     def subtype(type)
89:       return unless @type == type
90:       yield if block_given?
91:     end

Validation helper method.

Execute the validation only for selected plugin type. TODO make this prettier somehow? Maybe moving validation to a class?

[Source]

    # File lib/boxgrinder-build/plugins/base-plugin.rb, line 88
88:     def subtype(type)
89:       return unless @type == type
90:       yield if block_given?
91:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 127
127:     def supported_oses
128:       supported = ""
129: 
130:       @supported_oses.sort.each do |name, versions|
131:         supported << ", " unless supported.empty?
132:         supported << "#{name} (versions: #{versions.join(", ")})"
133:       end
134: 
135:       supported
136:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 127
127:     def supported_oses
128:       supported = ""
129: 
130:       @supported_oses.sort.each do |name, versions|
131:         supported << ", " unless supported.empty?
132:         supported << "#{name} (versions: #{versions.join(", ")})"
133:       end
134: 
135:       supported
136:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 148
148:     def validate_plugin_config(fields = [], doc = nil)
149:       more_info = doc.nil? ? '' : "See #{doc} for more info"
150: 
151:       fields.each do |field|
152:         raise PluginValidationError, "Please specify a valid '#{field}' key in BoxGrinder configuration file: '#{@config.file}' or use CLI '--#{@plugin_info[:type]}-config #{field}:DATA' argument. #{more_info}" if @plugin_config[field].nil?
153:       end
154:     end

[Source]

     # File lib/boxgrinder-build/plugins/base-plugin.rb, line 148
148:     def validate_plugin_config(fields = [], doc = nil)
149:       more_info = doc.nil? ? '' : "See #{doc} for more info"
150: 
151:       fields.each do |field|
152:         raise PluginValidationError, "Please specify a valid '#{field}' key in BoxGrinder configuration file: '#{@config.file}' or use CLI '--#{@plugin_info[:type]}-config #{field}:DATA' argument. #{more_info}" if @plugin_config[field].nil?
153:       end
154:     end

[Validate]