ABS: Support infra switch
authorhyokeun <hyokeun.jeon@samsung.com>
Thu, 3 Aug 2017 01:05:13 +0000 (10:05 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Thu, 3 Aug 2017 01:05:13 +0000 (10:05 +0900)
Change-Id: I5e60b8863277b0e0cd99eb097a9c5502c5528bc9

abs/job_abs_build.py

index f83d2f2..2bf1551 100644 (file)
@@ -118,6 +118,30 @@ class MailSender(object):
         if 'SUCCESS' not in self.email_title:
             return 4
 
+def get_gerrit_infra(switch):
+    """ Retrieve matching gerrit configuration """
+
+    merged_env = {}
+
+    if switch == 'PUBLIC':
+        gerrit_env = GerritEnv('PUBLIC_')
+        merged_env['username'] = os.getenv('ABS_GERRIT_USERNAME_PUBLIC')
+        merged_env['obs_api_url'] = os.getenv('PUBLIC_OBS_API_URL')
+        merged_env['obs_api_username'] = os.getenv('PUBLIC_OBS_API_USERNAME')
+        merged_env['obs_api_password'] = os.getenv('PUBLIC_OBS_API_PASSWD')
+    elif switch == 'LOCAL':
+        gerrit_env = GerritEnv('')
+        merged_env['username'] = os.getenv('ABS_GERRIT_USERNAME_LOCAL')
+        merged_env['obs_api_url'] = os.getenv('OBS_API_URL')
+        merged_env['obs_api_username'] = os.getenv('OBS_API_USERNAME')
+        merged_env['obs_api_password'] = os.getenv('OBS_API_PASSWD')
+
+    merged_env['hostname'] =  gerrit_env.hostname
+    merged_env['gitcache'] = gerrit_env.gitcache
+    merged_env['sshport'] = int(gerrit_env.sshport)
+
+    return merged_env
+
 def set_gerrit_event(fields):
     """ Merge all the required input arguments """
 
@@ -272,19 +296,19 @@ def fetch_source(gerrit_env, src_root, project, branch=None, tag=None, patchset=
                  build_check=None, profile=None, obs_project=None):
     """ Cloning APP source code """
 
-    print 'Cloning into bare repo %s' % os.path.join(gerrit_env.gitcache, project)
+    print 'Cloning into bare repo %s' % os.path.join(gerrit_env.get('gitcache'), project)
     clone_gitproject(project, \
-                     '%s.git' % os.path.join(gerrit_env.gitcache, project), \
-                     gerrit_hostname=gerrit_env.hostname, \
-                     gerrit_username=os.getenv('ABS_GERRIT_USERNAME'), \
-                     gerrit_sshport=gerrit_env.sshport, \
+                     '%s.git' % os.path.join(gerrit_env.get('gitcache'), project), \
+                     gerrit_hostname=gerrit_env.get('hostname'), \
+                     gerrit_username=gerrit_env.get('username'), \
+                     gerrit_sshport=gerrit_env.get('sshport'), \
                      bare=True)
     print 'Cloning clone %s' % src_root
     if not clone_gitproject(project, \
                             src_root, \
-                            gerrit_hostname=gerrit_env.hostname, \
-                            gerrit_username=os.getenv('ABS_GERRIT_USERNAME'), \
-                            gerrit_sshport=gerrit_env.sshport):
+                            gerrit_hostname=gerrit_env.get('hostname'), \
+                            gerrit_username=gerrit_env.get('username'), \
+                            gerrit_sshport=gerrit_env.get('sshport')):
         raise LocalError('Error cloning project %s' % project)
 
     working_git = Git(src_root)
@@ -515,10 +539,10 @@ def post_processing(tizen_studio, src_git, prop, data, builddir, mailer, build):
         mailer.add_message('\n\nGit push branch %s failed %s' % (new_branch, push_err_msg))
         return
 
-    gerrit_env = GerritEnv(data.get('gerrit_infra', ''))
-    remote = 'ssh://%s@%s:%d/%s' % (os.getenv('ABS_GERRIT_USERNAME'), \
-                                    gerrit_env.hostname, \
-                                    int(gerrit_env.sshport), \
+    gerrit_env = get_gerrit_infra(data.get('gerrit_infra', 'LOCAL'))
+    remote = 'ssh://%s@%s:%d/%s' % (gerrit_env.get('username'), \
+                                    gerrit_env.get('hostname'), \
+                                    gerrit_env.get('sshport'), \
                                     data.get('project'))
 
     message = '%s\n' \
@@ -572,17 +596,11 @@ def start_build(fields):
 
     print '\n"Title": "%s/%s"\n' % (os.path.basename(data.get('project')), data.get('tag'))
 
-    if fields.get('gerrit_infra').startswith('PUBLIC_'):
-        gerrit_env = GerritEnv('PUBLIC_')
-        data['gerrit_infra'] = 'PUBLIC_'
-        build = BuildService(os.getenv('PUBLIC_OBS_API_URL'), \
-                             os.getenv('PUBLIC_OBS_API_USERNAME'), \
-                             os.getenv('PUBLIC_OBS_API_PASSWD'))
-    else:
-        gerrit_env = GerritEnv('')
-        build = BuildService(os.getenv('OBS_API_URL'), \
-                             os.getenv('OBS_API_USERNAME'), \
-                             os.getenv('OBS_API_PASSWD'))
+    data['gerrit_infra'] = fields.get('gerrit_infra', 'LOCAL')
+    gerrit_env = get_gerrit_infra(data.get('gerrit_infra'))
+    build = BuildService(gerrit_env.get('obs_api_url'), \
+                         gerrit_env.get('obs_api_username'), \
+                         gerrit_env.get('obs_api_password'))
 
     mailer = MailSender()
     mailer.add_receiver(data.get('email'))
@@ -678,10 +696,10 @@ def main(argv):
     elif os.getenv('GERRIT_PROJECT', None):
 
         if os.getenv('GERRIT_NAME').startswith('public_gerrit'):
-            gerrit_infra = 'PUBLIC_'
+            gerrit_infra = 'PUBLIC'
         else:
-            gerrit_infra = ''
-        gerrit_env = GerritEnv(gerrit_infra)
+            gerrit_infra = 'LOCAL'
+        gerrit_env = get_gerrit_infra(gerrit_infra)
 
         project = os.getenv('GERRIT_PROJECT')
 
@@ -705,10 +723,10 @@ def main(argv):
         print derived_data
 
         obs_target_prjs = git_obs_map(project, derived_data.get('branch'), \
-                                      gitcache=gerrit_env.gitcache, \
-                                      gerrit_hostname=gerrit_env.hostname, \
-                                      gerrit_username=gerrit_env.username, \
-                                      gerrit_sshport=gerrit_env.sshport)
+                                      gitcache=gerrit_env.get('gitcache'), \
+                                      gerrit_hostname=gerrit_env.get('hostname'), \
+                                      gerrit_username=gerrit_env.get('username'), \
+                                      gerrit_sshport=gerrit_env.get('sshport'))
 
         index = 0
         for item in obs_target_prjs: