如何轻松格式化 AWS CLI 命令

How Can I Effortlessness Format An AWS CLI Command

我正在使用 AWS EMR 做很多工作,当您通过 AWS 管理控制台构建 EMR 集群时,您可以单击一个按钮来导出创建 EMR 集群的 AWS CLI 命令。

然后它会为您提供一个没有任何格式的大 CLI 命令,即,如果您复制并粘贴该命令,它全部在一行中。

我正在使用这些由其他人创建的 EMR CLI 命令,使用 AWS SDK Boto3 库在 Python 中创建 EMR 集群,即,我正在查看 CLI 命令以获取所有配置细节。 AWS 管理控制台 UI 上提供了一些配置详细信息,但不是全部,因此我可以更轻松地使用您可以导出的 CLI 命令。

但是,由于 AWS CLI 命令没有格式化,因此很难阅读。是否有类似于 JSON formatters?

的在线 AWS CLI 命令格式化程序

我可以使用的另一个解决方案是克隆 EMR 集群并通过 AWS 管理控制台上的 EMR 集群创建屏幕获取所有配置详细信息,但我仍然很好奇我是否可以格式化 CLI 命令并执行就那样能够格式化导出的 CLI 命令的另一个额外好处是我可以将它放在 Confluence 页面上以供文档使用。

这里有一些快速的python代码:

import shlex
import json
import re

def format_command(command):
    tokens = shlex.split(command)
    formatted = ''
    for token in tokens:
        # Flags get a new line
        if token.startswith("--"):
            formatted += '\\n    '
        # JSON data
        if token[0] in ('[', '{'):
            json_data = json.loads(token)
            data = json.dumps(json_data, indent=4).replace('\n', '\n    ')
            formatted += "'{}' ".format(data)
        # Quote token when it contains whitespace
        elif re.match('\s', token):
            formatted += "'{}' ".format(token)
        # Simple print for remaining tokens
        else:
            formatted += token + ' '
    return formatted


example = """aws emr create-cluster --applications Name=spark Name=ganglia Name=hadoop --tags 'Project=MyProj' --ec2-attributes '{"KeyName":"emr-key","AdditionalSlaveSecurityGroups":["sg-3822994c","sg-ccc76987"],"InstanceProfile":"EMR_EC2_DefaultRole","ServiceAccessSecurityGroup":"sg-60832c2b","SubnetId":"subnet-3c76ee33","EmrManagedSlaveSecurityGroup":"sg-dd832c96","EmrManagedMasterSecurityGroup":"sg-b4923dff","AdditionalMasterSecurityGroups":["sg-3822994c","sg-ccc76987"]}' --service-role EMR_DefaultRole --release-label emr-5.14.0 --name 'Test Cluster' --instance-groups '[{"InstanceCount":1,"EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"SizeInGB":32,"VolumeType":"gp2"},"VolumesPerInstance":1}]},"InstanceGroupType":"MASTER","InstanceType":"m4.xlarge","Name":"Master"},{"InstanceCount":1,"EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"SizeInGB":32,"VolumeType":"gp2"},"VolumesPerInstance":1}]},"InstanceGroupType":"CORE","InstanceType":"m4.xlarge","Name":"CORE"}]' --configurations '[{"Classification":"spark-defaults","Properties":{"spark.sql.avro.compression.codec":"snappy","spark.eventLog.enabled":"true","spark.dynamicAllocation.enabled":"false"},"Configurations":[]},{"Classification":"spark-env","Properties":{},"Configurations":[{"Classification":"export","Properties":{"SPARK_DAEMON_MEMORY":"4g"},"Configurations":[]}]}]' --scale-down-behavior TERMINATE_AT_TASK_COMPLETION --region us-east-1"""
print(format_command(example))

输出如下所示:

aws emr create-cluster \
    --applications Name=spark Name=ganglia Name=hadoop \
    --tags Project=MyProj \
    --ec2-attributes '{
        "ServiceAccessSecurityGroup": "sg-60832c2b", 
        "InstanceProfile": "EMR_EC2_DefaultRole", 
        "EmrManagedMasterSecurityGroup": "sg-b4923dff", 
        "KeyName": "emr-key", 
        "SubnetId": "subnet-3c76ee33", 
        "AdditionalMasterSecurityGroups": [
            "sg-3822994c", 
            "sg-ccc76987"
        ], 
        "AdditionalSlaveSecurityGroups": [
            "sg-3822994c", 
            "sg-ccc76987"
        ], 
        "EmrManagedSlaveSecurityGroup": "sg-dd832c96"
    }' \
    --service-role EMR_DefaultRole \
    --release-label emr-5.14.0 \
    --name Test Cluster \
    --instance-groups '[
        {
            "EbsConfiguration": {
                "EbsBlockDeviceConfigs": [
                    {
                        "VolumeSpecification": {
                            "VolumeType": "gp2", 
                            "SizeInGB": 32
                        }, 
                        "VolumesPerInstance": 1
                    }
                ]
            }, 
            "InstanceCount": 1, 
            "Name": "Master", 
            "InstanceType": "m4.xlarge", 
            "InstanceGroupType": "MASTER"
        }, 
        {
            "EbsConfiguration": {
                "EbsBlockDeviceConfigs": [
                    {
                        "VolumeSpecification": {
                            "VolumeType": "gp2", 
                            "SizeInGB": 32
                        }, 
                        "VolumesPerInstance": 1
                    }
                ]
            }, 
            "InstanceCount": 1, 
            "Name": "CORE", 
            "InstanceType": "m4.xlarge", 
            "InstanceGroupType": "CORE"
        }
    ]' \
    --configurations '[
        {
            "Properties": {
                "spark.eventLog.enabled": "true", 
                "spark.dynamicAllocation.enabled": "false", 
                "spark.sql.avro.compression.codec": "snappy"
            }, 
            "Classification": "spark-defaults", 
            "Configurations": []
        }, 
        {
            "Properties": {}, 
            "Classification": "spark-env", 
            "Configurations": [
                {
                    "Properties": {
                        "SPARK_DAEMON_MEMORY": "4g"
                    }, 
                    "Classification": "export", 
                    "Configurations": []
                }
            ]
        }
    ]' \
    --scale-down-behavior TERMINATE_AT_TASK_COMPLETION \
    --region us-east-1