Class: CmdbService

Inherits:
Object
  • Object
show all
Defined in:
lib/cap_recipes/tasks/cmdbutils.rb

Class Method Summary collapse

Class Method Details

.complete_deploy(cse_base, unit_code, deployment_id, is_success, description) ⇒ Object

deployUnitCode : 发布单元编号 deploymentId : 开始发布接口生成的唯一标识 success : 是否发布成功 description : 发布结果描述



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 92

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

  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}@#{deployment_id} complete_deploy faile! return data: #{data}"
    return false
  end

  return true
end

.do_deploy(cse_base, unit_code, stage, version) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 137

def self.do_deploy(cse_base, unit_code, stage, version)
    codes = unit_code.split(/[,;\s]+/)
    deploy_hash = Hash.new
    codes.each {|code| deploy_hash[code] = CmdbService.start_deploy(cse_base, code, stage, version) }

    begin
      yield
      deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, true, "通过capistrano部署成功") }
    rescue Exception => e
      deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, false, "capistrano部署失败,撤销发布,原因:#{e.message}") }
      raise e
    end
end

.do_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir) ⇒ Object

servers: 为IP加端口以逗号分隔的形式,如“10.241.12.12:22,10.241.12.13:58422”



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 163

def self.do_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir)
    codes = unit_code.split(/[,;\s]+/)
    deploy_hash = Hash.new
    codes.each {|code| deploy_hash[code] = CmdbService.start_deploy_with_server(cse_base, code, stage, version, servers, deploy_dir) }

    begin
      yield
      deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, true, "通过capistrano部署成功") }
    rescue Exception => e
      deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, false, "capistrano部署失败,撤销发布,原因:#{e.message}") }
      raise e
    end
end

.get_app_role(cse_base, unit_code, stage) ⇒ Object



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
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 10

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'] == true
    raise "#{unit_code}@#{stage} get_app_role faile! return data: #{data}"
  end

  server = result['servers']
  if server.nil?
    raise "Not fout servers for #{unit_code}@#{stage}"
  end

  servers = server.collect {|s| "#{s['ip']}:#{s['sshPort']}"}
  puts "Roles: #{servers.join(', ')}"
  servers
end

.get_deploy_id(file) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 119

def self.get_deploy_id(file)
  unless File.exists? file
    puts "file #{file} NOT exists!"
    return nil
  end

  deploy_id = nil
  open(file, 'r') do |f|
    deploy_id = f.readline
    #if f.lines.count == 0
     # puts "empty deploy_id, please delete file: #{file}."
    #else
     # deploy_id = f.readline
    #end
  end
  deploy_id
end

.split_servers(servers) ⇒ Object

servers: 为IP加端口以逗号分隔的形式,如“10.241.12.12:22,10.241.12.13:58422”,如果没有写端口,默认为58422



152
153
154
155
156
157
158
159
160
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 152

def self.split_servers(servers)
  servers.split(/[,\s]+/).collect do|s|
    unless s.include?(":")
      "#{s}:58422"
    else
      s
    end
  end
end

.start_deploy(cse_base, unit_code, stage, version) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 39

def self.start_deploy(cse_base, unit_code, stage, version)
  url = URI.parse("#{cse_base}/deploy/start-deploy.do")
  puts "version=#{version}"
  param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version, 'git_revision' => '' }

  http = Net::HTTP.new(url.host, url.port)

  resp = http.post(url.path, param.to_json)
  data = resp.body

  result = JSON.parse(data)

  if !result.has_key? 'success'
    raise "web service error"
  end

  unless result['success']
    raise "#{unit_code}@#{stage} start_deploy faile! return data: #{data}"
  end

  result['deploymentId']
end

.start_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir) ⇒ Object

启动发布过程,并注册服务器



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cap_recipes/tasks/cmdbutils.rb', line 63

def self.start_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir)
  url = URI.parse("#{cse_base}/deploy/start-deploy-with-server.do")
  puts "version=#{version}"
  param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version,
    'git_revision' => '',  'deploy_user' => 'capistrano', 'task_id' => '0', 'servers' => servers, 'deploy_dir' => deploy_dir }

  http = Net::HTTP.new(url.host, url.port)

  resp = http.post(url.path, param.to_json)
  data = resp.body

  result = JSON.parse(data)

  if !result.has_key? 'success'
    raise "web service error"
  end

  unless result['success']
    raise "#{unit_code}@#{stage} start_deploy faile! return data: #{data}"
  end

  result['deploymentId']
end