Last active
September 15, 2020 07:04
-
-
Save abhaybhargav/4dc29aa6d7418a5b792d28c2518f4536 to your computer and use it in GitHub Desktop.
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
import googleapiclient.discovery | |
import sys | |
def main(name, url): | |
compute = googleapiclient.discovery.build('compute', 'beta') | |
result = compute.images().get(project="boreal-rain-289205", image = "bti-base").execute() | |
config = { | |
"name": name, | |
"zone": "projects/boreal-rain-289205/zones/us-central1-f", | |
"machineType": "projects/boreal-rain-289205/zones/us-central1-f/machineTypes/e2-small", | |
"disks": [ | |
{ | |
"kind": "compute#attachedDisk", | |
"type": "PERSISTENT", | |
"boot": True, | |
"mode": "READ_WRITE", | |
"autoDelete": True, | |
"deviceName": "bti-buster", | |
"initializeParams": { | |
"sourceImage": "projects/boreal-rain-289205/global/images/bti-buster", | |
"diskType": "projects/boreal-rain-289205/zones/us-central1-f/diskTypes/pd-ssd", | |
"diskSizeGb": "10" | |
}, | |
"diskEncryptionKey": {} | |
} | |
], | |
"networkInterfaces": [ | |
{ | |
"kind": "compute#networkInterface", | |
"subnetwork": "projects/boreal-rain-289205/regions/us-central1/subnetworks/image-vpc", | |
"accessConfigs": [ | |
{ | |
"kind": "compute#accessConfig", | |
"name": "External NAT", | |
"type": "ONE_TO_ONE_NAT", | |
"networkTier": "STANDARD" | |
} | |
], | |
"aliasIpRanges": [] | |
} | |
], | |
"metadata": { | |
"kind": "compute#metadata", | |
"items": [ | |
{ | |
"key": "startup-script", | |
"value": "mkdir -p /root/{}/ && git clone {} /root/{}/\n/bin/chmod +x /root/{}/setup.sh\n/bin/bash +x /root/{}/setup.sh\nrm /root/{}/.gitlab-ci.yml\nrm /root/{}/.gitignore\nrm /root/{}/README.md\nrm -rf /root/{}/.git\nrm /root/set.sh\nrm /root/setup.sh".format(name, url, name, name, name, name, name, name, name) | |
} | |
] | |
}, | |
"tags": { | |
"items": [ | |
"http-server", | |
"https-server" | |
] | |
} | |
} | |
final = compute.instances().insert(project="boreal-rain-289205", zone = "us-central1-f", body = config).execute() | |
get_instance = compute.instances().get(project="boreal-rain-289205", zone = "us-central1-f", instance = name).execute() | |
if 'id' in get_instance: | |
print("[+] Compute has been provisioned") | |
else: | |
print("[-] Unable to provision server") | |
if __name__ == "__main__": | |
args = sys.argv | |
if len(args) < 3: | |
print("Arguments not declared correctly") | |
sys.exit(1) | |
else: | |
url = sys.argv[1] | |
name = sys.argv[2] | |
main(name, url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment