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
|
#!/usr/bin/python
# vi: ts=4 expandtab
#
# Copyright (C) 2009-2010 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# 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, see <http://www.gnu.org/licenses/>.
import subprocess
import sys
import cloudinit
import cloudinit.util as util
import time
import logging
import errno
def warn(str):
sys.stderr.write(str)
def main():
cmds = ( "start", "start-local" )
cmd = ""
if len(sys.argv) > 1:
cmd = sys.argv[1]
if not cmd in cmds:
sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds))
sys.exit(1)
now = time.strftime("%a, %d %b %Y %H:%M:%S %z")
try:
uptimef=open("/proc/uptime")
uptime=uptimef.read().split(" ")[0]
uptimef.close()
except IOError as e:
warn("unable to open /proc/uptime\n")
uptime = "na"
msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime)
sys.stderr.write(msg + "\n")
sys.stderr.flush()
source_type = "all"
if cmd == "start-local":
source_type = "local"
cloudinit.logging_set_from_cfg_file()
log = logging.getLogger()
log.info(msg)
# cache is not instance specific, so it has to be purged
# but we want 'start' to benefit from a cache if
# a previous start-local populated one
if cmd == "start-local":
cloudinit.purge_cache()
cloud = cloudinit.CloudInit(source_type=source_type)
try:
cloud.get_data_source()
except cloudinit.DataSourceNotFoundException as e:
sys.stderr.write("no instance data found in %s\n" % cmd)
sys.exit(1)
# store the metadata
cloud.update_cache()
msg = "found data source: %s" % cloud.datasource
sys.stderr.write(msg + "\n")
log.debug(msg)
# parse the user data (ec2-run-userdata.py)
try:
cloud.sem_and_run("consume_userdata", "once-per-instance",
cloud.consume_userdata,[],False)
except:
warn("consuming user data failed!\n")
raise
try:
if util.get_cfg_option_bool(cloud.cfg,"preserve_hostname",False):
log.debug("preserve_hostname is set. not managing hostname")
else:
hostname = cloud.get_hostname()
cloud.sem_and_run("set_hostname", "once-per-instance",
set_hostname, [ hostname, log ], False)
cloud.sem_and_run("update_hostname", "always",
update_hostname, [ hostname, log ], False)
except:
warn("failed to set hostname\n")
#print "user data is:" + cloud.get_user_data()
# set the defaults (like what ec2-set-defaults.py did)
try:
cloud.sem_and_run("set_defaults", "once-per-instance",
set_defaults,[ cloud ],False)
except:
warn("failed to set defaults\n")
# finish, send the cloud-config event
cloud.initctl_emit()
sys.exit(0)
# read hostname from a 'hostname' file
# allow for comments and stripping line endings.
# if file doesn't exist, or no contents, return default
def read_hostname(filename, default=None):
try:
fp = open(filename,"r")
lines = fp.readlines()
fp.close()
for line in lines:
hpos = line.find("#")
if hpos != -1:
line = line[0:hpos]
line = line.rstrip()
if line:
return line
except IOError, e:
if e.errno == errno.ENOENT: pass
return default
def set_hostname(hostname, log):
try:
subprocess.Popen(['hostname', hostname]).communicate()
util.write_file("/etc/hostname","%s\n" % hostname, 0644)
log.debug("populated /etc/hostname with %s on first boot", hostname)
except:
log.error("failed to set_hostname")
def update_hostname(hostname, log):
prev_file="%s/%s" % (cloudinit.datadir,"previous-hostname")
etc_file = "/etc/hostname"
hostname_prev = None
hostname_in_etc = None
try:
hostname_prev = read_hostname(prev_file)
except:
log.warn("Failed to open %s" % prev_file)
try:
hostname_in_etc = read_hostname(etc_file)
except:
log.warn("Failed to open %s" % etc_file)
update_files = []
if not hostname_prev or hostname_prev != hostname:
update_files.append(prev_file)
if (not hostname_in_etc or
(hostname_in_etc == hostname_prev and hostname_in_etc != hostname)):
update_files.append(etc_file)
try:
for fname in update_files:
util.write_file(fname,"%s\n" % hostname, 0644)
log.debug("wrote %s to %s" % (hostname,fname))
except:
log.warn("failed to write hostname to %s" % fname)
if hostname_in_etc and hostname_prev and hostname_in_etc != hostname_prev:
log.debug("%s differs from %s. assuming user maintained" %
(prev_file,etc_file))
if etc_file in update_files:
log.debug("setting hostname to %s" % hostname)
subprocess.Popen(['hostname', hostname]).communicate()
if __name__ == '__main__':
main()
|