diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a2ac0f..312553b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # CHANGELOG +## 1.1.1 (2022-01-22) + +### Refactor +- Rearranges the state conditionnals in the pm2_process module to remove duplicates + +### Documentation +- Fixes typos + ## 1.1.0 (2022-01-20) ### Features diff --git a/galaxy.yml b/galaxy.yml index d355429..9e75a0f 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,7 +1,7 @@ --- namespace: just1not2 name: pm2 -version: 1.1.0 +version: 1.1.1 readme: README.md authors: - Justin Béra diff --git a/plugins/module_utils/pm2.py b/plugins/module_utils/pm2.py index 1884f5e..c919089 100644 --- a/plugins/module_utils/pm2.py +++ b/plugins/module_utils/pm2.py @@ -68,7 +68,7 @@ def to_dict(self): ) def run(self, command): - ''' Runs a PM2 based command and return standard output ''' + ''' Runs a PM2 based command and returns standard output ''' rc, out, err = self.module.run_command("%s %s" % (self.executable, command)) @@ -78,9 +78,9 @@ def run(self, command): return out def get_matching_processes(self, name): - ''' Returns the list of processes whose name matches the name module parameter ''' + ''' Returns the list of processes whose name matches the provided argument ''' - # Returns every processes for specified reserved name "*" + # For reserved name "*", returns all processes if name == "*": return self.processes @@ -112,7 +112,7 @@ def create_ecosystem_file(self, process_json): return ecosystem_file_path def delete_ecosystem_file(self, ecosystem_file_path): - ''' Deletes a temporary file ''' + ''' Deletes a temporary ecosystem file ''' try: remove(ecosystem_file_path) @@ -133,7 +133,7 @@ def create_process(self, name, file): script=file ) - # Create a temporary ecosystem file for the new process + # Creates a temporary ecosystem file for the new process ecosystem_file_path = self.create_ecosystem_file(process_json) self.run("start %s" % ecosystem_file_path) @@ -166,7 +166,7 @@ def to_dict(self): ) def execute_ecosystem_action(self, action, name, file): - ''' Executes a PM2 action with an temporary ecosystem file and returns if process was deleted and recreated ''' + ''' Executes a PM2 action with a temporary ecosystem file and returns if the process had to be deleted and recreated ''' # If its script file changes, the process has to be deleted and restarted delete_and_restart = file and self.file != file @@ -181,7 +181,7 @@ def execute_ecosystem_action(self, action, name, file): name=name, script=self.file ) - # Create a temporary ecosystem file to update the process + # Creates a temporary ecosystem file to update the process temporary_file = self.env.create_ecosystem_file(process_json) self.env.run("%s %s" % (action, temporary_file)) diff --git a/plugins/modules/pm2_process.py b/plugins/modules/pm2_process.py index 90694ca..f36390b 100644 --- a/plugins/modules/pm2_process.py +++ b/plugins/modules/pm2_process.py @@ -106,9 +106,10 @@ def main(): ) changed = False - if module.params["state"] == "started": - # If there are no matching process, creates one - if not len(processes): + # If there is no matching process, creates one if state is "reloaded", "restarted" or "started" + if not len(processes): + if module.params["state"] in ["reloaded", "restarted", "started"]: + # Fails if the file option is not defined if not module.params["file"]: module.fail_json(msg="Cannot create '%s' pm2 process: 'file' option not provided" % module.params["name"]) @@ -119,75 +120,43 @@ def main(): diff["after"] += "'%s' state: started\n" % module.params["name"] changed = True - else: - # Gets the matching processes that do not follow the provided options and restarts them + else: + # Initializes the list of processes that will be affected + diff_processes = processes + + if module.params["state"] == "started": + # Only keeps the matching processes that do not follow the provided options diff_processes = [process for process in processes if ( process.status != "online" or (module.params["file"] and module.params["file"] != process.file) )] - for process in diff_processes: - process.restart(module.params["file"]) - diff["before"] += "'%s' state: %s\n" % (process.name, process.status) - diff["after"] += "'%s' state: restarted\n" % process.name - changed = True - - elif module.params["state"] == "restarted": - # If there are no matching process, creates one - if not len(processes): - # Fails if the file option is not defined - if not module.params["file"]: - module.fail_json(msg="Cannot create '%s' pm2 process: 'file' option not provided" % module.params["name"]) + elif module.params["state"] == "stopped": + # Only keeps the matching processes that are not already stopped + diff_processes = [process for process in processes if process.status != "stopped"] - # Otherwise, creates the process with the provided options - else: - env.create_process(module.params["name"], module.params["file"]) - diff["after"] += "'%s' state: started\n" % module.params["name"] - changed = True - - else: - # Restarts all matching processes - for process in processes: + for process in diff_processes: + if module.params["state"] in ["restarted", "started"]: + # Restarts the process and checks if it had to be deleted delete_and_restart = process.restart(module.params["file"]) diff["before"] += "'%s' state: %s\n" % (process.name, process.status) diff["after"] += "'%s' state: %s\n" % (process.name, "deleted and restarted" if delete_and_restart else "restarted") - changed = True - elif module.params["state"] == "reloaded": - # If there are no matching process, creates one - if not len(processes): - # Fails if the file option is not defined - if not module.params["file"]: - module.fail_json(msg="Cannot create '%s' pm2 process: 'file' option not provided" % module.params["name"]) - - # Otherwise, creates the process with the provided options - else: - env.create_process(module.params["name"], module.params["file"]) - diff["after"] += "'%s' state: started\n" % module.params["name"] - changed = True - - else: - # Reloads all matching processes - for process in processes: + elif module.params["state"] == "reloaded": + # Reloads the process and checks if it had to be deleted delete_and_restart = process.reload(module.params["file"]) diff["before"] += "'%s' state: %s\n" % (process.name, process.status) diff["after"] += "'%s' state: %s\n" % (process.name, "deleted and restarted" if delete_and_restart else "reloaded") - changed = True - elif module.params["state"] == "stopped": - # Stops all matching processes that are not already stopped or errored - for process in processes: - if process.status != "stopped": + elif module.params["state"] == "stopped": process.stop() diff["before"] += "'%s' state: %s\n" % (process.name, process.status) diff["after"] += "'%s' state: stopped\n" % process.name - changed = True - elif module.params["state"] == "deleted": - # Deletes all matching processes - for process in processes: - process.delete() - diff["before"] += "'%s' state: %s\n" % (process.name, process.status) - diff["after"] += "'%s' state: deleted\n" % process.name + elif module.params["state"] == "deleted": + process.delete() + diff["before"] += "'%s' state: %s\n" % (process.name, process.status) + diff["after"] += "'%s' state: deleted\n" % process.name + changed = True result = dict(