From 1d5fb4f906e60ca4b8deb4cbbced6007046504b1 Mon Sep 17 00:00:00 2001
From: John Eckersberg <jeckersb@redhat.com>
Date: Wed, 7 May 2008 21:16:18 -0400
Subject: [PATCH] allow --mac=random to generate a random MAC address

---
 cobbler/modules/cli_system.py |    8 ++++++--
 cobbler/remote.py             |   20 ++++----------------
 cobbler/utils.py              |   21 +++++++++++++++++++++
 cobbler/webui/master.py       |    4 ++--
 4 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/cobbler/modules/cli_system.py b/cobbler/modules/cli_system.py
index 0c9a5b8..419b561 100644
--- a/cobbler/modules/cli_system.py
+++ b/cobbler/modules/cli_system.py
@@ -19,7 +19,7 @@ plib = distutils.sysconfig.get_python_lib()
 mod_path="%s/cobbler" % plib
 sys.path.insert(0, mod_path)
 
-from utils import _
+from utils import _, get_random_mac
 import commands
 import cexceptions
 
@@ -98,7 +98,11 @@ class SystemFunction(commands.CobblerFunction):
             my_interface = "intf0"
 
         if self.options.hostname:    obj.set_hostname(self.options.hostname, my_interface)
-        if self.options.mac:         obj.set_mac_address(self.options.mac,   my_interface)
+        if self.options.mac:
+            if self.options.mac.lower() == 'random':
+                obj.set_mac_address(get_random_mac(self.api), my_interface)
+            else:
+                obj.set_mac_address(self.options.mac,   my_interface)
         if self.options.ip:          obj.set_ip_address(self.options.ip,     my_interface)
         if self.options.subnet:      obj.set_subnet(self.options.subnet,     my_interface)
         if self.options.gateway:     obj.set_gateway(self.options.gateway,   my_interface)
diff --git a/cobbler/remote.py b/cobbler/remote.py
index 18c3947..749298e 100644
--- a/cobbler/remote.py
+++ b/cobbler/remote.py
@@ -475,25 +475,13 @@ class CobblerXMLRPCInterface:
 
     def get_random_mac(self,token=None):
         """
-        Generate a random MAC address.
-        from xend/server/netif.py
-        Generate a random MAC address.
-        Uses OUI 00-16-3E, allocated to
-        Xensource, Inc.  Last 3 fields are random.
-        return: MAC address string
+        Wrapper for utils.get_random_mac
+
+        Used in the webui
         """
         self.log("get_random_mac",token=None)
         self._refresh()
-        mac = [ 0x00, 0x16, 0x3e,
-            random.randint(0x00, 0x7f),
-            random.randint(0x00, 0xff),
-            random.randint(0x00, 0xff) ]
-        mac = ':'.join(map(lambda x: "%02x" % x, mac))
-        systems = self.api.systems()
-        while ( systems.find(mac_address=mac) ):
-            mac = self.get_random_mac()
-
-        return mac
+        return utils.get_random_mac(self.api)
 
     def _fix_none(self,data):
         """
diff --git a/cobbler/utils.py b/cobbler/utils.py
index 7bd37ad..14019e8 100644
--- a/cobbler/utils.py
+++ b/cobbler/utils.py
@@ -17,6 +17,7 @@ import os
 import re
 import socket
 import glob
+import random
 import sub_process
 import shutil
 import string
@@ -111,6 +112,26 @@ def is_mac(strdata):
         return True
     return False
 
+def get_random_mac(api_handle):
+    """
+    Generate a random MAC address.
+    from xend/server/netif.py
+    Generate a random MAC address.
+    Uses OUI 00-16-3E, allocated to
+    Xensource, Inc.  Last 3 fields are random.
+    return: MAC address string
+    """
+    mac = [ 0x00, 0x16, 0x3e,
+        random.randint(0x00, 0x7f),
+        random.randint(0x00, 0xff),
+        random.randint(0x00, 0xff) ]
+    mac = ':'.join(map(lambda x: "%02x" % x, mac))
+    systems = api_handle.systems()
+    while ( systems.find(mac_address=mac) ):
+        mac = get_random_mac(api_handle)
+
+    return mac
+
 
 def resolve_ip(strdata):
     """
diff --git a/cobbler/webui/master.py b/cobbler/webui/master.py
index cdde14d..50f9fd1 100644
--- a/cobbler/webui/master.py
+++ b/cobbler/webui/master.py
@@ -33,8 +33,8 @@ VFN=valueForName
 currentTime=time.time
 __CHEETAH_version__ = '2.0.1'
 __CHEETAH_versionTuple__ = (2, 0, 1, 'final', 0)
-__CHEETAH_genTime__ = 1209998447.8930521
-__CHEETAH_genTimestamp__ = 'Mon May  5 10:40:47 2008'
+__CHEETAH_genTime__ = 1210208830.681746
+__CHEETAH_genTimestamp__ = 'Wed May  7 21:07:10 2008'
 __CHEETAH_src__ = 'webui_templates/master.tmpl'
 __CHEETAH_srcLastModified__ = 'Thu May  1 13:51:29 2008'
 __CHEETAH_docstring__ = 'Autogenerated by CHEETAH: The Python-Powered Template Engine'
-- 
1.5.4.1

