From 822eb97ec8baa7e98cba2dd9b6b0b8e38a6d687c Mon Sep 17 00:00:00 2001 From: Robert Lu Date: Sat, 5 Dec 2015 19:03:51 +0800 Subject: [PATCH] update current_link without downtime fix support of check mode fix check module when needn't update current link --- web_infrastructure/deploy_helper.py | 39 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/web_infrastructure/deploy_helper.py b/web_infrastructure/deploy_helper.py index beec56419e9..6eefbd1f891 100644 --- a/web_infrastructure/deploy_helper.py +++ b/web_infrastructure/deploy_helper.py @@ -308,12 +308,27 @@ class DeployHelper(object): self.module.fail_json(msg="%s exists but is not a symbolic link" % path) def create_link(self, source, link_name): - if not self.module.check_mode: - if os.path.islink(link_name): - os.unlink(link_name) - os.symlink(source, link_name) + changed = False - return True + if os.path.islink(link_name): + norm_link = os.path.normpath(os.path.realpath(link_name)) + norm_source = os.path.normpath(os.path.realpath(source)) + if norm_link == norm_source: + changed = False + else: + changed = True + if not self.module.check_mode: + tmp_link_name = link_name + '.' + self.unfinished_filename + if os.path.islink(tmp_link_name): + os.unlink(tmp_link_name) + os.symlink(source, tmp_link_name) + os.rename(tmp_link_name, link_name) + else: + changed = True + if not self.module.check_mode: + os.symlink(source, link_name) + + return changed def remove_unfinished_file(self, new_release_path): changed = False @@ -329,7 +344,7 @@ class DeployHelper(object): changes = 0 for release in os.listdir(releases_path): - if (os.path.isfile(os.path.join(releases_path, release, self.unfinished_filename))): + if os.path.isfile(os.path.join(releases_path, release, self.unfinished_filename)): if self.module.check_mode: changes += 1 else: @@ -337,6 +352,16 @@ class DeployHelper(object): return changes + def remove_unfinished_link(self, path): + changed = False + + tmp_link_name = os.path.join(path, self.release + '.' + self.unfinished_filename) + if not self.module.check_mode and os.path.exists(tmp_link_name): + changed = True + os.remove(tmp_link_name) + + return changed + def cleanup(self, releases_path): changes = 0 @@ -415,10 +440,12 @@ def main(): changes += deploy_helper.remove_unfinished_file(facts['new_release_path']) changes += deploy_helper.create_link(facts['new_release_path'], facts['current_path']) if deploy_helper.clean: + changes += deploy_helper.remove_unfinished_link(facts['path']) changes += deploy_helper.remove_unfinished_builds(facts['releases_path']) changes += deploy_helper.cleanup(facts['releases_path']) elif deploy_helper.state == 'clean': + changes += deploy_helper.remove_unfinished_link(facts['path']) changes += deploy_helper.remove_unfinished_builds(facts['releases_path']) changes += deploy_helper.cleanup(facts['releases_path'])