-
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 #11 from william-cass-wright/eighth
logger fix and config endpoints
- Loading branch information
Showing
8 changed files
with
256 additions
and
73 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
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,66 @@ | ||
""" | ||
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager.html | ||
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html | ||
# aws = AwsSecretMgmt() | ||
# secret_name = "test/test_secret" | ||
# secret = aws.get_secret(secret_name) | ||
# secret_name = 'test/test_secret_2' | ||
# secret_string = json.dumps(secret) | ||
# aws.create_secret(secret_string,secret_name) | ||
## write aws secrets to local | ||
""" | ||
|
||
from .aws import AwsSecretMgmt | ||
from .config import ConfigHandler | ||
|
||
|
||
aws = AwsSecretMgmt() | ||
|
||
|
||
def write_secret_to_local_config(project_name): | ||
config = ConfigHandler(project_name) | ||
secrets_prefix = "projects/dev" | ||
secret = aws.get_secret(os.path.join(secrets_prefix, project_name)) | ||
config.write_config_file_from_dict(config_dict=secret) | ||
return config.print_configs() | ||
|
||
|
||
# for project_name in projects: | ||
# write_secret_to_local_config(project_name) | ||
|
||
|
||
def find_all(name, path): | ||
result = [] | ||
for root, dirs, files in os.walk(path): | ||
if name in files: | ||
result.append(os.path.join(root, name)) | ||
return result | ||
|
||
|
||
def create_res_dict_from_envrc(): | ||
files = find_all(".envrc", "../.") | ||
projects = ["media_mgmt_cli", "twl_app"] | ||
res = {} | ||
for file_path, project_name in zip(files, projects): | ||
tmp_res = {} | ||
with open(file_path, "r") as file: | ||
tmp_lines = file.readlines() | ||
for pos, line in enumerate(tmp_lines): | ||
tmp_var = line.replace("\n", "").replace("export ", "").split("=") | ||
if "KEY" in tmp_var[0]: | ||
pass | ||
else: | ||
tmp_res[tmp_var[0]] = tmp_var[1] | ||
res[project_name] = tmp_res | ||
return res | ||
|
||
|
||
def create_secret_from_dict(project_name, secrets_dict): | ||
secrets_prefix = "projects/dev" | ||
secret_name = os.path.join(secrets_prefix, project_name) | ||
secret_string = json.dumps(secrets_dict) | ||
return aws.create_secret(secret_string, secret_name) |
This file was deleted.
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
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,95 @@ | ||
import os | ||
import pathlib | ||
import configparser | ||
|
||
|
||
class ConfigHandler: | ||
def __init__(self, project_name="tmp"): | ||
p = pathlib.Path.home() | ||
self.home_path = p | ||
self.config_path = p / ".config" / project_name | ||
self.config_file_path = self.config_path / "config" | ||
|
||
self.config = configparser.ConfigParser() | ||
if os.path.isfile(self.config_file_path): | ||
self.config.read(self.config_file_path) | ||
print("-- config file exists --") | ||
print(self.print_configs()) | ||
|
||
def export_configs(self): | ||
# export configs as environment variables | ||
for key, val in self.config.defaults().items(): | ||
if key is not None: | ||
os.environ[key.upper()] = val | ||
|
||
def print_configs(self): | ||
# print('print configs') | ||
print(self.config.defaults()) | ||
# self.config.read_file( | ||
for key, val in self.config.defaults().items(): | ||
# if key is not None: | ||
# print(key.upper(),(20-int(len(key)))*' ', val) | ||
self.formatted_print(key, val) | ||
|
||
def write_config_file(self): | ||
# rewrite config file | ||
with open(self.config_file_path, "w") as configfile: | ||
self.config.write(configfile) | ||
|
||
def create_file_and_dir(self): | ||
self.config_path.mkdir(parents=True, exist_ok=True) | ||
self.config_file_path.touch() | ||
|
||
def config_file_input(self, config_dict: dict, section: str = "DEFAULT"): | ||
""" | ||
example: | ||
config['DEFAULT'] = {'ServerAliveInterval': '45', | ||
'Compression': 'yes', | ||
'CompressionLevel': '8',} | ||
""" | ||
self.config[section] = config_dict | ||
|
||
def write_config_file_from_dict(self, config_dict: dict): | ||
self.config_file_input(config_dict) | ||
self.write_config_file() | ||
|
||
def put_project(self, project_name): | ||
self.project_name = project_name | ||
self.config_path = self.home_path / ".config" / project_name | ||
self.config_file_path = self.config_path / "config" | ||
self.config = configparser.ConfigParser() | ||
if os.path.isfile(self.config_file_path): | ||
self.config.read(self.config_file_path) | ||
print("-- config file exists --") | ||
print(self.print_configs()) | ||
|
||
def list_config_dirs(self): | ||
# list directories | ||
p = self.home_path / ".config" | ||
active = [] | ||
other = [] | ||
for x in p.iterdir(): | ||
if x.is_dir(): | ||
tmp = x / "config" | ||
if os.path.isfile(tmp): | ||
active.append(tmp.resolve()) | ||
resp = "config file exists" | ||
else: | ||
other.append(x.resolve()) | ||
resp = "no config file" | ||
self.formatted_print(x, resp, n=45) | ||
|
||
for file_path in active: | ||
print("\n-- ", file_path, " --") | ||
tmp = configparser.ConfigParser() | ||
tmp.read(file_path) | ||
for key, val in tmp.defaults().items(): | ||
self.formatted_print(key, val) | ||
|
||
def formatted_print(self, key, val, n=20): | ||
key = str(key) | ||
val = str(val) | ||
print(key, (n - int(len(key))) * ".", val) | ||
|
||
|
||
config_handler = ConfigHandler() |
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
Oops, something went wrong.