summaryrefslogtreecommitdiff
path: root/lib/tasks/opentox.rb
blob: e330c8793432d38c6848565a8b6cf4e2b0139368 (plain)
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
106
107
require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb')

namespace :opentox do

	namespace :services do

		desc "Run opentox services"
		task :start do
			@@config[:services].each do |service,uri|
				dir = File.join(@@config[:base_dir], service)
				server = @@config[:webserver]
				case server
				when /thin|mongrel|webrick/
					port = uri.sub(/^.*:/,'').sub(/\/$/,'')
					Dir.chdir dir
					pid_file = File.join(@@tmp_dir,"#{service}.pid") 
					begin
						`#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &`
						puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}."
					rescue
						puts "Cannot start #{service} on port #{port}."
					end
				when 'passenger'
					FileUtils.mkdir_p File.join(dir, 'tmp')
					FileUtils.touch File.join(dir, 'tmp/restart.txt')
					puts "#{service} restarted."
				else
					puts "not yet implemented"
				end
			end
		end

		desc "Stop opentox services"
		task :stop do
			server = @@config[:webserver]
			if server =~ /thin|mongrel|webrick/
				@@config[:services].each do |service,uri|
					port = uri.sub(/^.*:/,'').sub(/\/$/,'')
					pid_file = File.join(@@tmp_dir,"#{service}.pid") 
					begin
						puts `#{server} stop -P #{pid_file}` 
						puts "#{service} stopped on localhost:#{port}"
					rescue
						puts "Cannot stop #{service} on port #{port}."
					end
				end
			end
		end

		desc "Restart opentox services"
		task :restart => [:stop, :start]

	end

	desc "Run all OpenTox tests"
	task :test do
		@@config[:services].each do |service,uri|
			dir = File.join(@@config[:base_dir], service)
			Dir.chdir dir
			puts "Running tests in #{dir}"
			`rake test -t 1>&2`
		end
	end

end

desc "Start service in current directory"
task :start do
	service = File.basename(Dir.pwd).intern
	server = @@config[:webserver]
	case server 
		when /thin|mongrel|webrick/
			port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'')
			pid_file = File.join(@@tmp_dir,"#{service}.pid") 
			begin
				`#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &`
				puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}."
			rescue
				puts "Cannot start #{service} on port #{port}."
			end
		when 'passenger'
			FileUtils.mkdir_p File.join(dir, 'tmp')
			FileUtils.touch File.join(dir, 'tmp/restart.txt')
			puts "#{service} restarted."
		else
			puts "not yet implemented"
		end
end

desc "Stop service in current directory"
task :stop do
	service = File.basename(Dir.pwd).intern
	server = @@config[:webserver]
	if server =~ /thin|mongrel|webrick/
		port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'')
		pid_file = File.join(@@tmp_dir,"#{service}.pid") 
		begin
			puts `thin stop -P #{pid_file}` 
			puts "#{service} stopped on localhost:#{port}"
		rescue
			puts "Cannot stop #{service} on port #{port}."
		end
	end
end

desc "Restart service in current directory"
task :restart  => [:stop, :start]