Skip to content

Commit

Permalink
we are now able to ping and set a specific password in the started ga…
Browse files Browse the repository at this point in the history
…teway
  • Loading branch information
sanderegg committed Aug 29, 2023
1 parent 83c460e commit 6770346
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
LogLevel,
VersionTag,
)
from pydantic import Field, NonNegativeInt, PositiveInt, parse_obj_as, validator
from pydantic import (
Field,
NonNegativeInt,
PositiveInt,
SecretStr,
parse_obj_as,
validator,
)
from settings_library.base import BaseCustomSettings
from settings_library.docker_registry import RegistrySettings
from settings_library.rabbit import RabbitSettings
Expand Down Expand Up @@ -156,6 +163,16 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
description="Max number of missed heartbeats before a cluster is terminated",
)

CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_DOCKER_IMAGE_TAG: str = Field(
default="master-github-latest",
description="defines the image tag to use for the computational backend",
)

CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_GATEWAY_PASSWORD: SecretStr = Field(
default=SecretStr("my_secure_P1ssword"),
description="very secure password, should change soon",
)

@cached_property
def LOG_LEVEL(self): # noqa: N802
return self.CLUSTERS_KEEPER_LOGLEVEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ class ClusterGet(BaseModel):

@classmethod
def from_ec2_instance_data(
cls, instance: EC2InstanceData, user_id: UserID, wallet_id: WalletID
cls,
instance: EC2InstanceData,
user_id: UserID,
wallet_id: WalletID,
gateway_password: SecretStr,
) -> "ClusterGet":
return cls(
endpoint=parse_obj_as(AnyUrl, f"http://{instance.aws_public_ip}"),
authentication=SimpleAuthentication(
username="bing", password=SecretStr("bingo")
username=f"{user_id}", password=gateway_password
),
state=_convert_ec2_state_to_cluster_state(instance.state),
user_id=user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def create_cluster(
),
),
tags=creation_ec2_tags(app_settings, user_id=user_id, wallet_id=wallet_id),
startup_script=create_startup_script(),
startup_script=create_startup_script(app_settings),
number_of_instances=1,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import contextlib
import asyncio
import logging

import dask_gateway
from aiohttp.client_exceptions import ClientError
from pydantic import SecretStr

from ..models import EC2InstanceData

_logger = logging.getLogger(__name__)


async def ping_gateway(ec2_instance: EC2InstanceData) -> bool:
basic_auth = dask_gateway.BasicAuth(username="bing", password="asdf")
with contextlib.suppress(ClientError):
async def ping_gateway(
ec2_instance: EC2InstanceData, gateway_password: SecretStr
) -> bool:
basic_auth = dask_gateway.BasicAuth(
username="clusters-keeper", password="my_secure_P1ssword"
)
try:
async with dask_gateway.Gateway(
address=f"http://{ec2_instance.aws_public_ip}:8000",
auth=basic_auth,
asynchronous=True,
) as gateway:
cluster_reports = await gateway.list_clusters()
cluster_reports = await asyncio.wait_for(gateway.list_clusters(), timeout=5)
_logger.info("found %s clusters", len(cluster_reports))
return True
except (ClientError, asyncio.TimeoutError):
_logger.info("dask-gateway is unavailable", exc_info=True)

return False
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI
from models_library.users import UserID
from models_library.wallets import WalletID
from simcore_service_clusters_keeper.core.settings import get_application_settings

from ..core.errors import Ec2InstanceNotFoundError
from ..models import ClusterGet, EC2InstanceData
Expand Down Expand Up @@ -34,11 +35,19 @@ async def get_or_create_cluster(
assert len(new_ec2_instances) == 1 # nosec
ec2_instance = new_ec2_instances[0]
assert ec2_instance is not None # nosec

cluster_get = ClusterGet.from_ec2_instance_data(ec2_instance, user_id, wallet_id)
app_settings = get_application_settings(app)
cluster_get = ClusterGet.from_ec2_instance_data(
ec2_instance,
user_id,
wallet_id,
app_settings.CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_GATEWAY_PASSWORD,
)

if ec2_instance.state == "running":
cluster_get.gateway_ready = await ping_gateway(ec2_instance)
cluster_get.gateway_ready = await ping_gateway(
ec2_instance,
app_settings.CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_GATEWAY_PASSWORD,
)

return cluster_get

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
def create_startup_script() -> str:
from ..core.settings import ApplicationSettings


def create_startup_script(app_settings: ApplicationSettings) -> str:
return "\n".join(
[
"git clone https://github.com/ITISFoundation/osparc-simcore.git",
"git clone --depth=1 https://github.com/ITISFoundation/osparc-simcore.git",
"cd osparc-simcore/services/osparc-gateway-server",
"make up-latest",
"make config",
f"echo 'c.Authenticator.password = \"{app_settings.CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_GATEWAY_PASSWORD.get_secret_value()}\"' >> .osparc-dask-gateway-config.py",
f"DOCKER_IMAGE_TAG={app_settings.CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_DOCKER_IMAGE_TAG} make up",
]
)

0 comments on commit 6770346

Please sign in to comment.