forked from cerc-io/stack-orchestrator
Delete deployment and DNS names for removed domains
This commit is contained in:
parent
42103ef551
commit
50508644be
@ -13,6 +13,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 shutil
|
||||||
import click
|
import click
|
||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
@ -20,13 +21,14 @@ from pathlib import Path
|
|||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
|
from stack_orchestrator import constants
|
||||||
from stack_orchestrator.util import error_exit, global_options2
|
from stack_orchestrator.util import error_exit, global_options2
|
||||||
from stack_orchestrator.deploy.deployment_create import init_operation, create_operation
|
from stack_orchestrator.deploy.deployment_create import init_operation, create_operation
|
||||||
from stack_orchestrator.deploy.deploy import create_deploy_context
|
from stack_orchestrator.deploy.deploy import create_deploy_context
|
||||||
from stack_orchestrator.deploy.deploy_types import DeployCommandContext
|
from stack_orchestrator.deploy.deploy_types import DeployCommandContext
|
||||||
|
|
||||||
|
|
||||||
def fixup_container_tag(deployment_dir: str, image: str):
|
def _fixup_container_tag(deployment_dir: str, image: str):
|
||||||
deployment_dir_path = Path(deployment_dir)
|
deployment_dir_path = Path(deployment_dir)
|
||||||
compose_file = deployment_dir_path.joinpath("compose", "docker-compose-webapp-template.yml")
|
compose_file = deployment_dir_path.joinpath("compose", "docker-compose-webapp-template.yml")
|
||||||
# replace "cerc/webapp-container:local" in the file with our image tag
|
# replace "cerc/webapp-container:local" in the file with our image tag
|
||||||
@ -37,7 +39,7 @@ def fixup_container_tag(deployment_dir: str, image: str):
|
|||||||
wfile.write(contents)
|
wfile.write(contents)
|
||||||
|
|
||||||
|
|
||||||
def fixup_url_spec(spec_file_name: str, urls: list[str]):
|
def _fixup_url_spec(spec_file_name: str, urls: list[str]):
|
||||||
spec_file_path = Path(spec_file_name)
|
spec_file_path = Path(spec_file_name)
|
||||||
|
|
||||||
# Load existing spec
|
# Load existing spec
|
||||||
@ -100,7 +102,7 @@ def create_deployment(ctx, deployment_dir, image, urls, kube_config, image_regis
|
|||||||
None
|
None
|
||||||
)
|
)
|
||||||
# Add the TLS and DNS spec
|
# Add the TLS and DNS spec
|
||||||
fixup_url_spec(spec_file_name, urls)
|
_fixup_url_spec(spec_file_name, urls)
|
||||||
create_operation(
|
create_operation(
|
||||||
deploy_command_context,
|
deploy_command_context,
|
||||||
spec_file_name,
|
spec_file_name,
|
||||||
@ -109,10 +111,24 @@ def create_deployment(ctx, deployment_dir, image, urls, kube_config, image_regis
|
|||||||
None
|
None
|
||||||
)
|
)
|
||||||
# Fix up the container tag inside the deployment compose file
|
# Fix up the container tag inside the deployment compose file
|
||||||
fixup_container_tag(deployment_dir, image)
|
_fixup_container_tag(deployment_dir, image)
|
||||||
os.remove(spec_file_name)
|
os.remove(spec_file_name)
|
||||||
|
|
||||||
|
|
||||||
|
def update_deployment(deployment_dir, image, urls, env_file):
|
||||||
|
# Update config if required
|
||||||
|
if env_file:
|
||||||
|
deployment_config_file = os.path.join(deployment_dir, "config.env")
|
||||||
|
shutil.copyfile(env_file, deployment_config_file)
|
||||||
|
|
||||||
|
# Update existing deployment spec with new urls
|
||||||
|
_fixup_url_spec(os.path.join(deployment_dir, constants.spec_file_name), urls)
|
||||||
|
|
||||||
|
# Update the image name if required
|
||||||
|
if image:
|
||||||
|
_fixup_container_tag(deployment_dir, image)
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def command(ctx):
|
def command(ctx):
|
||||||
|
@ -170,7 +170,9 @@ def process_app_deployment_request(
|
|||||||
|
|
||||||
# Existing deployment record: take the first lrn that resolves
|
# Existing deployment record: take the first lrn that resolves
|
||||||
deployment_record = None
|
deployment_record = None
|
||||||
|
fqdns_to_release = []
|
||||||
existing_deployment_dir = deployment_dir # Default to target dir in case the app had been undeployed
|
existing_deployment_dir = deployment_dir # Default to target dir in case the app had been undeployed
|
||||||
|
|
||||||
for app_deployment_lrn in app_deployment_lrns:
|
for app_deployment_lrn in app_deployment_lrns:
|
||||||
deployment_record = laconic.get_record(app_deployment_lrn)
|
deployment_record = laconic.get_record(app_deployment_lrn)
|
||||||
if deployment_record:
|
if deployment_record:
|
||||||
@ -183,6 +185,10 @@ def process_app_deployment_request(
|
|||||||
% (app_deployment_lrn, existing_deployment_dir)
|
% (app_deployment_lrn, existing_deployment_dir)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
previous_app_deployment_lrns: list[str] = deployment_record.names
|
||||||
|
previous_fqdns = [lrn.removeprefix(f"{deployment_record_namespace}/") for lrn in previous_app_deployment_lrns]
|
||||||
|
fqdns_to_release = list(set(previous_fqdns) - set(fqdns))
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Use the last fqdn for unique deployment container tag
|
# Use the last fqdn for unique deployment container tag
|
||||||
@ -209,18 +215,14 @@ def process_app_deployment_request(
|
|||||||
# Rename deployment dir according to new request (last fqdn from given dns)
|
# Rename deployment dir according to new request (last fqdn from given dns)
|
||||||
os.rename(existing_deployment_dir, deployment_dir)
|
os.rename(existing_deployment_dir, deployment_dir)
|
||||||
|
|
||||||
# Update config if required
|
|
||||||
if env_filename:
|
|
||||||
deployment_config_file = os.path.join(deployment_dir, "config.env")
|
|
||||||
shutil.copyfile(env_filename, deployment_config_file)
|
|
||||||
|
|
||||||
# Update existing deployment spec with new urls
|
|
||||||
deploy_webapp.fixup_url_spec(os.path.join(deployment_dir, constants.spec_file_name))
|
|
||||||
|
|
||||||
# Update the image name deployment_container_tag
|
# Update the image name deployment_container_tag
|
||||||
# Skip for redeployment as deployment_container_tag won't get built
|
# Skip for redeployment as deployment_container_tag won't get built
|
||||||
|
updated_image = None
|
||||||
if deployment_record.attributes.application != app.id:
|
if deployment_record.attributes.application != app.id:
|
||||||
deploy_webapp.fixup_container_tag(deployment_dir, deployment_container_tag)
|
updated_image = deployment_container_tag
|
||||||
|
|
||||||
|
# Update the existing deployment
|
||||||
|
deploy_webapp.update_deployment(deployment_dir, updated_image, [f"https://{fqdn}" for fqdn in fqdns], env_filename)
|
||||||
|
|
||||||
needs_k8s_deploy = False
|
needs_k8s_deploy = False
|
||||||
if force_rebuild:
|
if force_rebuild:
|
||||||
@ -270,19 +272,15 @@ def process_app_deployment_request(
|
|||||||
else:
|
else:
|
||||||
logger.log("Requested app is already deployed, skipping build and image push")
|
logger.log("Requested app is already deployed, skipping build and image push")
|
||||||
|
|
||||||
# 7. update config (if needed)
|
# 7. restart deployment on config or url spec change
|
||||||
# TODO: Also check if domains set has changed
|
|
||||||
if (
|
if (
|
||||||
not deployment_record
|
not deployment_record
|
||||||
or file_hash(deployment_config_file) != deployment_record.attributes.meta.config
|
or file_hash(os.path.join(deployment_dir, "config.env")) != deployment_record.attributes.meta.config
|
||||||
|
or len(fqdns_to_release) != 0
|
||||||
):
|
):
|
||||||
needs_k8s_deploy = True
|
needs_k8s_deploy = True
|
||||||
|
|
||||||
# TODO: 8. delete unused names
|
# 8. update k8s deployment
|
||||||
# if deployment_record:
|
|
||||||
# ...
|
|
||||||
|
|
||||||
# 9. update k8s deployment
|
|
||||||
if needs_k8s_deploy:
|
if needs_k8s_deploy:
|
||||||
deploy_to_k8s(deployment_record, deployment_dir, recreate_on_deploy, logger)
|
deploy_to_k8s(deployment_record, deployment_dir, recreate_on_deploy, logger)
|
||||||
|
|
||||||
@ -302,6 +300,17 @@ def process_app_deployment_request(
|
|||||||
logger,
|
logger,
|
||||||
)
|
)
|
||||||
logger.log("Publication complete.")
|
logger.log("Publication complete.")
|
||||||
|
|
||||||
|
# 9. delete unused names from previous deployment (app and dns)
|
||||||
|
for fqdn in fqdns_to_release:
|
||||||
|
# Delete app deployment name and DNS name
|
||||||
|
deployment_name = "{deployment_record_namespace}/{fqdn}"
|
||||||
|
dns_name = "{deployment_record_namespace}/{fqdn}"
|
||||||
|
|
||||||
|
logger.log(f"Removing names {deployment_name} and {dns_name}")
|
||||||
|
laconic.delete_name(deployment_name)
|
||||||
|
laconic.delete_name(dns_name)
|
||||||
|
|
||||||
logger.log("END - process_app_deployment_request")
|
logger.log("END - process_app_deployment_request")
|
||||||
|
|
||||||
|
|
||||||
|
@ -711,7 +711,7 @@ def publish_deployment(
|
|||||||
else:
|
else:
|
||||||
dns_ver = "0.0.%d" % (int(dns_record.attributes.version.split(".")[-1]) + 1)
|
dns_ver = "0.0.%d" % (int(dns_record.attributes.version.split(".")[-1]) + 1)
|
||||||
|
|
||||||
fqdn = dns_lrn.removeprefix(f"${dns_record_namespace}/")
|
fqdn = dns_lrn.removeprefix(f"{dns_record_namespace}/")
|
||||||
uniq = uuid.uuid4()
|
uniq = uuid.uuid4()
|
||||||
|
|
||||||
new_dns_record = {
|
new_dns_record = {
|
||||||
|
Loading…
Reference in New Issue
Block a user