Commits

Albert Hopkins committed 7cf34f2

* Added freeze() and thaw() functions.

Now hemp clouds can be frozen, relocated and thawed back to it's original
state. This can be used as a poor man's migration, or as a way to save
your work and continue later (as unregistered domains are not automatically
saved/restored by libvirt).

  • Participants
  • Parent commits c9364ab
  • Tags 0.4.1

Comments (0)

Files changed (2)

File hemp/fabfile.py

                 return False
         return True
 
+    def get_state_file(self, hostname):
+        """
+        Returns a tuple of (filename, exists) where:
+
+        filename is the path to the hosts saved state file and
+
+        exists is a boolean and True if said filename exists
+        """
+        filename = '%s/images/%s.state' % (DIRNAME, hostname)
+        return (filename, os.path.exists(filename))
+
+
 
 DIRNAME = os.getcwd()
 PREFIX = os.path.basename(DIRNAME)
     cleanup()
 
 
+def freeze():
+    """Save and destroy a running cloud"""
+    if not cloud.started:
+        print colors.red('%s does not appear to be running.' % env.host,
+                bold=True)
+        return
+    dom = conn.lookupByName(env.host)
+    state_file, exists = cloud.get_state_file(env.host)
+    if exists:
+        print colors.red('Saved state for %s already exists.  Refusing to '
+                'overwrite.', bold=True)
+        print colors.red('If you want you can manually remove %s' %
+                stage_file, bold=True)
+        return
+    dom.save(state_file)
+
+    # libvirt makes it so that only root can read/write the state_file.  In
+    # order for us to be able to relocate the project, we need read permission
+    cloud.helpers.sudo_popen(('chmod', '+r', state_file)).wait()
+
+
+def thaw():
+    """Restore the cloud from it's saved state"""
+
+    state_file, exists = cloud.get_state_file(env.host)
+    if not exists:
+        print colors.red('%s was not frozen.' % env.host, bold=True)
+        return
+    conn.restore(state_file)
+    os.unlink(state_file)
+
+
 def cleanup():
     """Clean up misc. files.  Assumes all guests are destroyed"""
     image_file = 'images/%s.qcow2' % env.host
 
 def status():
     """Show vm status"""
-    states = ['None', 'Running', 'Blocked', 'Paused', 'Shutdown',
+    states = ['Gone', 'Running', 'Blocked', 'Paused', 'Shutdown',
         'Shutoff', 'Crashed']
+
     try:
         dom = conn.lookupByName(env.host)
         state = dom.info()[0]
     except libvirt.libvirtError:
-        return
+        state = 0
+
     if state == 1:
-        state_s = colors.green(states[state])
+        state_s = colors.green(states[state], bold=True)
     else:
-        state_s = colors.red(states[state])
-    print '%s: %s' % (env.host, state_s)
+        state_s = colors.red(states[state], bold=True)
+        state_file, exists = cloud.get_state_file(env.host)
+        if exists:
+            state_s = '%s (%s)' % (state_s, colors.blue('frozen', bold=True))
+    print '%s: %s' % (colors.yellow(env.host, bold=True), state_s)
+

File hemp/hemp.py

 
 import settings
 
-__version__ = '0.4.0'
+__version__ = '0.4.1'
 __revision__ = '$Revision$'
 
 DIRS = ('images',)