-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from nsidc/issue-38
Issue 38
- Loading branch information
Showing
14 changed files
with
2,167 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__pycache__ | ||
dist | ||
dist | ||
example/test.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
if (( $# != 1 )); then | ||
echo "Usage: source env.sh aws_profile_name" | ||
echo " where aws_profile_name is an AWS CLI named profile" | ||
echo " https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html" | ||
exit 1 | ||
else | ||
export AWS_PROFILE=$1 | ||
|
||
AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile "$AWS_PROFILE") | ||
AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile "$AWS_PROFILE") | ||
AWS_REGION=$(aws configure get region --profile "$AWS_PROFILE" || echo "$AWS_DEFAULT_REGION") | ||
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text) | ||
|
||
export AWS_ACCESS_KEY_ID | ||
export AWS_SECRET_ACCESS_KEY | ||
export AWS_REGION | ||
export AWS_ACCOUNT_ID | ||
|
||
echo "AWS environment:" | ||
echo " AWS_PROFILE: $AWS_PROFILE" | ||
echo " AWS_REGION: $AWS_REGION" | ||
echo " AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import boto3 | ||
|
||
|
||
KINESIS_PARTITION_KEY = "metgenc-duck" | ||
|
||
def kinesis_stream_exists(stream_name): | ||
client = boto3.client("kinesis", region_name="us-west-2") | ||
try: | ||
summary = client.describe_stream_summary(StreamName=stream_name) | ||
return True | ||
except Exception as e: | ||
return False | ||
|
||
def post_to_kinesis(stream_name, cnm_message): | ||
"""Posts a message to a Kinesis stream.""" | ||
client = boto3.client("kinesis", region_name="us-west-2") | ||
try: | ||
result = client.put_record( | ||
StreamName=stream_name, | ||
Data=cnm_message, | ||
PartitionKey=KINESIS_PARTITION_KEY | ||
) | ||
print(f'Published CNM message {cnm_message} to stream: {stream_name}') | ||
return result['ShardId'] | ||
except Exception as e: | ||
print(e) | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import configparser | ||
import dataclasses | ||
from datetime import datetime, timezone | ||
import os.path | ||
import uuid | ||
|
||
from nsidc.metgen import aws | ||
from nsidc.metgen import constants | ||
|
||
|
||
@dataclasses.dataclass | ||
class Config: | ||
environment: str | ||
data_dir: str | ||
auth_id: str | ||
version: str | ||
provider: str | ||
local_output_dir: str | ||
ummg_dir: str | ||
kinesis_stream_name: str | ||
write_cnm_file: bool | ||
checksum_type: str | ||
number: int | ||
|
||
def show(self): | ||
# TODO add section headings in the right spot (if we think we need them in the output) | ||
print() | ||
print('Using configuration:') | ||
for k,v in self.__dict__.items(): | ||
print(f' + {k}: {v}') | ||
|
||
def enhance(self, producer_granule_id): | ||
mapping = dataclasses.asdict(self) | ||
collection_details = self.collection_from_cmr(mapping) | ||
|
||
mapping['auth_id'] = collection_details['auth_id'] | ||
mapping['version'] = collection_details['version'] | ||
mapping['producer_granule_id'] = producer_granule_id | ||
mapping['submission_time'] = datetime.now(timezone.utc).isoformat() | ||
mapping['uuid'] = str(uuid.uuid4()) | ||
|
||
return mapping | ||
|
||
# Is the right place for this function? | ||
def collection_from_cmr(self, mapping): | ||
# TODO: Use auth_id and version from mapping object to retrieve collection | ||
# metadata from CMR, including formatted version number, temporal range, and | ||
# spatial coverage. | ||
return { | ||
'auth_id': mapping['auth_id'], | ||
'version': mapping['version'] | ||
} | ||
|
||
def config_parser_factory(configuration_file): | ||
if configuration_file is None or not os.path.exists(configuration_file): | ||
raise ValueError(f'Unable to find configuration file {configuration_file}') | ||
cfg_parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) | ||
cfg_parser.read(configuration_file) | ||
return cfg_parser | ||
|
||
|
||
def _get_configuration_value(environment, section, name, value_type, config_parser, overrides, default=None): | ||
vars = { 'environment': environment } | ||
if overrides.get(name) is None: | ||
if value_type is bool: | ||
return config_parser.getboolean(section, name, fallback=default) | ||
elif value_type is int: | ||
return config_parser.getint(section, name, fallback=default) | ||
else: | ||
value = config_parser.get(section, name, vars=vars, fallback=default) | ||
print(name, vars, value) | ||
return value | ||
else: | ||
return overrides.get(name) | ||
|
||
def configuration(config_parser, overrides, environment=constants.DEFAULT_CUMULUS_ENVIRONMENT): | ||
try: | ||
return Config( | ||
environment, | ||
_get_configuration_value(environment, 'Source', 'data_dir', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Collection', 'auth_id', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Collection', 'version', int, config_parser, overrides), | ||
_get_configuration_value(environment, 'Collection', 'provider', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Destination', 'local_output_dir', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Destination', 'ummg_dir', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Destination', 'kinesis_stream_name', str, config_parser, overrides), | ||
_get_configuration_value(environment, 'Destination', 'write_cnm_file', bool, config_parser, overrides, False), | ||
_get_configuration_value(environment, 'Settings', 'checksum_type', str, config_parser, overrides, 'SHA256'), | ||
_get_configuration_value(environment, 'Settings', 'number', int, config_parser, overrides, -1), | ||
) | ||
except Exception as e: | ||
return Exception('Unable to read the configuration file', e) | ||
|
||
def validate(configuration): | ||
"""Validates each value in the configuration.""" | ||
validations = [ | ||
['data_dir', lambda dir: os.path.exists(dir), 'The data_dir does not exist.'], | ||
['local_output_dir', lambda dir: os.path.exists(dir), 'The local_output_dir does not exist.'], | ||
# ['ummg_dir', lambda dir: os.path.exists(dir), 'The ummg_dir does not exist.'], ## Not sure what validation to do | ||
['kinesis_stream_name', lambda name: aws.kinesis_stream_exists(name), 'The kinesis stream does not exist.'], | ||
] | ||
errors = [msg for name, fn, msg in validations if not fn(getattr(configuration, name))] | ||
return len(errors) == 0, errors | ||
|
Oops, something went wrong.