Module: Harker

Defined in:
lib/harker.rb,
lib/harker/gemify.rb

Overview

Harker lets you deploy Rails apps via RubyGems.

These commands get invoked by the bin wrapper of the rails app gem rather than by Harker itself.

Constant Summary collapse

VERSION =
'0.5.3'
ACTIONS =
%w(start stop restart init migrate console)
GEM_ROOT =
Gem.loaded_specs[File.basename($0)].full_gem_path rescue '.'

Class Method Summary collapse

Class Method Details

.configure(config) ⇒ Object



107
108
109
110
111
# File 'lib/harker.rb', line 107

def configure(config)
  config.database_configuration_file = File.join(@root, 'database.yml')
  config.log_path = File.join(@root, 'log', "#{RAILS_ENV}.log")
  config.tmp_dir = File.join(@root, 'tmp')
end

.consoleObject



102
103
104
105
# File 'lib/harker.rb', line 102

def console
  require 'irb'
  IRB.start
end

.gemify(rails_root) ⇒ Object

Turn an existing Rails app into a gem.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/harker/gemify.rb', line 7

def self.gemify(rails_root)
  @project_name = File.basename(rails_root)

  if File.exist?(rails_root + "/bin/#{@project_name}") or
      File.exist?(rails_root + "/lib/#{@project_name}.rb")
    abort "Can't write gem files without overwriting existing ones.
Try manually gemifying."
  end

  File.open(File.join(rails_root, '/Rakefile'), 'a') do |fp|
    fp.puts template('hoe')
    puts "Added hoe block to Rakefile."
  end

  FileUtils.mkdir_p(File.join(rails_root, '/bin'))
  File.open(File.join(rails_root, "/bin/#{@project_name}"), 'w') do |fp|
    fp.puts template('bin')
    puts "Wrote bin launcher."
  end

  File.open(File.join(rails_root, "/lib/#{@project_name}.rb"), 'w') do |fp|
    fp.puts template('lib')
    puts "Wrote lib file."
  end

  # Submitted a patch to hoe to make it ignore log files by default,
  # but folks should still give it a once-over manually anyway.
  system "cd #{rails_root}; touch Manifest.txt; rake check_manifest | patch"
  puts "Wrote Manifest.txt."
  puts "Ensure the manifest doesn't contain files you don't want in the gem."
  puts "Then try running rake install_gem."
end

.initObject

Initialize a new instance of your application



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/harker.rb', line 66

def init
  %w(tmp log db extensions).each do |d|
    FileUtils.mkdir_p(File.join(@root, d))
  end

  base_db_file = File.join(GEM_ROOT, 'config', 'database.yml')

  File.open("#{@root}/database.yml", 'w') do |fp|
    # Need to make sure any sqlite3 DB paths are absolute.
    db_config = YAML.load_file(base_db_file)
    db_config.each do |env, hash|
      if hash['adapter'] =~ /sqlite3?/
        hash['database'] = File.join(@root, hash['database'])
      end
    end

    fp.puts(db_config.to_yaml)
  end

  puts <<-END
  Initialized #{@name} instance in #{@root}...
  
  Configure your database by editing #{@root}/database.yml.
  Optionally configure your web server via rack in #{@root}/config.ru.
  
  Migrate your DB with: #{@name} migrate
  Then launch with: #{@name} start --daemon
  END
end

.launch(name, args) ⇒ Object

Dispatch based on user’s command



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/harker.rb', line 16

def launch(name, args)
  action = args.shift

  unless ACTIONS.include?(action)
    abort "Usage: #{name} (#{ACTIONS.join('|')}) [INSTANCE_DIR] [START_ARGS]
The start command takes the same arguments as script/server."
  end

  # We need to get rid of the first arg if it's the optional
  # instance directory so script/server doesn't get confused.
  @root = if File.directory? args.first.to_s or action == 'init'
            File.expand_path args.shift
          else
            Dir.pwd
          end

  @name = name

  unless action == 'init'
    # 2.3.2 doesn't support tmp_dir config option; needs a monkeypatch.
    require 'harker/rails_configuration'
    require @name
    Dir["#{@root}/extensions/*rb"].each { |f| load f }
  end

  self.send(action)
end

.migrateObject



96
97
98
99
100
# File 'lib/harker.rb', line 96

def migrate
  puts "Migrating the #{RAILS_ENV} environment of #{@name}..."
  ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'),
                                 ENV["VERSION"] && ENV["VERSION"].to_i)
end

.pidfileObject



113
114
115
# File 'lib/harker.rb', line 113

def pidfile
  File.join(@root, 'tmp', 'pids', 'server.pid')
end

.restartObject



59
60
61
62
63
# File 'lib/harker.rb', line 59

def restart
  stop
rescue SystemExit
  start
end

.startObject

Start and optionally daemonize the application server



45
46
47
48
49
50
51
52
# File 'lib/harker.rb', line 45

def start
  # http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2350
  # Submitted a patch to Rails, but this lets it work with 2.3.2
  Rails::Rack::LogTailer::EnvironmentLog.replace(Rails.configuration.log_path)

  abort "Can't start; pidfile exists at #{pidfile}." if File.exist? pidfile
  require 'harker/server'
end

.stopObject



54
55
56
57
# File 'lib/harker.rb', line 54

def stop
  abort "No pid at #{pidfile}." unless File.exist?(pidfile)
  Process.kill('TERM', File.read(pidfile).to_i)
end

.template(name) ⇒ Object



40
41
42
43
# File 'lib/harker/gemify.rb', line 40

def self.template(name)
  template_path = File.join File.dirname(__FILE__), 'templates', "#{name}.erb"
  ERB.new(File.read(template_path)).result(binding)
end