ruby-station 0.0.4

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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ tmp
3
+ misc
data/Rakefile ADDED
@@ -0,0 +1,54 @@
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
44
+
45
+ # for runtime
46
+
47
+ runtime_version = File.read("runtime/VERSION").chomp
48
+
49
+ desc "Create runtime gem"
50
+ task :runtime do
51
+ cd "runtime/"
52
+ sh "rake gemspec"
53
+ sh "gem build ruby-station-runtime.gemspec"
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
data/bin/ruby-station ADDED
@@ -0,0 +1,19 @@
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,
18
+ :root => __DIR__("../"),
19
+ :mode => :live
data/config.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+
4
+ #Encoding.default_external = 'UTF-8'
5
+
6
+ module RubyStation
7
+ VERSION = File.read(__DIR__("VERSION")).chomp
8
+ RUNTIME_VERSION = File.read(__DIR__("runtime", "VERSION")).chomp
9
+ end
10
+
11
+ class Conf
12
+ %w(ruby_command gem_command gem_dir gem_bin_dir gem_install_option data_dir server_port).each do |m|
13
+ class_eval <<-EOD
14
+ def self.#{m}; @yaml[:#{m}]; end
15
+ EOD
16
+ end
17
+
18
+ def self.db_path
19
+ File.join(@home, "ruby-station.db")
20
+ end
21
+
22
+ def self.parse_opt
23
+ @home = File.expand_path("~/.ruby-station")
24
+
25
+ @opt = OptionParser.new{|o|
26
+ o.on("--home PATH", "path to save ruby-station data (default: #@home)"){|path|
27
+ @home = File.expand_path(path)
28
+ }
29
+ o.on("-h", "--help", "show this message"){
30
+ puts @opt.to_s
31
+ exit
32
+ }
33
+ }
34
+ @opt.parse!(ARGV)
35
+ end
36
+
37
+ def self.load_yaml
38
+ yaml_path = File.join(@home, "config.yaml")
39
+ unless File.exist?(yaml_path)
40
+ Ramaze::Log.warn "#{yaml_path} not found: creating"
41
+ FileUtils.makedirs(@home)
42
+ File.open(yaml_path, "w"){|f|
43
+ f.write File.read(__DIR__("sample.config.yaml"))
44
+ }
45
+ end
46
+ YAML.load_file(yaml_path)
47
+ end
48
+
49
+ # TODO: refactor X-(
50
+ def self.init
51
+ @yaml = self.load_yaml
52
+ @yaml[:gem_dir] = File.expand_path(@yaml[:gem_dir], @home)
53
+ @yaml[:gem_bin_dir] = File.expand_path(@yaml[:gem_bin_dir], @home)
54
+ @yaml[:data_dir] = File.expand_path(@yaml[:data_dir], @home)
55
+ FileUtils.makedirs(@yaml[:gem_dir])
56
+ FileUtils.makedirs(@yaml[:gem_bin_dir])
57
+ FileUtils.makedirs(@yaml[:data_dir])
58
+
59
+ runtime_ver = RubyStation::RUNTIME_VERSION
60
+ unless GemManager.installed?("ruby-station-runtime", runtime_ver)
61
+ gem_path = __DIR__("runtime/ruby-station-runtime-#{runtime_ver}.gem")
62
+ GemManager.install_file(gem_path)
63
+ end
64
+ # unless `#{self.gem_command} sources`.include?("github")
65
+ # cmd = "#{self.gem_command} sources -a http://gems.github.com"
66
+ # Ramaze::Log.info cmd
67
+ # `#{cmd}`
68
+ # end
69
+ end
70
+ end
@@ -0,0 +1,84 @@
1
+ class Applications < Controller
2
+ map '/applications'
3
+
4
+ layout{|path, ext|
5
+ "default" unless path =~ /\Ado_/
6
+ }
7
+
8
+ def install
9
+ if request["name"]
10
+ @name = request["name"]
11
+ session[:gemname] = @name
12
+ else
13
+ tempfile = request["gem"][:tempfile]
14
+ @filename = request["gem"][:filename]
15
+ @size = tempfile.size
16
+ session[:tempfile] = tempfile
17
+ end
18
+ end
19
+
20
+ def do_install
21
+ if gemname = session[:gemname]
22
+ result, name, version = GemManager.install_gem(gemname)
23
+ elsif session[:tempfile]
24
+ path = session[:tempfile].path
25
+ result, name, version = GemManager.install_file(path)
26
+ else
27
+ raise
28
+ end
29
+
30
+ unless Application.first(:name => name, :version => version)
31
+ Application.create({
32
+ :pid => nil,
33
+ :port => 30000 + rand(9999),
34
+ :name => name,
35
+ :version => version,
36
+ })
37
+ end
38
+
39
+ session.clear
40
+ h result
41
+ end
42
+
43
+ def create
44
+ end
45
+
46
+ def show(name)
47
+ end
48
+
49
+ def uninstall(id)
50
+ app = Application.get(id)
51
+ raise "application not found(id=#{id})" unless app
52
+
53
+ @app = app
54
+ end
55
+
56
+ def do_uninstall(id)
57
+ app = Application.get(id)
58
+ raise "application not found(id=#{id})" unless app
59
+
60
+ result = GemManager.uninstall(app.name, app.version)
61
+ app.destroy
62
+ result
63
+ end
64
+
65
+ def start(id)
66
+ app = Application.get(id)
67
+ raise "application not found(id=#{id})" unless app
68
+
69
+ app.start
70
+ sleep 3
71
+
72
+ redirect "http://localhost:#{app.port}/"
73
+ end
74
+
75
+ def stop(id)
76
+ app = Application.get(id)
77
+ raise "application not found(id=#{id})" unless app
78
+
79
+ app.stop
80
+
81
+ redirect_referer
82
+ end
83
+
84
+ 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,62 @@
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
+ <title>RubyStation</title>
7
+ <style type='text/css'>
8
+ body { font-size: 16px; line-height: 20px;
9
+ font-family: Georgia, serif; background: white; margin: 0 10%;
10
+ background-color:#D01;
11
+ }
12
+ a { color: #D01; text-decoration: none; }
13
+ h1, h2, h3, h4, p, hr, div { padding: 0px; margin-bottom: 20px; }
14
+ h1, h2, h3, h4, form * { font-family: sans-serif }
15
+ h1 { font-size: 45px; line-height: 45px; padding-bottom: 13px;
16
+ border-bottom: 2px solid black;
17
+ margin: 10px;}
18
+ h1 span#subtitle { font-size: 20px; }
19
+ h2 { font-size: 20px; line-height: 20px;}
20
+ h4 { padding: 0px; margin: 0px; }
21
+ a.delete { color: maroon; }
22
+ div#main { width: 100%; background-color:white; padding:10px;}
23
+ div.create { margin-left: 20px; float: right; }
24
+ div.entry { border-bottom: 2px solid silver; }
25
+ div.comment { border-bottom: 1px solid silver;}
26
+ div.comment p.info { font-style: italic;}
27
+
28
+ div#left {
29
+ width: 50%;
30
+ float: left;
31
+ }
32
+
33
+ div#right {
34
+ width: 50%;
35
+ float: right;
36
+ }
37
+
38
+ span.max_catches{ font-size: small; }
39
+
40
+ div#trick_description { margin-left: 2em; }
41
+
42
+ table#trick_records { border-collapse: collapse; }
43
+ table#trick_records td, table#trick_records th {
44
+ border: 1px solid black;
45
+ }
46
+ td#trick_add_record { border: 0px solid white;; }
47
+
48
+ div#footer{ text-align: right; }
49
+ </style>
50
+ </head>
51
+ <body>
52
+ <div id="main">
53
+ <h1>
54
+ <a href="/">RubyStation</a>
55
+ </h1>
56
+ <div>
57
+ #@content
58
+ </div>
59
+ </div>
60
+ </body>
61
+ </html>
62
+
@@ -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,5 @@
1
+ require 'dm-core'
2
+ Ramaze::Log.info("opening database: #{Conf.db_path}")
3
+ DataMapper.setup(:default, "sqlite3://#{Conf.db_path}")
4
+
5
+ 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