summaryrefslogtreecommitdiff
path: root/cloudinit/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/__init__.py')
-rw-r--r--cloudinit/__init__.py93
1 files changed, 81 insertions, 12 deletions
diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py
index be366d4c..1de32f2e 100644
--- a/cloudinit/__init__.py
+++ b/cloudinit/__init__.py
@@ -18,9 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
+varlibdir = '/var/lib/cloud'
datadir = '/var/lib/cloud/data'
semdir = '/var/lib/cloud/sem'
-pluginsdir = datadir + '/plugins'
cachedir = datadir + '/cache'
userdata_raw = datadir + '/user-data.txt'
userdata = datadir + '/user-data.txt.i'
@@ -32,6 +32,9 @@ system_config = '/etc/cloud/cloud.cfg'
cfg_env_name = "CLOUD_CFG"
def_log_file = '/var/log/cloud-init.log'
+def_log_user = "syslog"
+def_log_group = "adm"
+
cfg_builtin = """
log_cfgs: [ ]
cloud_type: auto
@@ -52,9 +55,10 @@ import util
import logging
import logging.config
import StringIO
+import glob
class NullHandler(logging.Handler):
- def emit(self,record): pass
+ def emit(self,record): pass
log = logging.getLogger(logger_name)
log.addHandler(NullHandler())
@@ -104,6 +108,12 @@ class CloudInit:
"all": ( "nocloud-net", "ec2" ),
"local" : ( "nocloud", ),
}
+ dirmap = {
+ "handlers" : "/handlers",
+ "scripts" : "/scripts",
+ "sem" : "/sem",
+ None : "",
+ }
cfg = None
part_handlers = { }
@@ -200,6 +210,21 @@ class CloudInit:
log.debug("did not find data source from %s" % dslist)
raise DataSourceNotFoundException("Could not find data source")
+ def set_cur_instance(self):
+ lname = "%s/instance" % varlibdir
+ try:
+ os.unlink(lname)
+ except OSError, e:
+ if e.errno != errno.ENOENT: raise
+
+ os.symlink("./instances/%s" % self.get_instance_id(), lname)
+ idir = self.get_idir()
+ dlist = []
+ for d in [ "handlers", "scripts", "sem" ]:
+ dlist.append("%s/%s" % (idir, d))
+
+ util.ensure_dirs(dlist)
+
def get_userdata(self):
return(self.datasource.get_userdata())
@@ -222,11 +247,9 @@ class CloudInit:
'%s=%s' % (cfg_env_name,cloud_config)]).communicate()
def sem_getpath(self,name,freq):
- freqtok = freq
if freq == 'once-per-instance':
- freqtok = self.datasource.get_instance_id()
-
- return("%s/%s.%s" % (semdir,name,freqtok))
+ return("%s/%s" % (self.get_idir("sem"),name))
+ return("%s/%s.%s" % (self.get_cdir("sem"), name, freq))
def sem_has_run(self,name,freq):
if freq == "always": return False
@@ -285,9 +308,45 @@ class CloudInit:
self.sem_clear(semname,freq)
raise
+ # get_cdir : get the "clouddir" (/var/lib/cloud/<name>)
+ # for a name in dirmap
+ def get_idir(self, name=None):
+ return("%s/instances/%s%s"
+ % (varlibdir,self.get_instance_id(), self.dirmap[name]))
+
+ # get_cdir : get the "clouddir" (/var/lib/cloud/<name>)
+ # for a name in dirmap
+ def get_cdir(self, name=None):
+ return("%s%s" % (varlibdir, self.dirmap[name]))
+
def consume_userdata(self):
self.get_userdata()
data = self
+
+ cdir = self.get_cdir("handlers")
+ idir = self.get_idir("handlers")
+
+ # add the path to the plugins dir to the top of our list for import
+ # instance dir should be read before cloud-dir
+ sys.path.insert(0,cdir)
+ sys.path.insert(0,idir)
+
+ # add handlers in cdir
+ for fname in glob.glob("%s/*.py" % cdir):
+ if not os.path.isfile(fname): continue
+ modname = os.path.basename(fname)[0:-3]
+ try:
+ mod = __import__(modname)
+ lister = getattr(mod, "list_types")
+ handler = getattr(mod, "handle_part")
+ mtypes = lister()
+ for mtype in mtypes:
+ self.part_handlers[mtype]=handler
+ log.debug("added handler for [%s] from %s" % (mtypes,fname))
+ except:
+ log.warn("failed to initialize handler in %s" % fname)
+ util.logexc(log)
+
# give callbacks opportunity to initialize
for ctype, func in self.part_handlers.items():
func(data, "__begin__",None,None)
@@ -304,16 +363,13 @@ class CloudInit:
self.handlercount = 0
return
- # add the path to the plugins dir to the top of our list for import
- if self.handlercount == 0:
- sys.path.insert(0,pluginsdir)
-
self.handlercount=self.handlercount+1
- # write content to pluginsdir
+ # write content to instance's handlerdir
+ handlerdir = self.get_idir("handler")
modname = 'part-handler-%03d' % self.handlercount
modfname = modname + ".py"
- util.write_file("%s/%s" % (pluginsdir,modfname), payload, 0600)
+ util.write_file("%s/%s" % (handlerdir,modfname), payload, 0600)
try:
mod = __import__(modname)
@@ -418,6 +474,19 @@ class CloudInit:
return(self.datasource.device_name_to_device(name))
+def initfs():
+ subds = [ 'scripts', 'seed', 'instances', 'handlers', 'sem' ]
+ dlist = [ ]
+ for subd in subds:
+ dlist.append("%s/%s" % (varlibdir, subd))
+ util.ensure_dirs(dlist)
+
+ fp = open(def_log_file,"ab")
+ fp.close()
+ util.chownbyname(def_log_file,def_log_user, def_log_group)
+
+
+
def purge_cache():
try:
os.unlink(data_source_cache)