Skip to content

Commit

Permalink
Release 1.1.1 (#3)
Browse files Browse the repository at this point in the history
* refactor(pm2_process): re-arrange state conditionals

* docs(pm2 util): fix typos

* docs: version 1.1.1
  • Loading branch information
just1not2 authored Jan 22, 2022
1 parent 4f3df2c commit 1a58d16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 63 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
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.0
version: 1.1.1
readme: README.md
authors:
- Justin Béra <[email protected]>
Expand Down
14 changes: 7 additions & 7 deletions plugins/module_utils/pm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
79 changes: 24 additions & 55 deletions plugins/modules/pm2_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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(
Expand Down

0 comments on commit 1a58d16

Please sign in to comment.