Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold an execution-environment project using init subcommand #368

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -457,6 +457,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 @@ -518,6 +519,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 @@ -560,7 +582,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,27 @@
# 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]
{% raw %}
jobs:
ee-build:
uses: ansible/ansible-content-actions/.github/workflows/ee-build.yml@main
with:
registry: ghcr.io
secrets:
registry_username: ${{ github.actor }}
registry_password: ${{ secrets.GITHUB_TOKEN }}
# Only needed if base image of execution-environment.yml file is from Red Hat (ee-minimal)
# registry_redhat_username: ${{ secrets.registry_redhat_username }}
# registry_redhat_password: ${{ secrets.registry_redhat_password }}
{% endraw %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
context/
.DS_Store
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
12 changes: 9 additions & 3 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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()
Expand All @@ -66,7 +66,7 @@ def run(self) -> None:

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 @@ -119,6 +119,7 @@ def _scaffold(self) -> None:
CreatorError: When the destination directory contains files that will be overwritten and
the user chooses not to proceed.
"""
resources: tuple[str, ...]
self.output.debug(
msg=f"started copying {self._project} skeleton to destination",
)
Expand All @@ -129,8 +130,13 @@ def _scaffold(self) -> None:
dev_file_name=self.unique_name_in_devfile(),
)

if self._project == "execution_env":
resources = (f"{self._project}_project",)
else:
resources = (f"{self._project}_project", *self.common_resources)

walker = Walker(
resources=(f"{self._project}_project", *self.common_resources),
resources=resources,
resource_id=f"{self._project}_project",
dest=self._init_path,
output=self.output,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,22 @@ def test_run_init_basic(cli: CliRunCallable, tmp_path: Path) -> None:
result = cli(f"{CREATOR_BIN} init testorg.testcol --init-path {tmp_path} --no-overwrite")
assert result.returncode != 0
assert re.search(r"The flag `--no-overwrite` restricts overwriting.", result.stderr) is not None


def test_run_init_ee(cli: CliRunCallable, tmp_path: Path) -> None:
"""Test running ansible-creator init for ee_project.

Args:
cli: cli_run function.
tmp_path: Temporary path.
"""
final_dest = f"{tmp_path}/ee_project"
cli(f"mkdir -p {final_dest}")

result = cli(
f"{CREATOR_BIN} init execution_env {final_dest}",
)
assert result.returncode == 0

# check stdout
assert re.search(r"Note: execution_env project created at", result.stdout) is not None