forked from nesquena/cap-recipes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.rb
executable file
·105 lines (76 loc) · 2.8 KB
/
test.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
# -*- coding: utf-8 -*-
require "net/http"
require "uri"
require "json"
class CmdbService
def self.get_app_role(cse_base, unit_code, stage)
url = "#{cse_base}/deploy/get-server.do?deployUnitCode=#{unit_code}&stage=#{stage}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
# we convert the returned JSON data to native Ruby
# data structure - a hash
result = JSON.parse(data)
# if the hash has 'Error' as a key, we raise an error
if !result.has_key? 'success'
raise "web service error"
end
unless result['success']
raise "#{unit_code}@#{stage} get_app_role faile!"
end
server = result['servers']
if server.nil?
raise "Not fout servers for #{unit_code}@#{stage}"
end
server.collect {|s| "#{s['ip']}:#{s['sshPort']}"}
end
def self.start_deploy(cse_base, unit_code, stage)
url = URI.parse("#{cse_base}/deploy/start-deploy.do")
param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => '1.0.1' }
http = Net::HTTP.new(url.host, url.port)
resp = http.post(url.path, param.to_json)
data = resp.body
puts "data=#{data}"
# we convert the returned JSON data to native Ruby
# data structure - a hash
result = JSON.parse(data)
# if the hash has 'Error' as a key, we raise an error
if !result.has_key? 'success'
raise "web service error"
end
unless result['success']
raise "#{unit_code}@#{stage} start_deploy faile!"
end
result['deploymentId']
end
#deployUnitCode : 发布单元编号
#deploymentId : 开始发布接口生成的唯一标识
#success : 是否发布成功
#description : 发布结果描述
def self.complete_deploy(cse_base, unit_code, deployment_id, is_success, description)
url = URI.parse("#{cse_base}/deploy/complete-deploy.do")
param = { 'deployUnitCode' => unit_code, 'deploymentId' => deployment_id,
'success' => is_success, 'description' => description }
http = Net::HTTP.new(url.host, url.port)
resp = http.post(url.path, param.to_json)
data = resp.body
puts "data=#{data}"
# we convert the returned JSON data to native Ruby
# data structure - a hash
result = JSON.parse(data)
# if the hash has 'Error' as a key, we raise an error
if !result.has_key? 'success'
raise "web service error"
end
unless result['success']
puts "#{unit_code}@#{stage} start_deploy faile!"
return false
end
return true
end
end
s = CmdbService.get_app_role("http://10.241.14.166:8080/cse", "zf00032", "test")
puts "s=#{s}"
did = CmdbService.start_deploy("http://10.241.14.166:8080/cse", "zf00032", "test")
puts "deploy=#{did}"
r = CmdbService.complete_deploy("http://10.241.14.166:8080/cse", "zf00032", did, true, "发布成功")
puts "r=#{r}"