From 9939547bee91fe8a9ba95a74f2aa7bdc9db0095f Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 23 Jan 2009 13:33:32 +0000
Subject: [PATCH] Fix batcher for 2.0-style disks and nics

This patch fixes the gnt-instance batch-create command, and in doing so
also slightly changes two other functions:
  - we change utils.ParseUnit so that it accepts integer values also
    (both ParseUnit(5) and ParseUnit("5") return the same value)
  - a bridge 'None' in LUCreateInstance will be converted to the default
    bridge; currently only missing bridges will be accepted to mean the
    default one

The main changes to batcher were the change to variable number of disks
and NICs.

The patch also adds a batcher-instances.json example file copied from
the 1.2 branch and properly modified.

Reviewed-by: imsnah, killerfoxi
---
 doc/examples/batcher-instances.json | 14 ++++++++++++++
 lib/cmdlib.py                       |  4 +++-
 lib/utils.py                        |  2 +-
 scripts/gnt-instance                | 26 +++++++++++++++++---------
 4 files changed, 35 insertions(+), 11 deletions(-)
 create mode 100644 doc/examples/batcher-instances.json

diff --git a/doc/examples/batcher-instances.json b/doc/examples/batcher-instances.json
new file mode 100644
index 000000000..0f24606de
--- /dev/null
+++ b/doc/examples/batcher-instances.json
@@ -0,0 +1,14 @@
+{
+  "instance1.example.com": {
+    "template": "drbd",
+    "os": "debootstrap",
+    "disk_size": ["25G"],
+    "ram_size": 512
+  },
+  "instance2.example.com": {
+    "template": "plain",
+    "os": "debootstrap",
+    "disk_size": ["100G"],
+    "ram_size": 512
+  }
+}
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index e54c17962..4c3865f09 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -4193,7 +4193,9 @@ class LUCreateInstance(LogicalUnit):
           raise errors.OpPrereqError("Invalid MAC address specified: %s" %
                                      mac)
       # bridge verification
-      bridge = nic.get("bridge", self.cfg.GetDefBridge())
+      bridge = nic.get("bridge", None)
+      if bridge is None:
+        bridge = self.cfg.GetDefBridge()
       self.nics.append(objects.NIC(mac=mac, ip=nic_ip, bridge=bridge))
 
     # disk checks/pre-build
diff --git a/lib/utils.py b/lib/utils.py
index 691b0ee6b..f1d0f993f 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -751,7 +751,7 @@ def ParseUnit(input_string):
   is always an int in MiB.
 
   """
-  m = re.match('^([.\d]+)\s*([a-zA-Z]+)?$', input_string)
+  m = re.match('^([.\d]+)\s*([a-zA-Z]+)?$', str(input_string))
   if not m:
     raise errors.UnitParseError("Invalid format")
 
diff --git a/scripts/gnt-instance b/scripts/gnt-instance
index 959990ad9..a57c7b4e3 100755
--- a/scripts/gnt-instance
+++ b/scripts/gnt-instance
@@ -375,13 +375,12 @@ def BatchCreate(opts, args):
   in the form::
 
     {"instance-name":{
-      "disk_size": 25,
-      "swap_size": 1024,
+      "disk_size": [20480],
       "template": "drbd",
       "backend": {
         "memory": 512,
         "vcpus": 1 },
-      "os": "etch-image",
+      "os": "debootstrap",
       "primary_node": "firstnode",
       "secondary_node": "secondnode",
       "iallocator": "dumb"}
@@ -397,8 +396,7 @@ def BatchCreate(opts, args):
   @return: the desired exit code
 
   """
-  _DEFAULT_SPECS = {"disk_size": 20 * 1024,
-                    "swap_size": 4 * 1024,
+  _DEFAULT_SPECS = {"disk_size": [20 * 1024],
                     "backend": {},
                     "iallocator": None,
                     "primary_node": None,
@@ -458,19 +456,29 @@ def BatchCreate(opts, args):
     if specs['hypervisor']:
       hypervisor, hvparams = specs['hypervisor'].iteritems()
 
+    disks = []
+    for elem in specs['disk_size']:
+      try:
+        size = utils.ParseUnit(elem)
+      except ValueError, err:
+        raise errors.OpPrereqError("Invalid disk size '%s' for"
+                                   " instance %s: %s" %
+                                   (elem, name, err))
+      disks.append({"size": size})
+
+    nic0 = {'ip': specs['ip'], 'bridge': specs['bridge'], 'mac': specs['mac']}
+
     op = opcodes.OpCreateInstance(instance_name=name,
-                                  disk_size=specs['disk_size'],
-                                  swap_size=specs['swap_size'],
+                                  disks=disks,
                                   disk_template=specs['template'],
                                   mode=constants.INSTANCE_CREATE,
                                   os_type=specs['os'],
                                   pnode=specs['primary_node'],
                                   snode=specs['secondary_node'],
-                                  ip=specs['ip'], bridge=specs['bridge'],
+                                  nics=[nic0],
                                   start=specs['start'],
                                   ip_check=specs['ip_check'],
                                   wait_for_sync=True,
-                                  mac=specs['mac'],
                                   iallocator=specs['iallocator'],
                                   hypervisor=hypervisor,
                                   hvparams=hvparams,
-- 
GitLab