【问题标题】:Run elastic beanstalk .ebextensions config for specific environments为特定环境运行弹性 beanstalk .ebextensions 配置
【发布时间】:2021-06-15 17:27:58
【问题描述】:

我有以下配置

0_logdna.config
commands:
  01_install_logdna:
    command: "/home/ec2-user/logdna.sh"
  02_restart_logdna:
    command: "service logdna-agent restart"

files:
  "/home/ec2-user/logdna.sh" :
    mode: "000777"
    owner: root
    group: root
    content: |
      #!/bin/sh
      RACK_ENV=$(/opt/elasticbeanstalk/bin/get-config environment -k RACK_ENV)
      echo "$RACK_ENV"
      if [ $RACK_ENV == production ]
      then
        rpm --import https://repo.logdna.com/logdna.gpg
        echo "[logdna]
        name=LogDNA packages
        baseurl=https://repo.logdna.com/el6/
        enabled=1
        gpgcheck=1
        gpgkey=https://repo.logdna.com/logdna.gpg" | sudo tee /etc/yum.repos.d/logdna.repo
        LOGDNA_INGESTION_KEY=$(/opt/elasticbeanstalk/bin/get-config environment -k LOGDNA_INGESTION_KEY)
        yum -y install logdna-agent
        logdna-agent -k $LOGDNA_INGESTION_KEY # this is your unique Ingestion Key
        # /var/log is monitored/added by default (recursively), optionally add more dirs here
        logdna-agent -d /var/app/current/log/logstasher.log
        logdna-agent -d /var/app/containerfiles/logs/sidekiq.log
        # logdna-agent --hostname allows you to pass your AWS env metadata to LogDNA (remove # to uncomment the line below)
        # logdna-agent --hostname `{"Ref": "AWSEBEnvironmentName" }`
        # logdna -t option allows you to tag the host with tags (remove # to uncomment the line below)
        #logdna-agent -t `{"Ref": "AWSEBEnvironmentName" }`
        chkconfig logdna-agent on
        service logdna-agent start
      fi

我希望能够只为我的生产环境运行此配置,但每次运行此代码时,我都会收到一条错误提示

ERROR   [Instance: i-091794aa00f84ab36,i-05b6d0824e7a0f5da] Command failed on instance. Return code: 1 Output: (TRUNCATED)...not found
/home/ec2-user/logdna.sh: line 17: logdna-agent: command not found
/home/ec2-user/logdna.sh: line 18: logdna-agent: command not found
error reading information on service logdna-agent: No such file or directory
logdna-agent: unrecognized service.

不知道为什么这不起作用。当我回显 RACK_ENV 时,我得到 production 作为值,所以我知道这是正确的,但为什么我的 if 语句失败了,为什么它不能正常工作?

【问题讨论】:

    标签: amazon-web-services shell environment-variables amazon-elastic-beanstalk ebextensions


    【解决方案1】:

    您对echo 的使用将导致/etc/yum.repos.d/logdna.repo 格式错误。要正确设置它,请使用以下方法(EOL2缩进很重要):

    files:
      "/home/ec2-user/logdna.sh" :
        mode: "000777"
        owner: root
        group: root
        content: |
          #!/bin/sh
          RACK_ENV=$(/opt/elasticbeanstalk/bin/get-config environment -k RACK_ENV)
          echo "$RACK_ENV"
          if [ $RACK_ENV == production ]
          then
            rpm --import https://repo.logdna.com/logdna.gpg
            cat >/etc/yum.repos.d/logdna.repo << 'EOL2'
          [logdna]
          name=LogDNA packages
          baseurl=https://repo.logdna.com/el6/
          enabled=1
          gpgcheck=1
          gpgkey=https://repo.logdna.com/logdna.gpg
          EOL2
            LOGDNA_INGESTION_KEY=$(/opt/elasticbeanstalk/bin/get-config environment -k LOGDNA_INGESTION_KEY)
            yum -y install logdna-agent
            logdna-agent -k $LOGDNA_INGESTION_KEY # this is your unique Ingestion Key
            # /var/log is monitored/added by default (recursively), optionally add more dirs here
            logdna-agent -d /var/app/current/log/logstasher.log
            logdna-agent -d /var/app/containerfiles/logs/sidekiq.log
            # logdna-agent --hostname allows you to pass your AWS env metadata to LogDNA (remove # to uncomment the line below)
            # logdna-agent --hostname `{"Ref": "AWSEBEnvironmentName" }`
            # logdna -t option allows you to tag the host with tags (remove # to uncomment the line below)
            #logdna-agent -t `{"Ref": "AWSEBEnvironmentName" }`
            chkconfig logdna-agent on
            service logdna-agent start
          fi
    

    如需进一步故障排除,请查看/var/log/cfn-init-cmd.log 文件。

    【讨论】:

    • 感谢您的回答。所以目前如果我运行你的答案,它工作正常,因为我正在部署到我的生产环境。但我也想将它部署到例如我的登台环境,但我不想安装 logdna,我该如何执行?基本上我还可以在我的命令部分添加一个 if 条件吗?
    • @KingsleySimon 嗨。我的回答解决了原始错误和发布的问题。对于暂存问题,也许您可​​以在commands 中使用test,以便它们仅在测试成功时运行。您可以尝试,如果有错误,可以提出新的问题。
    猜你喜欢
    • 2016-10-08
    • 2015-07-20
    • 2017-05-06
    • 2016-04-09
    • 2017-06-03
    • 2023-03-26
    • 2017-08-14
    • 2020-12-08
    • 2021-11-25
    相关资源
    最近更新 更多