Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to restart deployments in DeviceSet #5

Merged
merged 2 commits into from
Jul 26, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Next version

### 🚀 New

* [#5](https://github.com/sdss/lvmgort/pull/5) Add option to restart deployments associated with a device set as `GortDeviceSet.restart()`.


## 0.2.2 - July 23, 2023

### 🚀 New
Expand Down
1 change: 1 addition & 0 deletions src/gort/devices/ag.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class AGSet(GortDeviceSet[AG]):
"""A set of auto-guiders."""

__DEVICE_CLASS__ = AG
__DEPLOYMENTS__ = ["lvmagcam"]

async def reconnect(self):
"""Reconnects all the AG cameras.
Expand Down
10 changes: 9 additions & 1 deletion src/gort/devices/enclosure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import TYPE_CHECKING

from gort.exceptions import GortEnclosureError
from gort.gort import GortDevice
from gort.gort import GortDevice, GortDeviceSet


if TYPE_CHECKING:
Expand All @@ -25,9 +25,17 @@
class Enclosure(GortDevice):
"""Class representing the LVM enclosure."""

__DEPLOYMENTS__ = ["lvmecp"]

def __init__(self, gort: GortClient, name: str, actor: str, **kwargs):
super().__init__(gort, name, actor)

async def restart(self):
"""Restarts the ``lvmecp`` deployment."""

# HACK: GortDevice has the everything that is needed to run restart.
await GortDeviceSet.restart(self) # type: ignore

async def status(self):
"""Retrieves the status of the power outlet."""

Expand Down
1 change: 1 addition & 0 deletions src/gort/devices/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class GuiderSet(GortDeviceSet[Guider]):
"""A set of telescope guiders."""

__DEVICE_CLASS__ = Guider
__DEPLOYMENTS__ = ["lvmguider"]

async def expose(self, *args, continuous: bool = False, **kwargs):
"""Exposes all the cameras using the guider.
Expand Down
1 change: 1 addition & 0 deletions src/gort/devices/nps.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ class NPSSet(GortDeviceSet[NPS]):
"""A set of networked power switches."""

__DEVICE_CLASS__ = NPS
__DEPLOYMENTS__ = ["lvmnps"]
1 change: 1 addition & 0 deletions src/gort/devices/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ class SpectrographSet(GortDeviceSet[Spectrograph]):
"""A set of LVM spectrographs."""

__DEVICE_CLASS__ = Spectrograph
__DEPLOYMENTS__ = ["lvmscp"]

def __init__(self, gort: GortClient, data: dict[str, dict], **kwargs):
super().__init__(gort, data, **kwargs)
Expand Down
15 changes: 15 additions & 0 deletions src/gort/devices/telescope.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,13 @@ class TelescopeSet(GortDeviceSet[Telescope]):
"""A representation of a set of telescopes."""

__DEVICE_CLASS__ = Telescope
__DEPLOYMENTS__ = [
"lvm-sci-pwi",
"lvm-spec-pwi",
"lvm-skye-pwi",
"lvm-skyw-pwi",
"lvmtan",
]

def __init__(self, gort: GortClient, data: dict[str, dict]):
super().__init__(gort, data)
Expand All @@ -666,6 +673,14 @@ async def initialise(self):

await asyncio.gather(*[tel.initialise() for tel in self.values()])

async def restart(self):
"""Restarts the ``lvmpwi`` and ``lvmtan`` deployments and re-homes."""

await super().restart()

self.write_to_log("Homing all devices after a restart.")
await self.home(home_subdevices=True)

async def home(self, home_subdevices: bool = False):
"""Initialises and homes all telescopes.

Expand Down
20 changes: 20 additions & 0 deletions src/gort/gort.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class GortDeviceSet(dict[str, GortDeviceType], Generic[GortDeviceType]):
"""

__DEVICE_CLASS__: ClassVar[Type["GortDevice"]]
__DEPLOYMENTS__: ClassVar[list[str]] = []

def __init__(self, gort: GortClient, data: dict[str, dict], **kwargs):
self.gort = gort
Expand Down Expand Up @@ -301,6 +302,25 @@ def write_to_log(

self.gort.log.log(level, message)

async def restart(self):
"""Restarts the set deployments and resets all controllers."""

if not isinstance(self.gort, Gort):
raise GortError("Client is not a Gort instance.")

if self.gort.kubernetes is None:
raise GortError("The Kubernetes cluster is not accessible.")

for deployment in self.__DEPLOYMENTS__:
self.gort.kubernetes.restart_deployment(deployment, from_file=True)

await asyncio.sleep(5)

running_deployments = self.gort.kubernetes.list_deployments()
for deployment in self.__DEPLOYMENTS__:
if deployment not in running_deployments:
self.write_to_log(f"Deployment {deployment} did not restart.", "error")


class GortDevice:
"""A gort-managed device.
Expand Down
2 changes: 1 addition & 1 deletion src/gort/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def restart_deployment(self, deployment: str, from_file: bool = True):
namespace = self.get_deployment_namespace(deployment)
self.log.debug(f"Deleting deployment {deployment}.")
self.apps_v1.delete_namespaced_deployment(deployment, namespace)
sleep(5) # Give some time for the pods to exit.
sleep(3) # Give some time for the pods to exit.
else:
self.log.warning(f"{deployment!r} is not running.")

Expand Down