Class: TomcatManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tomcat_manager/core.rb

Overview

© 2012 Dakota Bailey

Instance Method Summary collapse

Constructor Details

#initialize(base_url, user, pass, timeout = nil, open_timeout = nil) ⇒ TomcatManager

Returns a new instance of TomcatManager.



4
5
6
7
8
9
10
# File 'lib/tomcat_manager/core.rb', line 4

def initialize(base_url, user, pass, timeout = nil, open_timeout = nil)
	@base_url = base_url
	@user = user
	@pass = pass
   @timeout = timeout
   @open_timeout = open_timeout
end

Instance Method Details

#deploy(ctx_path, war_path) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/tomcat_manager/core.rb', line 77

def deploy(ctx_path, war_path)
	params = {"path" => ctx_path,
     "war" => war_path}
	results = do_get("deploy", {:headers => {:params => params}})
		if !/^OK.*/.match results
		puts "Unknown error: \n" + results
		exit 1
	end
	return results
end

#do_get(cmd, options = {}) ⇒ Object



121
122
123
124
125
126
# File 'lib/tomcat_manager/core.rb', line 121

def do_get(cmd, options = {})
	url = @base_url + '/' + cmd
	opts = options.merge({:user => @user, :password => @pass, :timeout => @timeout, :open_timeout => @open_timeout})
	resource = RestClient::Resource.new url, opts
	resource.get
end

#do_put_with_file(cmd, file, options = {}) ⇒ Object



128
129
130
131
132
133
# File 'lib/tomcat_manager/core.rb', line 128

def do_put_with_file(cmd, file, options = {})
	url = @base_url + '/' + cmd
	opts = options.merge({:user => @user, :password => @pass, :timeout => @timeout, :open_timeout => @open_timeout})
	resource = RestClient::Resource.new url, opts
	resource.put File.read(file)
end

#installed?(name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/tomcat_manager/core.rb', line 117

def installed?(name)
	self.list[name]
end

#listObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tomcat_manager/core.rb', line 98

def list()
   apps_raw = do_get("list")
   lines = apps_raw.split "\n"
   if !/^OK.*/.match lines[0]
     die "Unknown error: \n" + apps_raw
   end
   apps = {}
   lines.slice(1, lines.length).each { |line|
     rg = /^\/(.*):(.*):(.*):(.*)$/
     data = rg.match line
     name = data[4].chomp
     ctx = data[1]
     status = data[2]
     sess_cnt = data[3]
     apps[name] = {:name => name, :ctx => ctx, :status => status, :sessions => sess_cnt}
   }
   return apps
end

#redeploy(ctx_path) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/tomcat_manager/core.rb', line 57

def redeploy(ctx_path)
  params = {"path" => ctx_path}
  results = do_get("redeploy", {:headers => {:params => params}})
  if !/^OK.*/.match results
    puts "Unknown error: \n" + results
    exit 1
  end
  return results
end

#remote_deploy(ctx_path, file) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/tomcat_manager/core.rb', line 88

def remote_deploy(ctx_path, file)
	params = {"path" => ctx_path}
	results = do_put_with_file("deploy", file, {:headers => {:params => params}})
		if !/^OK.*/.match results
		puts "Unknown error: \n" + results
		exit 1
	end
	return results
end

#resources(type = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tomcat_manager/core.rb', line 12

def resources(type = nil)
  opts = {}
  if type != nil
    params = {"type" => type}
    opts[:headers] = {:params => params}
  end
  results = do_get("resources", opts)
  lines = results.split "\n"
  if !/^OK.*/.match lines[0]
    puts "Unknown error: \n" + results
    exit 1
  end
  info = {}
  rg = /^(.*):(.*)$/
  lines.slice(1, lines.length).each { |line|
    data = rg.match line
    name = data[1].strip
    value = data[2].strip
    info[name] = value
  }
  return info

  return results
end

#serverinfoObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tomcat_manager/core.rb', line 37

def serverinfo()
  results = do_get("serverinfo")
  lines = results.split "\n"
  if !/^OK.*/.match lines[0]
    puts "Unknown error: \n" + results
    exit 1
  end
  info = {}
  rg = /^(.*):(.*)$/
  lines.slice(1, lines.length).each { |line|
    data = rg.match line
    name = data[1].strip
    value = data[2].strip
    info[name] = value
  }
  return info

  return results
end

#undeploy(ctx_path) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/tomcat_manager/core.rb', line 67

def undeploy(ctx_path)
  params = {"path" => ctx_path}
  results = do_get("undeploy", {:headers => {:params => params}})
  if !/^OK.*/.match results
    puts "Unknown error: \n" + results
    exit 1
  end
  return results
end