运行 针对特定环境的 elastic beanstalk .ebextensions 配置

Run elastic beanstalk .ebextensions config for specific environments

我有以下配置

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