Commits

Albert Hopkins  committed 64a0755

PREPEND replaced with new "clusters" functionality

  • Participants
  • Parent commits 288d2e5
  • Tags 0.4.7

Comments (0)

Files changed (4)

File hemp/fabfile.py

     import helpers
     import settings
     hypervisor = libvirt.open(settings.LIBVIRT_URI)
+    _cluster_file = '.cluster'
 
     @staticmethod
     def created(host):
         filename = '%s/images/%s.state' % (DIRNAME, hostname)
         return filename if os.path.exists(filename) else None
 
+    @staticmethod
+    def get_cluster():
+        """Get the name of the active 'cluster'"""
+        if not os.path.exists(_Cloud._cluster_file):
+            return ''
+        else:
+            return open(_Cloud._cluster_file).read().strip()
+
+    @staticmethod
+    def set_cluster(name):
+        """Set the active cluster"""
+        f = open(_Cloud._cluster_file, 'w')
+        f.write(name)
+        f.close()
+        return
+
+    @staticmethod
+    def del_cluster():
+        if os.path.exists(_Cloud._cluster_file):
+            os.unlink(_Cloud._cluster_file)
+
+
 DIRNAME = os.getcwd()
 PREFIX = os.path.basename(DIRNAME)
 BASE_IMG = 'base.qcow2'
 env.user = 'root'
 env.hosts, env.roledefs = cloud.helpers.get_hosts_and_roledefs(PREFIX,
         cloud.settings.NUM_SERVERS, cloud.settings.ROLES,
-        cloud.settings.PREPEND)
+        cloud.get_cluster())
 
 
 def create():
     while True:
         time.sleep(5)
 
+@runs_once
+def cluster(name=''):
+    """[H] Set the active cluster"""
+    if not name:
+        cloud.del_cluster()
+    cloud.set_cluster(name)
+
+@runs_once
+def clusters():
+    """[H] List clusters"""
+    # this is kludgy
+    clusters = set()
+    active = cloud.get_cluster()
+    if active:
+        clusters.add(active)
+    files = os.listdir('images')
+    for fname in files:
+        if '-' in fname:
+            cluster = fname.rsplit('-', 1)[0]
+            clusters.add(cluster)
+    for cluster in clusters:
+        if cluster == active:
+            print colors.green('%s*' % cluster, bold=True)
+        else:
+            print cluster
+
+
+
 def _hilight_hemp_tasks():
     """Decorate hemp task docstrings (for fab --list)"""
     for i in globals().values():

File hemp/helpers.py

 ATTEMPTS = 5
 SUDO = (os.getuid() != 0)
 
-def _clusterize_hostnames(basename, num_servers, prepend=''):
+def _clusterize_hostnames(basename, num_servers, cluster=''):
     """
     Internal function to create return:
         [basename01, basename02, basename03...]
     If basename has a '.' in it, e.g. "basename.domain" then the will return:
         [basename01.domain, basename02.domain...]
 
-    If prepend is passed, each hostname will be prepended with the supplied
+    If cluster is passed, each hostname will be prepended with the supplied
     string
     """
     domain = None
+    if cluster:
+        cluster = cluster + '-'
 
     if num_servers == 1:
-        return [prepend + basename]
+        return [cluster + basename]
 
     if '.' in basename:
         prefix, domain = basename.split('.', 1)
     else:
         prefix = basename
 
-    prefix = prepend + prefix
+    prefix = cluster + prefix
 
     hostnames = ['%s%02d' % (prefix, i) for i in range(1, num_servers+1)]
     if domain:
     return hostnames
 
 
-def get_hosts_and_roledefs(basename, num_servers, roles, prepend=''):
+def get_hosts_and_roledefs(basename, num_servers, roles, cluster=''):
     """
     Return hosts, and roledefs based on parameters. This could (should)
     be assigned directly to fabric's env.hosts and env.roledefs,
     remaining = num_servers
     if roles:
         for role in roles:
-            servers = _clusterize_hostnames(role, roles[role], prepend)
+            servers = _clusterize_hostnames(role, roles[role], cluster)
             hosts.extend(servers)
             roledefs[role] = servers
         remaining = max(num_servers - len(hosts), 0)
 
     if remaining:
-        servers = _clusterize_hostnames(basename, remaining, prepend)
+        servers = _clusterize_hostnames(basename, remaining, cluster)
         hosts.extend(servers)
 
     return (hosts, roledefs)

File hemp/hemp.py

 
 import settings
 
-__version__ = '0.4.6.1'
+__version__ = '0.4.7'
 __revision__ = '$Revision$'
 
 DIRS = ('images',)

File hemp/settings.py

 # host for that role will have a name like role01, role02, etc.
 ROLES = {}
 
-PREPEND = ''
-
 # The FORWARD variable is a dict of FORWARD[host] = ...
 # where for each «host» defined, you provide a list of local → remote
 # port forwarding pairs, and the physical host will listen on the local port