#!/usr/bin/env python3
#
# Requirements:
# - Install dependencies: `pip3 install pybuildkite requests`
# - Get a Buildkite API token with read_agents and write_agents permissions
#   and put it into the BUILDKITE_TOKEN environment variable.
# - Optional: Modify "FILTER" below to restart different groups of agents.
#   For example, you can put "windows" there to restart Windows agents.

import os
from pybuildkite.buildkite import Buildkite
from requests.exceptions import HTTPError

FILTER = "docker"
ORGANIZATION = "bazel"

print("Using Buildkite API Token: {}".format(os.environ["BUILDKITE_TOKEN"]))

buildkite = Buildkite()
buildkite.set_access_token(os.environ["BUILDKITE_TOKEN"])

response = buildkite.agents().list_all(
    organization=ORGANIZATION, page=1, with_pagination=True
)
agents = response.body

# Keep looping until next_page is not populated
while response.next_page:
    response = buildkite.agents().list_all(
        organization=ORGANIZATION, page=response.next_page, with_pagination=True
    )
    agents += response.body

for agent in agents:
    if FILTER in agent["name"]:
        print(
            "Stopping agent {} (https://buildkite.com/organizations/{}/agents/{}): ".format(
                agent["name"], ORGANIZATION, agent["id"]
            ),
            end="",
        )
        try:
            buildkite.agents().stop_agent(
                organization=ORGANIZATION, agent_id=agent["id"], force=False
            )
        except HTTPError:
            print("Error")
        else:
            print("OK")

@@ -392,16 +392,27 @@
 port os%0A
+import sys%0A
 from pyb
@@ -503,16 +503,19 @@
 R = 
-%22docker%22
+sys.argv%5B1%5D
 %0AORG
@@ -594,32 +594,36 @@
 iron%5B%22BUILDKITE_
+API_
 TOKEN%22%5D))%0A%0Abuild
@@ -686,24 +686,28 @@
 %5B%22BUILDKITE_
+API_
 TOKEN%22%5D)%0A%0Are

Improve restart-agents.py