Class: Killbill::PluginHelper

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/killbill/rake_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_name, plugin_name, gem_name, gemfile_name, gemfile_lock_name, verbose) ⇒ PluginHelper

Returns a new instance of PluginHelper.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/killbill/rake_task.rb', line 25

def initialize(base_name, plugin_name, gem_name, gemfile_name, gemfile_lock_name, verbose)
  @verbose = verbose

  @logger = Logger.new(STDOUT)
  @logger.formatter = proc do |severity, datetime, _, msg|
    date_format = datetime.strftime('%Y-%m-%d %H:%M:%S.%L')
    if severity == "INFO" || severity == "WARN"
      "KillBill [#{date_format}] #{severity}  : #{msg}\n"
    else
      "KillBill [#{date_format}] #{severity} : #{msg}\n"
    end
  end
  @logger.level = @verbose ? Logger::DEBUG : Logger::INFO

  @base_name = base_name
  @plugin_name = plugin_name
  @gem_name = gem_name
  @gemfile_name = gemfile_name
  @gemfile_lock_name = gemfile_lock_name

  # Plugin base directory
  @base = Pathname.new(@base_name).expand_path

  # Find the gemspec to determine name and version
  @plugin_gemspec = load_plugin_gemspec

  @package_dir = Pathname.new('pkg').expand_path
  FileUtils.mkdir_p @package_dir
  # Temporary build directory
  # will hard link all files from @package_tmp_dir to pkg to avoid tar'ing
  # up symbolic links (similar to how Rake::PackageTask does prepare files)
  @package_tmp_dir = Pathname.new(File.join('tmp', name)).expand_path

  @root_dir_path = 'ROOT' # plugin's ROOT directory
  @gems_dir_path = File.join(@root_dir_path, 'gems')
  # Staging area to install the killbill.properties and config.ru files
  @plugin_target_dir = @package_tmp_dir.join("#{version}").expand_path
  @plugin_root_target_dir = @plugin_target_dir.join(@root_dir_path)

  # Staging area to install gem dependencies
  # Note the Killbill friendly structure (which we will keep in the tarball)
  @plugin_gem_target_dir = @plugin_target_dir.join(@gems_dir_path)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



69
70
71
# File 'lib/killbill/rake_task.rb', line 69

def base
  @base
end

Class Method Details

