Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding version info to module deployments #505

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
### New

### Changes
- adding `AwsCodeSeederDeployed` and `SeedFarmerDeployed` to all module metadata output for reference (versions used to deploy successfully)
- adding `AWS_CODESEEDER_VERSION` and `SEEDFARMER_VERSION` to all module environment parameters for reference (versions currently in use)
- added `--update-seedkit` support to `apply`
- SeedFarmer will no longer try to update the seedkit on every request
- Users can override this with the `--update-seedkit` flag in case AWS CodeSeeder has updated the SeedKit

### Fixes


Expand Down
15 changes: 13 additions & 2 deletions seedfarmer/commands/_module_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import time
from typing import Any, Callable, Dict, List, Optional, Tuple, cast

import aws_codeseeder
import botocore.exceptions
from aws_codeseeder import EnvVar, codeseeder
from aws_codeseeder.errors import CodeSeederRuntimeError
from boto3 import Session

import seedfarmer
import seedfarmer.errors
from seedfarmer import config
from seedfarmer.commands._runtimes import get_runtimes
Expand Down Expand Up @@ -75,6 +77,8 @@ def _env_vars(
env_vars[_param("PERMISSIONS_BOUNDARY_ARN", use_project_prefix)] = permissions_boundary_arn
# Add the partition to env for ease of fetching
env_vars["AWS_PARTITION"] = deployment_partition
env_vars["AWS_CODESEEDER_VERSION"] = aws_codeseeder.__version__
env_vars["SEEDFARMER_VERSION"] = seedfarmer.__version__
return env_vars


Expand Down Expand Up @@ -119,11 +123,13 @@ def deploy_module(
)
]
metadata_env_variable = _param("MODULE_METADATA", use_project_prefix)
sf_version__add = [f"seedfarmer metadata add -k AwsCodeSeederDeployed -v { aws_codeseeder.__version__} || true"]
cs_version_add = [f"seedfarmer metadata add -k SeedFarmerDeployed -v {seedfarmer.__version__} || true"]
metadata_put = [
f"if [[ -f {metadata_env_variable} ]]; then export {metadata_env_variable}=$(cat {metadata_env_variable}); fi",
(
f"echo ${metadata_env_variable} | seedfarmer store moduledata "
f"-d {deployment_name} -g {group_name} -m {module_manifest.name}"
f"-d {deployment_name} -g {group_name} -m {module_manifest.name} "
),
]

Expand Down Expand Up @@ -153,7 +159,12 @@ def deploy_module(
extra_install_commands=["cd module/"] + _phases.install.commands,
extra_pre_build_commands=["cd module/"] + _phases.pre_build.commands,
extra_build_commands=["cd module/"] + _phases.build.commands,
extra_post_build_commands=["cd module/"] + _phases.post_build.commands + md5_put + metadata_put,
extra_post_build_commands=["cd module/"]
+ _phases.post_build.commands
+ md5_put
+ sf_version__add
+ cs_version_add
+ metadata_put,
extra_env_vars=env_vars,
codebuild_compute_type=module_manifest.deploy_spec.build_type,
codebuild_role_name=module_role_name,
Expand Down
27 changes: 24 additions & 3 deletions seedfarmer/mgmt/metadata_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def _read_metadata_file(mms: ModuleMetadataSupport) -> Dict[str, Any]:
return {}


def _read_metadata_env_param(mms: ModuleMetadataSupport) -> Dict[str, Any]:
p = mms.metadata_file_name()
if p in os.environ:
env_data = os.getenv(p)
return cast(Dict[str, Any], json.loads(str(env_data)))
else:
_logger.info("Cannot find existing metadata env param at %s, moving on", p)
return {}


def _mod_dep_key(mms: ModuleMetadataSupport) -> str:
return (
f"{os.getenv(mms.project_param_name())}-"
Expand Down Expand Up @@ -107,15 +117,26 @@ def _clean_jq(jq: str) -> str:

def add_json_output(json_string: str) -> None:
mms = ModuleMetadataSupport()
existing_metadata = _read_metadata_file(mms=mms)
json_new = json.loads(json_string)
_write_metadata_file(mms=mms, data={**json_new, **existing_metadata})
file_dict = _read_metadata_file(mms=mms)
json_new = {**json_new, **file_dict} if file_dict else json_new
_logger.debug(f"Current Dict {json.dumps(json_new, indent=4)}")
env_dict = _read_metadata_env_param(mms=mms)
json_new = {**json_new, **env_dict} if env_dict else json_new
_logger.debug(f"Current Dict {json.dumps(json_new, indent=4)}")
_write_metadata_file(mms=mms, data=json_new)


def add_kv_output(key: str, value: str) -> None:
mms = ModuleMetadataSupport()
data = _read_metadata_file(mms=mms)
data = {}
data[key] = value
file_dict = _read_metadata_file(mms=mms)
data = {**data, **file_dict} if file_dict else data
_logger.debug(f"Current Dict {json.dumps(data, indent=4)}")
env_dict = _read_metadata_env_param(mms=mms)
data = {**data, **env_dict} if env_dict else data
_logger.debug(f"Current Dict {json.dumps(data, indent=4)}")
_write_metadata_file(mms=mms, data=data)


Expand Down
Loading