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

Make deploy script create venv while SSH'd into control machine #1369

Merged
merged 7 commits into from
May 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ install_requires =
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@890027c1bcd95f8fe35576630e320b97b7959006
pydantic<2.0 # See https://github.com/DiamondLightSource/hyperion/issues/774
scipy
pyzmq<25 # See https://github.com/DiamondLightSource/hyperion/issues/1103
pyzmq

[options.entry_points]
console_scripts =
Expand Down
25 changes: 25 additions & 0 deletions utility_scripts/deploy/create_venv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import sys
from subprocess import PIPE, CalledProcessError, Popen


def setup_venv(path_to_create_venv_script, deployment_directory):
# Set up environment and run /dls_dev_env.sh...
os.chdir(deployment_directory)
print(f"Setting up environment in {deployment_directory}")

with Popen(
path_to_create_venv_script, stdout=PIPE, bufsize=1, universal_newlines=True
) as p:
if p.stdout is not None:
for line in p.stdout:
print(line, end="")
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)


if __name__ == "__main__":
# This should only be entered from the control machine
path_to_create_venv_script = sys.argv[1]
deployment_directory = sys.argv[2]
setup_venv(path_to_create_venv_script, deployment_directory)
54 changes: 41 additions & 13 deletions utility_scripts/deploy/deploy_hyperion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import argparse
import os
import re
from subprocess import PIPE, CalledProcessError, Popen
import subprocess
from uuid import uuid1

from create_venv import setup_venv
from git import Repo
from packaging.version import VERSION_PATTERN, Version

Expand All @@ -13,6 +14,8 @@
f"^{VERSION_PATTERN}$", re.VERBOSE | re.IGNORECASE
)

DEV_DEPLOY_LOCATION = "/scratch/30day_tmp/hyperion_release_test/bluesky"


class repo:
# Set name, setup remote origin, get the latest version"""
Expand Down Expand Up @@ -71,11 +74,38 @@ def get_hyperion_release_dir_from_args() -> str:
args = parser.parse_args()
if args.beamline == "dev":
print("Running as dev")
return "/scratch/30day_tmp/hyperion_release_test/bluesky"
return DEV_DEPLOY_LOCATION
else:
return f"/dls_sw/{args.beamline}/software/bluesky"


def create_environment_from_control_machine():
try:
user = os.environ["USER"]
except KeyError:
user = input(
"Couldn't find username from the environment. Enter FedID in order to SSH to control machine:"
)
cmd = f"ssh {user}@i03-control python3 {path_to_create_venv} {path_to_dls_dev_env} {hyperion_repo.deploy_location}"

process = None
try:
# Call python script on i03-control to create the environment
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = process.communicate()
if process.returncode != 0:
print(f"Error occurred: {stderr.decode()}")
else:
print(f"Output: {stdout.decode()}")
except Exception as e:
print(f"Exception while trying to install venv on i03-control: {e}")
finally:
if process:
process.kill()


if __name__ == "__main__":
# Gives path to /bluesky
release_area = get_hyperion_release_dir_from_args()
Expand Down Expand Up @@ -120,21 +150,19 @@ def get_hyperion_release_dir_from_args() -> str:
# Now deploy the correct version of dodal
dodal_repo.deploy(dodal_url)

# Set up environment and run /dls_dev_env.sh...
os.chdir(hyperion_repo.deploy_location)
print(f"Setting up environment in {hyperion_repo.deploy_location}")

if hyperion_repo.name == "hyperion":
env_script = os.path.join(
path_to_dls_dev_env = os.path.join(
hyperion_repo.deploy_location, "utility_scripts/dls_dev_env.sh"
)
with Popen(env_script, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
if p.stdout is not None:
for line in p.stdout:
print(line, end="")
path_to_create_venv = os.path.join(
hyperion_repo.deploy_location, "utility_scripts/deploy/create_venv.py"
)

if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
# SSH into control machine if not in dev mode
if release_area != DEV_DEPLOY_LOCATION:
create_environment_from_control_machine()
else:
setup_venv(path_to_create_venv, hyperion_repo.deploy_location)

def create_symlink_by_tmp_and_rename(dirname, target, linkname):
tmp_name = str(uuid1())
Expand Down
11 changes: 11 additions & 0 deletions utility_scripts/deploy/test_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import subprocess

cmd = "ssh qqh35939@i03-control"
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = process.communicate()
if process.returncode != 0:
print(f"Error occurred: {stderr.decode()}")
else:
print(f"Output: {stdout.decode()}")
Loading