blob: f4305c37cc7e5df4622e9f1ac503cddce79ab2f8 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Get and set the status of the Fuchsia tree.
Fetch tree status (prints results to stdout):
./tree_status.py fuchsia.stem-status.appspot.com get
> {"general_state": "closed", "message": "tree is closed :(", ...}
Update tree status:
./tree_status.py fuchsia-stem-status.appspot.com set 'tree is closed'
--username someuser --password pas$$w0rd
> OK
"""
import argparse
import contextlib
import httplib
import urllib
def main():
parser = argparse.ArgumentParser(description="Get and set tree status.")
parser.add_argument("hostname", help="Hostname for the status page")
subparsers = parser.add_subparsers(help="Subcommands")
get_parser = subparsers.add_parser("get", help="Get the tree status")
get_parser.set_defaults(func=get_status)
set_parser = subparsers.add_parser("set", help="Set the tree status")
set_parser.set_defaults(func=set_status)
set_parser.add_argument("message", help="Message to set as tree status")
set_parser.add_argument(
"--username", help="Username to use for setting the status", required=True
)
set_parser.add_argument(
"--password", help="Password for accessing tree status page", required=True
)
args = parser.parse_args()
return args.func(args)
def get_status(args):
print make_request(args.hostname, "/current?format=json")
def set_status(args):
params = {
"message": args.message,
"username": args.username,
"password": args.password,
}
print make_request(args.hostname, "/status", "POST", params)
def make_request(url, path, method="GET", params=None):
# Pylint can't tell that `closing()` returns the same thing as the function
# that it wraps.
# pylint: disable=no-member
with contextlib.closing(httplib.HTTPSConnection(url)) as conn:
conn.request(method, path, urllib.urlencode(params) if params else "")
response = conn.getresponse()
if response.status >= 300:
raise Exception(
"bad response status %d: %s" % (response.status, response.reason)
)
return response.read()
# pylint: enable=no-member
if __name__ == "__main__":
main()