Skip to content

Commit

Permalink
Add support to scaffold devcontainer (#320)
Browse files Browse the repository at this point in the history
Co-authored-by: abhikdps <abanand@redhat.com>
  • Loading branch information
tanwigeetika1618 and abhikdps authored Dec 4, 2024
1 parent eed80a8 commit a702f22
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 9 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ chakarborty
conftest
delenv
devcontainer
devcontainers
devfile
docsite
endraw
Expand Down
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ updates:
labels:
- "dependencies"
- "skip-changelog"
- package-ecosystem: npm
directory: "/"
schedule:
interval: weekly
labels:
- dependabot-deps-updates
- skip-changelog
2 changes: 2 additions & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ concurrency:
jobs:
tox:
uses: ansible/team-devtools/.github/workflows/tox.yml@main
with:
node-version-file: .tool-versions
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.18.0
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"@devcontainers/cli": "^0.72.0"
}
}
11 changes: 10 additions & 1 deletion src/ansible_creator/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
MIN_COLLECTION_NAME_LEN = 2

COMING_SOON = (
"add resource devcontainer",
"add resource role",
"add plugin action",
)
Expand Down Expand Up @@ -255,6 +254,16 @@ def _add_resource_devcontainer(self, subparser: SubParser[ArgumentParser]) -> No
"current working directory.",
)

parser.add_argument(
"-i",
"--image",
default="auto",
dest="image",
required=False,
help="Image with which devcontainer needs to be scaffolded",
)

self._add_overwrite(parser)
self._add_args_common(parser)

def _add_resource_devfile(self, subparser: SubParser[ArgumentParser]) -> None:
Expand Down
2 changes: 2 additions & 0 deletions src/ansible_creator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Config:
plugin_type: The type of plugin to be scaffolded.
type: The type of the project for which the resource is being scaffolded.
path: The file path where the resource should be added.
image: The image to be used while scaffolding devcontainer.
"""

creator_version: str
Expand All @@ -53,6 +54,7 @@ class Config:
plugin_type: str = ""
type: str = ""
path: str | Path = "./"
image: str = ""

def __post_init__(self) -> None:
"""Post process config values."""
Expand Down
7 changes: 7 additions & 0 deletions src/ansible_creator/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@


GLOBAL_TEMPLATE_VARS = {
# DEV_CONTAINER_IMAGE gets updated with the downstream image when the package
# is installed using rpm
# See: https://gitlab.cee.redhat.com/aap-cpaas/config/ansible-creator/-/blob/ansible-automation-platform-2.5/distgit/rpms/ansible-creator/ansible-creator.spec.in?ref_type=heads#L34
"DEV_CONTAINER_IMAGE": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"DEV_CONTAINER_UPSTREAM_IMAGE": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"DEV_CONTAINER_DOWNSTREAM_IMAGE": (
"registry.redhat.io/ansible-automation-platform-25/ansible-dev-tools-rhel8:latest"
),
"DEV_FILE_IMAGE": "ghcr.io/ansible/ansible-workspace-env-reference:latest",
"RECOMMENDED_EXTENSIONS": ["redhat.ansible", "redhat.vscode-redhat-account"],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-codespaces",
"image": "{{ dev_container_image }}",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-docker",
"image": "{{ dev_container_image }}",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
28 changes: 28 additions & 0 deletions src/ansible_creator/subcommands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from ansible_creator.constants import GLOBAL_TEMPLATE_VARS
from ansible_creator.exceptions import CreatorError
from ansible_creator.templar import Templar
from ansible_creator.types import TemplateData
Expand Down Expand Up @@ -41,6 +42,7 @@ def __init__(
self._no_overwrite = config.no_overwrite
self._creator_version = config.creator_version
self._project = config.project
self._dev_container_image = config.image
self.output: Output = config.output
self.templar = Templar()

Expand Down Expand Up @@ -101,6 +103,8 @@ def _resource_scaffold(self) -> None:
# Call the appropriate scaffolding function based on the resource type
if self._resource_type == "devfile":
template_data = self._get_devfile_template_data()
elif self._resource_type == "devcontainer":
template_data = self._get_devcontainer_template_data()

else:
msg = f"Unsupported resource type: {self._resource_type}"
Expand Down Expand Up @@ -242,6 +246,30 @@ def _get_devfile_template_data(self) -> TemplateData:
dev_file_name=self.unique_name_in_devfile(),
)

def _get_devcontainer_template_data(self) -> TemplateData:
"""Get the template data for devcontainer resources.
Returns:
TemplateData: Data required for templating the devcontainer resource.
"""
image_mapping = {
"auto": GLOBAL_TEMPLATE_VARS["DEV_CONTAINER_IMAGE"],
"upstream": GLOBAL_TEMPLATE_VARS["DEV_CONTAINER_UPSTREAM_IMAGE"],
"aap": GLOBAL_TEMPLATE_VARS["DEV_CONTAINER_DOWNSTREAM_IMAGE"],
}

dev_container_image = image_mapping.get(
self._dev_container_image,
self._dev_container_image,
)

return TemplateData(
resource_type=self._resource_type,
creator_version=self._creator_version,
dev_file_name=self.unique_name_in_devfile(),
dev_container_image=dev_container_image,
)

def _get_plugin_template_data(self) -> TemplateData:
"""Get the template data for lookup plugin.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-codespaces",
"image": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-docker",
"image": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-codespaces",
"image": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansible-dev-container-docker",
"image": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"containerUser": "podman",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
Expand Down
Loading

0 comments on commit a702f22

Please sign in to comment.