.install_tasks(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/killbill/rake_task.rb', line 13

def install_tasks(opts = {})
  gemfile_name = ENV['BUNDLE_GEMFILE'] || 'Gemfile'
  new(opts[:base_name] || Dir.pwd,          # Path to the plugin root directory (where the gempec and/or Gemfile should be)
      opts[:plugin_name],                   # Plugin name, e.g. 'klogger'
      opts[:gem_name],                      # Gem file name, e.g. 'klogger-1.0.0.gem'
      opts[:gemfile_name] || gemfile_name,  # Gemfile name
      opts[:gemfile_lock_name] || "#{gemfile_name}.lock",
      opts.key?(:verbose) ? opts[:verbose] : ENV['VERBOSE'] == 'true')
  .install
end

Instance Method Details

#installObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/killbill/rake_task.rb', line 115

def install
  namespace :killbill do
    desc "Validate plugin tree"
    # The killbill.properties file is required, but not the config.ru one
    task :validate, [:verbose] => killbill_properties_file do |t, args|
      set_verbosity(args)
      validate
    end

    desc "Build all package files for #{name} plugin #{version}"
    task :package, [:verbose] => :stage # builds .tar.gz & .zip packages

    package_name = "#{name}-#{version}"
    package_dir = @package_dir.realpath # pkg
    package_dir_path = File.join(package_dir, package_name) # pkg/killbill-xxx-0.1.2

    task :package => [ tar_gz_file = File.join(package_dir, "#{package_name}.tar.gz") ]
    file tar_gz_file => [ package_dir_path, 'package:files' ] do
      chdir(package_dir) do
        tar_command = 'tar'
        sh tar_command, "zcvf", tar_gz_file, package_name
      end
    end

    task :package => [ zip_file = File.join(package_dir, "#{package_name}.zip") ]
    file zip_file => [ package_dir_path, 'package:files' ] do
      chdir(package_dir) do
        zip_command = 'zip'
        sh zip_command, "-r", zip_file, package_name
      end
    end

    directory package_dir_path
    task 'package:files' do
      # move files from tmp directory to pkg (based on how rake does)
      basename = Regexp.escape(@package_tmp_dir.basename.to_s)
      tmp_path = @package_tmp_dir.realpath.to_s
      realpath_tmp_prefix = tmp_path.sub(/\/#{basename}$/, '')
      package_files = Rake::FileList.new("#{tmp_path}/**/*")

      package_files.each do |fn|
        f = File.join(package_dir_path, fn.sub(realpath_tmp_prefix, ''))
        fdir = File.dirname(f)
        mkdir_p(fdir) unless File.exist?(fdir)
        if File.directory?(fn)
          mkdir_p(f)
        else
          rm_f f
          safe_ln(fn, f)
        end
      end
    end

    desc "Force a rebuild of package files for #{name} plugin #{version}"
    task :repackage => [:clobber_package, :package]

    task :clobber_tmp do
      rm_r @package_tmp_dir rescue nil
    end

    desc "Remove package files from #{@package_dir}"
    task :clobber_package do
      rm_f tar_gz_file if File.exist?(tar_gz_file)
      rm_f zip_file if File.exist?(zip_file)
      rm_r package_dir_path rescue nil
    end
    task :clobber => [:clobber_tmp, :clobber_package]

    task 'stage:init' do
      # NOOP task for plugins to hook up if they need some sort of initialization
      #  (task will be run in the context of the Killbill::PluginHelper instance)
      # NOTE: no need for post (stage:done) hook since it's easy using Rake :
      #  Rake::Task["killbill:package"].enhance { ... }
    end

    desc "Stage dependencies for #{name} plugin #{version}"
    task :stage, [:verbose] => [ :validate, 'stage:init' ] do |t, args|
      set_verbosity(args)

      mkdir_p @plugin_target_dir.to_s, :verbose => @verbose

      stage_dependencies
      stage_extra_files
    end

    desc "Deploy #{name} plugin #{version} to KillBill server"
    task :deploy, [:force, :plugin_dir, :verbose] => :stage do |t, args|
      plugins_dir = prepare_deploy(t, args)

      cp_r @package_tmp_dir, plugins_dir, :verbose => @verbose

      deploy_config_files plugin_path(plugins_dir) # .../[name]/[version]
    end

    desc "Deploy plugin in development mode (without staging)"
    task 'deploy:dev', [:force, :plugin_dir, :verbose] => :validate do |t, args|
      raise 'development deployment only works with Bundler' unless bundler?

      plugins_dir = prepare_deploy(t, args)

      # prepare "temporary" deployment at tmp/deploy:dev
      package_tmp_dir = Pathname.new(File.join('tmp', 'deploy:dev')).expand_path
      rm_r package_tmp_dir if File.exist?(package_tmp_dir)
      mkdir_p package_tmp_dir.to_s, :verbose => @verbose

      mkdir target_dir = package_tmp_dir.join(version.to_s)

      # NOTE: although same _boot.rb_ as with regular deploys might not work
      stage_extra_files target_dir
      if boot_rb_file.nil?
        generate_dev_boot_rb target_dir
      else
        @logger.info "Make sure the suplied #{boot_rb_file} is removed/adjusted before doing a regular killbill:deploy (same boot.rb won't likely work)"
      end

      # here we assume Gemfile declares gemspec and we link ROOT to base :
      ln_s @base, target_dir.join(@root_dir_path), :verbose => @verbose

      # ln -s /var/tmp/bundles/plugins/ruby/killbill-xxx -> tmp/deploy:dev
      ln_s package_tmp_dir, plugins_dir.join(name), :verbose => @verbose

      deploy_config_files target_dir
    end

    desc "List all dependencies"
    task :dependencies => :validate do
      print_dependencies
    end
    task :dependency => :dependencies

    desc "Delete #{@package_dir}"
    task :clean => :clobber do
      rm_r @package_dir if File.exist?(@package_dir)
    end

    namespace :db do
      desc 'Display the current migration version'
      task :current_version do
        puts migration.current_version
      end

      desc 'Display the migration SQL'
      task :sql_for_migration do
        puts migration.sql_for_migration.join("\n")
      end

      desc 'Run all migrations'
      task :migrate do
        migration.migrate
      end

      desc 'Dump the current schema structure (Ruby)'
      task :ruby_dump do
        puts migration.ruby_dump.string
      end

      desc 'Dump the current schema structure (SQL)'
      task :sql_dump do
        puts migration.sql_dump.string
      end
    end
  end
end

#nameObject



71
72
73
# File 'lib/killbill/rake_task.rb', line 71

def name
  @plugin_gemspec.name
end

#specsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/killbill/rake_task.rb', line 79

def specs
  # Rely on the Gemfile definition, if it exists, to get all dependencies
  # (we assume the Gemfile includes the plugin gemspec, as it should).
  # Otherwise, use recursively retrieve the plugin gemspec' runtime dependencies
  # ... resolves all gems as they are currently installed with RubyGems
  @specs ||=
    if @gemfile_definition
      # don't include the :development group
      @gemfile_definition.specs_for([:default])
    else
      get_dependencies = lambda do |spec|
        deps = ( spec.runtime_dependencies || [] ).map(&:to_spec)
        deps.dup.each { |spec| deps += get_dependencies.call(spec) }
        deps.uniq
      end
      all_dependencies = get_dependencies.call(@plugin_gemspec)
      all_dependencies.dup.each do |spec|
        next unless all_dependencies.include?(spec) # removed previously

        # duplicate gemspec of same name might get included since we did
        # not really do through the gem activation hustle, lowest version
        # should win since those tend to be matched by strict requirements
        if other_spec = all_dependencies.find { |s| s.name == spec.name && s != spec }
          if other_spec.version > spec.version
            @logger.debug "Discarding matched gem '#{spec.name}' version #{other_spec.version} in favor of #{spec.version}"
            all_dependencies.delete(other_spec)
          else # other_spec.version < spec.version
            @logger.debug "Discarding matched gem '#{spec.name}' version #{spec.version} in favor of #{other_spec.version}"
            all_dependencies.delete(spec)
          end
        end
      end
      [ @plugin_gemspec ] + all_dependencies
    end
end

#versionObject



75
76
77
# File 'lib/killbill/rake_task.rb', line 75

def version
  @plugin_gemspec.version
end