diff --git a/CHANGELOG.md b/CHANGELOG.md index 312553b..c2bef0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 1.2.0 (2022-01-25) + +### Features +- Adds the chdir option in the pm2_process module to change the working directory of the script +- Adds a new return value cwd in the pm2_facts module that contains the working directory of the script + + ## 1.1.1 (2022-01-22) ### Refactor @@ -8,6 +15,7 @@ ### Documentation - Fixes typos + ## 1.1.0 (2022-01-20) ### Features diff --git a/docs/pm2_facts_module.rst b/docs/pm2_facts_module.rst index 1c167ae..d5f6e28 100644 --- a/docs/pm2_facts_module.rst +++ b/docs/pm2_facts_module.rst @@ -235,6 +235,25 @@ Facts returned by this module are added/updated in the hostvars host facts and c
The list of PM2 processes.
+ +    +    + + cwd +
+ string +
+
+ added in 1.2 +
+ + success + +
The path to the directory from which the script runs.
+
Sample:
+
"/home/user"
+ +       diff --git a/docs/pm2_process_module.rst b/docs/pm2_process_module.rst index 57ad05d..46c8496 100644 --- a/docs/pm2_process_module.rst +++ b/docs/pm2_process_module.rst @@ -65,6 +65,22 @@ Parameters
If this option is set to no, the module will return an error with out-of-date in-memory PM2.
+ + + chdir +
+ path +
+
+ added in 1.2 +
+ + + + +
The working directory of the script.
+ + executable diff --git a/galaxy.yml b/galaxy.yml index 9e75a0f..0ed7a7a 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,7 +1,7 @@ --- namespace: just1not2 name: pm2 -version: 1.1.1 +version: 1.2.0 readme: README.md authors: - Justin Béra diff --git a/plugins/module_utils/pm2.py b/plugins/module_utils/pm2.py index c919089..8689754 100644 --- a/plugins/module_utils/pm2.py +++ b/plugins/module_utils/pm2.py @@ -42,6 +42,7 @@ def __init__(self, module): self.processes.append( Pm2Process( self, + cwd=process["pm2_env"]["pm_cwd"], file=process["pm2_env"]["pm_exec_path"], id=process["pm_id"], interpreter=process["pm2_env"]["exec_interpreter"], @@ -120,7 +121,7 @@ def delete_ecosystem_file(self, ecosystem_file_path): except Exception as err: self.module.fail_json(msg="Cannot delete temporary ecosystem file '%s'" % ecosystem_file_path, err=repr(err)) - def create_process(self, name, file): + def create_process(self, name, file, chdir): ''' Create a PM2 process ''' # Raises an error if the specified name is "*" (reserved) @@ -133,6 +134,10 @@ def create_process(self, name, file): script=file ) + # Adds current working directory if it is defined + if chdir is not None: + process_json["cwd"] = chdir + # Creates a temporary ecosystem file for the new process ecosystem_file_path = self.create_ecosystem_file(process_json) @@ -154,6 +159,7 @@ def to_dict(self): ''' Returns a dictionary translation of the process ''' return dict( + cwd=self.cwd, file=self.file, id=self.id, interpreter=self.interpreter, @@ -165,22 +171,33 @@ def to_dict(self): status=self.status ) - def execute_ecosystem_action(self, action, name, file): + def execute_ecosystem_action(self, action, name, file, chdir): ''' 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 + delete_and_restart = ( + (file and self.file != file) or + (chdir and self.cwd != chdir) + ) if not self.module.check_mode: if delete_and_restart: self.delete() - self.env.create_process(name, file) + self.env.create_process( + name, + file, + chdir if chdir else self.cwd + ) else: process_json = dict( name=name, script=self.file ) + + # Defines current working directory + process_json["cwd"] = chdir if chdir is not None else self.cwd + # Creates a temporary ecosystem file to update the process temporary_file = self.env.create_ecosystem_file(process_json) @@ -189,15 +206,15 @@ def execute_ecosystem_action(self, action, name, file): return delete_and_restart - def restart(self, file): + def restart(self, file, chdir): ''' Restarts the process ''' - return self.execute_ecosystem_action("restart", self.name, file) + return self.execute_ecosystem_action("restart", self.name, file, chdir) - def reload(self, file): + def reload(self, file, chdir): ''' Reloads the process ''' - return self.execute_ecosystem_action("reload", self.name, file) + return self.execute_ecosystem_action("reload", self.name, file, chdir) def stop(self): ''' Stops the process ''' diff --git a/plugins/modules/pm2_facts.py b/plugins/modules/pm2_facts.py index 9fa3de0..3d77785 100644 --- a/plugins/modules/pm2_facts.py +++ b/plugins/modules/pm2_facts.py @@ -81,6 +81,13 @@ type: list elements: dict contains: + cwd: + description: + - The path to the directory from which the script runs. + returned: success + type: str + sample: /home/user + version_added: "1.2.0" file: description: - The path to the process script file. diff --git a/plugins/modules/pm2_process.py b/plugins/modules/pm2_process.py index f36390b..03da370 100644 --- a/plugins/modules/pm2_process.py +++ b/plugins/modules/pm2_process.py @@ -24,6 +24,11 @@ - If this option is set to no, the module will return an error with out-of-date in-memory PM2. type: bool default: yes + chdir: + description: + - The working directory of the script. + type: path + version_added: "1.2.0" executable: description: - The explicit executable or pathname for the pm2 executable. @@ -90,6 +95,7 @@ def main(): module = AnsibleModule( argument_spec=dict( allow_update=dict(type="bool", default=True), + chdir=dict(type="path"), executable=dict(type="path"), file=dict(type="path"), name=dict(type="str", required=True), @@ -116,7 +122,7 @@ def main(): # Otherwise, creates the process with the provided options else: - env.create_process(module.params["name"], module.params["file"]) + env.create_process(module.params["name"], module.params["file"], module.params["chdir"]) diff["after"] += "'%s' state: started\n" % module.params["name"] changed = True @@ -128,7 +134,8 @@ def main(): # 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) + (module.params["file"] and module.params["file"] != process.file) or + (module.params["chdir"] and module.params["chdir"] != process.cwd) )] elif module.params["state"] == "stopped": # Only keeps the matching processes that are not already stopped @@ -137,13 +144,13 @@ def main(): 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"]) + delete_and_restart = process.restart(module.params["file"], module.params["chdir"]) 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") elif module.params["state"] == "reloaded": # Reloads the process and checks if it had to be deleted - delete_and_restart = process.reload(module.params["file"]) + delete_and_restart = process.reload(module.params["file"], module.params["chdir"]) 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")