yhara-ruby-station 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ PROJECT_NAME = "ruby-station"
2
+
3
+ begin
4
+ require 'jeweler'
5
+ rescue LoadError
6
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
7
+ end
8
+
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "#{PROJECT_NAME}"
11
+ gemspec.summary = "Create, Distribute, and Install Ruby applications easily"
12
+ gemspec.email = "yutaka.hara/at/gmail.com"
13
+ gemspec.homepage = "http://github.com/yhara/#{PROJECT_NAME}"
14
+ gemspec.description = gemspec.summary
15
+ gemspec.authors = ["Yutaka HARA"]
16
+ # gemspec.add_dependency('ramaze', '= 2009.06.12')
17
+ # gemspec.add_dependency('dm-core')
18
+ # gemspec.add_dependency('do_sqlite3')
19
+ end
20
+
21
+ desc "install current source as gem"
22
+ task :dogfood => [:gemspec, :build] do
23
+ sh "sudo gem install pkg/#{PROJECT_NAME}-#{File.read("VERSION").chomp}.gem"
24
+ end
25
+
26
+ desc "uninstall the installed gem"
27
+ task :undogfood do
28
+ sh "sudo gem uninstall #{PROJECT_NAME}"
29
+ end
30
+
31
+ desc "uninstall, then install current source as gem"
32
+ task :redogfood => [:undogfood, :dogfood]
33
+
34
+ desc "uninstall temporary gem and install from github"
35
+ task :nodogfood do
36
+ sh "sudo gem uninstall #{PROJECT_NAME}"
37
+ sh "sudo gem install yhara-#{PROJECT_NAME}"
38
+ end
39
+
40
+ desc "check for gem to be built"
41
+ task :stalk do
42
+ sh "gemstalk yhara #{PROJECT_NAME}"
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/ruby-station ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require 'optparse'
4
+ require 'rubygems'
5
+ require 'ramaze'
6
+
7
+ $LOAD_PATH.unshift __DIR__("../")
8
+
9
+ require 'util/gem-manager.rb'
10
+ require 'config.rb'
11
+ Conf.parse_opt
12
+ Conf.init
13
+
14
+ require 'controller/init'
15
+ require 'model/init'
16
+
17
+ Ramaze.start :port => Conf.server_port, :root => __DIR__("../")
data/config.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+
4
+ #Encoding.default_external = 'UTF-8'
5
+
6
+ module RubyStation
7
+ VERSION = File.read(File.expand_path("./VERSION", File.dirname(__FILE__))).chomp
8
+ end
9
+
10
+ class Conf
11
+ %w(ruby_command gem_command gem_dir gem_bin_dir gem_install_option data_dir server_port).each do |m|
12
+ module_eval <<-EOD
13
+ def self.#{m}; @yaml[:#{m}]; end
14
+ EOD
15
+ end
16
+
17
+ def self.parse_opt
18
+ @home = File.expand_path("~/.ruby-station")
19
+
20
+ @opt = OptionParser.new{|o|
21
+ o.on("--home PATH", "path to save ruby-station data (default: #@home)"){|path|
22
+ @home = path
23
+ }
24
+ o.on("-h", "--help", "show this message"){
25
+ puts @opt.to_s
26
+ exit
27
+ }
28
+ }
29
+ @opt.parse!(ARGV)
30
+ end
31
+
32
+ def self.load_yaml
33
+ yaml_path = File.join(@home, "config.yaml")
34
+ unless File.exist?(yaml_path)
35
+ Ramaze::Log.warn "#{yaml_path} not found: creating"
36
+ FileUtils.makedirs(@home)
37
+ File.open(yaml_path, "w"){|f|
38
+ f.write File.read(__DIR__("sample.config.yaml"))
39
+ }
40
+ end
41
+ YAML.load_file(yaml_path)
42
+ end
43
+
44
+ # TODO: refactor X-(
45
+ def self.init
46
+ @yaml = self.load_yaml
47
+ @yaml[:gem_dir] = File.expand_path(@yaml[:gem_dir], @home)
48
+ @yaml[:gem_bin_dir] = File.expand_path(@yaml[:gem_bin_dir], @home)
49
+ @yaml[:data_dir] = File.expand_path(@yaml[:data_dir], @home)
50
+ FileUtils.makedirs(@yaml[:gem_dir])
51
+ FileUtils.makedirs(@yaml[:gem_bin_dir])
52
+ FileUtils.makedirs(@yaml[:data_dir])
53
+
54
+ unless GemManager.installed?("ruby-station", RubyStation::VERSION)
55
+ gem_path = __DIR__("pkg/ruby-station-#{RubyStation::VERSION}.gem")
56
+ GemManager.install_file(gem_path)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,79 @@
1
+ class Applications < Controller
2
+ map '/applications'
3
+
4
+ layout{|path, ext|
5
+ "default" unless path =~ /\Ado_/
6
+ }
7
+
8
+ def install(name=nil)
9
+ if name.nil?
10
+ tempfile = request["gem"][:tempfile]
11
+ @filename = request["gem"][:filename]
12
+ @size = tempfile.size
13
+ session[:tempfile] = tempfile
14
+ else
15
+ raise
16
+ end
17
+ end
18
+
19
+ def do_install(name=nil)
20
+ if name.nil?
21
+ path = session[:tempfile].path
22
+ result, name, version = GemManager.install_file(path)
23
+ ver = version.to_s
24
+
25
+ @result = h result
26
+
27
+ unless Application.first(:name => name, :version => ver)
28
+ Application.create({
29
+ :pid => nil,
30
+ :port => 30000 + rand(9999),
31
+ :name => name,
32
+ :version => ver,
33
+ })
34
+ end
35
+ end
36
+ end
37
+
38
+ def create
39
+ end
40
+
41
+ def show(name)
42
+ end
43
+
44
+ def uninstall(id)
45
+ app = Application.get(id)
46
+ raise "application not found(id=#{id})" unless app
47
+
48
+ @app = app
49
+ end
50
+
51
+ def do_uninstall(id)
52
+ app = Application.get(id)
53
+ raise "application not found(id=#{id})" unless app
54
+
55
+ result = GemManager.uninstall(app.name, app.version)
56
+ app.destroy
57
+ result
58
+ end
59
+
60
+ def start(id)
61
+ app = Application.get(id)
62
+ raise "application not found(id=#{id})" unless app
63
+
64
+ app.start
65
+ sleep 3
66
+
67
+ redirect "http://localhost:#{app.port}/"
68
+ end
69
+
70
+ def stop(id)
71
+ app = Application.get(id)
72
+ raise "application not found(id=#{id})" unless app
73
+
74
+ app.stop
75
+
76
+ redirect_referer
77
+ end
78
+
79
+ end
@@ -0,0 +1,8 @@
1
+ class Controller < Ramaze::Controller
2
+ helper :xhtml
3
+ engine :Etanni
4
+ layout 'default'
5
+ end
6
+
7
+ require 'controller/main.rb'
8
+ require 'controller/applications.rb'
@@ -0,0 +1,8 @@
1
+ class MainController < Controller
2
+ map '/'
3
+
4
+ def index
5
+ @applications = Application.all
6
+ end
7
+
8
+ end
@@ -0,0 +1,61 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="Content-Type",
4
+ content="text/html; charset=utf-8"/>
5
+ <script type="text/javascript" src="/js/jquery.js"></script>
6
+ <style type='text/css'>
7
+ body { font-size: 16px; line-height: 20px;
8
+ font-family: Georgia, serif; background: white; margin: 0 10%;
9
+ background-color:#D01;
10
+ }
11
+ a { color: #D01; text-decoration: none; }
12
+ h1, h2, h3, h4, p, hr, div { padding: 0px; margin-bottom: 20px; }
13
+ h1, h2, h3, h4, form * { font-family: sans-serif }
14
+ h1 { font-size: 45px; line-height: 45px; padding-bottom: 13px;
15
+ border-bottom: 2px solid black;
16
+ margin: 10px;}
17
+ h1 span#subtitle { font-size: 20px; }
18
+ h2 { font-size: 20px; line-height: 20px;}
19
+ h4 { padding: 0px; margin: 0px; }
20
+ a.delete { color: maroon; }
21
+ div#main { width: 100%; background-color:white; padding:10px;}
22
+ div.create { margin-left: 20px; float: right; }
23
+ div.entry { border-bottom: 2px solid silver; }
24
+ div.comment { border-bottom: 1px solid silver;}
25
+ div.comment p.info { font-style: italic;}
26
+
27
+ div#left {
28
+ width: 50%;
29
+ float: left;
30
+ }
31
+
32
+ div#right {
33
+ width: 50%;
34
+ float: right;
35
+ }
36
+
37
+ span.max_catches{ font-size: small; }
38
+
39
+ div#trick_description { margin-left: 2em; }
40
+
41
+ table#trick_records { border-collapse: collapse; }
42
+ table#trick_records td, table#trick_records th {
43
+ border: 1px solid black;
44
+ }
45
+ td#trick_add_record { border: 0px solid white;; }
46
+
47
+ div#footer{ text-align: right; }
48
+ </style>
49
+ </head>
50
+ <body>
51
+ <div id="main">
52
+ <h1>
53
+ <a href="/">RubyStation</a>
54
+ </h1>
55
+ <div>
56
+ #@content
57
+ </div>
58
+ </div>
59
+ </body>
60
+ </html>
61
+
@@ -0,0 +1,28 @@
1
+ require 'optparse'
2
+
3
+ module RubyStation
4
+ @port = nil
5
+ @data_dir = nil
6
+
7
+ def self.parse_argv
8
+ OptionParser.new{|o|
9
+ o.on("--port N"){|n| @port = n}
10
+ o.on("--data-dir PATH"){|d| @data_dir = d}
11
+ }.parse!(ARGV)
12
+
13
+ unless @port
14
+ @port = 40000 + rand(10000)
15
+ warn "--port is not specified; assuming it is #{@port}"
16
+ end
17
+ unless @data_dir
18
+ @data_dir = File.expand_path("./")
19
+ warn "--data-dir is not specified; assuming it is #{@data_dir}"
20
+ end
21
+ end
22
+
23
+ def self.port; @port; end
24
+ def self.data_dir; @data_dir; end
25
+ def self.data_path(filename)
26
+ File.expand_path(filename, @data_dir)
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ require 'ramaze/tool/bin'
2
+
3
+ class Application
4
+ include DataMapper::Resource
5
+ include Ramaze::Tool::Bin::Helpers
6
+
7
+ property :id, Serial
8
+ property :pid, Integer
9
+ property :port, Integer
10
+ property :name, String
11
+ property :version, String
12
+
13
+ def start
14
+ return if self.pid
15
+
16
+ cmd = [
17
+ Conf.ruby_command,
18
+ script_path,
19
+ "--port", self.port.to_s,
20
+ "--data-dir", data_dir,
21
+ ].join(" ")
22
+
23
+ self.pid = fork {
24
+ exec(cmd)
25
+ }
26
+ self.save
27
+ end
28
+
29
+ def stop
30
+ return unless self.pid
31
+
32
+ Process.kill("INT", self.pid)
33
+ if is_running?(self.pid)
34
+ sleep 2
35
+ Ramaze::Log.warn "Process #{self.pid} did not die, forcing it with -9"
36
+ Process.kill(9, self.pid)
37
+ end
38
+
39
+ self.pid = nil
40
+ self.save
41
+ end
42
+
43
+ def full_name
44
+ "#{self.name}-#{self.version}"
45
+ end
46
+
47
+ private
48
+
49
+ def script_path
50
+ File.join(Conf.gem_dir, "gems",
51
+ full_name, "main.rb")
52
+ end
53
+
54
+ def data_dir
55
+ File.join(Conf.data_dir, full_name)
56
+ end
57
+ end
58
+ DataMapper.auto_upgrade!
59
+
60
+ Application.all.each{|a| a.update_attributes(:pid => nil)}
data/model/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'dm-core'
2
+ DataMapper.setup(:default, "sqlite3://#{__DIR__("../dev.db")}")
3
+
4
+ require 'model/application'
@@ -0,0 +1,90 @@
1
+ body {
2
+ font-family: sans-serif;
3
+ }
4
+
5
+ h1.main {
6
+ text-align: center;
7
+ }
8
+
9
+ td {
10
+ padding: 4px;
11
+ }
12
+
13
+ table.main {
14
+ background: #444;
15
+ margin-left: 2em;
16
+ }
17
+
18
+ div.additional h3 {
19
+ cursor: pointer;
20
+ }
21
+
22
+ div.additional pre {
23
+ margin-left: 2em;
24
+ }
25
+
26
+ table.main tr.head {
27
+ background: #fee;
28
+ width: 100%;
29
+ }
30
+
31
+ table.main tr {
32
+ background: #fff;
33
+ }
34
+
35
+ table.main tr.source_container {
36
+ display: none;
37
+ }
38
+
39
+ tr.source td {
40
+ padding: 0;
41
+ }
42
+
43
+ tr.source_container div {
44
+ width: 100%;
45
+ overflow: auto;
46
+ }
47
+
48
+ tr.source_container div table {
49
+ background: #ddd;
50
+ width: 100%;
51
+ }
52
+
53
+ tr.source_container div table tr {
54
+ text-align:center;
55
+ }
56
+
57
+ tr.source_container div table tr.source {
58
+ text-align:left;
59
+ }
60
+
61
+ div.source {
62
+ background: #fff;
63
+ padding: 6px;
64
+ }
65
+
66
+ div.source td.editor {
67
+ font-size: 0.8em;
68
+ }
69
+
70
+ tr.source {
71
+ font-size: 1.0em;
72
+ }
73
+
74
+ tr.source td.lineno {
75
+ font-size: 0.9em;
76
+ font-family: monospace;
77
+ }
78
+
79
+ tr.source td.lineno, tr.line td.lineno {
80
+ text-align: right;
81
+ }
82
+
83
+ tr.line td {
84
+ cursor: pointer;
85
+ }
86
+
87
+ tr.source td.code pre {
88
+ margin: 0;
89
+ font-family: monospace;
90
+ }
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'ramaze'
5
+
6
+ # FCGI doesn't like you writing to stdout
7
+ Ramaze::Log.loggers = [ Ramaze::Logger::Informer.new( __DIR__("../ramaze.fcgi.log") ) ]
8
+ Ramaze::Global.adapter = :fcgi
9
+
10
+ $0 = __DIR__("../start.rb")
11
+ require $0
Binary file