4
# Copyright (C) 2009-2010 Canonical Ltd.
6
# Author: Scott Moser <scott.moser@canonical.com>
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License version 3, as
10
# published by the Free Software Foundation.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
import cloudinit.util as util
25
import cloudinit.CloudConfig as CC
26
import cloudinit.DataSource as ds
33
sys.stderr.write("WARN:%s" % wstr)
36
cmds = ( "start", "start-local" )
37
deps = { "start" : ( ds.DEP_FILESYSTEM, ds.DEP_NETWORK ),
38
"start-local" : ( ds.DEP_FILESYSTEM, ) }
46
# this is really for debugging only
47
# but you can invoke on development system with ./config/cloud.cfg
48
cfg_path = sys.argv[2]
51
sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds))
54
now = time.strftime("%a, %d %b %Y %H:%M:%S %z",time.gmtime())
56
uptimef=open("/proc/uptime")
57
uptime=uptimef.read().split(" ")[0]
60
warn("unable to open /proc/uptime\n")
64
cfg = cloudinit.get_base_cfg(cfg_path)
65
except Exception as e:
66
warn("Failed to get base config. falling back to builtin: %s\n" % e)
68
cfg = cloudinit.get_builtin_cfg()
69
except Exception as e:
70
warn("Unable to load builtin config\n")
74
(outfmt, errfmt) = CC.get_output_cfg(cfg,"init")
75
CC.redirect_output(outfmt, errfmt)
76
except Exception as e:
77
warn("Failed to get and set output config: %s\n" % e)
79
cloudinit.logging_set_from_cfg(cfg)
80
log = logging.getLogger()
84
except Exception as e:
85
warn("failed to initfs, likely bad things to come: %s\n" % str(e))
87
nonet_path = "%s/%s" % (cloudinit.get_cpath("data"), "no-net")
90
stop_files = ( cloudinit.get_ipath_cur("obj_pkl"), nonet_path )
91
# if starting as the network start, there are cases
92
# where everything is already done for us, and it makes
93
# most sense to exit early and silently
96
fp = open("/var/lib/cloud/instance/obj.pkl","r")
101
log.debug("no need for cloud-init start to run (%s)\n", f)
103
elif cmd == "start-local":
104
# cache is not instance specific, so it has to be purged
105
# but we want 'start' to benefit from a cache if
106
# a previous start-local populated one
107
manclean = util.get_cfg_option_bool(cfg, 'manual_cache_clean',False)
109
log.debug("not purging cache, manual_cache_clean = True")
110
cloudinit.purge_cache(not manclean)
113
os.unlink(nonet_path)
115
if e.errno != errno.ENOENT: raise
117
msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime)
118
sys.stderr.write(msg + "\n")
123
cloud = cloudinit.CloudInit(ds_deps=deps[cmd])
126
cloud.get_data_source()
127
except cloudinit.DataSourceNotFoundException as e:
128
sys.stderr.write("no instance data found in %s\n" % cmd)
131
# set this as the current instance
132
cloud.set_cur_instance()
137
msg = "found data source: %s" % cloud.datasource
138
sys.stderr.write(msg + "\n")
141
# parse the user data (ec2-run-userdata.py)
143
cloud.sem_and_run("consume_userdata", "once-per-instance",
144
cloud.consume_userdata,[],False)
146
warn("consuming user data failed!\n")
149
# finish, send the cloud-config event
152
cfg_path = cloudinit.get_ipath_cur("cloud_config")
153
cc = CC.CloudConfig(cfg_path, cloud)
155
# if the output config changed, update output and err
159
(outfmt, errfmt) = CC.get_output_cfg(cc.cfg,"init")
160
if outfmt_orig != outfmt or errfmt_orig != errfmt:
161
warn("stdout, stderr changing to (%s,%s)" % (outfmt,errfmt))
162
CC.redirect_output(outfmt, errfmt)
163
except Exception as e:
164
warn("Failed to get and set output config: %s\n" % e)
166
module_list = CC.read_cc_modules(cc.cfg,"cloud_init_modules")
170
failures = CC.run_cc_modules(cc,module_list,log)
172
msg = "no cloud_init_modules to run"
173
sys.stderr.write(msg + "\n")
177
sys.exit(len(failures))
179
if __name__ == '__main__':