Skip to content

Commit

Permalink
feat(ANSIBLE): enhance process logging
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Feb 10, 2023
1 parent f754d1b commit 222e934
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 53 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ If you'd like to try it out, head over to the [Mac Maker Releases](https://githu

### OK, but you still didn't tell me how to get started...

Are you on Monterey? It may not ship with python anymore! We better check:
- open a terminal and type `python3`, and if prompted to install the [x-code](https://developer.apple.com/xcode/) cli tools click `install`.
- this is less than ideal, but it gets you into a compatible state quickly

For Catalina, Big Sur and Monterey (once you've confirmed [python](https://python.org) is present):
- Copy the `mac_maker` binary to the OSX machine you'd like to put under configuration management.
- If you have a working internet connection, you can start working with `Mac Maker Profiles`.
- To try creating your own `Profile`, check out [this](https://github.com/osx-provisioner/profile-generator) repository.
Expand Down
23 changes: 23 additions & 0 deletions documentation/source/project/0.python.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Ensure Python is Installed
==========================

==================
Monterey and Later
==================

Apple has removed Python from the newest stock system installs.

Since `Ansible <https://www.ansible.com/>`_ uses a `Python <https://www.python.org/>`_ interpreter to do the heavy lifting, we'll need to perform a manual validation step prior to getting started on a new OS install:

Open the `Terminal <https://wikipedia.org/wiki/Terminal_(macOS)>`_ app:

.. code-block:: console
python3
Once you hit ENTER, one of two things will happen:
1. Python will start. Great don't sweat it! You're good to go.
2. A popup to install the `X-Code Cli Tools <https://developer.apple.com/xcode/>`_ will appear. This also isn't so bad, you'll need them to use `homebrew <https://brew.sh/>`_ on your Mac. It will also install `Python <https://www.python.org/>`_, so click **Install** and once it's finished you can begin using this tool.

For users of Catalina, or Big Sur: don't worry about this.

68 changes: 57 additions & 11 deletions mac_maker/ansible_controller/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
from click_shell.exceptions import ClickShellCleanExit, ClickShellUncleanExit
from .. import config
from ..utilities.cli import was_started_without_shell
from ..utilities import cli as cli_utilities
from ..utilities.state import TypeState
from . import environment

Expand Down Expand Up @@ -40,14 +40,15 @@ def spawn(self, command: str) -> None:
self.log.debug("AnsibleProcess: Preparing to Fork for Ansible Process.")
pid = os.fork()
if pid == 0:
self._forked_process(command)
self._forked_process(command, pid)
else:
self._main_process(command, pid)

def _forked_process(self, command: str) -> None:
def _forked_process(self, command: str, pid: int) -> None:
try:
self.log.debug(
"AnsibleProcess: Forked process is now executing: %s.",
"AnsibleProcess - PID: %s: Forked process is now executing: %s.",
pid,
command,
)

Expand All @@ -59,12 +60,32 @@ def _forked_process(self, command: str) -> None:

instance = ansible_cli_class(shlex.split(command))

self.log.debug(
(
"AnsibleProcess - PID: %s: Forked process Ansible CLI Class "
"instance has been created: %s."
),
pid,
str(instance),
)

try:
self.log.debug(
(
"AnsibleProcess - PID: %s: Forked process Ansible CLI Class "
"instance is calling run."
),
pid,
)
instance.run()
self.log.debug(
"AnsibleProcess - PID: %s: Forked process has finished.",
pid,
)
except Exception:
traceback.print_exc()
raise ClickShellUncleanExit() # pylint: disable=raise-missing-from
self._perform_clean_exit()
self._perform_clean_exit(pid)
except KeyboardInterrupt:
raise ClickShellUncleanExit() from KeyboardInterrupt

Expand All @@ -78,17 +99,42 @@ def _execution_location(self) -> None:
def _main_process(self, command: str, pid: int) -> None:
try:
_, exit_status = os.waitpid(pid, 0)
if os.WEXITSTATUS(exit_status):
status_code = os.WEXITSTATUS(exit_status)
self.log.debug(
"AnsibleProcess - PID: %s: Waited, and received exit code: %s.",
pid,
status_code,
)
if status_code:
click.echo("ANSIBLE ERROR: Non zero exit code.")
click.echo(f"COMMAND: {command}")
self.log.error("AnsibleProcess: Forked process reports error.")
self.log.error(
(
"AnsibleProcess - PID: %s: Forked process has reported an "
"error state."
),
pid,
)
raise ClickShellUncleanExit()
self.log.debug("AnsibleProcess: Forked process has completed.")
self.log.debug(
(
"AnsibleProcess - PID: %s: Forked process has reported no "
"error state."
),
pid,
)
except KeyboardInterrupt:
self.log.error("AnsibleProcess: Keyboard Interrupt Intercepted.")
self.log.error(
"AnsibleProcess - PID: %s: Keyboard Interrupt Intercepted.",
pid,
)
raise ClickShellUncleanExit() from KeyboardInterrupt

def _perform_clean_exit(self) -> None:
if was_started_without_shell():
def _perform_clean_exit(self, pid: int) -> None:
if cli_utilities.was_started_without_shell():
self.log.warning(
"AnsibleProcess - PID: %s: Terminating process.",
pid,
)
sys.exit(0)
raise ClickShellCleanExit()
Loading

0 comments on commit 222e934

Please sign in to comment.