-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdigital_ocean.pl
194 lines (162 loc) · 5.58 KB
/
digital_ocean.pl
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
#!/usr/bin/perl
use 5.010;
use Moo;
use MooX::Options;
use Config::Tiny;
use Data::Dumper qw(Dumper);
use DigitalOcean;
use File::HomeDir;
option list => (is => 'ro', doc => 'List available droplets');
option verbose => (is => 'ro');
option droplet => (is => 'rw', format => 's', doc => 'Droplet ID - Use this Droplet instead of creating new');
option create => (is => 'ro');
option base => (is => 'ro', format => 'i', doc => 'Base the build on this subversion (1, 2, ...)');
option full => (is => 'ro', doc => 'Create the whole thing from scratch');
#option perl => (is => 'ro', doc => 'Create from scratch but only Perl, the external dependencies and the minimum requried');
has config_file => (is => 'rw');
has tag => (is => 'rw');
main->new_with_options->run;
exit;
sub run {
my ($self) = @_;
$self->config_file(File::HomeDir->my_home . '/.digitalocean');
#$self->tag('experiment-0.01');
my $url = 'https://github.com/dwimperl/dwimperl-linux/archive';
$self->tag('master');
my $zip_file = $self->tag . '.zip';
my $bare_file = $self->tag;
my $dir = 'dwimperl-linux-' . $self->tag;
$self->usage('Missing config') if !-e $self->config_file;
say "\$zip_file=$zip_file" if $self->verbose;
say "\$dir=$dir" if $self->verbose;
my $Config = Config::Tiny->read( $self->config_file, 'utf8' );
my $ssh_key_id = delete $Config->{one}{ssh_key_id};
my $do = DigitalOcean->new(%{ $Config->{one} });
if ($self->list) {
say 'Droplets';
for my $droplet (@{ $do->droplets }) {
printf "Droplet %s has id %s and IP address %s\n", $droplet->name, $droplet->id, $droplet->ip_address;
}
say '------';
say 'Images';
foreach my $img (sort { $a->distribution cmp $b->distribution or $a->name cmp $b->name } @{ $do->images }) {
printf "%-10s %7s %-50s - %s\n", $img->distribution, $img->id, $img->name, ($img->slug || '');
}
say '------';
return;
}
die "Exactly on of --create, --droplet ID, --list, --help\n" if not $self->create xor $self->droplet;
if (not defined $self->base and not $self->full) {
die "--base N or --full is required\n"
}
if ($self->create) {
printf "Creating droplet. Can take about 60 seconds. Started at %s, Please wait. \n", scalar localtime;
my $t0 = time;
my $droplet = $do->create_droplet(
name => 'dwimperl',
size_id => 66, # 512Mb
#image_id => '1601', # CentOS 5.8 x64
#image_id => 6344382, # CentOS 5.10 x64
image_id => 6372321, #CentOS 5.10 x64
ssh_key_ids => $ssh_key_id,
region_id => 8, # New York 3
wait_on_event => 1,
);
my $t1 = time;
say 'Elapsed time creating the Droplet: ' . ($t1-$t0) . ' seconds';
$self->droplet( $droplet->id );
}
my $server = $do->droplet($self->droplet);
printf "ID: %s name %s IP: %s\n", $server->id, $server->name, $server->ip_address;
my $username = 'dwimperl';
my @root_cmds = (
"adduser $username",
"cp -r .ssh/ /home/$username/",
'yum -y install make.x86_64',
'yum -y install gcc.x86_64'
);
$self->ssh('root', $server->ip_address, \@root_cmds);
my @user_cmds = (
"rm -rf $dir $bare_file $zip_file", # remove old files (mostly interesting when reusing the same VPS)
"wget $url/$zip_file",
# for some reason wget on the old CentOS will download a file called master even when we ask for master.zip
# that's what we are fixing by renaming bare_file to zip_file
"[ -e $bare_file ] && mv $bare_file $zip_file",
"unzip $zip_file",
);
my $pref = '';
if ($self->base) {
$pref .= ' DWIM_BASE_VERSION=' . $self->base;
}
if ($self->full) {
# build perl from scratch with cpanm, only installing modules that need special treatment
push @user_cmds, map { "cd $dir; $pref ./build.sh $_" } (
'perl',
'external',
'cpanm',
'dwim',
'special_modules',
'modules',
'test_perl',
'zip',
);
} elsif ($self->base) {
# based on an earlier release
push @user_cmds, map { "cd $dir; $pref ./build.sh $_" } (
'get_base_perl',
#'dwim',
#'special_modules',
'geoip',
'modules',
'test_cpanfile',
#'test_all',
'zip',
);
} else {
die "Hmm, how comes?\n";
}
my $results = $self->ssh($username, $server->ip_address, \@user_cmds);
my ($remote_filename) = map { substr $_, 19 } grep { /^GENERATED_ZIP_FILE=/ } @{ $results->[-1] };
chomp $remote_filename;
say "\$remote_filename='$remote_filename'" if $self->verbose;
(my $local_filename = $remote_filename) =~ s{^.*/}{};
say "\$local_filename='$local_filename'" if $self->verbose;
# download the zip file
my $cmd = sprintf 'scp -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s:%s %s', $username, $server->ip_address, $remote_filename, $local_filename;
say $cmd if $self->verbose;
system $cmd;
if ($self->create) {
say 'Destroying the server';
$server->destroy;
}
}
sub ssh {
my ($self, $username, $ip_address, $cmds) = @_;
my @results;
foreach my $cmd (@$cmds) {
say "===================" if $self->verbose;
my $full_cmd = sprintf 'ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s "%s"', $username, $ip_address, $cmd;
say "\$full_cmd=$full_cmd" if $self->verbose;
my @out = qx{$full_cmd};
say '@out:' if $self->verbose;
print @out if $self->verbose;
say '---';
push @results, \@out;
}
return \@results;
}
sub usage {
my ($self, $msg) = @_;
say $msg if $msg;
my $config_file = $self->config_file;
print <<"END_USAGE";
Usage: $0
In order to access the DigitaOcean API we need the API keys in $config_file.
[one]
client_id = ...
api_key = ...
ssh_key_id = ...
See also http://perlmaven.com/cloud-automation-at-digital-ocean
END_USAGE
exit 1;
}