def deploy_minikube(args): """Create a VM and setup minikube.""" credentials = GoogleCredentials.get_application_default() gce = discovery.build("compute", "v1", credentials=credentials) instances = gce.instances() body = { "name": args.vm_name, "machineType": "zones/{0}/machineTypes/n1-standard-16".format(args.zone), "disks": [ { "boot": True, "initializeParams": { "sourceImage": "projects/ubuntu-os-cloud/global/images/family/ubuntu-1604-lts", "diskSizeGb": 100, "autoDelete": True, }, }, ], "networkInterfaces": [ { "accessConfigs": [ { "name": "external-nat", "type": "ONE_TO_ONE_NAT", }, ], "network": "global/networks/default", }, ], } request = instances.insert(project=args.project, zone=args.zone, body=body) response = None try: response = request.execute() print("done") except errors.HttpError as e: if not e.content: raise content = json.loads(e.content) if content.get("error", {}).get("code") == requests.codes.CONFLICT: # We don't want to keep going so we reraise the error after logging # a helpful error message. logging.error( "Either the VM or the disk %s already exists in zone " "%s in project %s ", args.vm_name, args.zone, args.project) raise else: raise op_id = response.get("name") final_op = vm_util.wait_for_operation(gce, args.project, args.zone, op_id) logging.info("Final result for insert operation: %s", final_op) if final_op.get("status") != "DONE": raise ValueError("Insert operation has status %s", final_op.get("status")) if final_op.get("error"): message = "Insert operation resulted in error %s".format( final_op.get("error")) logging.error(message) raise ValueError(message) # Locate the install minikube script. install_script = os.path.join(os.path.dirname(__file__), "install_minikube.sh") if not os.path.exists(install_script): logging.error("Could not find minikube install script: %s", install_script) vm_util.wait_for_vm(args.project, args.zone, args.vm_name) vm_util.execute_script(args.project, args.zone, args.vm_name, install_script) # Copy the .kube and .minikube files to test_dir target = "~/.kube" full_target = "{0}:{1}".format(args.vm_name, target) logging.info("Copying %s to %s", target, args.test_dir) util.run([ "gcloud", "compute", "--project=" + args.project, "scp", "--recurse", full_target, args.test_dir, "--zone=" + args.zone ]) # The .minikube directory contains some really large ISO and other files that we don't need; so we # only copy the files we need. minikube_dir = os.path.join(args.test_dir, ".minikube") if not os.path.exists(minikube_dir): os.makedirs(minikube_dir) for target in ["~/.minikube/*.crt", "~/.minikube/client.key"]: full_target = "{0}:{1}".format(args.vm_name, target) logging.info("Copying %s to %s", target, minikube_dir) util.run([ "gcloud", "compute", "--project=" + args.project, "scp", "--recurse", full_target, minikube_dir, "--zone=" + args.zone ]) config_path = os.path.join(args.test_dir, ".kube", "config") modify_minikube_config(config_path, minikube_dir)
def deploy_minikube(args): """Create a VM and setup minikube.""" credentials = GoogleCredentials.get_application_default() gce = discovery.build("compute", "v1", credentials=credentials) instances = gce.instances() body = { "name": args.vm_name, "machineType": "zones/{0}/machineTypes/n1-standard-16".format(args.zone), "disks": [ { "boot": True, "initializeParams": { "sourceImage": "projects/ubuntu-os-cloud/global/images/family/ubuntu-1604-lts", "diskSizeGb": 100, "autoDelete": True, }, }, ], "networkInterfaces": [ { "accessConfigs": [ { "name": "external-nat", "type": "ONE_TO_ONE_NAT", }, ], "network": "global/networks/default", }, ], } request = instances.insert(project=args.project, zone=args.zone, body=body) try: request.execute() except errors.HttpError as e: if not e.content: raise content = json.loads(e.content) # TODO(jlewi): We can get this error if the disk exists but not the VM. If the disk exists but not the VM # and we keep going we will have a problem. However, that should be extremely unlikely now that # we set auto-delete on the disk to true. if content.get("error", {}).get("code") == requests.codes.CONFLICT: logging.warn("VM %s already exists in zone %s in project %s ", args.vm_name, args.zone, args.project) else: raise # Locate the install minikube script. install_script = os.path.join(os.path.dirname(__file__), "install_minikube.sh") if not os.path.exists(install_script): logging.error("Could not find minikube install script: %s", install_script) vm_util.wait_for_vm(args.project, args.zone, args.vm_name) vm_util.execute_script(args.project, args.zone, args.vm_name, install_script) vm_util.execute(args.project, args.zone, args.vm_name, ["sudo minikube start --vm-driver=none --disk-size=40g"]) # Copy the .kube and .minikube files to test_dir # The .minikube directory contains some really large ISO and other files that we don't need; so we # only copy the files we need. minikube_dir = os.path.join(args.test_dir, ".minikube") if not os.path.exists(minikube_dir): os.makedirs(minikube_dir) for target, local_dir in [("~/.minikube/*.crt", minikube_dir), ("~/.minikube/client.key", minikube_dir), ("~/.kube", args.test_dir)]: full_target = "{0}:{1}".format(args.vm_name, target) logging.info("Copying %s to %s", target, local_dir) util.run([ "gcloud", "compute", "--project=" + args.project, "scp", "--recurse", full_target, local_dir, "--zone=" + args.zone ]) config_path = os.path.join(args.test_dir, ".kube", "config") modify_minikube_config(config_path, minikube_dir)