Class: Patriot::Controller::PackageController

Inherits:
Object
  • Object
show all
Includes:
Util::Config, Util::Logger
Defined in:
lib/patriot/controller/package_controller.rb

Overview

Controller class for remote management of workers

Constant Summary

Constants included from Util::Config

Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY

Instance Method Summary collapse

Methods included from Util::Logger

#create_logger

Methods included from Util::Config

#load_config, #load_plugins

Constructor Details

#initialize(config) ⇒ PackageController

constructor

Parameters:



17
18
19
20
21
22
# File 'lib/patriot/controller/package_controller.rb', line 17

def initialize(config)
  @config = config
  @logger = create_logger(config)
  @plugin_dir = config.get(Patriot::Util::Config::PLUGIN_DIR_KEY, Patriot::Util::Config::DEFAULT_PLUGIN_DIR)
  @plugin_dir = File.expand_path(@plugin_dir, $home)
end

Instance Method Details

#install_plugin(plugin, opts = {}) ⇒ Object

install plugin to plugin directory

Parameters:

  • plugin (String)

    name of the plugin

  • opts (defaults to: {})
  • [Boolean] (Hash)

    a customizable set of options



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
# File 'lib/patriot/controller/package_controller.rb', line 50

def install_plugin(plugin, opts = {})
  @logger.info "install #{plugin}"
  dependency = Gem::Dependency.new plugin, opts[:version]
  path = dependency.name =~ /\.gem$/i ? dependency.name : Gem::RemoteFetcher.fetcher.download_to_cache(dependency) 
  raise "Gem '#{plugin}' not fetchable."  unless path
  basename = File.basename path, '.gem'
  # remvoe version
  basename = basename.gsub(/-[\d]+\.[\d]+\.[\d]+$/, "")
  target_dir = File.join(@plugin_dir, basename)
  if opts[:force] == true
    @logger.info "remove old #{target_dir}"
    FileUtils.rm_r target_dir if File.exist?(target_dir)
  else
    raise "#{target_dir} alrady exist" if File.exist?(target_dir)
  end
  FileUtils.mkdir_p target_dir
  if opts[:unpack]
    installer = Gem::Installer.new(path, :unpack=>true)
    installer.unpack target_dir
  else
    installer = Gem::DependencyInstaller.new
    installer.install path
    installed_dir = Gem::Installer.new(path).dir
    init_rb = File.join(installed_dir, "init.rb")
    FileUtils.cp(init_rb, target_dir)
  end
  @logger.info "#{path} installed: #{target_dir}'"
end

#upgrade(pkg = 'patriot-workflow-scheduler') ⇒ Object

upgrade deployment



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/patriot/controller/package_controller.rb', line 25

def upgrade(pkg = 'patriot-workflow-scheduler')
  # upgrade plugins
  plugins = @config.get(Patriot::Util::Config::PLUGIN_KEY, [])
  plugins = [plugins] unless plugins.is_a?(Array)
  plugins.each{|plugin| install_plugin(plugin, {:force => true})}

  # upgrade core package
  dependency = Gem::Dependency.new(pkg || 'patriot-workflow-scheduler')
  path = dependency.name =~ /\.gem$/i ? dependency.name : Gem::RemoteFetcher.fetcher.download_to_cache(dependency) 
  installed_dir = Gem::Installer.new(path).dir
  installer = Gem::DependencyInstaller.new
  @logger.info "upgrade to #{dependency}"
  installer.install path

  public_dir = File.join(installed_dir, "skel", "public")
  @logger.info "copy #{public_dir} to  #{$home}"
  FileUtils.cp_r(public_dir, $home)
  FileUtils.cp(File.join(installed_dir, 'bin', 'patriot'), File.join($home, 'bin', 'patriot'))

end