-
Notifications
You must be signed in to change notification settings - Fork 18
/
cloud_init.rb
executable file
·90 lines (82 loc) · 2.38 KB
/
cloud_init.rb
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
#!/usr/bin/env ruby
# Copyright (C) 2015 Oregon State University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
require 'yaml'
def cloud_init
host = ENV['INSTANCE_NAME'] || ''
user = ENV['CINIT_USER']
ssh_key = ENV['CINIT_SSH_KEY']
disable_root = ENV['CINIT_DISABLE_ROOT'] == 'yes' ? 1 : 0
ssh_pwauth = ENV['CINIT_SSH_PWAUTH'] == 'yes' ? 1 : 0
manage_resolv_conf = ENV['CINIT_MANAGE_RESOLV_CONF'] == 'yes' ? true : false
name_servers = ENV['DNS_SERVERS']
search_domains = ENV['DNS_SEARCH']
domain = host.split('.').drop(1).join('.')
resolv_conf = nil
if manage_resolv_conf
manage_conf = true
resolv_conf = {
'nameservers' => name_servers.split,
'searchdomains' => search_domains.split,
'domain' => domain,
'options' => {
'rotate' => true,
'timeout' => 1
}
}
else
manage_conf = false
end
init_modules =
%w(
migrator
bootcmd
write-files
growpart
resizefs
set_hostname
update_hostname
update_etc_hosts
resolv_conf
rsyslog
users-groups
ssh
)
config = {
'hostname' => host.sub(/\..*$/, ''),
'fqdn' => host,
'instance-id' => host,
'disable_root' => disable_root,
'ssh_pwauth' => ssh_pwauth,
'manage-resolv-conf' => manage_conf,
'manage_resolv_conf' => manage_conf,
'cloud_init_modules' => init_modules,
'users' => [
'name' => user,
'primary-group' => user,
'groups' => %w(wheel adm systemd-journal),
'sudo' => ['ALL=(ALL) NOPASSWD:ALL'],
'shell' => '/bin/bash',
'lock_passwd' => true,
'ssh-authorized-keys' => [ssh_key]
],
'groups' => [user],
'resolv_conf' => resolv_conf
}
config.to_yaml
end
puts cloud_init