Skip to content

Commit

Permalink
core: services: versionchooser: Allow updating bootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Jul 13, 2023
1 parent 5c3f343 commit ca9efed
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/services/versionchooser/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ async def get_available_versions(repository: str, image: str) -> Any:
return await versionChooser.get_available_versions(f"{repository}/{image}")


async def get_bootstrap_version() -> Any:
return await versionChooser.get_bootstrap_version()


async def set_bootstrap_version(request: web.Request) -> Any:
data = await request.json()
tag = data["tag"]
return await versionChooser.set_bootstrap_version(tag)


async def load(request: web.Request) -> Any:
data = await request.read()
return await versionChooser.load(data)
Expand Down
25 changes: 25 additions & 0 deletions core/services/versionchooser/openapi/versionchooser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ paths:
type: string
example: master

/bootstrap/current:
get:
operationId: main.get_bootstrap_version
summary: Return the current running version of BlueOS-bootstrap
responses:
'200':
description: The current running version of BlueOS-bootstrap
post:
operationId: main.set_bootstrap_version
summary: Sets the current version of BlueOS-bootstrap to a new tag
responses:
'200':
description: Successfully set a new version
'400':
description: Attempted to set an unavailable version
requestBody:
content:
application/json:
schema:
type: object
properties:
tag:
type: string
example: master

/version/restart:
post:
operationId: main.restart
Expand Down
69 changes: 69 additions & 0 deletions core/services/versionchooser/utils/chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class VersionChooser:
def __init__(self, client: aiodocker.Docker):
self.client = client
self.cleanup()
self.bootstrap_name = "blueos-bootstrap"

@staticmethod
def cleanup() -> None:
Expand Down Expand Up @@ -136,6 +137,74 @@ async def pull_version(self, request: web.Request, repository: str, tag: str) ->
# TODO: restore pruning
return response

async def get_bootstrap_version(self) -> str:
"""Get the current bootstrap container image version.
Retrieves the bootstrap container and returns the image version
from the container configuration.
Returns:
str: The image version string for the bootstrap container.
"""
bootstrap = await self.client.containers.get(self.bootstrap_name) # type: ignore
return str(bootstrap["Config"]["Image"])

async def set_bootstrap_version(self, tag: str) -> web.StreamResponse:
"""Set the bootstrap container to a new version.
Stops the current bootstrap container, renames it to a backup,
creates a new bootstrap container with the provided image tag,
starts the new container, and returns a response.
Args:
tag (str): The image tag for the new bootstrap container.
Returns:
web.StreamResponse: Response indicating success.
"""

bootstrap = None
logging.info(f"Setting new bootstrap version: {tag}")
try:
bootstrap = await self.client.containers.get(self.bootstrap_name) # type: ignore
logging.info(f"Got bootstrap..")
except Exception as error:
logging.critical("Warning: %s: %s", type(error), error)

backup_name = "bootstrap-backup"
try:
backup = None
backup = await self.client.containers.get(backup_name) # type: ignore
logging.info(f"Got {backup_name}, going to delete and create a new one..")
await backup.delete(force=False, noprune=False) # type: ignore
except Exception as error:
logging.critical("warning: %s: %s", type(error), error)

if bootstrap:
logging.info(f"Setting current {await self.get_bootstrap_version()} as {backup_name}")
await bootstrap.rename(backup_name)
logging.info(f"Stop {self.bootstrap_name}")
await bootstrap.stop()

HOME = "/root"
bootstrap_config = {
"Image": f"bluerobotics/blueos-bootstrap:{tag}",
"HostConfig": {
"RestartPolicy": {"Name": "unless-stopped"},
"NetworkMode": "host",
"Binds": [
f"{HOME}/.config/blueos/bootstrap:/root/.config/bootstrap",
"/var/run/docker.sock:/var/run/docker.sock",
],
},
"Env": [f"BLUEOS_CONFIG_PATH={HOME}/.config/blueos"],
}

container = await self.client.containers.create(bootstrap_config, name=self.bootstrap_name) # type: ignore
await container.start()
logging.info(f"Bootstrap updated to {bootstrap_config['Image']}")
return web.Response(status=200, text=f"Bootstrap update to {tag}")

async def set_version(self, image: str, tag: str) -> web.StreamResponse:
"""Sets the current version.
Expand Down

0 comments on commit ca9efed

Please sign in to comment.