Deal properly with existing links during lib symlinking

Change-Id: I9cb835605480cf678caf7e5156d40e864240d0ba
diff --git a/container-images/gerrit-init/tools/gerrit-initializer/initializer/tasks/download_plugins.py b/container-images/gerrit-init/tools/gerrit-initializer/initializer/tasks/download_plugins.py
index e6620cd..9d5292b 100755
--- a/container-images/gerrit-init/tools/gerrit-initializer/initializer/tasks/download_plugins.py
+++ b/container-images/gerrit-init/tools/gerrit-initializer/initializer/tasks/download_plugins.py
@@ -119,10 +119,22 @@
     def _symlink_plugins_to_lib(self):
         if not os.path.exists(self.lib_dir):
             os.makedirs(self.lib_dir)
+        else:
+            for f in os.listdir(self.lib_dir):
+                path = os.path.join(self.lib_dir, f)
+                if (
+                    os.path.islink(path)
+                    and os.path.splitext(f)[0] not in self.config.install_as_library
+                ):
+                    os.unlink(path)
+                    LOG.info("Removed symlink %s", f)
         for lib in self.config.install_as_library:
             plugin_path = os.path.join(self.plugin_dir, "%s.jar" % lib)
             if os.path.exists(plugin_path):
-                os.symlink(plugin_path, os.path.join(self.lib_dir, "%s.jar" % lib))
+                try:
+                    os.symlink(plugin_path, os.path.join(self.lib_dir, "%s.jar" % lib))
+                except FileExistsError:
+                    continue
             else:
                 raise FileNotFoundError(
                     "Could not find plugin %s to symlink to lib-directory." % lib