-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauto-deploy
More file actions
194 lines (167 loc) · 4.3 KB
/
auto-deploy
File metadata and controls
194 lines (167 loc) · 4.3 KB
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
use Config::Simple;
use Data::Dumper;
use File::Copy;
use strict;
use Getopt::Long;
my @overrides;
my $target;
my @modules;
my $rc = GetOptions("target=s" => \$target,
"module=s" => \@modules,
"override=s" => \@overrides);
@ARGV == 2 || @ARGV == 1 or die "Usage: $0 [--module module-dir] [--override key=value] deploy.cfg tag\n";
my $cfg_file = shift;
my $tag = shift;
my $cfg = Config::Simple->new();
$cfg->read($cfg_file);
if ($target)
{
#
# Need to override the config param, since code elsewhere uses it.
#
$cfg->param('target', $target);
}
else
{
$target = $cfg->param('target');
}
my $runtime = $cfg->param('deploy-runtime');
my $deployment_config_name = "deployment.cfg";
my @deploy_servers = ($cfg->param('deploy-service'), $cfg->param('deploy-server'));
my @deploy_clients = $cfg->param('deploy-client');
my @deploy_master = $cfg->param('deploy-master');
my %modules;
if (@modules)
{
%modules = map { $_ => 1 } @modules;
}
else
{
%modules = map { $_ => 1 } (@deploy_servers, @deploy_clients, @deploy_master);
}
my @default_settings = make_settings($cfg, 'default');
#
# Apply overrides to the default settings.
#
for my $o (@overrides)
{
if ($o =~ /.+=.+/)
{
push(@default_settings, $o);
}
else
{
die "Invalid override $o\n";
}
}
chmod(0400, "id_rsa");
#
# Write default environment information to a file in the
# dev container for the use of the service startup
# templates.
#
my %vars = (kb_top => $target,
kb_runtime => $runtime,
);
open(F, ">", "common-envars.tt") or die "Cannot write common-envars.tt: $!";
while (my($var, $val) = each %vars)
{
print F "[% $var = '$val' %]\n";
}
print F <<END;
[% MACRO standard_envars(kb_service_name) BLOCK %]
export KB_TOP="[% kb_top %]"
export KB_RUNTIME="[% kb_runtime %]"
export KB_DEPLOYMENT_CONFIG="[% kb_top %]/$deployment_config_name"
export PATH="\$KB_TOP/bin:\$KB_RUNTIME/bin:\$PATH"
export PERL5LIB=\$KB_TOP/lib
[% IF kb_service_name != "" %]
export KB_SERVICE_DIR="\$KB_TOP/services/[% kb_service_name %]"
export KB_SERVICE_NAME="[% kb_service_name %]"
[% END %]
[% END %]
END
close(F);
for my $module (@deploy_servers, @deploy_clients, @deploy_master)
{
next unless $modules{$module};
if (-d "modules/$module")
{
warn "$module is already checked out\n";
next;
}
my $gitfile = "$module.git";
my @cmd = ('ssh-agent', 'bash', '-c', "ssh-add id_rsa; cd modules; git clone kbase\@git.kbase.us:/$gitfile $module; cd $module; git checkout $tag; cd ..");
print Dumper(\@cmd);
my $rc = system(@cmd);
if ($rc != 0)
{
die "failed with rc=$rc: @cmd\n";
}
}
my @cmd = ("make", "deploy-setup", @default_settings);
my $rc = system(@cmd);
if ($rc != 0)
{
die "failed with rc=$rc: @cmd\n";
}
for my $server (@deploy_master)
{
next unless $modules{$server};
print "DEPLOY master $server\n";
my @settings = make_settings($cfg, $server);
my @cmd = ("make", "-C", "modules/$server", "deploy", @default_settings, @settings);
print Dumper(\@cmd);
my $rc = system(@cmd);
if ($rc != 0)
{
die "failed with rc=$rc: @cmd\n";
}
}
for my $server (@deploy_servers)
{
next unless $modules{$server};
print "DEPLOY server $server\n";
my @settings = make_settings($cfg, $server);
my @cmd = ("make", "-C", "modules/$server", "deploy-service", @default_settings, @settings);
print Dumper(\@cmd);
my $rc = system(@cmd);
if ($rc != 0)
{
die "failed with rc=$rc: @cmd\n";
}
}
for my $client (@deploy_clients)
{
next unless $modules{$client};
print "DEPLOY client $client\n";
my @settings = make_settings($cfg, $client);
my @cmd = ("make", "-C", "modules/$client", "deploy-client", @default_settings, @settings);
print Dumper(\@cmd);
my $rc = system(@cmd);
if ($rc != 0)
{
die "failed with rc=$rc: @cmd\n";
}
}
#
# Copy our deployment config to the target directory.
#
copy($cfg_file, "$target/$deployment_config_name") or die "Cannot copy $cfg_file to $target/$deployment_config_name";
sub make_settings
{
my($cfg, $module) = @_;
my @out;
my $blk = $cfg->get_block($module);
while (my($n, $val) = each %$blk)
{
my $var = uc($n);
$var =~ s/-/_/g;
if (ref($val) eq 'ARRAY')
{
$val = join(",", @$val);
}
push(@out, join("=", $var, $val));
}
return @out;
}