-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
executable file
·183 lines (161 loc) · 4.64 KB
/
deploy
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
#!/usr/bin/perl -w
use strict;
use Config::Tiny;
use Getopt::Long;
use Term::ANSIColor;
## This needs to be configured to the directory containing this script
#
my $deployDir = '/home/deploy/deployment-scripts/';
my $logFile = "$deployDir/deploy.log";
chdir "$deployDir";
my $dryRun = '';
## Easy pretty printing
#
sub write_log {
unless ($dryRun) {
open (MYFILE, ">>$logFile");
print MYFILE "$_[0]\n";
close (MYFILE);
}
}
sub say {
print color 'yellow';
print "$_[0]\n";
print color 'reset';
write_log $_[0];
}
sub badnews {
print color 'red';
print "$_[0]\n";
print color 'reset';
write_log $_[0];
exit 1;
}
######################################################
# START SCRIPT ##
######################################################
## Brief rundown: ##
## ##
## Options handling ##
## Validate command-line options ##
## Read config file and setup variables ##
## Go into the module, checkout the right commit ##
## Set the permissions ##
## Rsync the module ##
## ##
######################################################
my $command_line = "$0 ". (join " ", @ARGV);
## Options handling
#
my $tag = '';
my $branch = '';
my $help = '';
my $man = '';
# $dryRun is declared above as I use it in the log subrouting
my $environment = 'staging';
GetOptions('tag=s' => \$tag, # e.g. --tag 1.2
'branch=s' => \$branch, # e.g. --branch master
'environment=s' => \$environment, # e.g. --environment live
'dry-run|n' => \$dryRun); # e.g. --dryrun
write_log "\n$ARGV[0]: $command_line";
## Validate command-line options
#
unless ($ARGV[0]) {
badnews "You haven't specified a module to update";
}
if ($tag && $branch) {
badnews "Can't specify both tag and branch";
}
unless ($tag || $branch) {
badnews "You must specify a branch or a tag";
}
## Read config file and setup variables
#
my $depFile = "configs/$ARGV[0]";
unless (-f "$depFile") {
badnews "Deployment File $depFile not found";
}
my $config = Config::Tiny->read($depFile);
unless ($config->{$environment}) {
badnews "Environment $environment not defined";
}
my $module = $config->{_}->{module};
my $path = $config->{_}->{path};
my $owner = $config->{_}->{owner};
my $group = $config->{_}->{group};
my $sshUser = $config->{_}->{ssh_user};
my @includes;
if ($config->{_}->{includes}) {
@includes = split /\s+/, $config->{_}->{includes};
}
my @excludes;
if ($config->{_}->{excludes}) {
@excludes = split /\s+/, $config->{_}->{excludes};
}
my @hosts;
@hosts = split /\s+/, $config->{$environment}->{hosts};
## Go into the module, checkout the right commit
#
unless (-d "modules/$module") {
badnews "$module doesn't seem to be checked out";
}
say "Updating modules/$module";
chdir "modules/$module";
system("git fetch --all");
if ($tag) {
say ("Checking out tag $tag");
my $retVal = system("git checkout $tag");
if ($retVal != 0) {
badnews "Could not checkout tag $tag - are you sure it exists?";
}
} else {
say ("Checking out branch $branch");
system("git checkout -t $branch origin/branch 2>/dev/null");
my $retVal = system("git checkout $branch");
if ($retVal != 0) {
badnews "Could not checkout branch $branch - are you sure it exists?";
}
system("git pull origin $branch");
}
system("git submodule init");
system("git submodule update");
## Set the permissions
#
my $ownerString;
my $groupString;
if ($owner && $group) {
$ownerString = "$owner:$group"
} elsif ($owner) {
$ownerString = $owner;
} elsif ($group) {
$groupString = $group;
}
my $retVal;
if ($ownerString) {
say "chowning module to $ownerString";
$retVal = system("chown -R $ownerString .");
} elsif ($groupString) {
say "chgrping module to $groupString";
$retVal = system("chgrp -R $groupString .");
}
if ($retVal != 0) {
badnews "chown -R $ownerString failed";
}
## Rsync the module
#
my $includeStr = "";
foreach my $include (@includes) {
$includeStr .= " --include '$include'";
}
my $excludeStr = "--exclude '.git*'";
foreach my $exclude (@excludes) {
$excludeStr .= " --exclude '$exclude'";
}
foreach my $host (@hosts) {
my $args = 'avz';
if ($dryRun) {
$args .= 'n';
}
say("rsync -$args --delete $includeStr $excludeStr $deployDir/modules/$module $sshUser\@$host:$path");
system("rsync -$args --delete $includeStr $excludeStr $deployDir/modules/$module $sshUser\@$host:$path");
}