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
|
# File 'lib/capistrano/isitdeployed.rb', line 19
def self.load_into(configuration)
configuration.load do
before 'deploy', 'isit:deploy'
after 'deploy', 'isit:status:success'
after 'deploy:rollback', 'isit:status:rollback'
after 'isit:status:success', 'isit:update'
after 'isit:status:rollback', 'isit:update'
namespace :isit do
desc "Creates a new deploy using the API"
task :deploy do
started = Capistrano::Isitdeployed.timestamp
set(:stage, "my platform") unless exists?(:stage)
RestClient.post(ENDPOINT + "/p/#{isitdeployed_project_id}/d", JSON.generate({ :status => 1, :platform => "#{stage}", :release => "#{release_name}", :started => started, :token => isitdeployed_api_secret, :version => VERSION }), :content_type => :json, :accept => :json, :timeout => 5, :open_timeout => 5){ |response, request, result| response
case response.code
when 201
logger.trace "IsItDeployed > New deploy created for #{application} to #{stage} with version: #{release_name}"
logger.trace "IsItDeployed > URL is #{ENDPOINT}/p/#{isitdeployed_project_id}"
json = JSON.parse(response)
if json['latest_version'] > VERSION
logger.trace "IsItDeployed > WARNING: Your 'capistrano-isitdeployed' gem is outdated. Consider upgrade to #{json['latest_version']}"
end
set(:isitdeployed_did, json['deploy_id'])
set(:isitdeployed_started, started)
set(:isitdeployed_created, 1)
when 401
logger.trace "IsItDeployed > ERROR: Invalid 'isitdeployed_api_secret'. Check your 'deploy.rb' file"
set(:isitdeployed_created, 0)
when 404
logger.trace "IsItDeployed > ERROR: Invalid 'isitdeployed_project_id'. Check your 'deploy.rb' file"
set(:isitdeployed_created, 0)
else
logger.trace "IsItDeployed > ERROR: Can't create deploy, server returned code #{response.code}"
set(:isitdeployed_created, 0)
end
}
end
namespace :status do
desc "Set the deploy status to success locally"
task :success do
set(:isitdeployed_status, 2)
end
desc "Set the deploy status to rollback locally"
task :rollback do
set(:isitdeployed_status, 3)
end
end
desc "Updates deploy status using the API (success or rollback)"
task :update do
if isitdeployed_created === 1
stopped = Capistrano::Isitdeployed.timestamp
duration = stopped.to_i - isitdeployed_started.to_i
RestClient.put(ENDPOINT + "/p/#{isitdeployed_project_id}/d/#{isitdeployed_did}", JSON.generate({ :status => isitdeployed_status, :stopped => stopped, :duration => duration, :token => isitdeployed_api_secret }), :content_type => :json, :accept => :json, :timeout => 5, :open_timeout => 5){ |response, request, result| response
case response.code
when 204
logger.trace "IsItDeployed > Deploy ##{isitdeployed_did} has been updated for #{application} to #{stage}."
when 401
logger.trace "IsItDeployed > ERROR: Invalid 'isitdeployed_api_secret'. Check your 'deploy.rb' file"
when 404
logger.trace "IsItDeployed > ERROR: Invalid 'isitdeployed_project_id'. Check your 'deploy.rb' file"
else
logger.trace "IsItDeployed > ERROR: Can't create deploy, server returned #{response.code}"
end
}
end
end
end
end
end
|