Skip to content

Commit b826d89

Browse files
committed
Update examples
1 parent e5bfa55 commit b826d89

File tree

7 files changed

+170
-25
lines changed

7 files changed

+170
-25
lines changed
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1-
# Infrahub Repository Template
1+
# Infrahub Repository
22

3-
This template allows you to initialize a repository that conforms to Opsmills best practices when organising a repository to be imported into Infrahub.
3+
Welcome! This repository was initialized via the `infrahubctl repo init` command. That bootstraps a repository for use with some example data.
44

5-
To use this template simply install the `infrahub-sdk[ctl]` package and run the following:
5+
## Installation
6+
Running `poetry install` will install all the main dependencies you need to interact with this repository.
67

7-
```shell
8-
infrahubctl repository init /path/to/folder
8+
## Starting Infrahub
9+
10+
Included in the repository are a set of helper commands to get Infrahub up and running using `invoke`.
11+
12+
```bash
13+
Available tasks:
14+
15+
destroy Stop and remove containers, networks, and volumes.
16+
download-compose-file Download docker-compose.yml from InfraHub if missing or override is True.
17+
load-schema Load schemas into InfraHub using infrahubctl.
18+
restart Restart all services or a specific one using docker-compose.
19+
start Start the services using docker-compose in detached mode.
20+
stop Stop containers and remove networks.
21+
test Run tests using pytest.
22+
```
23+
24+
To start infrahub simply use `invoke start`
25+
26+
## Tests
27+
By default there are some integration tests that will spin up Infrahub and its dependencies in docker and load the repository and schema. This can be run using the following:
28+
29+
```bash
30+
poetry install --with=dev
31+
pytest tests/integration
932
```
1033
11-
You will be prompted with several options.
34+
To change the version of infrahub being used you can use an environment variable: `export INFRAHUB_TESTING_IMAGE_VERSION=1.2.5`.

infrahub_sdk/ctl/example_repo/pyproject.toml.jinja

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ package-mode = false
1313
{% endif %}
1414

1515
[tool.poetry.dependencies]
16-
python = ">=3.9,<3.13"
17-
infrahub-sdk = {extras = ["ctl"], version = "^1.7.2"}
16+
python = ">=3.9,<=3.13.1"
17+
infrahub-sdk = { extras = ["all"], version = "*" }
18+
invoke = "*"
1819

1920
[tool.poetry.group.dev.dependencies]
20-
ruff = "^0.9.10"
21-
pytest = "^8.3.5"
22-
infrahub-testcontainers = "^1.1.8"
23-
pytest-asyncio = "^0.25.3"
21+
ruff = "*"
22+
pytest = "*"
23+
infrahub-testcontainers = "*"
24+
pytest-asyncio = "*"
2425

2526
[build-system]
2627
requires = ["poetry-core"]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from invoke import Context, task
2+
from pathlib import Path
3+
import httpx
4+
import os
5+
6+
# If no version is indicated, we will take the latest
7+
VERSION = os.getenv("INFRAHUB_IMAGE_VER", None)
8+
9+
10+
@task
11+
def start(context: Context) -> None:
12+
"""
13+
Start the services using docker-compose in detached mode.
14+
"""
15+
compose_file = download_compose_file(context, override=False)
16+
context.run("docker compose up -d")
17+
18+
19+
@task
20+
def destroy(context: Context) -> None:
21+
"""
22+
Stop and remove containers, networks, and volumes.
23+
"""
24+
compose_file = download_compose_file(context, override=False)
25+
context.run("docker compose down -v")
26+
27+
28+
@task
29+
def stop(context: Context) -> None:
30+
"""
31+
Stop containers and remove networks.
32+
"""
33+
compose_file = download_compose_file(context, override=False)
34+
context.run("docker compose down")
35+
36+
37+
@task(help={"component": "Optional name of a specific service to restart."})
38+
def restart(context: Context, component: str = "") -> None:
39+
"""
40+
Restart all services or a specific one using docker-compose.
41+
"""
42+
download_compose_file(context, override=False)
43+
if component:
44+
context.run(f"docker compose restart {component}")
45+
return
46+
47+
context.run("docker compose restart")
48+
49+
50+
@task
51+
def load_schema(ctx: Context):
52+
"""
53+
Load schemas into InfraHub using infrahubctl.
54+
"""
55+
ctx.run("infrahubctl schema load schemas")
56+
57+
58+
@task
59+
def test(ctx: Context):
60+
"""
61+
Run tests using pytest.
62+
"""
63+
ctx.run("pytest tests")
64+
65+
66+
@task(help={"override": "Redownload the compose file even if it already exists."})
67+
def download_compose_file(context: Context, override: bool = False) -> Path:
68+
"""
69+
Download docker-compose.yml from InfraHub if missing or override is True.
70+
"""
71+
compose_file = Path("./docker-compose.yml")
72+
73+
if compose_file.exists() and not override:
74+
return compose_file
75+
76+
response = httpx.get("https://infrahub.opsmill.io")
77+
response.raise_for_status()
78+
79+
with compose_file.open("w") as f:
80+
f.write(response.content.decode())
81+
82+
return compose_file
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from infrahub_sdk import InfrahubClient
2+
from infrahub_sdk.node import InfrahubNode
3+
from typing import List
4+
import logging
5+
6+
7+
def print_nodes(client: InfrahubClient, log: logging.Logger, nodes: List[InfrahubNode]):
8+
for node in nodes.keys():
9+
log.info(f"{node} present.")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import logging
2+
3+
from infrahub_sdk import InfrahubClient
4+
from lib.example import print_nodes
5+
6+
7+
async def run(
8+
client: InfrahubClient,
9+
log: logging.Logger,
10+
branch: str,
11+
):
12+
log.info("Running example script...")
13+
nodes = await client.schema.all()
14+
print_nodes(client, log, nodes)

infrahub_sdk/ctl/example_repo/{% if tests %}tests{% endif %}/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ def root_directory() -> Path:
1717

1818
@pytest.fixture
1919
def schemas_directory(root_directory: Path) -> Path:
20-
return root_directory / "schema"
20+
return root_directory / "schemas"
2121

2222

2323
@pytest.fixture
2424
def schemas(schemas_directory: Path) -> list[dict[str, Any]]:
2525
schema_files = SchemaFile.load_from_disk(paths=[schemas_directory])
26-
return [item.content for item in schema_files if item.content]
26+
return [item.content for item in schema_files if item.content]

infrahub_sdk/ctl/example_repo/{% if tests %}tests{% endif %}/integration/test_infrahub.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,34 @@
77

88

99
class TestInfrahub(TestInfrahubDockerClient):
10-
@pytest.fixture(scope="class")
11-
def infrahub_version(self) -> str:
12-
"""Required to define the version of infrahub to use."""
13-
return "1.1.8"
14-
1510
@pytest.mark.asyncio
16-
async def test_load_schema(
17-
self, default_branch: str, client: InfrahubClient, schemas
18-
):
11+
async def test_load_schema(self, default_branch: str, client: InfrahubClient, schemas):
1912
await client.schema.wait_until_converged(branch=default_branch)
2013

21-
resp = await client.schema.load(
22-
schemas=schemas, branch=default_branch, wait_until_converged=True
23-
)
14+
resp = await client.schema.load(schemas=schemas, branch=default_branch, wait_until_converged=True)
2415
assert resp.errors == {}
16+
17+
@pytest.mark.asyncio
18+
async def test_load_repository(
19+
self,
20+
client: InfrahubClient,
21+
remote_repos_dir: Path,
22+
root_directory: Path,
23+
) -> None:
24+
"""Add the local directory as a repository in Infrahub and wait for the import to be complete"""
25+
26+
repo = GitRepo(
27+
name="local-repository",
28+
src_directory=root_directory,
29+
dst_directory=remote_repos_dir,
30+
)
31+
await repo.add_to_infrahub(client=client)
32+
in_sync = await repo.wait_for_sync_to_complete(client=client)
33+
assert in_sync
34+
35+
repos = await client.all(kind=CoreGenericRepository)
36+
37+
### A breakpoint can be added to pause the tests from running and keep the test containers active
38+
# breakpoint()
39+
40+
assert repos

0 commit comments

Comments
 (0)