crazycode-cap-recipes 0.3.39.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Capfile +4 -0
- data/VERSION.yml +4 -4
- data/config/deploy.rb +38 -0
- data/crazycode-cap-recipes.gemspec +10 -3
- data/lib/cap_recipes/tasks/cmdb.rb +70 -0
- data/lib/cap_recipes/tasks/cmdbutils.rb +110 -0
- data/lib/cap_recipes/tasks/http.rb +102 -0
- data/lib/cap_recipes/tasks/sdpjenkins.rb +120 -0
- data/test.rb +105 -0
- metadata +11 -5
data/Capfile
ADDED
data/VERSION.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
---
|
1
|
+
---
|
2
2
|
:major: 0
|
3
|
-
:minor:
|
4
|
-
:
|
5
|
-
:
|
3
|
+
:minor: 4
|
4
|
+
:patch: 0
|
5
|
+
:build:
|
data/config/deploy.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# =============================================================================
|
3
|
+
# GENERAL SETTINGS
|
4
|
+
# =============================================================================
|
5
|
+
|
6
|
+
role :app, "10.241.14.166"
|
7
|
+
|
8
|
+
set :application, "demo"
|
9
|
+
set :deploy_to, "/var/apps/#{application}"
|
10
|
+
# set :deploy_via, :remote_cache
|
11
|
+
# set :scm, :git
|
12
|
+
# set :repository, "deploy@dev.demo.com:/home/demo.git"
|
13
|
+
# set :git_enable_submodules, 1
|
14
|
+
# set :keep_releases, 3
|
15
|
+
|
16
|
+
set :user, "pm030"
|
17
|
+
set :use_sudo, true
|
18
|
+
|
19
|
+
ssh_options[:paranoid] = false
|
20
|
+
default_run_options[:pty] = true
|
21
|
+
|
22
|
+
# 自定义变量
|
23
|
+
|
24
|
+
set :build_workspace, "/home/tanglq/pay/cmdb"
|
25
|
+
set :release_dir, "/tmp/cmdb_release"
|
26
|
+
set :upload_dir, "/tmp/cmdb_upload"
|
27
|
+
|
28
|
+
|
29
|
+
# =============================================================================
|
30
|
+
# RECIPE INCLUDES
|
31
|
+
# =============================================================================
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
# for development
|
35
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
36
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
37
|
+
|
38
|
+
require 'cap_recipes/tasks/sdpjenkins'
|
@@ -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.
|
8
|
+
s.version = "0.4.0"
|
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-
|
12
|
+
s.date = %q{2011-08-22}
|
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}
|
@@ -19,12 +19,14 @@ Gem::Specification.new do |s|
|
|
19
19
|
"README.textile"
|
20
20
|
]
|
21
21
|
s.files = [
|
22
|
+
"Capfile",
|
22
23
|
"LICENSE",
|
23
24
|
"README.textile",
|
24
25
|
"Rakefile",
|
25
26
|
"VERSION.yml",
|
26
27
|
"bin/cap-recipes",
|
27
28
|
"cap-recipes.gemspec",
|
29
|
+
"config/deploy.rb",
|
28
30
|
"crazycode-cap-recipes.gemspec",
|
29
31
|
"examples/advanced/deploy.rb",
|
30
32
|
"examples/advanced/deploy/experimental.rb",
|
@@ -41,6 +43,8 @@ Gem::Specification.new do |s|
|
|
41
43
|
"lib/cap_recipes/tasks/backgroundrb/manage.rb",
|
42
44
|
"lib/cap_recipes/tasks/bundler.rb",
|
43
45
|
"lib/cap_recipes/tasks/bundler/manage.rb",
|
46
|
+
"lib/cap_recipes/tasks/cmdb.rb",
|
47
|
+
"lib/cap_recipes/tasks/cmdbutils.rb",
|
44
48
|
"lib/cap_recipes/tasks/delayed_job.rb",
|
45
49
|
"lib/cap_recipes/tasks/delayed_job/hooks.rb",
|
46
50
|
"lib/cap_recipes/tasks/delayed_job/manage.rb",
|
@@ -51,6 +55,7 @@ Gem::Specification.new do |s|
|
|
51
55
|
"lib/cap_recipes/tasks/gitosis.rb",
|
52
56
|
"lib/cap_recipes/tasks/gitosis/install.rb",
|
53
57
|
"lib/cap_recipes/tasks/gitosis/manage.rb",
|
58
|
+
"lib/cap_recipes/tasks/http.rb",
|
54
59
|
"lib/cap_recipes/tasks/hudson.rb",
|
55
60
|
"lib/cap_recipes/tasks/hudson/manage.rb",
|
56
61
|
"lib/cap_recipes/tasks/jetty.rb",
|
@@ -79,6 +84,7 @@ Gem::Specification.new do |s|
|
|
79
84
|
"lib/cap_recipes/tasks/rubygems.rb",
|
80
85
|
"lib/cap_recipes/tasks/rubygems/install.rb",
|
81
86
|
"lib/cap_recipes/tasks/rubygems/manage.rb",
|
87
|
+
"lib/cap_recipes/tasks/sdpjenkins.rb",
|
82
88
|
"lib/cap_recipes/tasks/templates/hudson.erb",
|
83
89
|
"lib/cap_recipes/tasks/templates/mongod.conf.erb",
|
84
90
|
"lib/cap_recipes/tasks/templates/mongodb.init.erb",
|
@@ -106,7 +112,8 @@ Gem::Specification.new do |s|
|
|
106
112
|
"spec/cap/helper.rb",
|
107
113
|
"spec/cap_recipes_spec.rb",
|
108
114
|
"spec/spec_helper.rb",
|
109
|
-
"specs.watchr"
|
115
|
+
"specs.watchr",
|
116
|
+
"test.rb"
|
110
117
|
]
|
111
118
|
s.homepage = %q{http://github.com/crazycode/cap-recipes}
|
112
119
|
s.require_paths = ["lib"]
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
require 'cap_recipes/tasks/gitdeploy'
|
5
|
+
require 'cap_recipes/tasks/tomcat'
|
6
|
+
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + '/utilities')
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + '/cmdbutils')
|
9
|
+
|
10
|
+
Capistrano::Configuration.instance(true).load do |configuration|
|
11
|
+
|
12
|
+
set :use_sudo, true
|
13
|
+
|
14
|
+
set :cse_base, "http://10.241.14.166:8080/cse"
|
15
|
+
set :deploy_unit_code, ""
|
16
|
+
set :deploy_stage, "development"
|
17
|
+
|
18
|
+
set :deploy_id_file, ".deploy_id"
|
19
|
+
set :tag, ""
|
20
|
+
|
21
|
+
role :app do
|
22
|
+
CmdbService.get_app_role("#{cse_base}", deploy_unit_code, deploy_stage)
|
23
|
+
end
|
24
|
+
|
25
|
+
namespace :cmdb do
|
26
|
+
|
27
|
+
|
28
|
+
task :start do
|
29
|
+
if File.exists? deploy_id_file
|
30
|
+
puts "Previous Deploy NOT Complete, please run 'cap cmdb:failback' first."
|
31
|
+
exit(1)
|
32
|
+
end
|
33
|
+
|
34
|
+
deploy_id = CmdbService.start_deploy("#{cse_base}", deploy_unit_code, deploy_stage, tag)
|
35
|
+
|
36
|
+
open(deploy_id_file, 'w') do |f|
|
37
|
+
f.write deploy_id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "set current deploy FAILBACK!"
|
42
|
+
task :failback do
|
43
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file)
|
44
|
+
unless deploy_id.nil?
|
45
|
+
CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_id, false, "capistrano部署失败,撤销发布")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "set current deploy success DONE!"
|
50
|
+
task :done do
|
51
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file)
|
52
|
+
puts "deploy_id=#{deploy_id}, file=#{deploy_id_file}"
|
53
|
+
CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_id, true, "通过capistrano部署成功")
|
54
|
+
File.delete deploy_id_file
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "deploy to tomcat"
|
58
|
+
task :deploy_to_tomcat do
|
59
|
+
cmdb.start
|
60
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file) || CmdbService.start_deploy("#{cse_base}", deploy_unit_code, deploy_stage, tag)
|
61
|
+
puts "deploy_id=#{deploy_id}"
|
62
|
+
|
63
|
+
gitdeploy.deploy
|
64
|
+
tomcat.restart
|
65
|
+
|
66
|
+
cmdb.done
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + '/http')
|
7
|
+
|
8
|
+
class CmdbService
|
9
|
+
|
10
|
+
def self.get_app_role(cse_base, unit_code, stage)
|
11
|
+
url = "#{cse_base}/deploy/get-server.do?deployUnitCode=#{unit_code}&stage=#{stage}"
|
12
|
+
resp = Net::HTTP.get_response(URI.parse(url))
|
13
|
+
data = resp.body
|
14
|
+
|
15
|
+
# we convert the returned JSON data to native Ruby
|
16
|
+
# data structure - a hash
|
17
|
+
result = JSON.parse(data)
|
18
|
+
|
19
|
+
# if the hash has 'Error' as a key, we raise an error
|
20
|
+
if !result.has_key? 'success'
|
21
|
+
raise "web service error"
|
22
|
+
end
|
23
|
+
|
24
|
+
unless result['success']
|
25
|
+
raise "#{unit_code}@#{stage} get_app_role faile! return data: #{data}"
|
26
|
+
end
|
27
|
+
|
28
|
+
server = result['servers']
|
29
|
+
if server.nil?
|
30
|
+
raise "Not fout servers for #{unit_code}@#{stage}"
|
31
|
+
end
|
32
|
+
|
33
|
+
server.collect {|s| "#{s['ip']}:#{s['sshPort']}"}
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def self.start_deploy(cse_base, unit_code, stage, version)
|
38
|
+
url = URI.parse("#{cse_base}/deploy/start-deploy.do")
|
39
|
+
puts "version=#{version}"
|
40
|
+
param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => version }
|
41
|
+
|
42
|
+
http = Net::HTTP.new(url.host, url.port)
|
43
|
+
|
44
|
+
resp = http.post(url.path, param.to_json)
|
45
|
+
data = resp.body
|
46
|
+
|
47
|
+
result = JSON.parse(data)
|
48
|
+
|
49
|
+
if !result.has_key? 'success'
|
50
|
+
raise "web service error"
|
51
|
+
end
|
52
|
+
|
53
|
+
unless result['success']
|
54
|
+
raise "#{unit_code}@#{stage} start_deploy faile! return data: #{data}"
|
55
|
+
end
|
56
|
+
|
57
|
+
result['deploymentId']
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
#deployUnitCode : 发布单元编号
|
62
|
+
#deploymentId : 开始发布接口生成的唯一标识
|
63
|
+
#success : 是否发布成功
|
64
|
+
#description : 发布结果描述
|
65
|
+
def self.complete_deploy(cse_base, unit_code, deployment_id, is_success, description)
|
66
|
+
url = URI.parse("#{cse_base}/deploy/complete-deploy.do")
|
67
|
+
|
68
|
+
param = { 'deployUnitCode' => unit_code, 'deploymentId' => deployment_id,
|
69
|
+
'success' => is_success, 'description' => description }
|
70
|
+
|
71
|
+
http = Net::HTTP.new(url.host, url.port)
|
72
|
+
|
73
|
+
resp = http.post(url.path, param.to_json)
|
74
|
+
data = resp.body
|
75
|
+
|
76
|
+
result = JSON.parse(data)
|
77
|
+
|
78
|
+
# if the hash has 'Error' as a key, we raise an error
|
79
|
+
if !result.has_key? 'success'
|
80
|
+
raise "web service error"
|
81
|
+
end
|
82
|
+
|
83
|
+
unless result['success']
|
84
|
+
puts "#{unit_code}@#{deployment_id} complete_deploy faile! return data: #{data}"
|
85
|
+
return false
|
86
|
+
end
|
87
|
+
|
88
|
+
return true
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def self.get_deploy_id(file)
|
93
|
+
unless File.exists? file
|
94
|
+
puts "file #{file} NOT exists!"
|
95
|
+
return nil
|
96
|
+
end
|
97
|
+
|
98
|
+
deploy_id = nil
|
99
|
+
open(file, 'r') do |f|
|
100
|
+
deploy_id = f.readline
|
101
|
+
#if f.lines.count == 0
|
102
|
+
# puts "empty deploy_id, please delete file: #{file}."
|
103
|
+
#else
|
104
|
+
# deploy_id = f.readline
|
105
|
+
#end
|
106
|
+
end
|
107
|
+
deploy_id
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri' # there be bugs here
|
4
|
+
require "addressable/uri" # no more URI::InvalidURIError: bad URI(is not URI?)
|
5
|
+
|
6
|
+
class HTTP
|
7
|
+
class << self
|
8
|
+
def get(url, options = {})
|
9
|
+
execute(url, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def post(url, options = {})
|
13
|
+
options = { :method => :post }.merge(options)
|
14
|
+
execute(url, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def encoding(response)
|
18
|
+
return $1.downcase if response['content-type'] =~ /charset=(.*)/i
|
19
|
+
return $1.downcase if response.body =~ /<meta.*?charset=([^"'>]+)/mi
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def proxy
|
25
|
+
http_proxy = ENV["http_proxy"]
|
26
|
+
Addressable::URI.parse(http_proxy) rescue nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_uri(url)
|
30
|
+
if !url.respond_to?(:scheme)
|
31
|
+
url = Addressable::URI.parse(url)
|
32
|
+
end
|
33
|
+
url
|
34
|
+
end
|
35
|
+
|
36
|
+
def execute(url, options = {})
|
37
|
+
options = { :parameters => {}, :debug => false, :follow_redirects => true,
|
38
|
+
:http_timeout => 60, :method => :get,
|
39
|
+
:headers => {}, :redirect_count => 0,
|
40
|
+
:max_redirects => 10 }.merge(options)
|
41
|
+
|
42
|
+
url = to_uri(url)
|
43
|
+
|
44
|
+
if proxy
|
45
|
+
http = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port)
|
46
|
+
else
|
47
|
+
http = Net::HTTP.new(url.host, url.port)
|
48
|
+
end
|
49
|
+
|
50
|
+
if url.scheme == 'https'
|
51
|
+
http.use_ssl = true
|
52
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
53
|
+
end
|
54
|
+
|
55
|
+
http.open_timeout = http.read_timeout = options[:http_timeout]
|
56
|
+
|
57
|
+
http.set_debug_output $stderr if options[:debug]
|
58
|
+
|
59
|
+
request = case options[:method]
|
60
|
+
when :post
|
61
|
+
request = Net::HTTP::Post.new(url.request_uri)
|
62
|
+
request.set_form_data(options[:parameters])
|
63
|
+
request
|
64
|
+
else
|
65
|
+
Net::HTTP::Get.new(url.request_uri)
|
66
|
+
end
|
67
|
+
|
68
|
+
options[:headers].each { |key, value| request[key] = value }
|
69
|
+
response = http.request(request)
|
70
|
+
|
71
|
+
# Handle redirection
|
72
|
+
if options[:follow_redirects] && response.kind_of?(Net::HTTPRedirection)
|
73
|
+
options[:redirect_count] += 1
|
74
|
+
|
75
|
+
if options[:redirect_count] > options[:max_redirects]
|
76
|
+
raise "Too many redirects (#{options[:redirect_count]}): #{url}"
|
77
|
+
end
|
78
|
+
|
79
|
+
redirect_url = redirect_url(response)
|
80
|
+
|
81
|
+
if redirect_url.start_with?('/')
|
82
|
+
url = to_uri("#{url.scheme}://#{url.host}#{redirect_url}")
|
83
|
+
else
|
84
|
+
url = to_uri(redirect_url)
|
85
|
+
end
|
86
|
+
|
87
|
+
response, url = execute(url, options)
|
88
|
+
end
|
89
|
+
|
90
|
+
[response, url.to_s]
|
91
|
+
end
|
92
|
+
|
93
|
+
# From http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
|
94
|
+
def redirect_url(response)
|
95
|
+
if response['location'].nil?
|
96
|
+
response.body.match(/<a href=\"([^>]+)\">/i)[1]
|
97
|
+
else
|
98
|
+
response['location']
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/utilities')
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + '/cmdbutils')
|
6
|
+
|
7
|
+
include FileUtils
|
8
|
+
|
9
|
+
Capistrano::Configuration.instance(true).load do |configuration|
|
10
|
+
|
11
|
+
# 为测试环境发布使用
|
12
|
+
_cset :build_workspace, ""
|
13
|
+
_cset :release_dir, ""
|
14
|
+
_cset :release_pattern, "*.war"
|
15
|
+
_cset :upload_dir, ""
|
16
|
+
_cset :shell_commands, "cd #{upload_dir}; ls -all"
|
17
|
+
|
18
|
+
namespace :sdpjenkins do
|
19
|
+
|
20
|
+
task :start do
|
21
|
+
if File.exists? deploy_id_file
|
22
|
+
puts "Previous Deploy NOT Complete, please run 'cap cmdb:failback' first."
|
23
|
+
exit(1)
|
24
|
+
end
|
25
|
+
|
26
|
+
deploy_id = CmdbService.start_deploy("#{cse_base}", deploy_unit_code, deploy_stage, tag)
|
27
|
+
|
28
|
+
open(deploy_id_file, 'w') do |f|
|
29
|
+
f.write deploy_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "set current deploy FAILBACK!"
|
34
|
+
task :failback do
|
35
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file)
|
36
|
+
unless deploy_id.nil?
|
37
|
+
CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_id, false, "capistrano部署失败,撤销发布")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "set current deploy success DONE!"
|
42
|
+
task :done do
|
43
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file)
|
44
|
+
puts "deploy_id=#{deploy_id}, file=#{deploy_id_file}"
|
45
|
+
CmdbService.complete_deploy(cse_base, deploy_unit_code, deploy_id, true, "通过capistrano部署成功")
|
46
|
+
File.delete deploy_id_file
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "deploy to tomcat"
|
50
|
+
task :deploy_to_tomcat do
|
51
|
+
cmdb.start
|
52
|
+
deploy_id = CmdbService.get_deploy_id(deploy_id_file) || CmdbService.start_deploy("#{cse_base}", deploy_unit_code, deploy_stage, tag)
|
53
|
+
puts "deploy_id=#{deploy_id}"
|
54
|
+
|
55
|
+
gitdeploy.deploy
|
56
|
+
tomcat.restart
|
57
|
+
|
58
|
+
cmdb.done
|
59
|
+
end
|
60
|
+
|
61
|
+
desc "copy all war to specify dir"
|
62
|
+
task :copy_to_dir do
|
63
|
+
if build_workspace.empty?
|
64
|
+
puts "Please specify the build_workspace dir, set :build_workspace, '/home/foo/project/code'"
|
65
|
+
exit(1)
|
66
|
+
end
|
67
|
+
if release_dir.empty?
|
68
|
+
puts "Please specify the release_dir, set :release_dir, '/home/foo/project/release'"
|
69
|
+
exit(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
unless File.directory?(build_workspace)
|
73
|
+
puts "the build_workspace #{build_workspace} NOT exists!"
|
74
|
+
exit(1)
|
75
|
+
end
|
76
|
+
|
77
|
+
unless File.directory?(release_dir)
|
78
|
+
mkdir_p release_dir
|
79
|
+
end
|
80
|
+
|
81
|
+
files = []
|
82
|
+
release_pattern.split(/[,;\s]+/).each do |pattern|
|
83
|
+
files += Dir[File.join(build_workspace.split(/\\/), "**", pattern)]
|
84
|
+
end
|
85
|
+
|
86
|
+
files.each do |file|
|
87
|
+
target_name = File.basename(file).gsub(/-((\d+)\.?)+(-SNAPSHOT)?/, "")
|
88
|
+
cp file, "#{release_dir}/#{target_name}"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
desc "upload released file to remote hosts"
|
94
|
+
task :upload_file do
|
95
|
+
if upload_dir.empty?
|
96
|
+
puts "Please specify the remote upload_dir, set :upload_dir, '/opt/applications/project/upload'"
|
97
|
+
exit(1)
|
98
|
+
end
|
99
|
+
run "mkdir -p #{upload_dir}"
|
100
|
+
Dir[File.join(release_dir.split(/\\/), "**")].each do |file|
|
101
|
+
top.upload file, upload_dir, :via => :scp
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "execute commands from remote hosts"
|
106
|
+
task :execute_commands do
|
107
|
+
unless shell_commands.empty?
|
108
|
+
run shell_commands
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
desc "copy file, upload it, then execute commands"
|
113
|
+
task :doall do
|
114
|
+
copy_to_dir
|
115
|
+
upload_file
|
116
|
+
execute_commands
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
class CmdbService
|
7
|
+
|
8
|
+
def self.get_app_role(cse_base, unit_code, stage)
|
9
|
+
url = "#{cse_base}/deploy/get-server.do?deployUnitCode=#{unit_code}&stage=#{stage}"
|
10
|
+
resp = Net::HTTP.get_response(URI.parse(url))
|
11
|
+
data = resp.body
|
12
|
+
|
13
|
+
# we convert the returned JSON data to native Ruby
|
14
|
+
# data structure - a hash
|
15
|
+
result = JSON.parse(data)
|
16
|
+
|
17
|
+
# if the hash has 'Error' as a key, we raise an error
|
18
|
+
if !result.has_key? 'success'
|
19
|
+
raise "web service error"
|
20
|
+
end
|
21
|
+
|
22
|
+
unless result['success']
|
23
|
+
raise "#{unit_code}@#{stage} get_app_role faile!"
|
24
|
+
end
|
25
|
+
|
26
|
+
server = result['servers']
|
27
|
+
if server.nil?
|
28
|
+
raise "Not fout servers for #{unit_code}@#{stage}"
|
29
|
+
end
|
30
|
+
|
31
|
+
server.collect {|s| "#{s['ip']}:#{s['sshPort']}"}
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def self.start_deploy(cse_base, unit_code, stage)
|
36
|
+
url = URI.parse("#{cse_base}/deploy/start-deploy.do")
|
37
|
+
|
38
|
+
param = { 'deployUnitCode' => unit_code, 'stage' => stage, 'deployer' => 'capistrano', 'version' => '1.0.1' }
|
39
|
+
|
40
|
+
http = Net::HTTP.new(url.host, url.port)
|
41
|
+
|
42
|
+
resp = http.post(url.path, param.to_json)
|
43
|
+
data = resp.body
|
44
|
+
|
45
|
+
puts "data=#{data}"
|
46
|
+
# we convert the returned JSON data to native Ruby
|
47
|
+
# data structure - a hash
|
48
|
+
result = JSON.parse(data)
|
49
|
+
|
50
|
+
# if the hash has 'Error' as a key, we raise an error
|
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!"
|
57
|
+
end
|
58
|
+
|
59
|
+
result['deploymentId']
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
#deployUnitCode : 发布单元编号
|
64
|
+
#deploymentId : 开始发布接口生成的唯一标识
|
65
|
+
#success : 是否发布成功
|
66
|
+
#description : 发布结果描述
|
67
|
+
def self.complete_deploy(cse_base, unit_code, deployment_id, is_success, description)
|
68
|
+
url = URI.parse("#{cse_base}/deploy/complete-deploy.do")
|
69
|
+
|
70
|
+
param = { 'deployUnitCode' => unit_code, 'deploymentId' => deployment_id,
|
71
|
+
'success' => is_success, 'description' => description }
|
72
|
+
|
73
|
+
http = Net::HTTP.new(url.host, url.port)
|
74
|
+
|
75
|
+
resp = http.post(url.path, param.to_json)
|
76
|
+
data = resp.body
|
77
|
+
|
78
|
+
puts "data=#{data}"
|
79
|
+
# we convert the returned JSON data to native Ruby
|
80
|
+
# data structure - a hash
|
81
|
+
result = JSON.parse(data)
|
82
|
+
|
83
|
+
# if the hash has 'Error' as a key, we raise an error
|
84
|
+
if !result.has_key? 'success'
|
85
|
+
raise "web service error"
|
86
|
+
end
|
87
|
+
|
88
|
+
unless result['success']
|
89
|
+
puts "#{unit_code}@#{stage} start_deploy faile!"
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
|
93
|
+
return true
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
s = CmdbService.get_app_role("http://10.241.14.166:8080/cse", "zf00032", "test")
|
99
|
+
puts "s=#{s}"
|
100
|
+
|
101
|
+
did = CmdbService.start_deploy("http://10.241.14.166:8080/cse", "zf00032", "test")
|
102
|
+
puts "deploy=#{did}"
|
103
|
+
|
104
|
+
r = CmdbService.complete_deploy("http://10.241.14.166:8080/cse", "zf00032", did, true, "发布成功")
|
105
|
+
puts "r=#{r}"
|
metadata
CHANGED
@@ -4,10 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.3.39.3
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- crazycode
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-
|
17
|
+
date: 2011-08-22 00:00:00 +08:00
|
19
18
|
default_executable: cap-recipes
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -29,12 +28,14 @@ extra_rdoc_files:
|
|
29
28
|
- LICENSE
|
30
29
|
- README.textile
|
31
30
|
files:
|
31
|
+
- Capfile
|
32
32
|
- LICENSE
|
33
33
|
- README.textile
|
34
34
|
- Rakefile
|
35
35
|
- VERSION.yml
|
36
36
|
- bin/cap-recipes
|
37
37
|
- cap-recipes.gemspec
|
38
|
+
- config/deploy.rb
|
38
39
|
- crazycode-cap-recipes.gemspec
|
39
40
|
- examples/advanced/deploy.rb
|
40
41
|
- examples/advanced/deploy/experimental.rb
|
@@ -51,6 +52,8 @@ files:
|
|
51
52
|
- lib/cap_recipes/tasks/backgroundrb/manage.rb
|
52
53
|
- lib/cap_recipes/tasks/bundler.rb
|
53
54
|
- lib/cap_recipes/tasks/bundler/manage.rb
|
55
|
+
- lib/cap_recipes/tasks/cmdb.rb
|
56
|
+
- lib/cap_recipes/tasks/cmdbutils.rb
|
54
57
|
- lib/cap_recipes/tasks/delayed_job.rb
|
55
58
|
- lib/cap_recipes/tasks/delayed_job/hooks.rb
|
56
59
|
- lib/cap_recipes/tasks/delayed_job/manage.rb
|
@@ -61,6 +64,7 @@ files:
|
|
61
64
|
- lib/cap_recipes/tasks/gitosis.rb
|
62
65
|
- lib/cap_recipes/tasks/gitosis/install.rb
|
63
66
|
- lib/cap_recipes/tasks/gitosis/manage.rb
|
67
|
+
- lib/cap_recipes/tasks/http.rb
|
64
68
|
- lib/cap_recipes/tasks/hudson.rb
|
65
69
|
- lib/cap_recipes/tasks/hudson/manage.rb
|
66
70
|
- lib/cap_recipes/tasks/jetty.rb
|
@@ -89,6 +93,7 @@ files:
|
|
89
93
|
- lib/cap_recipes/tasks/rubygems.rb
|
90
94
|
- lib/cap_recipes/tasks/rubygems/install.rb
|
91
95
|
- lib/cap_recipes/tasks/rubygems/manage.rb
|
96
|
+
- lib/cap_recipes/tasks/sdpjenkins.rb
|
92
97
|
- lib/cap_recipes/tasks/templates/hudson.erb
|
93
98
|
- lib/cap_recipes/tasks/templates/mongod.conf.erb
|
94
99
|
- lib/cap_recipes/tasks/templates/mongodb.init.erb
|
@@ -117,6 +122,7 @@ files:
|
|
117
122
|
- spec/cap_recipes_spec.rb
|
118
123
|
- spec/spec_helper.rb
|
119
124
|
- specs.watchr
|
125
|
+
- test.rb
|
120
126
|
has_rdoc: true
|
121
127
|
homepage: http://github.com/crazycode/cap-recipes
|
122
128
|
licenses: []
|