Skip to content

Commit

Permalink
[task] add local docker host create and destroy tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
pducolin committed Nov 18, 2024
1 parent 374a974 commit 33d6206
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tasks.ci as ci
import tasks.setup as setup
import tasks.test as test
from tasks import aws, azure, gcp
from tasks import aws, azure, gcp, localdocker
from tasks.aks import create_aks, destroy_aks
from tasks.deploy import check_s3_image_exists
from tasks.docker import create_docker, destroy_docker
Expand Down Expand Up @@ -40,6 +40,7 @@
ns.add_collection(aws.collection, "aws")
ns.add_collection(azure.collection, "az")
ns.add_collection(gcp.collection, "gcp")
ns.add_collection(localdocker.collection, "localdocker")
ns.add_collection(ci)
ns.add_collection(setup)
ns.add_collection(test)
13 changes: 13 additions & 0 deletions tasks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class GCP(BaseModel, extra=Extra.forbid):

gcp: Optional[GCP] = None

class Local(BaseModel, extra=Extra.forbid):
publicKeyPath: Optional[str] = None

local: Optional[Local] = None

class Agent(BaseModel, extra=Extra.forbid):
apiKey: Optional[str]
appKey: Optional[str]
Expand Down Expand Up @@ -106,6 +111,14 @@ def get_aws(self) -> Params.Aws:
return default
return self.configParams.aws

def get_local(self) -> Params.Local:
default = Config.Params.Local(publicKeyPath=None)
if self.configParams is None:
return default
if self.configParams.local is None:
return default
return self.configParams.local

def get_agent(self) -> Params.Agent:
default = Config.Params.Agent(apiKey=None, appKey=None)
if self.configParams is None:
Expand Down
9 changes: 9 additions & 0 deletions tasks/localdocker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# type: ignore[reportArgumentType]

from invoke.collection import Collection

from tasks.localdocker.vm import create_vm, destroy_vm

collection = Collection()
collection.add_task(destroy_vm)
collection.add_task(create_vm)
94 changes: 94 additions & 0 deletions tasks/localdocker/vm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from typing import Optional

from invoke.context import Context
from invoke.exceptions import Exit
from invoke.tasks import task
from pydantic_core._pydantic_core import ValidationError

from tasks import config, doc
from tasks.deploy import deploy
from tasks.destroy import destroy
from tasks.tool import notify, show_connection_message

scenario_name = "localdocker/vm"
remote_hostname = "localdocker-vm"


@task(
help={
"config_path": doc.config_path,
"install_agent": doc.install_agent,
"pipeline_id": doc.pipeline_id,
"agent_version": doc.agent_version,
"stack_name": doc.stack_name,
"debug": doc.debug,
"use_fakeintake": doc.fakeintake,
"interactive": doc.interactive,
}
)
def create_vm(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
pipeline_id: Optional[str] = None,
install_agent: Optional[bool] = True,
agent_version: Optional[str] = None,
debug: Optional[bool] = False,
use_fakeintake: Optional[bool] = False,
interactive: Optional[bool] = True,
) -> None:
"""
Create a new virtual machine on local docker.
"""

try:
cfg = config.get_local_config(config_path)
except ValidationError as e:
raise Exit(f"Error in config {config.get_full_profile_path(config_path)}") from e

if not cfg.get_local().publicKeyPath:
raise Exit("The field `local.publicKeyPath` is required in the config file")

extra_flags = {
"ddinfra:local/defaultPublicKeyPath": cfg.get_gcp().publicKeyPath,
}

full_stack_name = deploy(
ctx,
scenario_name,
config_path,
stack_name=stack_name,
pipeline_id=pipeline_id,
install_agent=install_agent,
agent_version=agent_version,
debug=debug,
extra_flags=extra_flags,
use_fakeintake=use_fakeintake,
)

if interactive:
notify(ctx, "Your VM is now created")

show_connection_message(ctx, remote_hostname, full_stack_name, interactive)


@task(
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
}
)
def destroy_vm(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
):
"""
Destroy a new virtual machine on aws.
"""
destroy(
ctx,
scenario_name=scenario_name,
config_path=config_path,
stack=stack_name,
)
4 changes: 4 additions & 0 deletions tasks/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def __init__(self, name, stack_outputs: Any):
remoteHost: Any = stack_outputs[f"dd-Host-{name}"]
self.host: str = remoteHost["address"]
self.user: str = remoteHost["username"]
self.port: int | None = "port" in remoteHost and remoteHost["port"] or None


def show_connection_message(
Expand All @@ -252,6 +253,9 @@ def show_connection_message(

command = f"ssh {user}@{host}"

if remoteHost.port:
command += f" -p {remoteHost.port}"

print(f"\nYou can run the following command to connect to the host `{command}`.\n")
if copy_to_clipboard:
input("Press a key to copy command to clipboard...")
Expand Down

0 comments on commit 33d6206

Please sign in to comment.