sigs.k8s.io/cluster-api-provider-aws@v1.5.5/hack/boskos.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2019 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(resource_type, user, input_state="free"):
    29      url = f'http://{BOSKOS_HOST}/acquire?type={resource_type}&state={input_state}&dest=busy&owner={user}'
    30  
    31      r = requests.post(url)
    32  
    33      if r.status_code == 200:
    34          content = r.content.decode()
    35          result = json.loads(content)
    36  
    37          print(f"export BOSKOS_RESOURCE_NAME={result['name']}")
    38          print(f"export AWS_ACCESS_KEY_ID={result['userdata']['access-key-id']}")
    39          print(f"export AWS_SECRET_ACCESS_KEY={result['userdata']['secret-access-key']}")
    40  
    41      else:
    42          raise Exception(f"Got invalid response {r.status_code}: {r.reason}")
    43  
    44  
    45  def release_account(user):
    46      url = f'http://{BOSKOS_HOST}/release?name={BOSKOS_RESOURCE_NAME}&dest=dirty&owner={user}'
    47  
    48      r = requests.post(url)
    49      
    50      if r.status_code != 200:
    51          raise Exception(f"Got invalid response {r.status_code}: {r.reason}")
    52  
    53  
    54  def send_heartbeat(user):
    55      url = f'http://{BOSKOS_HOST}/update?name={BOSKOS_RESOURCE_NAME}&state=busy&owner={user}'
    56  
    57      while True:
    58          print(f"POST-ing heartbeat for resource {BOSKOS_RESOURCE_NAME} to {BOSKOS_HOST}")
    59          r = requests.post(url)
    60  
    61          if r.status_code == 200:
    62              print(f"response status: {r.status_code}")
    63          else:
    64              print(f"Got invalid response {r.status_code}: {r.reason}")
    65  
    66          time.sleep(60)
    67  
    68  
    69  def main():
    70      parser = argparse.ArgumentParser(description='Boskos AWS Account Management')
    71  
    72      parser.add_argument(
    73          '--get', dest='checkout_account', action="store_true",
    74          help='Checkout a Boskos AWS Account'
    75      )
    76  
    77      parser.add_argument(
    78          '--release', dest='release_account', action="store_true",
    79          help='Release a Boskos AWS Account'
    80      )
    81  
    82      parser.add_argument(
    83          '--heartbeat', dest='send_heartbeat', action="store_true",
    84          help='Send heartbeat for the checked out a Boskos AWS Account'
    85      )
    86  
    87      parser.add_argument(
    88          '--resource-type', dest="resource_type", type=str,
    89          default="aws-account",
    90          help="Type of Boskos resource to manage"
    91      )
    92  
    93      parser.add_argument(
    94          '--user', dest="user", type=str,
    95          default="cluster-api-provider-aws",
    96          help="username"
    97      )
    98  
    99      args = parser.parse_args()
   100  
   101      if args.checkout_account:
   102          checkout_account(args.resource_type, args.user)
   103  
   104      elif args.release_account:
   105          release_account(args.user)
   106  
   107      elif args.send_heartbeat:
   108          send_heartbeat(args.user)
   109  
   110  
   111  if __name__ == "__main__":
   112      main()