diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2018-10-11 14:40:05 +0200 |
---|---|---|
committer | Simon Sapin <simon.sapin@exyr.org> | 2018-10-29 22:23:00 +0100 |
commit | ba8b37f3bcec6fcbffbc448f4e83ad85462f161c (patch) | |
tree | b362f3b50a961a9f8fc66f0053830e6d6e199d61 /etc/taskcluster/packet.net | |
parent | 2f8dc655191455c285d4b4b5952d493fd4dc0782 (diff) | |
download | servo-ba8b37f3bcec6fcbffbc448f4e83ad85462f161c.tar.gz servo-ba8b37f3bcec6fcbffbc448f4e83ad85462f161c.zip |
List packet.net servers through their API
Diffstat (limited to 'etc/taskcluster/packet.net')
-rwxr-xr-x | etc/taskcluster/packet.net/list_devices.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/etc/taskcluster/packet.net/list_devices.py b/etc/taskcluster/packet.net/list_devices.py new file mode 100755 index 00000000000..533db67d63e --- /dev/null +++ b/etc/taskcluster/packet.net/list_devices.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import json +import os +import sys +import urllib.request + + +SERVO_PROJECT_ID = "e3d0d8be-9e4c-4d39-90af-38660eb70544" + + +def main(): + auth_token = os.environ.get("PACKET_AUTH_TOKEN") + if not auth_token: + sys.exit("$PACKET_AUTH_TOKEN is not set. See:\n" + "https://app.packet.net/projects/%s/settings/api-keys" % SERVO_PROJECT_ID) + response = api_request(auth_token, "/projects/%s/devices?per_page=1000" % SERVO_PROJECT_ID) + for device in response["devices"]: + print(device["id"]) + print(" Host:\t" + device["hostname"]) + print(" Plan:\t" + device["plan"]["name"]) + print(" OS:\t" + device["operating_system"]["name"]) + for address in device["ip_addresses"]: + if address["public"]: + print(" IPv%s:\t%s" % (address["address_family"], address["address"])) + assert response["meta"]["next"] is None + + +def api_request(auth_token, path, json_data=None, method=None): + request = urllib.request.Request("https://api.packet.net" + path, method=method) + request.add_header("X-Auth-Token", auth_token) + if json_data is not None: + request.add_header("Content-Type", "application/json") + request.data = json.dumps(json_data) + with urllib.request.urlopen(request) as response: + return json.load(response) + + +if __name__ == "__main__": + main(*sys.argv[1:]) |