Generate MAC address by random.
authorZhuoX Li <zhuox.li@intel.com>
Thu, 23 Oct 2014 11:16:00 +0000 (19:16 +0800)
committerZhuoX Li <zhuox.li@intel.com>
Wed, 29 Oct 2014 02:18:36 +0000 (10:18 +0800)
Using the simple random number instead of the global variables
to generate a MAC address.

Change-Id: If3700e1374e1be4410f7504659348dddd1a24637
Fixes: 2132

job_imager.py

index 738f89d..5f78ea7 100755 (executable)
@@ -7,6 +7,7 @@ import os
 import sys
 import shutil
 import subprocess
+from random import randint
 
 from common.buildtrigger import trigger_info, trigger_next
 from common.utils import sync, set_permissions, Workdir
@@ -36,40 +37,39 @@ def convert_image_dir(image_dir, repo_name, image_name):
             return
         shutil.move(image_name, repo_name)
 
-def octet_type(num):
-    """
-    change int data to the same otctet type
-    """
-    return '%02x' % int(num)
-
-def create_mac():
-    """
-    create unique MAC address from static part=52,
-    node IP, slot number, process number
-    """
-    ssh_connection = os.getenv('SSH_CONNECTION')
-    exe_number = os.getenv('EXECUTOR_NUMBER', '1')
-    pid = os.getpid()
-    nodenum = octet_type(ssh_connection.split(' ')[2].split('.')[3])
-    slotnum = octet_type(int(exe_number)%256)
-    p_1 = pid/65536
-    p_2 = pid/256
-    p_3 = pid - p_2*256
-    return '52:%s:%s:%s:%s:%s' % \
-           (nodenum, slotnum, octet_type(p_1), octet_type(p_2), octet_type(p_3))
-
 def run_inside_vm(vm_image, vm_memory, vm_cpus, basedir):
     """
     Run mic inside VM
     basedir(usually $WORKSPACE/mic) is shared by VM as Plan9 mount
     for cache and results
     """
+    # Create a unique MAC address by EXECUTOR_NUMBER,
+    # SSH_CONNECTION, BUILD_NUMBER, PID
+    ssh_connection = os.getenv('SSH_CONNECTION')
+    try:
+        lastip = int(ssh_connection.split(' ')[2].split('.')[3])
+    except IndexError:
+        # Use random if ssh_connection has a wrong format or doesn't exist.
+        print 'ssh_connection is %s, it is a incorrect format ,'\
+              'random instead' % ssh_connection
+        lastip = randint(0x00, 0xff)
+    exeid = int(os.getenv('EXECUTOR_NUMBER', '1')) % 256
+    processid = os.getpid() % 256
+    buildid = int(os.getenv('BUILD_NUMBER',
+                            randint(0x00, 0xff))) % 256
+    # lastip from SSH_CONNECTION
+    # exeid from EXECUTOR_NUMBER
+    # processid from pid
+    # buildid from BUILD_NUMBER
+    mac_address = '52:%02x:%02x:%02x:%02x:%02x' % \
+                   (lastip, exeid, processid,
+                    buildid, randint(0x00, 0xff))
     cmd = 'qemu-system-x86_64 -machine accel=kvm:xen:tcg -name '\
           'opensuse -M pc -m %d -smp %d -vga none -drive file=%s,'\
           'snapshot=on -nographic -virtfs local,id=test_dev,'\
           'path=%s,security_model=mapped,mount_tag=share '\
           '-net nic,macaddr=%s -net user' % \
-          (vm_memory, vm_cpus, vm_image, basedir, create_mac())
+          (vm_memory, vm_cpus, vm_image, basedir, mac_address)
 
     subprocess.call(cmd, stdout=sys.stdout,
                     stderr=sys.stderr, shell=True)