Skip to content

Commit

Permalink
mini up
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Mar 20, 2024
1 parent c2d04a5 commit 0c07227
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 8 deletions.
1 change: 1 addition & 0 deletions fakts/graphql/mutations/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .client import *
from .scan import *
30 changes: 30 additions & 0 deletions fakts/graphql/mutations/scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import hashlib
import json
import logging
import uuid

import namegenerator
import strawberry
import strawberry_django
from ekke.types import Info

from fakts import enums, inputs, models, scalars, types
from fakts.base_models import DevelopmentClientConfig, Manifest
from fakts.builders import create_client
from fakts.models import Composition
from fakts.scan import scan
logger = logging.getLogger(__name__)




def scan_backend(info: Info, input: inputs.ScanBackendInput) -> str:

token = scan()



return token



9 changes: 9 additions & 0 deletions fakts/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ class DevelopmentClientInput:
manifest: ManifestInput
composition: strawberry.ID | None = None


class ScanBackendInputModel(BaseModel):
backend: str | None



@pydantic.input(ScanBackendInputModel)
class ScanBackendInput:
backend: str | None = None
76 changes: 76 additions & 0 deletions fakts/scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import docker
import re
import socket

def get_my_container(client: docker.DockerClient):

container_id = socket.gethostname()

try:
return client.containers.get(container_id)
except docker.errors.NotFound:

potential_container_id = None

try:
with open("/proc/self/cgroup", "r") as file:
for line in file:
match = re.search(r'/docker/([a-f0-9]{64})$', line)
if match:
potential_container_id = match.group(1)
break

except FileNotFoundError:
raise Exception("Could not find container id")

if potential_container_id:
return client.containers.get(potential_container_id)
else:
raise Exception("Could not find container id")



def get_project_name(container):
return container.labels.get("com.docker.compose.project")


def retrieve_sibling_containers(client: docker.DockerClient, project_name: str):
# Filter containers by Docker Compose project label
filters = {"label": f"com.docker.compose.project={project_name}"}
containers = client.containers.list(all=True, filters=filters)
return containers



def scan():
client = docker.from_env()
container = get_my_container(client)

project_name = get_project_name(container)
print(project_name)
print(container)


sibling_containers = retrieve_sibling_containers(client, project_name)


for container in sibling_containers:
labels = container.labels

if "fakts.service.template" in labels:
print(container.attrs["NetworkSettings"]["Ports"])




print(container.name)
print(labels["fakts.service"])
print(container)

return "token"






3 changes: 3 additions & 0 deletions lok/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Mutation:
create_developmental_client = strawberry_django.mutation(
resolver=fakts_mutations.create_developmental_client,
)
scan = strawberry_django.mutation(
resolver=fakts_mutations.scan_backend,
)


@strawberry.type
Expand Down
30 changes: 23 additions & 7 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ django-cors-headers = "^4.2.0"
django-allauth = "^0.58.2"
pillow = "^10.1.0"
django-health-check = "^3.18.1"
docker = "^7.0.0"
docker = ">6,<7"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 0c07227

Please sign in to comment.