Permalink
Browse files

Start adding a 'migrator' module

that can be used to aid in the moving
of older versions of cloud-inits data
to newer versions of cloud-inits data.

1. Move the semaphores for the current instance
   to there canonicalized names and use the 
   canonicalized in the file 'locking' code
  • Loading branch information...
1 parent 84b6701 commit 7bde163d344737b99c594ae1d2cdcb3e31d45197 Joshua Harlow committed Nov 8, 2012
Showing with 62 additions and 1 deletion.
  1. +53 −0 cloudinit/config/cc_migrator.py
  2. +8 −1 cloudinit/helpers.py
  3. +1 −0 config/cloud.cfg
@@ -0,0 +1,53 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2012 Yahoo! Inc.
+#
+# Author: Joshua Harlow <harlowja@yahoo-inc.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 os
+import shutil
+
+from cloudinit import helpers
+from cloudinit import util
+
+from cloudinit.settings import PER_ALWAYS
+
+frequency = PER_ALWAYS
+
+
+def _migrate_canon_sems(cloud):
+ sem_path = cloud.paths.get_ipath('sem')
+ if not sem_path or not os.path.exists(sem_path):
+ return 0
+ am_adjusted = 0
+ for p in os.listdir(sem_path):
+ full_path = os.path.join(sem_path, p)
+ if os.path.isfile(full_path):
+ canon_p = helpers.canon_sem_name(p)
+ if canon_p != p:
+ new_path = os.path.join(sem_path, p)
+ shutil.move(full_path, new_path)
+ am_adjusted += 1
+ return am_adjusted
+
+
+def handle(name, cfg, cloud, log, _args):
+ do_migrate = util.get_cfg_option_str(cfg, "migrate", True)
+ if not util.translate_bool(do_migrate):
+ log.debug("Skipping module named %s, migration disabled", name)
+ return
+ sems_moved = _migrate_canon_sems(cloud)
+ log.debug("Migrated %s semaphore files to there canonicalized names",
@tamsky

tamsky Sep 9, 2015

This is a stunningly bad idea.

Nobody writing code in 2015 would expect that their lockfile, that was successfully in /var/lib/cloud/sem, and created using the appropriate tool: /usr/bin/cloud-init-per to ever be renamed for them?

This alters 'once' semantics for anyone using cloud-init-per once into always.

If you're going to author and set a default that creates a landmine, then you must also update cloud-init-per to warn users when they are creating a non-canonical name that could be renamed on the next boot.

@harlowja

harlowja Sep 9, 2015

Contributor

Thanks much for the feedback.

I'm thankful that I wrote this in 2012 vs. 2015, phew.

🍔

@tamsky

tamsky Sep 9, 2015

By renaming semaphore/lockfiles, this function silently breaks cloud-init-per.

Bug filed: https://bugs.launchpad.net/cloud-init/+bug/1494040

+ sems_moved)
View
@@ -71,12 +71,17 @@ def __str__(self):
return "<%s using file %r>" % (util.obj_name(self), self.fn)
+def canon_sem_name(name):
+ return name.replace("-", "_")
+
+
class FileSemaphores(object):
- def __init__(self, sem_path):
+ def __init__(self, sem_path):
self.sem_path = sem_path
@contextlib.contextmanager
def lock(self, name, freq, clear_on_fail=False):
+ name = canon_sem_name(name)
try:
yield self._acquire(name, freq)
except:
@@ -85,6 +90,7 @@ def lock(self, name, freq, clear_on_fail=False):
raise
def clear(self, name, freq):
+ name = canon_sem_name(name)
sem_file = self._get_path(name, freq)
try:
util.del_file(sem_file)
@@ -119,6 +125,7 @@ def _acquire(self, name, freq):
def has_run(self, name, freq):
if not freq or freq == PER_ALWAYS:
return False
+ name = canon_sem_name(name)
sem_file = self._get_path(name, freq)
# This isn't really a good atomic check
# but it suffices for where and when cloudinit runs
View
@@ -23,6 +23,7 @@ preserve_hostname: false
# The modules that run in the 'init' stage
cloud_init_modules:
+ - migrator
- bootcmd
- write-files
- resizefs

0 comments on commit 7bde163

Please sign in to comment.