github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/build/packer/teamcity-agent.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -euxo pipefail
     4  
     5  write_teamcity_config() {
     6    sudo -u agent tee /home/agent/conf/buildAgent.properties <<EOF
     7  serverUrl=https://teamcity.cockroachdb.com
     8  name=
     9  workDir=../work
    10  tempDir=../temp
    11  systemDir=../system
    12  EOF
    13  }
    14  
    15  # Avoid saving any Bash history.
    16  HISTSIZE=0
    17  
    18  # Add third-party APT repositories.
    19  apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0EBFCD88
    20  cat > /etc/apt/sources.list.d/docker.list <<EOF
    21  deb https://download.docker.com/linux/ubuntu bionic stable
    22  EOF
    23  # Per https://github.com/golang/go/wiki/Ubuntu
    24  add-apt-repository ppa:longsleep/golang-backports
    25  # Git 2.7, which ships with Xenial, has a bug where submodule metadata sometimes
    26  # uses absolute paths instead of relative paths, which means the affected
    27  # submodules cannot be mounted in Docker containers. Use the latest version of
    28  # Git until we upgrade to a newer Ubuntu distribution.
    29  add-apt-repository ppa:git-core/ppa
    30  apt-get update --yes
    31  
    32  # Install the necessary dependencies. Keep this list small!
    33  apt-get install --yes \
    34    docker-ce \
    35    docker-compose \
    36    gnome-keyring \
    37    git \
    38    golang-go \
    39    openjdk-11-jre-headless \
    40    unzip
    41  # Installing gnome-keyring prevents the error described in
    42  # https://github.com/moby/moby/issues/34048
    43  
    44  # Give the user for the TeamCity agent Docker rights.
    45  usermod -a -G docker agent
    46  
    47  # Download the TeamCity agent code and install its configuration.
    48  # N.B.: This must be done as the agent user.
    49  su - agent <<'EOF'
    50  set -euxo pipefail
    51  
    52  echo 'export GOPATH="$HOME"/work/.go' >> .profile && source .profile
    53  
    54  wget https://teamcity.cockroachdb.com/update/buildAgent.zip
    55  unzip buildAgent.zip
    56  rm buildAgent.zip
    57  
    58  # Cache the current version of the main Cockroach repository on the agent to
    59  # speed up the first build. As of 2017-10-13, the main repository is 450MB (!).
    60  # The other repositories we run CI on are small enough not to be worth caching,
    61  # but feel free to add them if it becomes necessary.
    62  #
    63  # WARNING: This uses undocumented implementation details of TeamCity's Git
    64  # alternate system.
    65  git clone --bare https://github.com/cockroachdb/cockroach system/git/cockroach.git
    66  cat > system/git/map <<EOS
    67  https://github.com/cockroachdb/cockroach = cockroach.git
    68  EOS
    69  
    70  # For master and the last two release, download the builder and acceptance
    71  # containers.
    72  repo="$GOPATH"/src/github.com/cockroachdb/cockroach
    73  git clone --shared system/git/cockroach.git "$repo"
    74  cd "$repo"
    75  # Work around a bug in the builder's git version (at the time of writing)
    76  # which would corrupt the submodule defs. Probably good to remove once the
    77  # builder uses Ubuntu 18.04 or higher.
    78  git submodule update --init --recursive
    79  for branch in $(git branch --all --list --sort=-committerdate 'origin/release-*' | head -n1) master
    80  do
    81    git checkout "$branch"
    82    COCKROACH_BUILDER_CCACHE=1 build/builder.sh make test testrace TESTS=-
    83    # TODO(benesch): store the acceptanceversion somewhere more accessible.
    84    docker pull $(git grep cockroachdb/acceptance -- '*.go' | sed -E 's/.*"([^"]*).*"/\1/') || true
    85  done
    86  cd -
    87  EOF
    88  write_teamcity_config
    89  
    90  # Configure the Teamcity agent to start when the server starts.
    91  #
    92  # systemd will nuke the auto-upgrade process unless we mark the service as
    93  # "oneshot". This has the unfortunate side-effect of making `systemctl start
    94  # teamcity-agent` hang forever when run manually, but it at least works when the
    95  # system starts the service at bootup.
    96  #
    97  # TODO(benesch): see if we can fix this with Type=forking, KillMode=process.
    98  cat > /etc/systemd/system/teamcity-agent.service <<EOF
    99  [Unit]
   100  Description=TeamCity Build Agent
   101  After=network.target
   102  Requires=network.target
   103  
   104  [Service]
   105  Type=oneshot
   106  RemainAfterExit=yes
   107  User=agent
   108  PIDFile=/home/agent/logs/buildAgent.pid
   109  ExecStart=/home/agent/bin/agent.sh start
   110  ExecStop=/home/agent/bin/agent.sh stop
   111  SuccessExitStatus=0 143
   112  
   113  [Install]
   114  WantedBy=multi-user.target
   115  EOF
   116  systemctl enable teamcity-agent.service
   117  
   118  # Enable LRU pruning of Docker images.
   119  # https://github.com/stepchowfun/docuum#running-docuum-in-a-docker-container
   120  DOCUUM_VERSION=0.9.4
   121  cat > /etc/systemd/system/docuum.service <<EOF
   122  [Unit]
   123  Description=Remove Stale Docker Images
   124  After=docker.service
   125  Requires=docker.service
   126  
   127  [Service]
   128  ExecStart=/usr/bin/docker run \
   129            --init \
   130            --rm \
   131            --tty \
   132            --name docuum \
   133            --volume /var/run/docker.sock:/var/run/docker.sock \
   134            --volume docuum:/root stephanmisc/docuum:$DOCUUM_VERSION \
   135            --threshold '128 GB'
   136  Restart=always
   137  
   138  [Install]
   139  WantedBy=multi-user.target
   140  EOF
   141  systemctl enable docuum.service
   142  # Prefetch the image
   143  docker pull stephanmisc/docuum:$DOCUUM_VERSION
   144  
   145  # Boot the TeamCity agent so it can be upgraded by the server (i.e., download
   146  # and install whatever plugins the server has installed) before we bake the
   147  # image.
   148  #
   149  # WARNING: There seems to be no clean way to check when the upgrade is complete.
   150  # As a hack, the string below seems to appear in the logs iff the upgrade is
   151  # successful.
   152  systemctl start teamcity-agent.service
   153  until grep -q 'Updating agent parameters on the server' /home/agent/logs/teamcity-agent.log
   154  do
   155    echo .
   156    sleep 5
   157  done
   158  
   159  # Re-write the TeamCity config to discard the name and authorization token
   160  # assigned by the TeamCity server; otherwise, agents created from this image
   161  # might look like unauthorized duplicates to the TeamCity server.
   162  systemctl stop teamcity-agent.service
   163  write_teamcity_config
   164  
   165  # Prepare for imaging by removing unnecessary files.
   166  rm -rf /home/agent/logs
   167  apt-get clean
   168  sync