为特定环境运行弹性 beanstalk .ebextensions 配置

问题描述

我有以下配置

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 语句失败,为什么它不能正常工作?

解决方法

您对 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 文件。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...