Working
All checks were successful
Lint Checks / Run linter (pull_request) Successful in 55s
Deploy Test / Run deploy test suite (pull_request) Successful in 4m19s
Webapp Test / Run webapp test suite (pull_request) Successful in 5m21s
Smoke Test / Run basic test suite (pull_request) Successful in 5m24s
K8s Deploy Test / Run deploy test suite on kind/k8s (pull_request) Successful in 6m29s

This commit is contained in:
Thomas E Lackey 2024-02-23 18:52:37 -06:00
parent d8f5d41261
commit b0d003cc01
2 changed files with 29 additions and 8 deletions

View File

@ -29,7 +29,7 @@ def _image_needs_pushed(image: str):
return image.endswith(":local") return image.endswith(":local")
def remote_image_exists(local_tag: str, remote_repo_url: str): def remote_image_exists(remote_repo_url: str, local_tag: str):
docker = DockerClient() docker = DockerClient()
try: try:
remote_tag = remote_tag_for_image(local_tag, remote_repo_url) remote_tag = remote_tag_for_image(local_tag, remote_repo_url)
@ -39,6 +39,19 @@ def remote_image_exists(local_tag: str, remote_repo_url: str):
return False return False
def add_tags_to_image(remote_repo_url: str, local_tag: str, *additional_tags):
if not additional_tags:
return
if not remote_image_exists(remote_repo_url, local_tag):
raise Exception(f"{local_tag} does not exist in {remote_repo_url}")
docker = DockerClient()
remote_tag = remote_tag_for_image(local_tag, remote_repo_url)
new_remote_tags = [remote_tag_for_image(tag, remote_repo_url) for tag in additional_tags]
docker.buildx.imagetools.create(sources=[remote_tag], tags=new_remote_tags)
def remote_tag_for_image(image: str, remote_repo_url: str): def remote_tag_for_image(image: str, remote_repo_url: str):
# Turns image tags of the form: foo/bar:local into remote.repo/org/bar:deploy # Turns image tags of the form: foo/bar:local into remote.repo/org/bar:deploy
major_parts = image.split("/", 2) major_parts = image.split("/", 2)

View File

@ -12,6 +12,7 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
import hashlib
import json import json
import os import os
import shlex import shlex
@ -23,7 +24,7 @@ import uuid
import click import click
from stack_orchestrator.deploy.images import remote_image_exists from stack_orchestrator.deploy.images import remote_image_exists, add_tags_to_image
from stack_orchestrator.deploy.webapp import deploy_webapp from stack_orchestrator.deploy.webapp import deploy_webapp
from stack_orchestrator.deploy.webapp.util import (LaconicRegistryClient, from stack_orchestrator.deploy.webapp.util import (LaconicRegistryClient,
build_container_image, push_container_image, build_container_image, push_container_image,
@ -92,34 +93,41 @@ def process_app_deployment_request(
deployment_record = laconic.get_record(app_deployment_crn) deployment_record = laconic.get_record(app_deployment_crn)
deployment_dir = os.path.join(deployment_parent_dir, fqdn) deployment_dir = os.path.join(deployment_parent_dir, fqdn)
deployment_config_file = os.path.join(deployment_dir, "config.env") deployment_config_file = os.path.join(deployment_dir, "config.env")
deployment_container_tag = f"laconic-webapp/{app.id}:local" deployment_container_unique_tag = "laconic-webapp/%s:local" % hashlib.md5(deployment_dir.encode()).hexdigest()
deployment_container_shared_tag = f"laconic-webapp/{app.id}:local"
# b. check for deployment directory (create if necessary) # b. check for deployment directory (create if necessary)
if not os.path.exists(deployment_dir): if not os.path.exists(deployment_dir):
if deployment_record: if deployment_record:
raise Exception("Deployment record %s exists, but not deployment dir %s. Please remove name." % raise Exception("Deployment record %s exists, but not deployment dir %s. Please remove name." %
(app_deployment_crn, deployment_dir)) (app_deployment_crn, deployment_dir))
print("deploy_webapp", deployment_dir) print("deploy_webapp", deployment_dir)
deploy_webapp.create_deployment(ctx, deployment_dir, deployment_container_tag, deploy_webapp.create_deployment(ctx, deployment_dir, deployment_container_unique_tag,
f"https://{fqdn}", kube_config, image_registry, env_filename) f"https://{fqdn}", kube_config, image_registry, env_filename)
elif env_filename: elif env_filename:
shutil.copyfile(env_filename, deployment_config_file) shutil.copyfile(env_filename, deployment_config_file)
needs_k8s_deploy = False needs_k8s_deploy = False
built_images = False
# 6. build container (if needed) # 6. build container (if needed)
if not deployment_record or deployment_record.attributes.application != app.id: if not deployment_record or deployment_record.attributes.application != app.id:
# check if the image already exists # check if the image already exists
if force_rebuild or not remote_image_exists(deployment_container_tag, image_registry): if force_rebuild or not remote_image_exists(image_registry, deployment_container_shared_tag):
# TODO: pull from request # TODO: pull from request
extra_build_args = [] extra_build_args = []
build_container_image(app, deployment_container_tag, extra_build_args, log_file) build_container_image(app, deployment_container_shared_tag, extra_build_args, log_file)
push_container_image(deployment_dir, log_file) push_container_image(deployment_dir, log_file)
built_images = True
needs_k8s_deploy = True needs_k8s_deploy = True
# 7. update config (if needed) # 7. Add tags (as needed)
if built_images or not remote_image_exists(image_registry, deployment_container_unique_tag):
add_tags_to_image(image_registry, deployment_container_shared_tag, deployment_container_unique_tag)
# 8. update config (if needed)
if not deployment_record or file_hash(deployment_config_file) != deployment_record.attributes.meta.config: if not deployment_record or file_hash(deployment_config_file) != deployment_record.attributes.meta.config:
needs_k8s_deploy = True needs_k8s_deploy = True
# 8. update k8s deployment # 9. update k8s deployment
if needs_k8s_deploy: if needs_k8s_deploy:
print("Deploying to k8s") print("Deploying to k8s")
deploy_to_k8s( deploy_to_k8s(