-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.xml
222 lines (176 loc) · 8.36 KB
/
build.xml
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?xml version="1.0" encoding="UTF-8"?>
<project name="rollback" description="utilities for rolling back deployments" default="info" >
<!-- rollback properties -->
<property name="rollback.direction" value="reverse"/>
<property name="rollback.depth" value="1" />
<property name="rollback.selected" value="false" />
<property name="rollback.sshlib" value="" />
<!-- ============================================ -->
<!-- Custom Taskdefs -->
<!-- ============================================ -->
<taskdef classpath="${phingistrano.dir}" classname="deploy.NetSshTask" name="netssh" />
<taskdef classpath="${phingistrano.dir}" classname="deploy.NetScpTask" name="netscp" />
<!-- ============================================ -->
<!-- Target: do -->
<!-- ============================================ -->
<target name="do" >
<echo msg="executing rollback... " />
<echo msg="============================ " />
<!-- transfer the file via scp -->
<foreach list="${deploy.servers}" param="rollback.server" target="rollback.deliver" />
<!-- cleanup -->
<delete file="${project.basedir}/pointer.tmp" />
<delete file="${project.basedir}/list.tmp" />
</target>
<!-- ============================================ -->
<!-- Target: download -->
<!-- ============================================ -->
<target name="download" >
<!-- take snapshot of all deployments -->
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="ls -t ${deploy.path}/releases > ${deploy.path}/list.tmp" />
<!-- take snapshot of current deployment pointer -->
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="ls -l ${deploy.path} | grep current > ${deploy.path}/pointer.tmp" />
<!-- download snapshots -->
<netscp username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
fetch="true"
todir="${project.basedir}"
file="${deploy.path}/list.tmp" />
<netscp username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
fetch="true"
todir="${project.basedir}"
file="${deploy.path}/pointer.tmp" />
<!-- delete temporary remote files -->
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="rm ${deploy.path}/*.tmp" />
</target>
<!-- ============================================ -->
<!-- Target: getlist -->
<!-- ============================================ -->
<target name="getlist" >
<!-- evaluate current deployment pointer -->
<exec dir="${project.basedir}"
outputProperty="rollback.current"
command="cat ${project.basedir}/pointer.tmp" />
<php expression="substr('${rollback.current}', (strrpos('${rollback.current}', '/'))+1)"
returnProperty="rollback.current" />
<echo msg="current: ${rollback.current}" />
<!-- normalize list -->
<exec dir="${project.basedir}"
outputProperty="rollback.deployments"
command="cat ${project.basedir}/list.tmp" />
<php function="preg_replace" returnProperty="rollback.deployments" >
<param value="/\s/" />
<param value="," />
<param value="${rollback.deployments}" />
</php>
<echo msg="deployment list: ${rollback.deployments}" />
<!-- identify position of current deployment -->
<php expression="array_search('${rollback.current}',explode(',','${rollback.deployments}'))+1"
returnProperty="rollback.position" />
<!-- get total deployments available -->
<php expression="count(explode(',','${rollback.deployments}'))"
returnProperty="rollback.count" />
<echo msg="list position of current: ${rollback.position} of ${rollback.count}" />
</target>
<!-- ============================================ -->
<!-- Target: deliver -->
<!-- ============================================ -->
<target name="deliver" depends="rollback.download,rollback.getlist" >
<!-- determine position of target deployment -->
<if>
<isfalse value="${rollback.selected}"/>
<then>
<echo msg="Determining the selected deployment" />
<if>
<not>
<equals arg1="${rollback.direction}" arg2="reverse" />
</not>
<then>
<php expression="${rollback.position}-${rollback.depth}"
returnProperty="rollback.selected" />
</then>
<else>
<php expression="${rollback.position}+${rollback.depth}"
returnProperty="rollback.selected" />
</else>
</if>
</then>
<else>
<echo msg="The deployment was selected by the user" />
</else>
</if>
<!-- protect miniumum and maximum selection parameters -->
<php expression="(${rollback.selected} > 0)?'true':'false'"
returnProperty="rollback.checkmin" />
<php expression="(${rollback.selected} <= ${rollback.count})?'true':'false'"
returnProperty="rollback.checkmax" />
<if>
<isfalse value="${rollback.checkmin}"/>
<then>
<property name="rollback.selected" value="1" />
</then>
</if>
<if>
<isfalse value="${rollback.checkmax}"/>
<then>
<property name="rollback.selected" value="${rollback.count}" />
</then>
</if>
<!-- assign the selected position -->
<php expression="current(array_slice(explode(',','${rollback.deployments}'),(${rollback.selected}-1),1))"
returnProperty="rollback.target" />
<echo msg="Rollback target determined to be: ${rollback.target}" />
<!-- link it -->
<echo msg="deleting symlink: ${deploy.deploy_to}/current" />
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="rm ${deploy.path}/current" />
<echo msg="creating new symlink: ${deploy.deploy_to}/current" />
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="ln -s ${deploy.path}/releases/${rollback.target}
${deploy.path}/current" />
<!-- restart memcached -->
<echo msg="restarting memcached" />
<netssh username="${deploy.user}"
password="${deploy.password}"
host="${rollback.server}"
sshlib="${rollback.sshlib}"
command="echo ${deploy.password} |
sudo -S /etc/init.d/memcached restart" />
</target>
<!-- ============================================ -->
<!-- Target: verbose -->
<!-- ============================================ -->
<target name="verbose" >
<echo msg="\\== Rolling back with the following properties ==//" />
<echo msg="Project: ${phing.project.name}" />
<echo msg="repository: ${deploy.repository}" />
<echo msg="user: ${deploy.user}" />
<echo msg="password: ${deploy.password}" />
<echo msg="branch: ${deploy.branch}" />
<echo msg="deploy path: ${deploy.path}" />
<echo msg="webservers: ${deploy.servers}" />
</target>
</project>