2023-09-10 19:28:26 +00:00
|
|
|
# Copyright © 2022, 2023 Vulcanize
|
2023-02-17 20:34:51 +00:00
|
|
|
|
|
|
|
# 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-06-22 12:36:05 +00:00
|
|
|
from decouple import config
|
2023-10-07 04:22:40 +00:00
|
|
|
from .deploy import get_stack_status
|
2023-02-20 13:23:21 +00:00
|
|
|
|
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-09-04 19:00:23 +00:00
|
|
|
print("ERROR: The package-registry stack is not running, and no external registry "
|
|
|
|
"specified with CERC_NPM_REGISTRY_URL")
|
2023-02-20 13:43:06 +00:00
|
|
|
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
|
2023-06-22 12:36:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_npm_registry_url():
|
|
|
|
# If an auth token is not defined, we assume the default should be the cerc registry
|
|
|
|
# If an auth token is defined, we assume the local gitea should be used.
|
2023-09-04 19:00:23 +00:00
|
|
|
default_npm_registry_url = "http://gitea.local:3000/api/packages/cerc-io/npm/" if config(
|
|
|
|
"CERC_NPM_AUTH_TOKEN", default=None
|
|
|
|
) else "https://git.vdb.to/api/packages/cerc-io/npm/"
|
2023-06-22 12:36:05 +00:00
|
|
|
return config("CERC_NPM_REGISTRY_URL", default=default_npm_registry_url)
|