|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2021 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +import json |
| 19 | +import os |
| 20 | + |
| 21 | +import requests |
| 22 | +import time |
| 23 | + |
| 24 | +BOSKOS_HOST = os.environ.get("BOSKOS_HOST", "boskos") |
| 25 | +BOSKOS_RESOURCE_NAME = os.environ.get('BOSKOS_RESOURCE_NAME') |
| 26 | + |
| 27 | + |
| 28 | +def checkout_account_request(resource_type, user, input_state): |
| 29 | + url = f'http://{BOSKOS_HOST}/acquire?type={resource_type}&state={input_state}&dest=busy&owner={user}' |
| 30 | + r = requests.post(url) |
| 31 | + status = r.status_code |
| 32 | + reason = r.reason |
| 33 | + result = "" |
| 34 | + |
| 35 | + if status == 200: |
| 36 | + content = r.content.decode() |
| 37 | + result = json.loads(content) |
| 38 | + |
| 39 | + return status, reason, result |
| 40 | + |
| 41 | + |
| 42 | +def checkout_account(resource_type, user): |
| 43 | + status, reason, result = checkout_account_request(resource_type, user, "clean") |
| 44 | + # TODO(sbueringer): find out if we still need this |
| 45 | + # replicated the implementation of cluster-api-provider-gcp |
| 46 | + # we're working around an issue with the data in boskos. |
| 47 | + # We'll remove the code that tries both free and clean once all the data is good. |
| 48 | + # Afterwards we should just check for free |
| 49 | + if status == 404: |
| 50 | + status, reason, result = checkout_account_request(resource_type, user, "free") |
| 51 | + |
| 52 | + if status != 200: |
| 53 | + raise Exception(f"Got invalid response {status}: {reason}") |
| 54 | + |
| 55 | + print(f"export BOSKOS_RESOURCE_NAME={result['name']}") |
| 56 | + print(f"export GCP_PROJECT={result['name']}") |
| 57 | + |
| 58 | + |
| 59 | +def release_account(user): |
| 60 | + url = f'http://{BOSKOS_HOST}/release?name={BOSKOS_RESOURCE_NAME}&dest=dirty&owner={user}' |
| 61 | + |
| 62 | + r = requests.post(url) |
| 63 | + |
| 64 | + if r.status_code != 200: |
| 65 | + raise Exception(f"Got invalid response {r.status_code}: {r.reason}") |
| 66 | + |
| 67 | + |
| 68 | +def send_heartbeat(user): |
| 69 | + url = f'http://{BOSKOS_HOST}/update?name={BOSKOS_RESOURCE_NAME}&state=busy&owner={user}' |
| 70 | + |
| 71 | + while True: |
| 72 | + print(f"POST-ing heartbeat for resource {BOSKOS_RESOURCE_NAME} to {BOSKOS_HOST}") |
| 73 | + r = requests.post(url) |
| 74 | + |
| 75 | + if r.status_code == 200: |
| 76 | + print(f"response status: {r.status_code}") |
| 77 | + else: |
| 78 | + print(f"Got invalid response {r.status_code}: {r.reason}") |
| 79 | + |
| 80 | + time.sleep(60) |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + parser = argparse.ArgumentParser(description='Boskos GCP Account Management') |
| 85 | + |
| 86 | + parser.add_argument( |
| 87 | + '--get', dest='checkout_account', action="store_true", |
| 88 | + help='Checkout a Boskos GCP Account' |
| 89 | + ) |
| 90 | + |
| 91 | + parser.add_argument( |
| 92 | + '--release', dest='release_account', action="store_true", |
| 93 | + help='Release a Boskos GCP Account' |
| 94 | + ) |
| 95 | + |
| 96 | + parser.add_argument( |
| 97 | + '--heartbeat', dest='send_heartbeat', action="store_true", |
| 98 | + help='Send heartbeat for the checked out a Boskos GCP Account' |
| 99 | + ) |
| 100 | + |
| 101 | + parser.add_argument( |
| 102 | + '--resource-type', dest="resource_type", type=str, |
| 103 | + default="gce-project", |
| 104 | + help="Type of Boskos resource to manage" |
| 105 | + ) |
| 106 | + |
| 107 | + parser.add_argument( |
| 108 | + '--user', dest="user", type=str, |
| 109 | + default="cluster-api", |
| 110 | + help="username" |
| 111 | + ) |
| 112 | + |
| 113 | + args = parser.parse_args() |
| 114 | + |
| 115 | + if args.checkout_account: |
| 116 | + checkout_account(args.resource_type, args.user) |
| 117 | + |
| 118 | + elif args.release_account: |
| 119 | + release_account(args.user) |
| 120 | + |
| 121 | + elif args.send_heartbeat: |
| 122 | + send_heartbeat(args.user) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments