Add Function : OBS project manager 41/163141/1
authorYonghee Han <onstudy@samsung.com>
Thu, 7 Dec 2017 10:59:43 +0000 (19:59 +0900)
committerYonghee Han <onstudy@samsung.com>
Thu, 7 Dec 2017 10:59:43 +0000 (19:59 +0900)
function : cleanbuild, rebuild, delete package ,
           undelete package, link package, project config

Change-Id: Ia6c9eaff56d743c8231bc8a85e689bbc9ac323bf

common/buildservice.py
debian/jenkins-scripts.install
job_obs_project_manager.py [new file with mode: 0644]
packaging/jenkins-scripts.spec

index 8826f7b..c6cab71 100755 (executable)
@@ -563,6 +563,15 @@ class BuildService(OSC):
 
         core.delete_package(self.apiurl, project, package)
 
+    def undelete_package(self, project, package):
+        """
+        undelete_package(project, package)
+
+        undelete the specific package in project
+        """
+
+        core.undelete_package(self.apiurl, project, package)
+
     def checkout(self, prj, pkg, rev='latest'):
         """ checkout the package to current dir with link expanded
         """
@@ -1400,3 +1409,48 @@ class BuildService(OSC):
             raise
         return
 
+    def rebuild(self, project, package, target=None, code=None):
+        """
+        rebuild(project, package, target, code=None)
+
+        Rebuild 'package' in 'project' for 'target'. If 'code' is specified,
+        all targets with that code will be rebuilt
+        """
+        try:
+            if target:
+                (repo, arch) = target.split('/')
+            else:
+                repo = None
+                arch = None
+            return core.rebuild(self.apiurl, project, package, repo, arch, code)
+        except urllib2.HTTPError, err:
+            raise ObsError('%s' % err)
+
+    def restartbuild(self, project, package=None, target=None):
+        """
+        restart(project, package=None, target=None)
+
+        Restart build of a package or all packages in a project
+        """
+        try:
+            if target:
+                (repo, arch) = target.split('/')
+            else:
+                repo = None
+                arch = None
+            return core.restartbuild(self.apiurl, project, package, arch, repo)
+        except urllib2.HTTPError, err:
+            raise ObsError('%s' % err)
+
+    def wipebinaries(self, project, package=None, arch=None, repo=None, code=None):
+        retry_count = 3
+        while retry_count > 0:
+            try:
+                core.wipebinaries(self.apiurl, project, package, arch, repo, code)
+                break
+            except Exception, e:
+                print 'Exception', e
+                sleep(1)
+                retry_count -= 1
+
+
index a4bb3ec..dba7df5 100644 (file)
@@ -47,3 +47,4 @@ debian/tmp/job_gbsfullbuild_buildlogs.py /var/lib/jenkins/jenkins-scripts/
 debian/tmp/job_obs_worker_auto_scailing.py /var/lib/jenkins/jenkins-scripts/
 debian/tmp/job_config_snapshot_repo_conf.py /var/lib/jenkins/jenkins-scripts/
 debian/tmp/job_notify_buildstatus.py /var/lib/jenkins/jenkins-scripts/
+debian/tmp/job_obs_project_manager.py /var/lib/jenkins/jenkins-scripts/
diff --git a/job_obs_project_manager.py b/job_obs_project_manager.py
new file mode 100644 (file)
index 0000000..8ab9189
--- /dev/null
@@ -0,0 +1,286 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (C) 2016 Samsung
+#
+#    This program is free software; you can redistribute it and/or
+#    modify it under the terms of the GNU General Public License
+#    as published by the Free Software Foundation; version 2
+#    of the License.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+"""
+This code is called by dashboard
+"""
+
+import os
+import re
+import sys
+import shutil
+
+from xml.dom import minidom
+import xml.etree.ElementTree as ElementTree
+
+from gbp.git.repository import GitRepositoryError
+from gitbuildsys.errors import ObsError
+from osc import core
+
+from common.buildservice import BuildService
+from common.buildtrigger import trigger_info, trigger_next
+from common import runner
+from common import buildmonitor_db
+from datetime import datetime
+
+
+# set default char-set endcoding to utf-8
+reload(sys)
+sys.setdefaultencoding('utf-8') # pylint: disable-msg=E1101
+
+class LocalError(Exception):
+        """Local error exception."""
+        pass
+
+def obs_project_restartbuild(build, prj, pkg, repo=None, arch=None):
+    """
+    obs project clean
+    """
+    try:
+        if pkg:
+            if not build.exists(prj, pkg):
+                raise LocalError("Not existes project %s: %s" % (prj,pkg))
+        else:
+            raise LocalError("Not existes project %s: %s" % (prj, pkg))
+
+        build.restartbuild(prj, pkg, arch, repo)
+        print "%s project restartbuild pakcage : %s " %(prj, pkg)
+    except:
+        raise LocalError("Unable to restartbuild the %s pacakge in %s project  " % (pkg, prj))
+
+def obs_project_cleanbuild(build, prj, pkg=None, repo=None, arch=None, code=None):
+    """
+    obs project clean
+    """
+    try:
+        if pkg == 'All':
+            pkg = None
+        elif pkg:
+            if not build.exists(prj, pkg):
+                raise LocalError("Not existes project %s: %s" % (prj,pkg))
+        else:
+             if not build.exists(prj):
+                raise LocalError("Not existes project %s" % (prj))
+
+        build.wipebinaries(prj, pkg, arch, repo, code)
+        print "%s project clean..." %(prj)
+    except:
+        raise LocalError("Unable to cleanbuild the %s pacakge in %s project" % (pkg, prj))
+
+def obs_project_rebuild(build, prj, pkg=None, repo=None, arch=None, code=None):
+    """
+    obs project rebuild
+    """
+
+    #code = 'failed'
+    try:
+        if pkg == 'All':
+            pkg = None
+        elif pkg:
+            if not build.exists(prj, pkg):
+                raise LocalError("Not existes project %s: %s" % (prj,pkg))
+        else:
+             if not build.exists(prj):
+                raise LocalError("Not existes project %s" % (prj))
+
+        build.rebuild(prj, pkg, None, code)
+        print "%s project rebuilding..." %(prj)
+    except:
+        raise LocalError("Unable to rebuild the %s pacakge in %s project" % (pkg, prj))
+
+def obs_project_delete_pkg(build, prj, pkg):
+    """
+    obs project delete package
+    """
+    try:
+        if pkg:
+            if not build.exists(prj, pkg):
+                raise LocalError("Not existes project %s: %s" % (prj,pkg))
+        else:
+            raise LocalError("Not existes project %s: %s" % (prj, pkg))
+
+        build.delete_package(prj, pkg)
+        print "%s project delete package : %s." %(prj, pkg)
+    except:
+        raise LocalError("Unable to delete the %s pacakge in %s project" % (pkg, prj))
+
+def obs_project_undelete_pkg(build, prj, pkg):
+    """
+    obs project undelete package
+    """
+    try:
+        if pkg:
+            if not build.exists(prj, pkg):
+                raise LocalError("Not existes project %s: %s" % (prj,pkg))
+        else:
+            raise LocalError("Not existes project %s: %s" % (prj, pkg))
+
+        build.undelete_package(prj, pkg)
+        print "%s project undelete package : %s." %(prj, pkg)
+    except:
+        raise LocalError("Unable to undelete the %s pacakge in %s project" % (pkg, prj))
+
+def obs_project_link_pkg(build, prj, src_pkg, dst_pkg):
+    """
+    obs project link package
+    """
+    try:
+        build.create_link_pac(prj, src_pkg, prj, dst_pkg)
+        print "%s project link package : %s -> %s" %(prj, src_pkg, dst_pkg)
+    except:
+        raise LocalError("Unable to linkpac the %s -> %s pacakge in %s project" % (src_pkg, dst_pkg, prj))
+
+def obs_project_config(build, prj, config):
+    """
+    obs create project
+    """
+    try:
+        if build.exists(prj):
+            # set project config
+            build.set_project_config(prj, config)
+            print "\nTarget project %s: config" %(prj)
+            return True
+        else:
+            raise LocalError("Unable to edit project config :  %s project" % (prj))
+    except:
+        raise LocalError("Unable to edit project config :  %s project" % (prj))
+
+def obs_project_create_prj(build, prj, info=None, meta=None, config=None):
+    """
+    obs create project
+    """
+    try:
+        if not build.exists(prj):
+            try:
+                build.create_project(prj, None, description=json.dumps(info))
+            except:
+                raise LocalError("Unable to create project %s: %s" % (prj))
+
+                if meta:
+                    # set meta
+                    xml_meta = ElementTree.fromstringlist(meta)
+                    #change the target project name
+                    xml_meta.set('name',prj)
+
+            print ElementTree.tostring(xml_meta)
+            build.set_meta(ElementTree.tostring(xml_meta), prj)
+            # set project config
+            #print config
+            build.set_project_config(prj, config)
+
+            #enable debuginfo flag
+            build.disable_build_flag(prj, repo = None, flag="debuginfo", status="enable")
+            print "\nTarget project %s created" %(prj)
+            return True
+        else:
+            print "\nTarget project %s exist" %(prj)
+            return False
+    except:
+        raise LocalError("Unable to create project %s: %s" % (prj))
+
+
+def send_status_buildmonitor(id, error_string):
+    """
+    send status to build monitor
+    """
+    buildmonitor_db.connect_db()
+    if error_string is None or error_string == "":
+        status = "succeeded"
+        error_string = "None"
+    else:
+        status = "failed"
+
+    query = "UPDATE project_mgr_log SET status=%s, reason=%s WHERE id=%s"
+    query_data = ( status, error_string, id)
+    buildmonitor_db.do_query(query, query_data)
+
+    buildmonitor_db.connect_db()
+
+def main():
+
+    """
+    Script entry point.
+    """
+
+    print '---[JOB STARTED: obs project manager]-------------------------'
+    id = os.getenv("ID")
+    submitter = os.getenv("SUBMITTER")
+    decision = os.getenv("DECISION")
+    project = os.getenv("PROJECT")
+    package = os.getenv("PACKAGE")
+    config = os.getenv("CONFIG")
+
+    src_pkg = os.getenv("SRCPKG")
+    dst_pkg = os.getenv("DSTPKG")
+
+    print "DECISION = ", decision
+    print "PROJECT = ", project
+    print "PACKAGE = ", package
+
+    obs_api = os.getenv("OBS_API_URL")
+    obs_user = os.getenv("OBS_API_USERNAME")
+    obs_passwd = os.getenv("OBS_API_PASSWD")
+
+    build = BuildService(obs_api, obs_user, obs_passwd)
+
+    #check project
+    #Check if we've got required field in env
+    failures = ''
+    try:
+        if decision is None :
+            raise LocalError("Unable to Projec manager...")
+
+        if decision == 'rebuild':
+           obs_project_rebuild(build, project, package)
+        elif decision == 'clean':
+           obs_project_cleanbuild(build, project, package)
+        elif decision == 'restart':
+           obs_project_restartbuild(build, project, package)
+        elif decision == 'delpkg':
+           obs_project_delete_pkg(build, project, package)
+        elif decision == 'undelpkg':
+           obs_project_undelete_pkg(build, project, package)
+        elif decision == 'linkpkg':
+           obs_project_link_pkg(build, project, src_pkg, dst_pkg)
+        elif decision == 'config':
+           obs_project_config(build, project, config)
+        elif decision == 'create':
+           obs_project_create(build, project, config)
+        else:
+            print "%s decision is not defined" %(decision)
+            failures = "%s decision is not defined" %(decision)
+    except LocalError, exc:
+        failures = (str(exc))
+    except ObsError, error:
+        failures = (str(exc))
+    finally:
+        send_status_buildmonitor(id, failures)
+    
+    return 0
+
+if __name__ == '__main__':
+    try:
+        exit_status = main()
+    except Exception as err:
+        print err
+        exit_status = 1
+
+sys.exit(exit_status)
index 5d24c74..685c887 100644 (file)
@@ -181,6 +181,7 @@ fi
 %{destdir}/job_obs_worker_auto_scailing.py
 %{destdir}/job_config_snapshot_repo_conf.py
 %{destdir}/job_notify_buildstatus.py
+%{destdir}/job_obs_project_manager.py
 
 %files common
 %defattr(-,jenkins,jenkins)