1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

update current_link without downtime

fix support of check mode
fix check module when needn't update current link
This commit is contained in:
Robert Lu 2015-12-05 19:03:51 +08:00 committed by Matt Clay
parent 40a4247611
commit d9337fbd9c

View file

@ -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:
changed = False
if os.path.islink(link_name):
os.unlink(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 True
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'])