update current_link without downtime

fix support of check mode
fix check module when needn't update current link
pull/18777/head
Robert Lu 9 years ago committed by Matt Clay
parent 40a4247611
commit d9337fbd9c

@ -308,12 +308,27 @@ class DeployHelper(object):
self.module.fail_json(msg="%s exists but is not a symbolic link" % path) self.module.fail_json(msg="%s exists but is not a symbolic link" % path)
def create_link(self, source, link_name): 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)
os.symlink(source, link_name)
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): def remove_unfinished_file(self, new_release_path):
changed = False changed = False
@ -329,7 +344,7 @@ class DeployHelper(object):
changes = 0 changes = 0
for release in os.listdir(releases_path): 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: if self.module.check_mode:
changes += 1 changes += 1
else: else:
@ -337,6 +352,16 @@ class DeployHelper(object):
return changes 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): def cleanup(self, releases_path):
changes = 0 changes = 0
@ -415,10 +440,12 @@ def main():
changes += deploy_helper.remove_unfinished_file(facts['new_release_path']) changes += deploy_helper.remove_unfinished_file(facts['new_release_path'])
changes += deploy_helper.create_link(facts['new_release_path'], facts['current_path']) changes += deploy_helper.create_link(facts['new_release_path'], facts['current_path'])
if deploy_helper.clean: if deploy_helper.clean:
changes += deploy_helper.remove_unfinished_link(facts['path'])
changes += deploy_helper.remove_unfinished_builds(facts['releases_path']) changes += deploy_helper.remove_unfinished_builds(facts['releases_path'])
changes += deploy_helper.cleanup(facts['releases_path']) changes += deploy_helper.cleanup(facts['releases_path'])
elif deploy_helper.state == 'clean': 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.remove_unfinished_builds(facts['releases_path'])
changes += deploy_helper.cleanup(facts['releases_path']) changes += deploy_helper.cleanup(facts['releases_path'])

Loading…
Cancel
Save