Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Starting the application server via Harker works.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Mar 26, 2009
1 parent 7b02cbe commit 48338a5
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 68 deletions.
3 changes: 2 additions & 1 deletion README.rdoc
Expand Up @@ -9,7 +9,8 @@ Rails apps via RubyGems.

== Usage

[Coming soon]
Your Rails app will need to be modified a bit to become a gem and work
with Harker. See test/sample for an example.

== Requirements

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Expand Up @@ -8,6 +8,8 @@ Hoe.new('harker', Harker::VERSION) do |p|
p.readme_file = 'README.rdoc'
p.summary = "Concourse is a tool to make planning gatherings easy."
p.extra_deps << ["rails", "~> 2.3.2"]
p.extra_dev_deps << ["minitest", "~> 1.3.1"]
p.extra_dev_deps << ["sqlite3-ruby", "~> 1.2.4"]
p.developer('Phil Hagelberg', 'technomancy@gmail.com')
end

Expand Down
75 changes: 48 additions & 27 deletions lib/harker.rb
@@ -1,40 +1,47 @@
require 'fileutils'

# Harker lets you deploy Rails apps via RubyGems
# Harker lets you deploy Rails apps via RubyGems.
module Harker
VERSION = '0.0.1'
ACTIONS = %w(start stop restart init migrate console)
VERSION = '0.0.2'
ACTIONS = %w(start stop restart init migrate console foreground)

module_function

# Dispatch based on user's command
def launch(name, args)
@root = args.shift || Dir.pwd
@root = File.expand_path(args.shift || Dir.pwd)
@name = name

action = args.shift

unless ACTIONS.include?(action)
abort("Usage: #{@name} INSTANCE_DIR (#{ACTIONS.join('|')})")
end

load_app
load_app unless action == 'init'
self.send(action)
end

# Start the server
def start
# TODO: this needs to be way more configurable
Rack::Handler::Mongrel.run(ActionController::Dispatcher.new,
:Port => 9292, :Host => "0.0.0.0", :AccessLog => [])
# Start the application server in the foreground.
def foreground
start(false)
end

# Start and daemonize the application server
def start(daemonize = true)
# can has internal consistency plz, Rails?
Rails::Rack::LogTailer::EnvironmentLog.replace(Rails.configuration.log_path)

ARGV.replace ["--config=#{@root}/config.ru"]
ARGV.push('--daemon') if daemonize
require 'commands/server'
end

# Stop the server
def stop
raise "Not implemented yet; sorry!" # TODO
# TODO: this depends on sane (non-shared) tmpdir behaviour
raise "Can't stop the funk! (Not implemented yet; sorry!)"
end

# Stop and start the application server
def restart
stop
start
Expand All @@ -45,34 +52,48 @@ def init
FileUtils.mkdir_p(@root)
FileUtils.mkdir_p(@root + '/log')
FileUtils.mkdir_p(@root + '/tmp')
FileUtils.cp("#{RAILS_ROOT}/config/database.yml", "#{@root}/database.yml")

# TODO: use the in-gem copy of database.yml as a base for this
File.open("#{@root}/database.yml", 'w') do |fp|
# TODO: be smart about environments; bleh
fp.puts({ 'production' => { 'adapter' => 'sqlite3',
'database' => @root + '/db.sqlite3',
'pool' => 5, 'timeout' => 5000 }}.to_yaml)
end

# TODO: write a default config.ru?

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

# Perform all pending migrations for this instance
def migrate
ActiveRecord::Migrator.migrate(RAILS_ROOT + '/db',
ActiveRecord::Migrator.migrate(RAILS_ROOT + '/db/migrate',
ENV["VERSION"] && ENV["VERSION"].to_i)
end

# Run script/console
def console
require 'commands/console'
require 'irb'
IRB.start
end

def configure(config)
# config.instance_eval { @root_path = root }
config.database_configuration_file = File.join(@root, 'database.yml')
config.log_path = File.join(@root, 'log', "#{environment}.log")
config.log_path = [:file_store,
File.join(@root, 'tmp', 'cache', "#{environment}.log")]
# TODO: pids? sessions? sockets?
config.log_path = File.join(@root, 'log', "#{RAILS_ENV}.log")
config.cache_store = [:file_store, File.join(@root, 'tmp', 'cache')]
# TODO: tmp dir? multiple instances will break if they share a tmp
# This may require fixing Rails. =\

# Currently you need to mkdir RAILS_ROOT/tmp and chmod/chown it
# after installing your app. This sucks!
end

# :nodoc:
def load_app
require name
require @name
end
end
17 changes: 9 additions & 8 deletions test/sample/Manifest.txt
Expand Up @@ -2,10 +2,10 @@ Manifest.txt
README.rdoc
Rakefile
app/controllers/application_controller.rb
app/controllers/harkers_controller.rb
app/controllers/harks_controller.rb
app/helpers/application_helper.rb
app/helpers/harkers_helper.rb
app/models/harker.rb
app/helpers/harks_helper.rb
app/models/hark.rb
bin/sample_rails
config/boot.rb
config/database.yml
Expand All @@ -20,7 +20,7 @@ config/initializers/new_rails_defaults.rb
config/initializers/session_store.rb
config/locales/en.yml
config/routes.rb
db/migrate/20090325225423_create_harkers.rb
db/migrate/20090326050232_create_harks.rb
lib/sample_rails.rb
public/404.html
public/422.html
Expand All @@ -44,9 +44,10 @@ script/performance/profiler
script/plugin
script/runner
script/server
test/fixtures/harkers.yml
test/functional/harkers_controller_test.rb
test/fixtures/harks.yml
test/functional/harks_controller_test.rb
test/performance/browsing_test.rb
test/test_helper.rb
test/unit/harker_test.rb
test/unit/helpers/harkers_helper_test.rb
test/unit/hark_test.rb
test/unit/helpers/harks_helper_test.rb
tmp
2 changes: 0 additions & 2 deletions test/sample/app/controllers/harkers_controller.rb

This file was deleted.

2 changes: 2 additions & 0 deletions test/sample/app/controllers/harks_controller.rb
@@ -0,0 +1,2 @@
class HarksController < ApplicationController
end
2 changes: 0 additions & 2 deletions test/sample/app/helpers/harkers_helper.rb

This file was deleted.

2 changes: 2 additions & 0 deletions test/sample/app/helpers/harks_helper.rb
@@ -0,0 +1,2 @@
module HarksHelper
end
2 changes: 2 additions & 0 deletions test/sample/app/models/hark.rb
@@ -0,0 +1,2 @@
class Hark < ActiveRecord::Base
end
2 changes: 0 additions & 2 deletions test/sample/app/models/harker.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/sample/config/routes.rb
@@ -1,5 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.resources :harkers
map.resources :harks

# The priority is based upon order of creation: first created -> highest priority.

Expand Down
13 changes: 0 additions & 13 deletions test/sample/db/migrate/20090325225423_create_harkers.rb

This file was deleted.

12 changes: 12 additions & 0 deletions test/sample/db/migrate/20090326050232_create_harks.rb
@@ -0,0 +1,12 @@
class CreateHarks < ActiveRecord::Migration
def self.up
create_table :harks do |t|
t.string :tidings
t.timestamps
end
end

def self.down
drop_table :harks
end
end
2 changes: 1 addition & 1 deletion test/sample/lib/sample_rails.rb
@@ -1,2 +1,2 @@
# Allow rubygems to load this app
require File.dirname(__FILE__) + '/../config/boot'
require File.dirname(__FILE__) + '/../config/environment'
File renamed without changes.
@@ -1,6 +1,6 @@
require 'test_helper'

class HarkersControllerTest < ActionController::TestCase
class HarksControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
Expand Down
@@ -1,6 +1,6 @@
require 'test_helper'

class HarkerTest < ActiveSupport::TestCase
class HarkTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
Expand Down
4 changes: 0 additions & 4 deletions test/sample/test/unit/helpers/harkers_helper_test.rb

This file was deleted.

4 changes: 4 additions & 0 deletions test/sample/test/unit/helpers/harks_helper_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class HarksHelperTest < ActionView::TestCase
end
1 change: 1 addition & 0 deletions test/sample/tmp
56 changes: 51 additions & 5 deletions test/test_harker.rb
@@ -1,8 +1,54 @@
require "test/unit"
require "harker"
require "rubygems"
require "minitest/unit"
require 'fileutils'

class TestHarker < Test::Unit::TestCase
def test_sanity
flunk "write tests or I will kneecap you"
require File.dirname(__FILE__) + "/../lib/harker"

begin
ENV['RAILS_ENV'] = 'production'
gem 'sample_rails'
rescue LoadError
abort "You need the sample_rails gem installed:
$ cd #{File.dirname(__FILE__) + '/sample'} && rake install_gem"
end

class TestHarker < MiniTest::Unit::TestCase
ROOT = "/tmp/harker-test-#{Process.pid}"

def setup
suppress_out { Harker.launch('sample_rails', [ROOT, 'init']) }
end

def teardown
FileUtils.rm_rf ROOT
end

def test_init
assert File.exist?(ROOT)
assert File.exist?(ROOT + '/database.yml')
end

# def test_start
# flunk "not yet"
# end

# def test_stop
# flunk "can't stop the funk"
# end

def test_migrate
suppress_out { Harker.launch('sample_rails', [ROOT, 'migrate']) }
assert_equal(['id', 'tidings', 'updated_at', 'created_at'].sort,
Hark.columns.map{ |c| c.name }.sort)
end

private
def suppress_out
$stdout = StringIO.new
yield
ensure
$stdout = STDOUT
end
end

MiniTest::Unit.autorun

0 comments on commit 48338a5

Please sign in to comment.