-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcloud-init.pl
executable file
·194 lines (157 loc) · 5.15 KB
/
cloud-init.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
194
#!/usr/bin/env perl
# Copyright (c) 2015 Pierre-Yves Ritschard
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use CPAN::Meta::YAML;
use HTTP::Tiny;
use File::Basename;
use File::Path qw(make_path mkpath);
use File::Temp qw(tempfile);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
use strict;
use constant {
METADATA_HOST => "169.254.169.254",
};
sub get_data {
my ($host, $path) = @_;
my $response = HTTP::Tiny->new->get("http://$host/latest/$path");
return unless $response->{success};
return $response->{content};
}
sub get_default_fqdn {
my $host = METADATA_HOST;
my $local_hostname = get_data($host, 'meta-data/local-hostname');
return $local_hostname . '.my.domain';
}
sub set_hostname {
my $fqdn = shift;
open my $fh, ">", "/etc/myname";
printf $fh "%s\n", $fqdn;
close $fh;
system("hostname " . $fqdn);
}
sub add_etc_hosts_entry {
my $fqdn = shift;
my ($shortname) = split(/\./, $fqdn);
open my $fh, ">>", "/etc/hosts";
printf $fh "127.0.1.1 %s %s\n", $shortname, $fqdn;
close $fh;
}
sub install_pubkeys {
my $pubkeys = shift;
make_path('/root/.ssh', { verbose => 0, mode => 0700 });
open my $fh, ">>", "/root/.ssh/authorized_keys";
printf $fh "#-- key added by cloud-init at your request --#\n";
printf $fh "%s\n", $pubkeys;
close $fh;
}
sub apply_user_data {
my $data = shift;
my $fqdn = $data->{fqdn} // get_default_fqdn;
set_hostname($fqdn);
if (!defined($data->{manage_etc_hosts}) ||
$data->{manage_etc_hosts} eq 'true' ||
$data->{manage_etc_hosts} eq 'localhost') {
add_etc_hosts_entry($fqdn);
}
if (defined($data->{ssh_authorized_keys})) {
install_pubkeys join("\n", @{ $data->{ssh_authorized_keys} });
}
if (defined($data->{packages})) {
foreach my $package (@{ $data->{packages} }) {
system("pkg_add " . $package);
}
}
if (defined($data->{write_files})) {
foreach my $item (@{ $data->{write_files} }) {
mkpath [dirname($item->{path})], 0, 0755;
open my $fh, ">", $item->{path};
print $fh $item->{content};
if (defined($item->{permissions})) {
my $perms = oct($item->{permissions});
chmod($perms, $fh);
}
if (defined($item->{owner})) {
my ($user_name, $group_name) = split(/\:/, $item->{owner});
my $uid = getpwnam $user_name;
my $gid = getgrnam $group_name;
chown $uid, $gid, $fh;
}
close $fh;
}
}
if (defined($data->{runcmd})) {
foreach my $runcmd (@{ $data->{runcmd} }) {
system("sh -c \"$runcmd\"");
}
}
}
sub run_user_script {
my $data = shift;
my ($fh, $filename) = tempfile("/tmp/cloud-config-XXXXXX");
print $fh $data;
chmod(0700, $fh);
close $fh;
system("sh -c \"$filename && rm $filename\"");
}
sub cloud_init {
my $host = METADATA_HOST;
my $compressed = get_data($host, 'user-data');
my $data;
gunzip \$compressed => \$data;
my $pubkeys = get_data($host, 'meta-data/public-keys');
chomp($pubkeys);
install_pubkeys $pubkeys;
if ($data =~ /^#cloud-config/) {
$data = CPAN::Meta::YAML->read_string($data)->[0];
apply_user_data($data);
} else {
set_hostname(get_default_fqdn);
add_etc_hosts_entry(get_default_fqdn);
if ($data =~ /^#\!/) {
run_user_script($data);
}
}
}
sub action_deploy {
#-- rc.firsttime stub
open my $fh, ">>", "/etc/rc.firsttime";
print $fh <<'EOF';
# run cloud-init
path=/usr/local/libdata/cloud-init.pl
echo -n "exoscale first boot: "
perl $path cloud-init && echo "done."
EOF
close $fh;
#-- remove generated keys and seeds
unlink glob "/etc/ssh/ssh_host*";
unlink "/etc/random.seed";
unlink "/var/db/host.random";
unlink "/etc/isakmpd/private/local.key";
unlink "/etc/isakmpd/local.pub";
unlink "/etc/iked/private/local.key";
unlink "/etc/isakmpd/local.pub";
#-- remove cruft
unlink "/tmp/*";
unlink "/var/db/dhclient.leases.vio0";
#-- disable root password
system("chpass -a 'root:*:0:0:daemon:0:0:Charlie &:/root:/bin/ksh'")
}
#-- main
my ($action) = @ARGV;
action_deploy if ($action eq 'deploy');
cloud_init if ($action eq 'cloud-init');