Add sign vm for imager 31/161031/1
authorYonghee Han <onstudy@samsung.com>
Tue, 21 Nov 2017 06:28:57 +0000 (15:28 +0900)
committerYonghee Han <onstudy@samsung.com>
Tue, 21 Nov 2017 06:28:57 +0000 (15:28 +0900)
mic-appliance-sign and USE_VM=SIGN

Change-Id: I41f6a165f6a87003f1668aae09c02d2ed446aef0

job_imager.py

index 786bc8d..05ff7a9 100755 (executable)
@@ -200,6 +200,43 @@ def run_inside_vm(vm_image, vm_memory, vm_cpus, basedir):
     except IOError:
         return -1
 
+def run_inside_sign_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
+    """
+    if not os.getenv('SIGN_VM_MAC_ADDRESS') and \
+       not os.getenv('SIGN_VM_USER_NET') and \
+       not os.getenv('SIGN_VM_USER_HOST') and \
+       not os.getenv('SIGN_VM_USER_DHCPSTART'):
+       print "Error : SIGN enviroment ..."
+       return -1
+
+    mac_address = os.getenv('SIGN_VM_MAC_ADDRESS')
+    user = "net=%s,host=%s,dhcpstart=%s" %(os.getenv('SIGN_VM_USER_NET'), \
+                                           os.getenv('SIGN_VM_USER_HOST'), \
+                                           os.getenv('SIGN_VM_USER_DHCPSTART'))
+    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,%s' % \
+          (vm_memory, vm_cpus, vm_image, basedir, mac_address, user)
+    print "cmd=%s" %(cmd)
+    sys.stdout.flush()
+    subprocess.call(cmd, stdout=sys.stdout,
+                    stderr=sys.stderr, shell=True)
+    # read mic status from file
+    try:
+        with open(os.path.join(basedir, 'status')) as statusf:
+            status = statusf.read().strip()
+        if status == 'success':
+            return 0
+        else:
+            return -1
+    except IOError:
+        return -1
 
 def main():
     """The main body"""
@@ -239,7 +276,7 @@ def main():
     with open(ksf, 'w') as ks_fh:
         ks_fh.write(fields["kickstart"])
 
-    if int(os.getenv('USE_VM')):
+    if os.getenv('USE_VM','0') == '1':
         vm_image = os.getenv("VM_IMAGE",
                              os.path.join(os.getenv('JENKINS_HOME'),
                                           'mic-appliance'))
@@ -258,6 +295,26 @@ def main():
                             int(os.getenv("VM_CPUS", 8)), basedir)
         # workaround for qemu/9p bug in mapping permissions
         set_permissions(outdir, (0644, 0755))
+    elif os.getenv('USE_VM','0') == 'SIGN':
+        vm_image = os.getenv("VM_IMAGE",
+                             os.path.join(os.getenv('JENKINS_HOME'),
+                                          'mic-appliance-sign'))
+        # check if tarball exists
+        if not vm_image or not os.access(vm_image, os.R_OK):
+            print 'SIGN VM image %s is not found' % vm_image
+            return -1
+
+        # Add --release option to mic command line
+        with open(os.path.join(outdir, 'command'), 'w') as fcmdl:
+            fcmdl.write('--release %s' % build_id)
+
+        print 'starting mic inside SIGN VM to create image'
+        sys.stdout.flush()
+        ret = run_inside_sign_vm(vm_image, int(os.getenv("VM_MEMORY", 8192)),
+                            int(os.getenv("VM_CPUS", 8)), basedir)
+        # workaround for qemu/9p bug in mapping permissions
+        set_permissions(outdir, (0644, 0755))
+
     else:
         log = os.path.join(outdir, '%s_%s.log' % (build_id, name))
         cache = os.path.join(basedir, 'cache')
@@ -301,7 +358,7 @@ def main():
                       fields['repo'], fields['name'])
 
     # Copy logfile to sync directory
-    if not int(os.getenv('USE_VM')):
+    if os.getenv('USE_VM','0') == '0':
         try:
             target_log = os.path.join(outdir, build_id, 'images', \
                                       fields['repo'], name, os.path.basename(log))