全部产品
云市场

从实例中拷贝文件到OSS

更新时间:2019-10-30 10:06:13

场景简介

我们经常遇到从某台线上的机器把日志文件拷贝出来或者是进程卡了,通过jstack输出到某个文件再进行分析。我们使用osscmd把数据导到oss里,但是手动配置过程实在是过于繁琐。通过如下模版您可很容易地处理此需求。

解决方案

  1. 从实例中拷贝文件可以利用云助手的RunCommand去到机器上去调用ossutil做数据拷贝,关键问题是AK怎么传,当然我们可以把AK当参数以命令的方式传入,但是这种太不安全了,相当于AK明文传输。那么有没有别的办法解决AK明文传输的问题呢?此时就想到了InstanceRole的功能,可以通过给instance attach一个ram role,那么在实例内部curl某个固定地址就能拿到以这个role身份产生的sts-token。详细参见文档https://help.aliyun.com/document_detail/54235.html
    1. curl http://100.100.100.200/latest/meta-data/Ram/security-credentials/{{InstanceAssumeRole}}
  2. 以上AK的问题解决了,另外的问题ossutil在实例中访问不了公网的情况怎么下载呢?咨询了oss的支持同事,他们并不提供内网下载的地址。 只能自己搭建了,我们自己创建了个bucket为oos-public的bucket提供全局只读的权限可以方便内网下载。解析json我们需要个方便的工具jq,我们同时把这个文件传到公共bucket上经过调试最终的脚本为

    1. test -e ossutil64 || wget https://oos-public.oss-{{ACS::RegionId}}-internal.aliyuncs.com/x64/ossutil64 && chmod 755 ossutil64
    2. test -e jq || wget https://oos-public.oss-{{ACS::RegionId}}-internal.aliyuncs.com/x64/jq && chmod 755 jq
    3. stsToken=`curl http://100.100.100.200/latest/meta-data/Ram/security-credentials/{{InstanceAssumeRole}}`
    4. accessKeyId=`echo $stsToken |./jq .AccessKeyId | awk -F'\"' '{print $2}'`
    5. accessKeySecret=`echo $stsToken | ./jq .AccessKeySecret | awk -F'\"' '{print $2}'
    6. securityToken=`echo $stsToken | ./jq .SecurityToken | awk -F'\"' '{print $2}'
    7. endpoint=https://oss-{{ACS::RegionId}}.aliyuncs.com;
    8. ./ossutil64 -i $accessKeyId -k $accessKeySecret -t $securityToken -e $endpoint cp {{SrcUrl}} {{DestUrl}}
  3. 再转换成OOS模板

    1. {
    2. "FormatVersion": "OOS-2019-06-01",
    3. "Description": "Tag ECS Instance by the RunCommand invocation result.",
    4. "Parameters": {
    5. "InstanceId": {
    6. "Type": "String",
    7. "Description": "the Instance Id to operate in linux.",
    8. "MinLength": 1,
    9. "MaxLength": 30
    10. },
    11. "SrcUrl": {
    12. "Type": "String",
    13. "Description": "command content to run in linux ecs."
    14. },
    15. "DestUrl": {
    16. "Type": "String",
    17. "Description": "command content to run in linux ecs."
    18. },
    19. "InstanceAssumeRole": {
    20. "Type": "String",
    21. "Description": ""
    22. },
    23. "OOSAssumeRole": {
    24. "Type": "String",
    25. "Description": "oos assume this role to execution task.",
    26. "Default": "OOSServiceRole"
    27. }
    28. },
    29. "RamRole": "{{OOSAssumeRole}}",
    30. "Tasks": [{
    31. "Name": "checkInstanceReady",
    32. "Action": "ACS::CheckFor",
    33. "Description": "describe instances with specified parameters, refer them here: https://help.aliyun.com/document_detail/63440.html",
    34. "Properties": {
    35. "API": "DescribeInstances",
    36. "Service": "ECS",
    37. "PropertySelector": "Instances.Instance[].Status",
    38. "DesiredValues": [
    39. "Running"
    40. ],
    41. "Parameters": {
    42. "InstanceIds": ["{{ InstanceId }}"]
    43. }
    44. }
    45. },
    46. {
    47. "Name": "runCommand",
    48. "Action": "ACS::ECS::RunCommand",
    49. "Description": "",
    50. "Properties": {
    51. "commandContent": {
    52. "Fn::Join": [ "\n", [
    53. "test -e oos || mkdir oos;",
    54. "cd oos;",
    55. "test -e ossutil64 || wget https://oos-public.oss-{{ACS::RegionId}}-internal.aliyuncs.com/x64/ossutil64 && chmod 755 ossutil64",
    56. "test -e jq || wget https://oos-public.oss-{{ACS::RegionId}}-internal.aliyuncs.com/x64/jq && chmod 755 jq",
    57. "stsToken=`curl http://100.100.100.200/latest/meta-data/Ram/security-credentials/{{InstanceAssumeRole}}`",
    58. "accessKeyId=`echo $stsToken |./jq .AccessKeyId | awk -F'\"' '{print $2}'` ;",
    59. "accessKeySecret=`echo $stsToken | ./jq .AccessKeySecret | awk -F'\"' '{print $2}'` ;",
    60. "securityToken=`echo $stsToken | ./jq .SecurityToken | awk -F'\"' '{print $2}'` ;",
    61. "endpoint=https://oss-{{ACS::RegionId}}-internal.aliyuncs.com;",
    62. "./ossutil64 -i $accessKeyId -k $accessKeySecret -t $securityToken -e $endpoint cp {{SrcUrl}} {{DestUrl}}"
    63. ]
    64. ]
    65. },
    66. "commandType": "RunShellScript",
    67. "instanceId": "{{InstanceId}}"
    68. },
    69. "Outputs": {
    70. "CommandOutput":{
    71. "Type": "String",
    72. "ValueSelector": "InvocationResult[].Output"
    73. }
    74. }
    75. }
    76. ],
    77. "Outputs": {}
    78. }
  4. 传入参数 实例ID,拷贝的文件,拷到哪,传好之后执行。oos

  5. 执行成功后的结果oos

  6. 根据Execution的执行日志可以看到执行过程 base64 decode出来后可以看到shell执行的具体信息,从结果上看是成功了,再到oss上看,文件在不在。oos

    总结

    以上我们介绍了如果使用运维编排方便的从实例中拷贝文件到OSS上,结合拷贝文件的场景与执行命令的组合,我们可以方便的做出到某台机器jstack输出文件并拷贝出来等相应场景。