Skip to content

Commit

Permalink
Release 1.2.0 (#4)
Browse files Browse the repository at this point in the history
* feat(facts): add cwd  to process vars

* feat(cwd): adds the chdir pm2_process option

* docs: version 1.2.0

* fix(docs): unique description of chdir option
  • Loading branch information
just1not2 authored Jan 25, 2022
1 parent 1a58d16 commit 444be0f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,6 +15,7 @@
### Documentation
- Fixes typos


## 1.1.0 (2022-01-20)

### Features
Expand Down
19 changes: 19 additions & 0 deletions docs/pm2_facts_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ Facts returned by this module are added/updated in the hostvars host facts and c
<div>The list of PM2 processes.</div>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;</td>
<td colspan="2">
<b>cwd</b>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
<div style="font-size: small">
<span style="color: green; font-style: italic">added in 1.2</span>
</div>
</td>
<td>success</td>
<td>
<div>The path to the directory from which the script runs.</div>
<div style="font-size: smaller"><b>Sample:</b></div>
<div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">"/home/user"</div>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;</td>
Expand Down
16 changes: 16 additions & 0 deletions docs/pm2_process_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ Parameters
<div>If this option is set to no, the module will return an error with out-of-date in-memory PM2.</div>
</td>
</tr>
<tr>
<td>
<b>chdir</b>
<div style="font-size: small">
<span style="color: purple">path</span>
</div>
<div style="font-size: small">
<span style="color: green; font-style: italic">added in 1.2</span>
</div>
</td>
<td>
</td>
<td>
<div>The working directory of the script.</div>
</td>
</tr>
<tr>
<td>
<b>executable</b>
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
namespace: just1not2
name: pm2
version: 1.1.1
version: 1.2.0
readme: README.md
authors:
- Justin Béra <[email protected]>
Expand Down
33 changes: 25 additions & 8 deletions plugins/module_utils/pm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -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 '''
Expand Down
7 changes: 7 additions & 0 deletions plugins/modules/pm2_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions plugins/modules/pm2_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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")

Expand Down

0 comments on commit 444be0f

Please sign in to comment.