generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: make one script to rule aws/gcp
- Loading branch information
Showing
5 changed files
with
147 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2019 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Checks out a Image-Builder AWS account""" | ||
|
||
import argparse | ||
import json | ||
import os | ||
import time | ||
|
||
import requests | ||
|
||
BOSKOS_HOST = os.environ.get("BOSKOS_HOST", "boskos") | ||
BOSKOS_RESOURCE_NAME = os.environ.get('BOSKOS_RESOURCE_NAME') | ||
|
||
|
||
def checkout_account(resource_type, user, cloud, input_state="clean"): | ||
url = f'http://{BOSKOS_HOST}/acquire' | ||
data = { | ||
'type': resource_type, | ||
'owner': user, | ||
'state': input_state, | ||
'dest': 'busy', | ||
} | ||
|
||
r = requests.post(url, data) | ||
|
||
if r.status_code == 404: | ||
data['state'] = "free" | ||
r = requests.post(url, data) | ||
|
||
if r.status_code == 200: | ||
content = r.content.decode() | ||
result = json.loads(content) | ||
|
||
print(f"export BOSKOS_RESOURCE_NAME={result['name']}") | ||
if cloud.lower() == "aws": | ||
print(f"export AWS_ACCESS_KEY_ID={result['userdata']['access-key-id']}") | ||
print(f"export AWS_SECRET_ACCESS_KEY={result['userdata']['secret-access-key']}") | ||
elif cloud.lower() == "gcp": | ||
print(f"export GCP_PROJECT={result['name']}") | ||
|
||
|
||
else: | ||
raise Exception(f"Got invalid response {r.status_code}: {r.reason}") | ||
|
||
|
||
def release_account(user): | ||
url = f'http://{BOSKOS_HOST}/release' | ||
data = { | ||
'name': BOSKOS_RESOURCE_NAME, | ||
'owner': user, | ||
'dest': 'dirty', | ||
} | ||
|
||
r = requests.post(url, data) | ||
if r.status_code != 200: | ||
raise Exception(f"Got invalid response {r.status_code}: {r.reason}") | ||
|
||
|
||
def send_heartbeat(user): | ||
url = f'http://{BOSKOS_HOST}/update' | ||
data = { | ||
'name': BOSKOS_RESOURCE_NAME, | ||
'owner': user, | ||
'state': 'busy', | ||
} | ||
|
||
while True: | ||
print(f"POST-ing heartbeat for resource {BOSKOS_RESOURCE_NAME} to {BOSKOS_HOST}") | ||
r = requests.post(url, data) | ||
|
||
if r.status_code == 200: | ||
print(f"response status: {r.status_code}") | ||
else: | ||
print(f"Got invalid response {r.status_code}: {r.reason}") | ||
|
||
time.sleep(60) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Boskos AWS Account Management') | ||
|
||
parser.add_argument( | ||
'--get', dest='checkout_account', action="store_true", | ||
help='Checkout a Boskos AWS/GCP Account' | ||
) | ||
|
||
parser.add_argument( | ||
'--release', dest='release_account', action="store_true", | ||
help='Release a Boskos AWS/GCP Account' | ||
) | ||
|
||
parser.add_argument( | ||
'--heartbeat', dest='send_heartbeat', action="store_true", | ||
help='Send heartbeat for the checked out a Boskos AWS Account' | ||
) | ||
|
||
parser.add_argument( | ||
'--resource-type', dest="resource_type", type=str, | ||
required=True, | ||
help="Type of Boskos resource to manage" | ||
) | ||
|
||
parser.add_argument( | ||
'--user', dest="user", type=str, | ||
default="sigs.k8s.io/image-builder", | ||
help="username" | ||
) | ||
|
||
parser.add_argument( | ||
'--cloud', dest="cloud", type=str, | ||
required=True, | ||
help="Which Cloud to use. AWS/GCP" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.checkout_account: | ||
checkout_account(args.resource_type, args.user, args.cloud) | ||
|
||
elif args.release_account: | ||
release_account(args.user) | ||
|
||
elif args.send_heartbeat: | ||
send_heartbeat(args.user) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters