crazycode-cap-recipes 0.4.13 → 0.4.14

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 13
4
+ :patch: 14
5
5
  :build:
data/config/deploy.rb CHANGED
@@ -26,6 +26,10 @@ set :release_dir, "/tmp/cmdb_release"
26
26
  set :upload_dir, "/tmp/cmdb_upload"
27
27
 
28
28
 
29
+ set :cse_base, "http://localhost:8081/cmdb-service-exporter"
30
+ set :user, "root"
31
+
32
+
29
33
  # =============================================================================
30
34
  # RECIPE INCLUDES
31
35
  # =============================================================================
@@ -36,3 +40,5 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
36
40
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
37
41
 
38
42
  require 'cap_recipes/tasks/sdpjenkins'
43
+ require 'cap_recipes/tasks/gitdeploy'
44
+ require 'cap_recipes/tasks/sft'
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{crazycode-cap-recipes}
8
- s.version = "0.4.13"
8
+ s.version = "0.4.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["crazycode"]
12
- s.date = %q{2011-09-01}
12
+ s.date = %q{2011-10-08}
13
13
  s.default_executable = %q{cap-recipes}
14
14
  s.description = %q{Battle-tested capistrano recipes for debian, passenger, apache, hudson, delayed_job, juggernaut, rubygems, backgroundrb, rails and more}
15
15
  s.email = %q{crazycode@gmail.com}
@@ -21,7 +21,7 @@ class CmdbService
21
21
  raise "web service error"
22
22
  end
23
23
 
24
- unless result['success']
24
+ unless result['success'] == true
25
25
  raise "#{unit_code}@#{stage} get_app_role faile! return data: #{data}"
26
26
  end
27
27
 
@@ -39,7 +39,32 @@ class CmdbService
39
39
  def self.start_deploy(cse_base, unit_code, stage, version)
40
40
  url = URI.parse("#{cse_base}/deploy/start-deploy.do")
41
41
  puts "version=#{version}"
42
- param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version }
42
+ param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version, 'git_revision' => '' }
43
+
44
+ http = Net::HTTP.new(url.host, url.port)
45
+
46
+ resp = http.post(url.path, param.to_json)
47
+ data = resp.body
48
+
49
+ result = JSON.parse(data)
50
+
51
+ if !result.has_key? 'success'
52
+ raise "web service error"
53
+ end
54
+
55
+ unless result['success']
56
+ raise "#{unit_code}@#{stage} start_deploy faile! return data: #{data}"
57
+ end
58
+
59
+ result['deploymentId']
60
+ end
61
+
62
+ # 启动发布过程,并注册服务器
63
+ def self.start_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir)
64
+ url = URI.parse("#{cse_base}/deploy/start-deploy-with-server.do")
65
+ puts "version=#{version}"
66
+ param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version,
67
+ 'git_revision' => '', 'servers' => servers, 'deploy_dir' => deploy_dir }
43
68
 
44
69
  http = Net::HTTP.new(url.host, url.port)
45
70
 
@@ -121,4 +146,29 @@ class CmdbService
121
146
  deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, false, "capistrano部署失败,撤销发布,原因:#{e.message}") }
122
147
  end
123
148
  end
149
+
150
+ # servers: 为IP加端口以逗号分隔的形式,如“10.241.12.12:22,10.241.12.13:58422”,如果没有写端口,默认为58422
151
+ def self.split_servers(servers)
152
+ servers.split(/[,\s]+/).collect do|s|
153
+ unless s.include?(":")
154
+ "#{s}:58422"
155
+ else
156
+ s
157
+ end
158
+ end
159
+ end
160
+
161
+ # servers: 为IP加端口以逗号分隔的形式,如“10.241.12.12:22,10.241.12.13:58422”
162
+ def self.do_deploy_with_server(cse_base, unit_code, stage, version, servers, deploy_dir)
163
+ codes = unit_code.split(/[,;\s]+/)
164
+ deploy_hash = Hash.new
165
+ codes.each {|code| deploy_hash[code] = CmdbService.start_deploy_with_server(cse_base, code, stage, version, servers, deploy_dir) }
166
+
167
+ begin
168
+ yield
169
+ deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, true, "通过capistrano部署成功") }
170
+ rescue Exception => e
171
+ deploy_hash.each {|code, deployid| CmdbService.complete_deploy(cse_base, code, deployid, false, "capistrano部署失败,撤销发布,原因:#{e.message}") }
172
+ end
173
+ end
124
174
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  Capistrano::Configuration.instance(true).load do |configuration|
2
3
  set :use_sudo, true
