Skip to content

Commit

Permalink
Scaffold an execution-environment project using init subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
shatakshiiii committed Feb 25, 2025
1 parent 72e0960 commit 351aa71
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
"cwd": "${workspaceFolder}/src",
"justMyCode": false
},
{
"name": "Debug subcommand: init (ee_project)",
"type": "debugpy",
"request": "launch",
"module": "ansible_creator",
"args": [
"init",
"execution_env",
"/Users/shamishr/Ansible/test_stuff/my_ee_proj"
],
"cwd": "${workspaceFolder}/src",
"justMyCode": false
},
{
"name": "Debug subcommand: add",
"type": "debugpy",
Expand Down
24 changes: 23 additions & 1 deletion src/ansible_creator/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def _init(self, subparser: SubParser[ArgumentParser]) -> None:

self._init_collection(subparser=subparser)
self._init_playbook(subparser=subparser)
self._init_ee_project(subparser=subparser)

def _init_collection(self, subparser: SubParser[ArgumentParser]) -> None:
"""Initialize an Ansible collection.
Expand Down Expand Up @@ -502,6 +503,27 @@ def _init_playbook(self, subparser: SubParser[ArgumentParser]) -> None:
self._add_args_common(parser)
self._add_args_init_common(parser)

def _init_ee_project(self, subparser: SubParser[ArgumentParser]) -> None:
"""Initialize an EE project.
Args:
subparser: The subparser to add EE project to
"""
parser = subparser.add_parser(
"execution_env",
help="Create a new execution environment project.",
formatter_class=CustomHelpFormatter,
)
parser.add_argument(
"init_path",
metavar="path",
nargs="?",
help="The destination directory for the EE project.",
)

self._add_args_common(parser)
self._add_args_init_common(parser)

def _valid_collection_name(self, collection: str) -> str:
"""Validate the collection name.
Expand Down Expand Up @@ -544,7 +566,7 @@ def handle_deprecations(self) -> bool: # noqa: C901
parser.add_argument("--init-path", help="")
args, extras = parser.parse_known_args()

if args.collection in ["playbook", "collection"]:
if args.collection in ["playbook", "collection", "execution_env"]:
return True
if args.project:
msg = "The `project` flag is no longer needed and will be removed."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Combine workflow for pull-request, push-to-main and release events.

name: Execution environment build

on:
pull_request_target:
branches:
- main
types: [opened, reopened, synchronize]
push:
branches:
- main
release:
types: [published]

jobs:
ee-build:
uses: ansible/ansible-content-actions/.github/workflows/ee-build.yml@main
with:
registry: ghcr.io
# TODO: add secrests back to this section
2 changes: 2 additions & 0 deletions src/ansible_creator/resources/ee_project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
context/
.DS_Store
1 change: 1 addition & 0 deletions src/ansible_creator/resources/ee_project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## This is a sample execution environment project to build and publish your EE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
version: 3

images:
base_image:
name: quay.io/fedora/fedora:41

dependencies:
# Use python3
python_interpreter:
package_system: python3
python_path: /usr/bin/python3

ansible_core:
package_pip: ansible-core

ansible_runner:
package_pip: ansible-runner

system:
- openssh-clients
- sshpass

python:
- ansible-navigator
- boto3
- requests

galaxy:
collections:
- name: ansible.posix
- name: ansible.utils

additional_build_steps:
append_base:
- RUN $PYCMD -m pip install -U pip

options:
tags:
- ansible_sample_ee
66 changes: 63 additions & 3 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ def __init__(
def run(self) -> None:
"""Start scaffolding skeleton."""
self._construct_init_path()
self.output.debug(msg=f"final collection path set to {self._init_path}")
self.output.debug(msg=f"final destination path set to {self._init_path}")

if self._init_path.exists():
self.init_exists()
self._init_path.mkdir(parents=True, exist_ok=True)

self._scaffold()
if self._project in ("playbook", "collection"):
self._scaffold()
if self._project == "execution_env":
self._scaffold_ee_proj()

def _construct_init_path(self) -> None:
"""Construct the init path based on project type."""
if self._project == "playbook":
if self._project in ("playbook", "execution_env"):
return

if (
Expand Down Expand Up @@ -171,3 +174,60 @@ def _scaffold(self) -> None:
raise CreatorError(msg)

self.output.note(f"{self._project} project created at {self._init_path}")

def _scaffold_ee_proj(self) -> None:
"""Scaffold an execution environment project.
Raises:
CreatorError: When the destination directory contains files that will be overwritten and
the user chooses not to proceed.
"""
self.output.debug(
msg=f"started copying {self._project} skeleton to destination",
)
template_data = TemplateData(
creator_version=self._creator_version,
)

walker = Walker(
resources=("ee_project",),
resource_id="ee_project",
dest=self._init_path,
output=self.output,
templar=self._templar,
template_data=template_data,
)
paths = walker.collect_paths()

copier = Copier(
output=self.output,
)

if self._no_overwrite and paths.has_conflicts():
msg = (
"The flag `--no-overwrite` restricts overwriting."
"\nThe destination directory contains files that can be overwritten."
"\nPlease re-run ansible-creator with --overwrite to continue."
)
raise CreatorError(msg)

if not paths.has_conflicts() or self._force or self._overwrite:
copier.copy_containers(paths)
self.output.note(f"{self._project} project created at {self._init_path}")
return

if not self._overwrite:
question = (
"Files in the destination directory will be overwritten. Do you want to proceed?"
)
answer = ask_yes_no(question)
if answer:
copier.copy_containers(paths)
else:
msg = (
"The destination directory contains files that will be overwritten."
" Please re-run ansible-creator with --overwrite to continue."
)
raise CreatorError(msg)

self.output.note(f"{self._project} project created at {self._init_path}")

0 comments on commit 351aa71

Please sign in to comment.