alpine-overlay/kubezero/zdt-base/write_parameters.py

64 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import os
import boto3
import argparse
parser = argparse.ArgumentParser(
description="Get SSM parameters beyond <path> and write to files")
parser.add_argument(
"--path",
dest="path",
action="store",
required=True,
help="SSM parameter path")
parser.add_argument(
"--root",
dest="root",
action="store",
required=True,
help="root filesystem path to create files")
args = parser.parse_args()
session = boto3.Session()
awsSSMClient = session.client('ssm')
def get_parameters_by_path(nextToken=None):
params = {
'Path': args.path,
'Recursive': True,
'WithDecryption': True
}
if nextToken is not None:
params['NextToken'] = nextToken
return awsSSMClient.get_parameters_by_path(**params)
def getParameters():
nextToken = None
while True:
response = get_parameters_by_path(nextToken)
parameters = response['Parameters']
if len(parameters) == 0:
break
for parameter in parameters:
yield parameter
if 'NextToken' not in response:
break
nextToken = response['NextToken']
for parameter in getParameters():
file_name = os.path.join(
args.root, parameter["Name"].removeprefix(
args.path).lstrip("/"))
os.makedirs(os.path.dirname(file_name), mode=0o755, exist_ok=True)
#print(f'{file_name}={parameter["Value"]}')
with open(file_name, "w") as file:
file.write(parameter["Value"])