3
4
 
@@ -27,6 +28,9 @@ Capistrano::Configuration.instance(true).load do |configuration|
27
28
  _cset :war_name, ""
28
29
  _cset :war_path, "not set.war"
29
30
 
31
+ _cset :servers, ""
32
+ _cset :deploy_to, ""
33
+
30
34
  set :local_git_dir do
31
35
  "/tmp/localgit_#{application}"
32
36
  end
@@ -104,6 +108,23 @@ Capistrano::Configuration.instance(true).load do |configuration|
104
108
  run update_repository_remote_command(tag_name)
105
109
  end
106
110
 
111
+ desc "register servers and deploy_dir on CMDB"
112
+ task :register_servers do
113
+ version = build_version
114
+ puts "version=#{version}, servers=#{servers}, deploy_dir=#{deploy_to}"
115
+ CmdbService.start_deploy_with_server(cse_base, deploy_unit_code, deploy_stage, version.strip, servers, deploy_to)
116
+ end
117
+
118
+ desc "send deploy success info to CMDB"
119
+ task :deploy_succ do
120
+ CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_stage, true, "部署成功")
121
+ end
122
+
123
+ desc "send deploy failure info to CMDB"
124
+ task :deploy_fail do
125
+ CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_stage, false, "capistrano部署失败,撤销发布。")
126
+ end
127
+
107
128
 
108
129
  def self.clone_repository_local_command
109
130
  [
@@ -22,8 +22,14 @@ Capistrano::Configuration.instance(true).load do |configuration|
22
22
  _cset :shell_commands, "cd #{upload_dir}; ls -all"
23
23
  _cset :build_version, ""
24
24
 
25
+ _cset :servers, ""
26
+ _cset :deploy_to, ""
27
+
25
28
  role :app, :primary => true do
26
- CmdbService.get_app_role("#{cse_base}", deploy_unit_code, deploy_stage)
29
+ if servers.empty?
30
+ return CmdbService.get_app_role("#{cse_base}", deploy_unit_code, deploy_stage)
31
+ end
32
+ CmdbService.split_servers(servers)
27
33
  end
28
34
 
29
35
  role :single, "1.1.1.1"
@@ -97,5 +103,19 @@ Capistrano::Configuration.instance(true).load do |configuration|
97
103
  end
98
104
  end
99
105
 
106
+ desc "register servers, upload release file, then execute commands"
107
+ task :deploy_with_servers, :roles => :single do
108
+ version = build_version
109
+ if version.empty? && File.exists?("#{release_dir}/version.txt")
110
+ version = File.open("#{release_dir}/version.txt") { |f| f.extend(Enumerable).inject { |_,ln| ln } }
111
+ end
112
+ puts "version=#{version}, servers=#{servers}, deploy_dir=#{deploy_to}"
113
+
114
+ CmdbService.do_deploy_with_server(cse_base, deploy_unit_code, deploy_stage, version.strip, servers, deploy_to) do
115
+ upload_file
116
+ execute_commands
117
+ end
118
+ end
119
+
100
120
  end
101
121
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 13
9
- version: 0.4.13
8
+ - 14
9
+ version: 0.4.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - crazycode
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-09-01 00:00:00 +08:00
17
+ date: 2011-10-08 00:00:00 +08:00
18
18
  default_executable: cap-recipes
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency