This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP * its alive (kinda) * it works but it's ugly * little less ugly * lil fix * fix lint * fix lint * fix tests * fix tests * fix windows bugs * fix tests * fix tests
- Loading branch information
Showing
29 changed files
with
1,079 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
COPY . ./ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% if packages %}RUN {{ package_install_cmd }} {{ packages|join(" ") }}{% endif %} | ||
COPY requirements.txt . | ||
RUN pip install -r requirements.txt | ||
{{ mlem_install }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import base64 | ||
import os | ||
from typing import ClassVar, Optional | ||
|
||
import boto3 | ||
import sagemaker | ||
from pydantic import BaseModel | ||
|
||
from ...core.objects import MlemModel | ||
from ...ui import EMOJI_BUILD, EMOJI_KEY, echo, set_offset | ||
from ..docker.base import DockerEnv, DockerImage, RemoteRegistry | ||
from ..docker.helpers import build_model_image | ||
from .runtime import SageMakerServer | ||
|
||
IMAGE_NAME = "mlem-sagemaker-runner" | ||
|
||
|
||
class AWSVars(BaseModel): | ||
profile: str | ||
bucket: str | ||
region: str | ||
account: str | ||
role_name: str | ||
|
||
@property | ||
def role(self): | ||
return f"arn:aws:iam::{self.account}:role/{self.role_name}" | ||
|
||
def get_sagemaker_session(self): | ||
return sagemaker.Session( | ||
self.get_session(), default_bucket=self.bucket | ||
) | ||
|
||
def get_session(self): | ||
return boto3.Session( | ||
profile_name=self.profile, region_name=self.region | ||
) | ||
|
||
|
||
def ecr_repo_check(region, repository, session: boto3.Session): | ||
client = session.client("ecr", region_name=region) | ||
|
||
repos = client.describe_repositories()["repositories"] | ||
|
||
if repository not in {r["repositoryName"] for r in repos}: | ||
echo(EMOJI_BUILD + f"Creating ECR repository {repository}") | ||
client.create_repository(repositoryName=repository) | ||
|
||
|
||
class ECRegistry(RemoteRegistry): | ||
class Config: | ||
exclude = {"aws_vars"} | ||
|
||
type: ClassVar = "ecr" | ||
account: str | ||
region: str | ||
|
||
aws_vars: Optional[AWSVars] = None | ||
|
||
def login(self, client): | ||
auth_data = self.ecr_client.get_authorization_token() | ||
token = auth_data["authorizationData"][0]["authorizationToken"] | ||
user, token = base64.b64decode(token).decode("utf8").split(":") | ||
self._login(self.get_host(), client, user, token) | ||
echo( | ||
EMOJI_KEY | ||
+ f"Logged in to remote registry at host {self.get_host()}" | ||
) | ||
|
||
def get_host(self) -> Optional[str]: | ||
return f"{self.account}.dkr.ecr.{self.region}.amazonaws.com" | ||
|
||
def image_exists(self, client, image: DockerImage): | ||
images = self.ecr_client.list_images(repositoryName=image.name)[ | ||
"imageIds" | ||
] | ||
return len(images) > 0 | ||
|
||
def delete_image(self, client, image: DockerImage, force=False, **kwargs): | ||
self.ecr_client.batch_delete_image( | ||
repositoryName=image.name, | ||
imageIds=[{"imageTag": image.tag}], | ||
) | ||
|
||
def with_aws_vars(self, aws_vars): | ||
self.aws_vars = aws_vars | ||
return self | ||
|
||
@property | ||
def ecr_client(self): | ||
return ( | ||
self.aws_vars.get_session().client("ecr") | ||
if self.aws_vars | ||
else boto3.client("ecr", region_name=self.region) | ||
) | ||
|
||
|
||
def build_sagemaker_docker( | ||
meta: MlemModel, | ||
method: str, | ||
account: str, | ||
region: str, | ||
image_name: str, | ||
repository: str, | ||
aws_vars: AWSVars, | ||
): | ||
docker_env = DockerEnv( | ||
registry=ECRegistry(account=account, region=region).with_aws_vars( | ||
aws_vars | ||
) | ||
) | ||
ecr_repo_check(region, repository, aws_vars.get_session()) | ||
echo(EMOJI_BUILD + "Creating docker image for sagemaker") | ||
with set_offset(2): | ||
return build_model_image( | ||
meta, | ||
name=repository, | ||
tag=image_name, | ||
server=SageMakerServer(method=method), | ||
env=docker_env, | ||
force_overwrite=True, | ||
templates_dir=[os.path.dirname(__file__)], | ||
) |
Empty file.
Oops, something went wrong.