Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Make MLEM work with flyctl 0.1.x #678

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mlem/cli/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def deploy_run_command(
if not meta.is_state_empty:
if not force:
raise DeploymentError(
f"Different deployment meta already exists at {meta.loc}. Please use `mlem deployment run --load <path> ...`"
f"The deployment file you specified ('{meta.loc.basename}') already describes a different deployment."
" Please add `--force` to overwrite it,"
" or use `mlem deployment run --load <path> ...` to reuse it."
)
echo(
EMOJI_STOP
Expand Down
45 changes: 26 additions & 19 deletions mlem/contrib/flyio/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,35 @@ def _create_app(self, state: FlyioAppState):
args["access-token"] = self.get_env().access_token
if self.org:
args["org"] = self.org
port = getattr(self.server, "ui_port", None) or self.server.port
if port:
args["internal-port"] = port
run_flyctl("launch", workdir=tempdir, kwargs=args)
state.fly_toml = read_fly_toml(tempdir)
port = getattr(self.server, "port", None) or getattr(
self.server, "ui_port", None
)
if port: # tell flyio to expose specific port
if getattr(self.server, "server_port", None):
fly_toml = parse(state.fly_toml)
fly_toml["services.ports"] = {
"handlers": ["http"],
"port": self.server.server_port,
"force_https": True,
}
state.fly_toml = fly_toml.as_string()
if getattr(self.server, "middlewares", None):
# set fly to collect metrics from prometheus if exposed
fly_toml = parse(state.fly_toml)
fly_toml["services"][0]["internal_port"] = port
if self.server and self.server.middlewares:
# set fly to collect metrics from prometheus if exposed
from mlem.contrib.prometheus import ( # noqa
PrometheusFastAPIMiddleware,
)

if any(
isinstance(m, PrometheusFastAPIMiddleware)
for m in self.server.middlewares.__root__
):
fly_toml["metrics"] = {
"port": port,
"path": "/metrics",
}
from mlem.contrib.prometheus import ( # noqa
PrometheusFastAPIMiddleware,
)

if any(
isinstance(m, PrometheusFastAPIMiddleware)
for m in self.server.middlewares.__root__
):
fly_toml["metrics"] = {
"port": getattr(self.server, "server_port", None)
or self.server.port,
"path": "/metrics",
}
state.fly_toml = fly_toml.as_string()
status = get_status(workdir=tempdir)
state.app_name = status.Name
Expand Down
14 changes: 13 additions & 1 deletion mlem/contrib/flyio/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os.path
import subprocess
from typing import Any, Dict
Expand All @@ -10,9 +11,20 @@
FLY_TOML = "fly.toml"


logger = logging.getLogger(__name__)


def check_flyctl_exec():
try:
run_flyctl("version", wrap_error=False)
cmd = run_flyctl("version --json", wrap_error=False)
output = json.loads(cmd.decode())
version = output.get("Version", None)
if not version or version.split(".")[1] != "1":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recommend using semver to parse and compare versions
here it would be:

version_str = output.get("Version", None)
if version_str is not None:
  version = semver.Version.parse(version_str)
  if ver.major < 1:
     ... 

logger.warning(
"If flyio deployment is causing problems, the `flyctl` version may be the cause. "
f"You have {version} installed. Try to install `flyctl` 0.1.x."
)
return output
except subprocess.SubprocessError as e:
raise DeploymentError(
"flyctl executable is not available. Please install it using <https://fly.io/docs/hands-on/install-flyctl/>"
Expand Down
10 changes: 9 additions & 1 deletion tests/contrib/test_flyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
from unittest.mock import ANY, patch

from mlem.contrib.flyio.meta import FlyioApp
from mlem.contrib.flyio.utils import FlyioStatusModel
from mlem.contrib.flyio.utils import FlyioStatusModel, check_flyctl_exec


def test_check_flyctl_exec():
output = check_flyctl_exec()
version = output["Version"]
assert (
version.split(".")[1] == "1"
), f"flyctl version was bumped ({version} now), this may cause problems with deployment"


def test_flyio_create_app(tmp_path: Path):
Expand Down