Пример #1
0
 def test_set_parent(self):
     child = Snapshot(SNAP_PREFIX + "2013-07-31_12:53:16-raring-to-go")
     old_parent = child.parent
     child.parent = None
     parent_file = os.path.join(self.sandbox, 
         SNAP_PREFIX + "2013-07-31_12:53:16-raring-to-go", PARENT_LINK)
     self.assertFalse(os.path.exists(parent_file))
     
     new_parent = SNAP_PREFIX + "2013-07-26_14:50:53"
     child.parent = new_parent
     self.assertEqual(os.readlink(parent_file), 
         os.path.join(PARENT_DOTS, new_parent))
    def set_default(self, snapshot, tag=""):
        """ backup @ and replace @ with a copy of given snapshot """
        if not tag:
            tag = self._prompt_for_tag()

        snapshot = Snapshot(snapshot)
        new_root = os.path.join(self.mp, snapshot.name)
        if (
                os.path.isdir(new_root) and
                snapshot.name.startswith(SNAP_PREFIX)):
            default_root = os.path.join(self.mp, "@")
            staging = os.path.join(self.mp, "@apt-btrfs-staging")
            if os.path.lexists(staging):
                raise Exception("Reserved directory @apt-btrfs-staging "
                    "exists\nPlease remove from btrfs volume root before "
                    "trying again")
            
            # find and store dpkg changes
            date, history = self._get_status()
            Snapshot("@").changes = history
                
            # snapshot the requested default so as not to remove it
            res = self.commands.btrfs_subvolume_snapshot(new_root, staging)
            if not res:
                raise Exception("Could not create snapshot")

            # make backup name
            backup = os.path.join(self.mp, 
                SNAP_PREFIX + self._get_now_str()) + tag
            # if backup name is already in use, wait a sec and try again
            if os.path.exists(backup):
                time.sleep(1)
                backup = os.path.join(self.mp, 
                    SNAP_PREFIX + self._get_now_str())

            # move everything into place
            os.rename(default_root, backup)
            os.rename(staging, default_root)
            
            # remove @/etc/apt-btrfs-changes & set root's new parent
            new_default = Snapshot("@")
            new_default.changes = None
            new_default.parent = snapshot.name
            
            print("Default changed to %s, please reboot for changes to take "
                  "effect." % snapshot.name)
        else:
            print("You have selected an invalid snapshot. Please make sure "
                  "that it exists, and that its name starts with "
                  "\"%s\"" % SNAP_PREFIX)
        return True