-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
66 lines (49 loc) · 1.96 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Replacement for Makefile."""
import os
import sys
import glob
from datetime import datetime
from pathlib import Path
from typing import Tuple
from invoke import task, Context # type: ignore
PROJECT_NAME = "infrahub-demo-ixp"
def git_info(context: Context) -> Tuple[str, str]:
"""Return the name of the current branch and hash of the current commit."""
branch_name = context.run("git rev-parse --abbrev-ref HEAD", hide=True, pty=False)
hash = context.run("git rev-parse --short HEAD", hide=True, pty=False)
return branch_name.stdout.strip(), hash.stdout.strip()
@task
def generate_archive(context: Context):
branch, commit = git_info(context=context)
directory_name = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
package_name = f"{PROJECT_NAME}-{commit[:8]}.tar.gz"
commands = [
f"rm -rf /tmp/{PROJECT_NAME}",
f"cp -a ../{directory_name} /tmp/{PROJECT_NAME}",
f"git --git-dir /tmp/{PROJECT_NAME}/.git remote remove origin",
f"tar -C /tmp -czf {package_name} {PROJECT_NAME}",
]
for command in commands:
result = context.run(command=command, pty=True)
print(f"Package {package_name!r} generated successfully.")
@task
def load_schema(context: Context, schema: Path="./models/infrastructure_base.yml") -> None:
context.run(f"infrahubctl schema load {schema}")
restart(context, component="infrahub-server")
@task
def load_data(context: Context, script: Path="./models/infrastructure_edge.py") -> None:
context.run(f"infrahubctl run {script}")
@task
def destroy(context: Context) -> None:
context.run("docker compose down -v")
@task
def stop(context: Context) -> None:
context.run("docker compose down")
@task
def start(context: Context) -> None:
context.run("docker compose up -d")
@task
def restart(context: Context, component: str="")-> None:
if not component:
context.run("docker compose restart")
context.run(f"docker compose restart {component}")