Class: BoxGrinder::BasePlugin

Inherits:
Object
  • Object
show all
Includes:
Plugins
Defined in:
lib/boxgrinder-build/plugins/base-plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plugins

#plugin

Constructor Details

#initializeBasePlugin

Returns a new instance of BasePlugin.



37
38
39
40
41
42
43
44
45
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 37

def initialize
  @plugin_config = {}

  @deliverables = AStruct.new
  @supported_oses = AStruct.new
  @supported_platforms = []
  @target_deliverables = AStruct.new
  @dir = AStruct.new
end

Instance Attribute Details

#deliverablesObject (readonly)

Returns the value of attribute deliverables.



35
36
37
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 35

def deliverables
  @deliverables
end

#plugin_infoObject (readonly)

Returns the value of attribute plugin_info.



34
35
36
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 34

def plugin_info
  @plugin_info
end

Instance Method Details

#current_platformObject



138
139
140
141
142
143
144
145
146
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 138

def current_platform
  platform = :raw

  if @previous_plugin_info[:type] == :platform
    platform = @previous_plugin_info[:name]
  end unless @previous_plugin_info.nil?

  platform.to_s
end

#deliverables_exists?Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 184

def deliverables_exists?
  raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?

  return false if deliverables.empty?

  exists = true

  deliverables.each_value do |file|
    unless File.exists?(file)
      exists = false
      break
    end
  end

  exists
end

#execute(args = nil) ⇒ Object



156
157
158
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 156

def execute(args = nil)
  raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
end

#init(config, appliance_config, info, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 47

def init(config, appliance_config, info, options = {})
  @config = config
  @appliance_config = appliance_config
  @options = options
  @plugin_info = info

  # Optional options :)
  @type = options[:type] || @plugin_info[:name]
  @previous_plugin = options[:previous_plugin]
  @log = options[:log] || LogHelper.new
  @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
  @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)

  @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
  @dir.tmp = "#{@dir.base}/tmp"

  if @previous_plugin
    @previous_deliverables = @previous_plugin.deliverables
    @previous_plugin_info = @previous_plugin.plugin_info
  else
    @previous_deliverables = AStruct.new
  end

  # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
  read_plugin_config
  merge_plugin_config

  # Indicate whether deliverables of select plugin should be moved to final destination or not.
  # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
  @move_deliverables = true

  # The plugin is initialized now. We can do some fancy stuff with it.
  @initialized = true

  self
end

#is_supported_os?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 121

def is_supported_os?
  return true if @supported_oses.empty?
  return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
  true
end

#is_supported_platform?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 115

def is_supported_platform?
  return true if @supported_platforms.empty?
  return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
  true
end

#merge_plugin_configObject

This merges the plugin config with configuration provided in command line



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 220

def merge_plugin_config
  config =
      case @plugin_info[:type]
        when :os
          @config.os_config
        when :platform
          @config.platform_config
        when :delivery
          @config.delivery_config
      end

  @plugin_config.merge!(config)
end

#read_plugin_configObject

This reads the plugin config from file



214
215
216
217
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 214

def read_plugin_config
  return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
  @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
end

#register_deliverable(deliverable) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 93

def register_deliverable(deliverable)
  raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
  raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)

  deliverable.each do |name, path|
    @deliverables[name] = "#{@dir.tmp}/#{path}"
    @target_deliverables[name] = "#{@dir.base}/#{path}"
  end
end

#register_supported_os(name, versions) ⇒ Object



103
104
105
106
107
108
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 103

def register_supported_os(name, versions)
  raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?

  @supported_oses[name] = AStruct.new if @supported_oses[name].nil?
  @supported_oses[name] = versions
end

#register_supported_platform(name) ⇒ Object



110
111
112
113
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 110

def register_supported_platform(name)
  raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
  @supported_platforms << name
end

#run(param = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 160

def run(param = nil)
  unless is_supported_os?
    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."
  end

  unless is_supported_platform?
    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."
  end

  FileUtils.rm_rf @dir.tmp
  FileUtils.mkdir_p @dir.tmp

  param.nil? ? execute : execute(param)

  # TODO execute post commands for platform plugins here?

  @deliverables.each do |name, path|
    @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
    FileUtils.mv(path, @target_deliverables[name])
  end if @move_deliverables

  FileUtils.rm_rf @dir.tmp
end

#set_default_config_value(key, default_value = nil, &blk) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 205

def set_default_config_value(key, default_value=nil, &blk)
  if block_given? && !(@plugin_config[key].nil?)
    @plugin_config[key] = yield(key, default_value, @plugin_config[key])
  else
    @plugin_config[key] ||= default_value
  end
end

#subtype(type) ⇒ Object

Validation helper method.

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



88
89
90
91
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 88

def subtype(type)
  return unless @type == type
  yield if block_given?
end

#supported_osesObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 127

def supported_oses
  supported = ""

  @supported_oses.sort.each do |name, versions|
    supported << ", " unless supported.empty?
    supported << "#{name} (versions: #{versions.join(", ")})"
  end

  supported
end

#validate_plugin_config(fields = [], doc = nil) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/boxgrinder-build/plugins/base-plugin.rb', line 148

def validate_plugin_config(fields = [], doc = nil)
  more_info = doc.nil? ? '' : "See #{doc} for more info"

  fields.each do |field|
    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?
  end
end