~ubuntu-branches/ubuntu/natty/cloud-init/natty

« back to all changes in this revision

Viewing changes to .pc/catchup-rev-385.diff/cloud-init.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2011-03-04 21:17:21 UTC
  • Revision ID: package-import@ubuntu.com-20110304211721-yrbpgdo3wlsfn2xa
* catch up to trunk cloud-init (rev 385).
* attempt to install packages on failed apt-get update (LP: #728167)
* enabled timezone and mcollective cloud-config plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# vi: ts=4 expandtab
 
3
#
 
4
#    Copyright (C) 2009-2010 Canonical Ltd.
 
5
#
 
6
#    Author: Scott Moser <scott.moser@canonical.com>
 
7
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
 
 
20
import subprocess
 
21
import sys
 
22
 
 
23
import cloudinit
 
24
import cloudinit.util as util
 
25
import cloudinit.CloudConfig as CC
 
26
import cloudinit.DataSource as ds
 
27
import time
 
28
import logging
 
29
import errno
 
30
import os
 
31
 
 
32
def warn(wstr):
 
33
    sys.stderr.write("WARN:%s" % wstr)
 
34
 
 
35
def main():
 
36
    cmds = ( "start", "start-local" )
 
37
    deps = { "start" : ( ds.DEP_FILESYSTEM, ds.DEP_NETWORK ),
 
38
             "start-local" : ( ds.DEP_FILESYSTEM, ) }
 
39
 
 
40
    cmd = ""
 
41
    if len(sys.argv) > 1:
 
42
        cmd = sys.argv[1]
 
43
 
 
44
    cfg_path = None
 
45
    if len(sys.argv) > 2:
 
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]
 
49
 
 
50
    if not cmd in cmds:
 
51
        sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds))
 
52
        sys.exit(1)
 
53
 
 
54
    now = time.strftime("%a, %d %b %Y %H:%M:%S %z",time.gmtime())
 
55
    try:
 
56
       uptimef=open("/proc/uptime")
 
57
       uptime=uptimef.read().split(" ")[0]
 
58
       uptimef.close()
 
59
    except IOError as e:
 
60
       warn("unable to open /proc/uptime\n")
 
61
       uptime = "na"
 
62
 
 
63
    try:
 
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)
 
67
        try:
 
68
            cfg = cloudinit.get_builtin_cfg()
 
69
        except Exception as e:
 
70
            warn("Unable to load builtin config\n")
 
71
            raise
 
72
 
 
73
    try:
 
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)
 
78
 
 
79
    cloudinit.logging_set_from_cfg(cfg)
 
80
    log = logging.getLogger()
 
81
 
 
82
    try:
 
83
        cloudinit.initfs()
 
84
    except Exception as e:
 
85
        warn("failed to initfs, likely bad things to come: %s\n" % str(e))
 
86
 
 
87
    nonet_path = "%s/%s" % (cloudinit.get_cpath("data"), "no-net")
 
88
 
 
89
    if cmd == "start":
 
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
 
94
        for f in stop_files:
 
95
            try:
 
96
                fp = open("/var/lib/cloud/instance/obj.pkl","r")
 
97
                fp.close()
 
98
            except:
 
99
                continue
 
100
            
 
101
            log.debug("no need for cloud-init start to run (%s)\n", f)
 
102
            sys.exit(0)
 
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)
 
108
        if manclean:
 
109
            log.debug("not purging cache, manual_cache_clean = True")
 
110
        cloudinit.purge_cache(not manclean)
 
111
 
 
112
        try:
 
113
            os.unlink(nonet_path)
 
114
        except OSError as e:
 
115
            if e.errno != errno.ENOENT: raise
 
116
 
 
117
    msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime)
 
118
    sys.stderr.write(msg + "\n")
 
119
    sys.stderr.flush()
 
120
 
 
121
    log.info(msg)
 
122
 
 
123
    cloud = cloudinit.CloudInit(ds_deps=deps[cmd])
 
124
 
 
125
    try:
 
126
        cloud.get_data_source()
 
127
    except cloudinit.DataSourceNotFoundException as e:
 
128
        sys.stderr.write("no instance data found in %s\n" % cmd)
 
129
        sys.exit(1)
 
130
 
 
131
    # set this as the current instance
 
132
    cloud.set_cur_instance()
 
133
 
 
134
    # store the metadata
 
135
    cloud.update_cache()
 
136
 
 
137
    msg = "found data source: %s" % cloud.datasource
 
138
    sys.stderr.write(msg + "\n")
 
139
    log.debug(msg)
 
140
 
 
141
    # parse the user data (ec2-run-userdata.py)
 
142
    try:
 
143
        cloud.sem_and_run("consume_userdata", "once-per-instance",
 
144
            cloud.consume_userdata,[],False)
 
145
    except:
 
146
        warn("consuming user data failed!\n")
 
147
        raise
 
148
 
 
149
    # finish, send the cloud-config event
 
150
    cloud.initctl_emit()
 
151
 
 
152
    cfg_path = cloudinit.get_ipath_cur("cloud_config")
 
153
    cc = CC.CloudConfig(cfg_path, cloud)
 
154
 
 
155
    # if the output config changed, update output and err
 
156
    try:
 
157
        outfmt_orig = outfmt
 
158
        errfmt_orig = errfmt
 
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)
 
165
 
 
166
    module_list = CC.read_cc_modules(cc.cfg,"cloud_init_modules")
 
167
 
 
168
    failures = []
 
169
    if len(module_list):
 
170
        failures = CC.run_cc_modules(cc,module_list,log)
 
171
    else:
 
172
        msg = "no cloud_init_modules to run"
 
173
        sys.stderr.write(msg + "\n")
 
174
        log.debug(msg)
 
175
        sys.exit(0)
 
176
 
 
177
    sys.exit(len(failures))
 
178
 
 
179
if __name__ == '__main__':
 
180
    main()