| Class | BoxGrinder::BasePlugin |
| In: |
lib/boxgrinder-build/plugins/base-plugin.rb
lib/boxgrinder-build/plugins/base-plugin.rb |
| Parent: | Object |
| deliverables | [R] | |
| deliverables | [R] | |
| plugin_info | [R] | |
| plugin_info | [R] |
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 35
35: def initialize
36: @plugin_config = {}
37:
38: @deliverables = OpenCascade.new
39: @supported_oses = OpenCascade.new
40: @supported_platforms = []
41: @target_deliverables = OpenCascade.new
42: @dir = OpenCascade.new
43: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 35
35: def initialize
36: @plugin_config = {}
37:
38: @deliverables = OpenCascade.new
39: @supported_oses = OpenCascade.new
40: @supported_platforms = []
41: @target_deliverables = OpenCascade.new
42: @dir = OpenCascade.new
43: end
Callback - executed after execution.
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 101
101: def after_execute
102: end
Callback - executed after execution.
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 101
101: def after_execute
102: end
Callback - executed after initialization.
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 97
97: def after_init
98: end
Callback - executed after initialization.
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 97
97: def after_init
98: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 158
158: def current_platform
159: platform = :raw
160:
161: if @previous_plugin_info[:type] == :platform
162: platform = @previous_plugin_info[:name]
163: end unless @previous_plugin_info.nil?
164:
165: platform.to_s
166: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 158
158: def current_platform
159: platform = :raw
160:
161: if @previous_plugin_info[:type] == :platform
162: platform = @previous_plugin_info[:name]
163: end unless @previous_plugin_info.nil?
164:
165: platform.to_s
166: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 223
223: def deliverables
224: @target_deliverables
225: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 223
223: def deliverables
224: @target_deliverables
225: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 206
206: def deliverables_exists?
207: raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
208:
209: return false if deliverables.empty?
210:
211: exists = true
212:
213: deliverables.each_value do |file|
214: unless File.exists?(file)
215: exists = false
216: break
217: end
218: end
219:
220: exists
221: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 206
206: def deliverables_exists?
207: raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
208:
209: return false if deliverables.empty?
210:
211: exists = true
212:
213: deliverables.each_value do |file|
214: unless File.exists?(file)
215: exists = false
216: break
217: end
218: end
219:
220: exists
221: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 176
176: def execute(args = nil)
177: raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
178: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 176
176: def execute(args = nil)
177: raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
178: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 45
45: def init(config, appliance_config, info, options = {})
46: @config = config
47: @appliance_config = appliance_config
48: @options = options
49: @plugin_info = info
50:
51: # Optional options :)
52: @type = options[:type] || @plugin_info[:name]
53: @previous_plugin = options[:previous_plugin]
54: @log = options[:log] || LogHelper.new
55: @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
56: @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)
57:
58: @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
59: @dir.tmp = "#{@dir.base}/tmp"
60:
61: if @previous_plugin
62: @previous_deliverables = @previous_plugin.deliverables
63: @previous_plugin_info = @previous_plugin.plugin_info
64: else
65: @previous_deliverables = OpenCascade.new
66: end
67:
68: # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
69: read_plugin_config
70: merge_plugin_config
71:
72: # Indicate whether deliverables of select plugin should be moved to final destination or not.
73: # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
74: @move_deliverables = true
75:
76: # Validate the plugin configuration.
77: # Please make the validate method as simple as possible, because it'll be executed also in unit tests.
78: validate
79:
80: # The plugin is initialized now. We can do some fancy stuff with it.
81: @initialized = true
82:
83: # If there is something defined in the plugin that should be executed after plugin initialization - it should go
84: # to after_init method.
85: after_init
86:
87: self
88: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 45
45: def init(config, appliance_config, info, options = {})
46: @config = config
47: @appliance_config = appliance_config
48: @options = options
49: @plugin_info = info
50:
51: # Optional options :)
52: @type = options[:type] || @plugin_info[:name]
53: @previous_plugin = options[:previous_plugin]
54: @log = options[:log] || LogHelper.new
55: @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
56: @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)
57:
58: @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
59: @dir.tmp = "#{@dir.base}/tmp"
60:
61: if @previous_plugin
62: @previous_deliverables = @previous_plugin.deliverables
63: @previous_plugin_info = @previous_plugin.plugin_info
64: else
65: @previous_deliverables = OpenCascade.new
66: end
67:
68: # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
69: read_plugin_config
70: merge_plugin_config
71:
72: # Indicate whether deliverables of select plugin should be moved to final destination or not.
73: # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
74: @move_deliverables = true
75:
76: # Validate the plugin configuration.
77: # Please make the validate method as simple as possible, because it'll be executed also in unit tests.
78: validate
79:
80: # The plugin is initialized now. We can do some fancy stuff with it.
81: @initialized = true
82:
83: # If there is something defined in the plugin that should be executed after plugin initialization - it should go
84: # to after_init method.
85: after_init
86:
87: self
88: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 141
141: def is_supported_os?
142: return true if @supported_oses.empty?
143: return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
144: true
145: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 141
141: def is_supported_os?
142: return true if @supported_oses.empty?
143: return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
144: true
145: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 135
135: def is_supported_platform?
136: return true if @supported_platforms.empty?
137: return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
138: true
139: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 135
135: def is_supported_platform?
136: return true if @supported_platforms.empty?
137: return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
138: true
139: end
This merges the plugin config with configuration provided in command line
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 238
238: def merge_plugin_config
239: config =
240: case @plugin_info[:type]
241: when :os
242: @config.os_config
243: when :platform
244: @config.platform_config
245: when :delivery
246: @config.delivery_config
247: end
248:
249: @plugin_config.merge!(config)
250: end
This merges the plugin config with configuration provided in command line
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 238
238: def merge_plugin_config
239: config =
240: case @plugin_info[:type]
241: when :os
242: @config.os_config
243: when :platform
244: @config.platform_config
245: when :delivery
246: @config.delivery_config
247: end
248:
249: @plugin_config.merge!(config)
250: end
This reads the plugin config from file
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 232
232: def read_plugin_config
233: return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
234: @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
235: end
This reads the plugin config from file
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 232
232: def read_plugin_config
233: return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
234: @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
235: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 113
113: def register_deliverable(deliverable)
114: raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
115: raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)
116:
117: deliverable.each do |name, path|
118: @deliverables[name] = "#{@dir.tmp}/#{path}"
119: @target_deliverables[name] = "#{@dir.base}/#{path}"
120: end
121: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 113
113: def register_deliverable(deliverable)
114: raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
115: raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)
116:
117: deliverable.each do |name, path|
118: @deliverables[name] = "#{@dir.tmp}/#{path}"
119: @target_deliverables[name] = "#{@dir.base}/#{path}"
120: end
121: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 123
123: def register_supported_os(name, versions)
124: raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
125:
126: @supported_oses[name] = OpenCascade.new if @supported_oses[name].nil?
127: @supported_oses[name] = versions
128: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 123
123: def register_supported_os(name, versions)
124: raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
125:
126: @supported_oses[name] = OpenCascade.new if @supported_oses[name].nil?
127: @supported_oses[name] = versions
128: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 130
130: def register_supported_platform(name)
131: raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
132: @supported_platforms << name
133: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 130
130: def register_supported_platform(name)
131: raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
132: @supported_platforms << name
133: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 180
180: def run(param = nil)
181: unless is_supported_os?
182: @log.error "#{@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."
183: return
184: end
185:
186: unless is_supported_platform?
187: @log.error "#{@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."
188: return
189: end
190:
191: FileUtils.rm_rf @dir.tmp
192: FileUtils.mkdir_p @dir.tmp
193:
194: param.nil? ? execute : execute(param)
195:
196: # TODO execute post commands for platform plugins here?
197:
198: @deliverables.each do |name, path|
199: @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
200: FileUtils.mv(path, @target_deliverables[name])
201: end if @move_deliverables
202:
203: FileUtils.rm_rf @dir.tmp
204: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 180
180: def run(param = nil)
181: unless is_supported_os?
182: @log.error "#{@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."
183: return
184: end
185:
186: unless is_supported_platform?
187: @log.error "#{@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."
188: return
189: end
190:
191: FileUtils.rm_rf @dir.tmp
192: FileUtils.mkdir_p @dir.tmp
193:
194: param.nil? ? execute : execute(param)
195:
196: # TODO execute post commands for platform plugins here?
197:
198: @deliverables.each do |name, path|
199: @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
200: FileUtils.mv(path, @target_deliverables[name])
201: end if @move_deliverables
202:
203: FileUtils.rm_rf @dir.tmp
204: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 227
227: def set_default_config_value(key, value)
228: @plugin_config[key] = @plugin_config[key].nil? ? value : @plugin_config[key]
229: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 227
227: def set_default_config_value(key, value)
228: @plugin_config[key] = @plugin_config[key].nil? ? value : @plugin_config[key]
229: end
Validation helper method.
Execute the validation only for selected plugin type. TODO make this prettier somehow? Maybe moving validation to a class?
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 108
108: def subtype(type)
109: return unless @type == type
110: yield if block_given?
111: end
Validation helper method.
Execute the validation only for selected plugin type. TODO make this prettier somehow? Maybe moving validation to a class?
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 108
108: def subtype(type)
109: return unless @type == type
110: yield if block_given?
111: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 147
147: def supported_oses
148: supported = ""
149:
150: @supported_oses.sort.each do |name, versions|
151: supported << ", " unless supported.empty?
152: supported << "#{name} (versions: #{versions.join(", ")})"
153: end
154:
155: supported
156: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 147
147: def supported_oses
148: supported = ""
149:
150: @supported_oses.sort.each do |name, versions|
151: supported << ", " unless supported.empty?
152: supported << "#{name} (versions: #{versions.join(", ")})"
153: end
154:
155: supported
156: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 168
168: def validate_plugin_config(fields = [], doc = nil)
169: more_info = doc.nil? ? '' : "See #{doc} for more info"
170:
171: fields.each do |field|
172: 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?
173: end
174: end
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 168
168: def validate_plugin_config(fields = [], doc = nil)
169: more_info = doc.nil? ? '' : "See #{doc} for more info"
170:
171: fields.each do |field|
172: 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?
173: end
174: end