stack-orchestrator/app/base.py

78 lines
2.8 KiB
Python
Raw Normal View History

2023-02-17 20:34:51 +00:00
# Copyright © 2022, 2023 Cerc
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
2023-02-20 13:09:35 +00:00
import os
from abc import ABC, abstractmethod
2023-02-20 13:23:21 +00:00
from .deploy_system import get_stack_status
2023-02-20 13:09:35 +00:00
2023-02-17 20:34:51 +00:00
def get_stack(config, stack):
2023-02-20 13:09:35 +00:00
if stack == "package-registry":
return package_registry_stack(config, stack)
else:
return base_stack(config, stack)
2023-02-17 20:34:51 +00:00
2023-02-20 13:09:35 +00:00
class base_stack(ABC):
2023-02-17 20:34:51 +00:00
def __init__(self, config, stack):
self.config = config
self.stack = stack
2023-02-20 13:09:35 +00:00
@abstractmethod
2023-02-17 20:34:51 +00:00
def ensure_available(self):
2023-02-20 13:09:35 +00:00
pass
2023-02-17 20:34:51 +00:00
2023-02-20 13:09:35 +00:00
@abstractmethod
2023-02-17 20:34:51 +00:00
def get_url(self):
2023-02-20 13:09:35 +00:00
pass
class package_registry_stack(base_stack):
2023-02-20 13:43:06 +00:00
def ensure_available(self):
2023-02-20 13:09:35 +00:00
self.url = "<no registry url set>"
# Check if we were given an external registry URL
url_from_environment = os.environ.get("CERC_NPM_REGISTRY_URL")
if url_from_environment:
if self.config.verbose:
print(f"Using package registry url from CERC_NPM_REGISTRY_URL: {url_from_environment}")
self.url = url_from_environment
else:
# Otherwise we expect to use the local package-registry stack
# First check if the stack is up
2023-02-20 13:43:06 +00:00
registry_running = get_stack_status(self.config, "package-registry")
2023-02-20 13:23:21 +00:00
if registry_running:
# If it is available, get its mapped port and construct its URL
if self.config.debug:
print("Found local package registry stack is up")
2023-02-20 19:46:56 +00:00
# TODO: get url from deploy-stack
2023-02-20 13:23:21 +00:00
self.url = "http://gitea.local:3000/api/packages/cerc-io/npm/"
else:
# If not, print a message about how to start it and return fail to the caller
2023-02-20 13:43:06 +00:00
print("ERROR: The package-registry stack is not running, and no external registry specified with CERC_NPM_REGISTRY_URL")
print("ERROR: Start the local package registry with: laconic-so --stack package-registry deploy-system up")
2023-02-20 13:23:21 +00:00
return False
2023-02-20 13:09:35 +00:00
return True
def get_url(self):
return self.url
# Temporary helper functions while we figure out a good interface to the stack deploy code
def _get_stack_mapped_port(stack, service, exposed_port):
return 3000