Skip to content

Commit

Permalink
feat(command): stop application if running before installing or upgra…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
maugde committed May 3, 2024
1 parent 1b6d038 commit d3e8dd8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 23 additions & 6 deletions src/antares_web_installer/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import click
import psutil
from antares_web_installer import DEBUG
from pathlib import Path
from installer import install
Expand Down Expand Up @@ -29,7 +30,7 @@


@click.group()
def cli():
def cli() -> None:
pass


Expand All @@ -39,7 +40,7 @@ def cli():
show_default=True,
type=click.Path(),
help="where to install your application")
def install_cli(target_dir):
def install_cli(target_dir: Path) -> None:
"""
Install Antares Web at the right file locations.
"""
Expand All @@ -51,11 +52,27 @@ def install_cli(target_dir):
src_dir = BASE_DIR.joinpath(Path(src_dir))
target_dir = target_dir.expanduser()

# TODO: check whether the server is running
# in case it is, stop it
# check whether the server is running
server_running_handler()

print(f"Starting installation in directory: '{target_dir}'...")

install(src_dir, target_dir)
print("Done.")


def server_running_handler() -> None:
"""
Check whether antares service is up.
In case it is, terminate the process
"""
for proc in psutil.process_iter(['pid', 'name', 'username']):
if 'antares' in proc.name().lower():
print("Cannot upgrade since the application is running.")

running_app = psutil.Process(pid=proc.pid)
running_app.kill() # ... or terminate ?
running_app.wait(30)
assert not running_app.is_running()

print("Installation complete!")
print("The application was successfully stopped.")
return
6 changes: 1 addition & 5 deletions src/antares_web_installer/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def copy_files(src_dir: Path, target_dir: Path):
"""
for elt_path in src_dir.iterdir():
if elt_path.name not in EXCLUDED_FILES:
# verbose action
print(f"copy {elt_path.name} to {target_dir.name}")

# verbose action ?
try:
if elt_path.is_file():
copy2(elt_path, target_dir)
Expand All @@ -103,5 +101,3 @@ def copy_files(src_dir: Path, target_dir: Path):
# handle other errors
except BaseException as e:
raise InstallError(f"{e}")
else:
print(f"ignoring {elt_path.name}")

0 comments on commit d3e8dd8

Please sign in to comment.