| 1 | # | ||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||
| 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<git push> 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 1 | ▶ | use Rex -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 1 | @EXPORT = qw(deploy switch_to_version); | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | sub deploy { | ||||||||||||||||||
|
Subroutine
deploy: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 51 | 0 | my ( $commit, %option ) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | if ( !$commit ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | my %task_args = Rex::Args->get; | |||||||||||||||||||
| 55 | 0 | ▶ | if ( exists $task_args{commit} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if ( !%option ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | ▶ | if ( Rex::Config->get("package_option") ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | %option = %{ Rex::Config->get("package_option") }; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | my $commit_to_deploy = $commit; | |||||||||||||||||||
| 71 | 0 | my $repo_path = $option{path}; | |||||||||||||||||||
| 72 | 0 | ▶ | my $force = ( $option{force} ? "-f" : "" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | my $user = $option{user} || Rex::Config->get_user; | ||||||||||||||||||
|
Condition0%0/3
$option{'user'} || 'Rex::Config'->get_user
MC/DC0%0/2
$option{'user'} || 'Rex::Config'->get_user
$option{'user'}
'Rex::Config'->get_user
Truth table0%0/3
A || B
A= $option{'user'}B= 'Rex::Config'->get_user
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | LOCAL { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 82 | 0 | run | |||||||||||||||||||
| 83 | "git push git+ssh://$user\@$server$repo_path $commit_to_deploy:refs/heads/master $force"; | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | die("Error pushing refs."); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 0 | }; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | if ($switch) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | switch_to_version( $commit_to_deploy, %option ); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub switch_to_version { | ||||||||||||||||||
|
Subroutine
switch_to_version: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 96 | 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 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | die("Error switching to $version"); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=10.1
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | 0 | for my $func (@EXPORT) { | |||||||||||||||||||
| 111 | 0 | Rex::Logger::debug("Registering main::$func"); | |||||||||||||||||||
| 112 | 0 | *{"$_[1]::$func"} = \&$func; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||