]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding handling of GIT submodules in lib/webhooks/deploy.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 Mar 2019 09:33:11 +0000 (10:33 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 Mar 2019 09:33:11 +0000 (10:33 +0100)
lib/webhooks/deploy.py

index fdbd0fd925fe309b777b57ac65355ed376068c5b..b6d066dccbfae1a5846ba59a21301c06143b7f11 100644 (file)
@@ -223,6 +223,7 @@ class WebhookDeployApp(BaseHookApp):
     def ensure_workingdir(self, parent_dir, workdir, branch=None):
 
         cur_dir = os.getcwd()
+        has_cloned = False
 
         try:
             os.chdir(parent_dir)
@@ -235,6 +236,7 @@ class WebhookDeployApp(BaseHookApp):
                 cmd = ['git', 'clone', self.git_ssh_url, workdir]
                 if branch:
                     cmd += ['-b', branch]
+                has_cloned = True
 
             (ret_val, stdoutdata, stderrdata) = self.handler.call(cmd, sudo=self.do_sudo)
 
@@ -251,9 +253,53 @@ class WebhookDeployApp(BaseHookApp):
                     self.error_data.append(msg)
                 self.print_out(msg)
 
+            if has_cloned:
+                os.chdir(workdir)
+
+            # Handling Submodules ...
+            if os.path.isfile('.gitmodules'):
+                self.handle_submodules()
+
         finally:
             os.chdir(cur_dir)
 
+    # -------------------------------------------------------------------------
+    def handle_submodules(self):
+
+        # Initialize the submodules recorded in the index
+        LOG.info("Initializing GIT submodules in working directory {!r}.".format(os.getcwd()))
+        cmd = ['git', 'submodule', 'init']
+        (ret_val, stdoutdata, stderrdata) = self.handler.call(cmd, sudo=self.do_sudo)
+        if stdoutdata:
+            msg = "Output:\n{}".format(to_str(stdoutdata))
+            self.print_out(msg)
+        else:
+            LOG.debug("No output.")
+
+        if stderrdata:
+            cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+            msg = "Error messages on '{c}':\n{e}".format(c=cmd_str, e=to_str(stderrdata))
+            if ret_val:
+                self.error_data.append(msg)
+            self.print_out(msg)
+
+        # Update the registered submodules to match what the superproject expects
+        LOG.info("Updating GIT submodules in working directory {!r}.".format(os.getcwd()))
+        cmd = ['git', 'submodule', 'update', '--init', '--recursive']
+        (ret_val, stdoutdata, stderrdata) = self.handler.call(cmd, sudo=self.do_sudo)
+        if stdoutdata:
+            msg = "Output:\n{}".format(to_str(stdoutdata))
+            self.print_out(msg)
+        else:
+            LOG.debug("No output.")
+
+        if stderrdata:
+            cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+            msg = "Error messages on '{c}':\n{e}".format(c=cmd_str, e=to_str(stderrdata))
+            if ret_val:
+                self.error_data.append(msg)
+            self.print_out(msg)
+
 # =============================================================================
 
 if __name__ == "__main__":