zoukankan      html  css  js  c++  java
  • Cloudinsight Agent install script

    #!/bin/bash
    # Cloudinsight Agent install script.
    set -e
    logfile="ci-agent-install.log"
    gist_request=/tmp/agent-gist-request.tmp
    gist_response=/tmp/agent-gist-response.tmp
    
    if [ $(command -v curl) ]; then
        cl_cmd="curl -f"
    else
        cl_cmd="wget --quiet"
    fi
    
    # Set up a named pipe for logging
    npipe=/tmp/$$.tmp
    mknod $npipe p
    
    # Log all output to a log for error checking
    tee <$npipe $logfile &
    exec 1>&-
    exec 1>$npipe 2>&1
    trap "rm -f $npipe" EXIT
    
    
    function on_error() {
        printf "33[31m$ERROR_MESSAGE
    It looks like you hit an issue when trying to install the Agent.
    
    Troubleshooting and basic usage information for the Agent are available at:
    
        http://docs-ci.oneapm.com/quick-start/
    
    If you're still having problems, please contact to support@oneapm.com
    and we'll try our best to help you solve your problem.
    33[0m
    "
    }
    trap on_error ERR
    
    if [ -n "$CI_LICENSE_KEY" ]; then
        license_key=$CI_LICENSE_KEY
    fi
    
    if [ -n "$CI_INSTALL_ONLY" ]; then
        no_start=true
    else
        no_start=false
    fi
    
    if [ ! $license_key ]; then
        printf "33[31mLicense key not available in CI_LICENSE_KEY environment variable.33[0m
    "
        exit 1;
    fi
    
    # OS/Distro Detection
    # Try lsb_release, fallback with /etc/issue then uname command
    KNOWN_DISTRIBUTION="(Debian|Ubuntu|RedHat|CentOS|Amazon)"
    DISTRIBUTION=$(lsb_release -d 2>/dev/null | grep -Eo $KNOWN_DISTRIBUTION  || grep -Eo $KNOWN_DISTRIBUTION /etc/issue 2>/dev/null || uname -s)
    
    if [ $DISTRIBUTION = "Darwin" ]; then
        printf "33[31mThis script does not support installing on the Mac..33[0m
    "
        exit 1;
    
    elif [ -f /etc/debian_version -o "$DISTRIBUTION" == "Debian" -o "$DISTRIBUTION" == "Ubuntu" ]; then
        OS="Debian"
    elif [ -f /etc/redhat-release -o "$DISTRIBUTION" == "RedHat" -o "$DISTRIBUTION" == "CentOS" -o "$DISTRIBUTION" == "Amazon" ]; then
        OS="RedHat"
    elif [ -f /etc/system-release -o "$DISTRIBUTION" == "Amazon" ]; then
        OS="RedHat"
    fi
    
    # Root user detection
    if [ $(echo "$UID") = "0" ]; then
        sudo_cmd=''
    else
        sudo_cmd='sudo'
    fi
    
    function backup_old_configuration {
        if [[ ! -d /etc/cloudinsight-agent ]]; then
            # New configuration does not exist, move legacy configuration to new location
            echo -e "Please note, Cloudinsight Agent's configuration is now located at '/etc/cloudinsight-agent' (previously '/etc/oneapm-ci-agent')."
            $sudo_cmd mkdir /etc/cloudinsight-agent
            $sudo_cmd cp -r /etc/oneapm-ci-agent/checks.d /etc/cloudinsight-agent/
            $sudo_cmd cp -r /etc/oneapm-ci-agent/conf.d /etc/cloudinsight-agent/
    
            backup_name="oneapm-ci-agent.conf.$(date +%s).backup"
            echo "A backup of your current configuration can be found at: /etc/cloudinsight-agent/$backup_name"
            $sudo_cmd cp -a /etc/oneapm-ci-agent/oneapm-ci-agent.conf /etc/cloudinsight-agent/$backup_name
        fi
    }
    
    # Install the necessary package sources
    if [ $OS = "RedHat" ]; then
        echo -e "33[34m
    * Installing YUM sources for Cloudinsight Agent
    33[0m"
    
        UNAME_M=$(uname -m)
        if [ "$UNAME_M"  == "i686" -o "$UNAME_M"  == "i386" -o "$UNAME_M"  == "x86" ]; then
            ARCHI="i386"
        else
            ARCHI="x86_64"
        fi
    
        $sudo_cmd sh -c "echo -e '[cloudinsight-agent]
    name = OneAPM, Inc.
    baseurl = http://yum.oneapm.com/$ARCHI/
    enabled=1
    gpgcheck=0
    priority=1' > /etc/yum.repos.d/cloudinsight-agent.repo"
    
        printf "33[34m* Installing the Cloudinsight Agent package
    33[0m
    "
    
        CI_OLD_INSTALLED=$(yum list installed oneapm-ci-agent > /dev/null 2>&1 || echo "no")
        if [ "$CI_OLD_INSTALLED" != "no" ]; then
            backup_old_configuration
    
            $sudo_cmd rm -f /etc/yum.repos.d/oneapm-ci-agent.repo
    
            echo -e "33[34m
    * Uninstall oneapm-ci-agent
    33[0m"
            $sudo_cmd yum -y remove oneapm-ci-agent
    
            # Remove extra files
            $sudo_cmd rm -rf /opt/oneapm-ci-agent
            $sudo_cmd rm -rf /var/log/oneapm-ci-agent
            $sudo_cmd rm -rf /etc/oneapm-ci-agent
        fi
    
        $sudo_cmd yum -y --disablerepo='*' --enablerepo='cloudinsight-agent' install cloudinsight-agent
    elif [ $OS = "Debian" ]; then
        printf "33[34m
    * Installing APT package sources for Cloudinsight Agent
    33[0m
    "
        $sudo_cmd sh -c "echo 'deb http://apt.oneapm.com/ stable main' > /etc/apt/sources.list.d/cloudinsight-agent.list"
        $sudo_cmd apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 54B043BC
    
        printf "33[34m
    * Installing the Cloudinsight Agent package
    33[0m
    "
        ERROR_MESSAGE="ERROR
    Failed to update the sources after adding the Cloudinsight repository.
    This may be due to any of the configured APT sources failing -
    see the logs above to determine the cause.
    If the failing repository is Cloudinsight, please contact Cloudinsight support.
    *****
    "
    
        $sudo_cmd apt-get update -o Dir::Etc::sourcelist="sources.list.d/cloudinsight-agent.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
        ERROR_MESSAGE="ERROR
    Failed to install the Cloudinsight package, sometimes it may be
    due to another APT source failing. See the logs above to
    determine the cause.
    If the cause is unclear, please contact Cloudinsight support.
    *****
    "
    
        if [ -f /etc/apt/sources.list.d/oneapm-ci-agent.list ]; then
            $sudo_cmd rm -f /etc/apt/sources.list.d/oneapm-ci-agent.list
        fi
    
        CI_OLD_INSTALLED=$(dpkg -l | grep oneapm-ci-agent > /dev/null 2>&1 || echo "no")
        if [ "$CI_OLD_INSTALLED" != "no" ]; then
            backup_old_configuration
    
            echo -e "33[34m
    * Uninstall oneapm-ci-agent
    33[0m"
            $sudo_cmd apt-get purge -y oneapm-ci-agent
        fi
    
        $sudo_cmd apt-get install -y --force-yes cloudinsight-agent
        ERROR_MESSAGE=""
    else
        printf "33[31mYour OS or distribution are not supported by this install script.
    Please follow the instructions on the Agent setup page.33[0m
    "
        exit;
    fi
    
    # Set the configuration
    if [ -e /etc/cloudinsight-agent/cloudinsight-agent.conf ]; then
        printf "33[34m
    * Keeping old cloudinsight-agent.conf configuration file
    33[0m
    "
    else
        printf "33[34m
    * Adding your license key to the Agent configuration: /etc/cloudinsight-agent/cloudinsight-agent.conf
    33[0m
    "
        $sudo_cmd sh -c "sed 's/license_key:.*/license_key: $license_key/' /etc/cloudinsight-agent/cloudinsight-agent.conf.example > /etc/cloudinsight-agent/cloudinsight-agent.conf"
    fi
    
    restart_cmd="$sudo_cmd /etc/init.d/cloudinsight-agent restart"
    if command -v invoke-rc.d >/dev/null 2>&1; then
        restart_cmd="$sudo_cmd invoke-rc.d cloudinsight-agent restart"
    fi
    
    if $no_start; then
        printf "33[34m
    * CI_INSTALL_ONLY environment variable set: the newly installed version of the agent
    will not start by itself. You will have to do it manually using the following
    command:
    
        $restart_cmd
    
    33[0m
    "
        exit
    fi
    
    printf "33[34m* Starting the Agent...
    33[0m
    "
    eval $restart_cmd
    
    # Wait for metrics to be submitted by the forwarder
    printf "33[32m
    Your Agent has started up for the first time. We're currently verifying that
    data is being submitted.33[0m
    
    Waiting for metrics..."
    
    c=0
    while [ "$c" -lt "30" ]; do
        sleep 1
        echo -n "."
        c=$(($c+1))
    done
    
    # Reuse the same counter
    c=0
    
    # The command to check the status of the forwarder might fail at first, this is expected
    # so we remove the trap and we set +e
    set +e
    trap - ERR
    
    $cl_cmd http://127.0.0.1:10010/status?threshold=0 > /dev/null 2>&1
    success=$?
    while [ "$success" -gt "0" ]; do
        sleep 1
        echo -n "."
        $cl_cmd http://127.0.0.1:10010/status?threshold=0 > /dev/null 2>&1
        success=$?
        c=$(($c+1))
    
        if [ "$c" -gt "15" -o "$success" -eq "0" ]; then
            # After 15 tries, we give up, we restore the trap and set -e
            # Also restore the trap on success
            set -e
            trap on_error ERR
        fi
    done
    
    # Metrics are submitted, echo some instructions and exit
    printf "33[32m
    
    Your Agent is running and functioning properly. It will continue to run in the
    background and submit metrics to Cloudinsight.
    
    If you ever want to stop the Agent, run:
    
        sudo /etc/init.d/cloudinsight-agent stop
    
    And to run it again run:
    
        sudo /etc/init.d/cloudinsight-agent start
    
    33[0m"
  • 相关阅读:
    POJ 1724 ROADS【最短路/搜索/DP】
    UVA 12716 GCD XOR【异或】
    UVA 10375 Choose and divide【唯一分解定理】
    UVA 12169 Disgruntled Judge【扩展欧几里德】
    UVA 11582 Colossal Fibonacci Numbers!【数学】
    011.progit笔记---git变基rebase
    010.progit笔记---git多个远程分支
    009.progit笔记---git单个远程分支
    008.progit笔记---git分支
    007.progit笔记---git别名
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6001186.html
Copyright © 2011-2022 走看看