Commits

Albert Hopkins  committed a2fe949

fabfile.py can use settings.ROLES to automagically configure roledefs and
hostnames

* helpers.py: Increase WAIT_TIME for those with inferior hardware

* helpers.py (get_hosts): new helper function to name hosts based on prefix
or role.

* helpers.py (set_image_to_hostname): Fix typo

* settings.py: New settings variable ROLES allows flexible hostnames based
on it's role

* fabfile.py: Set env.hosts and env.roledefs using helpers.get_hosts()

  • Participants
  • Parent commits ac78e6a
  • Tags 0.2.0

Comments (0)

Files changed (3)

File hemp/fabfile.py

 BASE_IMG = 'base.qcow2'
 TEMPLATE = '%s/template.xml' % DIRNAME
 
-env.hosts = ['%s%02d' % (PREFIX, i) for i in range(1, settings.NUM_SERVERS+1)]
 env.reject_unknown_hosts = False
 env.user = 'root'
+env.hosts, env.roledefs = helpers.get_hosts(PREFIX, settings.NUM_SERVERS,
+        settings.ROLES)
 conn = libvirt.open(settings.LIBVIRT_URI)
 
+
 def create():
     """Create (and start) the virtual machines"""
 

File hemp/helpers.py

 from subprocess import Popen, PIPE
 from time import sleep
 
+
 NULL = open('/dev/null', 'rw')
 NUM_DEVS = 16
 MTAB = r'(?P<dev>.+) (?P<mount_point>%s) (?P<type>.+) (?P<options>.+) 0 0'
 PORT = 2000
-WAIT_TIME = 0.1
-ATTEMPTS=5
+WAIT_TIME = 0.3
+ATTEMPTS = 5
 SUDO = (os.getuid() != 0)
 
 
+def get_hosts(basename, num_servers, roles):
+    """
+    Return hosts, and roledefs based on parameters. This could (should)
+    be assigned directly to fabric's env.hosts and env.roledefs,
+    respectively.
+
+    «num_servers» is the total number of hosts (i.e settings.NUM_SERVERS)
+    «roles» is a dict of rolename->num_servers in role
+    «basename» is the prefix for any remaining hosts not in a role. If
+        roles == {} then all hosts will use «basename»
+    """
+    # TODO: Rename this function.
+    roledefs = {}
+    hosts = []
+    remaining = num_servers
+    if roles:
+        for role in roles:
+            servers = ['%s%02d' % (role, i) for i in range(1, roles[role]+1)]
+            hosts.extend(servers)
+            roledefs[role] = servers
+        remaining = max(num_servers - len(hosts), 0)
+    
+    if remaining:
+        servers = ['%s%02d' % (basename, i) for i in range(1, remaining+1)]
+        hosts.extend(servers)
+
+    return (hosts, roledefs)
+
+        
 def sudo_popen(cargs, *args, **kwargs):
     """Use subprocess.Popen(cargs, *args, **kwargs), but inserts a sudo if not
     root"""
 
     return Popen(lcargs, *args, **kwargs)
 
+
 def set_image_to_hostname(mount_point, hostname):
     "Set the hostname of the image to «hostname»"
 
     udev_rules_file = ('%s/etc/udev/rules.d/70-persistent-net.rules' %
         mount_point)
     if os.path.exists(udev_rules_file):
-        sudo_popen(['rm', udev_rules_file).wait()
-
+        sudo_popen(['rm', udev_rules_file]).wait()
 
     if os.path.exists('%s/mnt/conf.d/hostname'):
         # Gentoo
         pipe.stdin.close()
     pipe.wait()
 
-    # local('echo "%s" | sudo dd of="%s/mnt/%s/etc/hostname"' %
-    #         (hostname, mount_point, hostname))
 
 @contextmanager
 def get_device(image):
                     % (port, ATTEMPTS))
 
 
-
 def get_device_for_mountpoint(mount_point):
     """Returns what device node is mounted on «mount_point» or None if not
     mounted."""
                 dev = match_obj.group('dev')
                 return dev
 
+
 def unmount_qcow(mount_point):
     """unmounts qcow device mounted at «mount_point» and disconnects device
     from the nbd"""
     else:
         raise OSError, ('Failed to unmount %s after %s attempts' %
                 (mount_point, ATTEMPTS))
-
-

File hemp/settings.py

+# -*- coding: utf-8 -*-
 """Common settings for fabfile.py and helper.py"""
 
 # How many servers to spin up
 
 # the libvert URI to our qemu hypervisor
 LIBVIRT_URI = 'qemu:///system'
+
+# the ROLES variable is a dict of ROLES[role] = int where «role» is the
+# name of the role and «int» is the number of hosts for that role, each
+# host for that role will have a name like role01, role02, etc.
+ROLES = {}