【问题标题】:How Can I Effortlessness Format An AWS CLI Command如何轻松格式化 AWS CLI 命令
【发布时间】:2018-06-23 02:04:23
【问题描述】:

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

然后它会为您提供一个未以任何方式格式化的大型 CLI 命令,即,如果您复制并粘贴该命令,则它全部位于一行中。

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

但是,AWS CLI 命令非常难以阅读,因为它没有格式化。是否有类似JSON formatters 的在线可用 AWS CLI 命令格式化程序?

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

【问题讨论】:

    标签: amazon-web-services aws-cli emr amazon-emr


    【解决方案1】:

    这里有一些快速的 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 
    

    【讨论】:

    • 这是完美的人,感谢分享。对于使用它的任何人,它都适用于 Python 2.7.14 而不是 Python 3。我还在 Notepad++ 中进行了“查找和替换”以删除 \ 字符。谢谢瑞恩·威德迈尔。
    • 我把 \'s 放进去,所以理论上如果你把它转储到 shell 中它会运行,尽管我实际上并没有测试它。此外,针对 python3 进行了更新,这实际上只是 print 调用。
    • 有道理,是的,我注意到这只是 print 调用只是想澄清一下,以防有人在使用这个不知道 Python 的人(愚蠢的 python 2.7-3 更改......)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2015-12-24
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多