line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 tw=0: |
5
|
|
|
|
|
|
|
# vim: set expandtab: |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Rex::Apache::Deploy::Git - Deploy applications with Git |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 DESCRIPTION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
This module gives you a simple interface to Git based deployments. It uses I to upload a given commit to the server. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Rex::Apache::Deploy qw/Git/; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
task "deploy", "server1", "server2", sub { |
20
|
|
|
|
|
|
|
my $param = shift; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
deploy $param->{commit}, |
23
|
|
|
|
|
|
|
path => "/var/www", |
24
|
|
|
|
|
|
|
switch => TRUE; |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#bash# rex deploy --commit=385816 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
task "rollback", "server1", "server2", sub { |
30
|
|
|
|
|
|
|
my $param = shift; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
switch_to_version $param->{commit}; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#bash# rex rollback --commit=138274 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
package Rex::Apache::Deploy::Git; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
1470
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
43
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
1
|
|
5
|
use Rex -base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
1
|
|
2832
|
use vars qw(@EXPORT); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
467
|
|
48
|
|
|
|
|
|
|
@EXPORT = qw(deploy switch_to_version); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub deploy { |
51
|
0
|
|
|
0
|
0
|
|
my ( $commit, %option ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
|
if ( !$commit ) { |
54
|
0
|
|
|
|
|
|
my %task_args = Rex::Args->get; |
55
|
0
|
0
|
|
|
|
|
if ( exists $task_args{commit} ) { |
56
|
0
|
|
|
|
|
|
$commit = $task_args{commit}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
0
|
|
|
|
|
|
print "Usage: rex \$task --commit=git-hash\n"; |
60
|
0
|
|
|
|
|
|
die("You have to give the commit you wish to deploy."); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
if ( !%option ) { |
65
|
0
|
0
|
|
|
|
|
if ( Rex::Config->get("package_option") ) { |
66
|
0
|
|
|
|
|
|
%option = %{ Rex::Config->get("package_option") }; |
|
0
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $commit_to_deploy = $commit; |
71
|
0
|
|
|
|
|
|
my $repo_path = $option{path}; |
72
|
0
|
0
|
|
|
|
|
my $force = ( $option{force} ? "-f" : "" ); |
73
|
0
|
|
|
|
|
|
my $switch = $option{switch}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
run "git init $repo_path"; |
76
|
0
|
|
|
|
|
|
run "GIT_DIR=$repo_path/.git git config receive.denyCurrentBranch ignore"; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $server = connection->server; |
79
|
0
|
|
0
|
|
|
|
my $user = $option{user} || Rex::Config->get_user; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
LOCAL { |
82
|
0
|
|
|
0
|
|
|
run |
83
|
|
|
|
|
|
|
"git push git+ssh://$user\@$server$repo_path $commit_to_deploy:refs/heads/master $force"; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
if ( $? != 0 ) { |
86
|
0
|
|
|
|
|
|
die("Error pushing refs."); |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
|
if ($switch) { |
91
|
0
|
|
|
|
|
|
switch_to_version( $commit_to_deploy, %option ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub switch_to_version { |
96
|
0
|
|
|
0
|
0
|
|
my ( $version, %option ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $repo_path = $option{path}; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
run "cd $repo_path && git reset --hard $version"; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if ( $? != 0 ) { |
103
|
0
|
|
|
|
|
|
die("Error switching to $version"); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub import { |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
101
|
|
110
|
0
|
|
|
0
|
|
|
for my $func (@EXPORT) { |
111
|
0
|
|
|
|
|
|
Rex::Logger::debug("Registering main::$func"); |
112
|
0
|
|
|
|
|
|
*{"$_[1]::$func"} = \&$func; |
|
0
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |