forked from chrismatthieu/nodefu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchnodefu.rb
106 lines (85 loc) · 2.28 KB
/
launchnodefu.rb
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'json'
# Couch DB Commands
module Couch
class Server
def initialize(host, port, options = nil)
@host = host
@port = port
@options = options
end
def delete(uri)
request(Net::HTTP::Delete.new(uri))
end
def get(uri)
request(Net::HTTP::Get.new(uri))
end
def put(uri, json)
req = Net::HTTP::Put.new(uri)
req["content-type"] = "application/json"
req.body = json
request(req)
end
def post(uri, json)
req = Net::HTTP::Post.new(uri)
req["content-type"] = "application/json"
req.body = json
request(req)
end
def request(req)
res = Net::HTTP.start(@host, @port) { |http|http.request(req) }
unless res.kind_of?(Net::HTTPSuccess)
handle_error(req, res)
end
res
end
private
def handle_error(req, res)
e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
raise e
end
end
end
# Kill all node processes
`killall node`
# Launch NodeFu
child_pid = fork do
# Must be started with SUDO to run on port 80
`sudo sh -c "/usr/local/bin/node proxy.js"`
# `nodemon proxy.js`
end
Process.detach(child_pid)
child_pid = fork do
`nodemon app.js`
end
Process.detach(child_pid)
# Launch Apps
server = Couch::Server.new("nodefu.couchone.com", "80")
res = server.get('/apps/_design/nodeapps/_view/all')
jsonresp = res.body
dbdata = JSON.parse(jsonresp)
nodefudir = Dir.pwd
dbdata["rows"].each do |app|
begin
child_pid = fork do
begin
Dir.chdir("apps/#{app["value"]["_rev"]}")
# Dir.chdir("apps/#{app["value"]["_id"]}")
`nodemon #{app["value"]["start"]}`
Dir.chdir("../..")
puts app["value"]["_id"] + ' : ' + app["value"]["_rev"] + ' : ' + app["value"]["start"]
# puts app["value"]["_id"] + ' : ' + app["value"]["_id"] + ' : ' + app["value"]["start"]
rescue Exception => e
Dir.chdir(nodefudir)
end
end
Process.detach(child_pid)
rescue Exception => e
Dir.chdir(nodefudir)
end
end
puts "NodeFu launched - HiYah!"
exit
# res = server.put("/imsms/phonosdk", '{ "jid":"' + jid.to_s + '", "_rev":"' + dbdata["_rev"].to_s + '" }')