Can I replicate `create-with-container` gcloud sdk command using the googleapiclient python client

Viewed 325

I currently have a docker container on the gcloud container repository which I want to launch an instance of in order to run a calculation, save a result and close:

gcloud compute instances create-with-container test-1 \
--machine-type=n1-standard-4 \
--boot-disk-size=20GB \
--container-image=eu.gcr.io/<container-link> \
--container-env=GCLOUD_INPUT_FILENAME=file.txt \
--container-env=GCLOUD_PROJECT=project-name

However, I want to be able to launch these instances using a web-interface (flask) which implies I want to use the googleapiclient (python) in order to create and manage these instances:

It looks like while you can create a instance creation order using the discovery api:

compute = googleclientapi.discovery.build('compute', 'v1')
compute.instances().insert(...).execute()

but it doesn't look like it is possible to emulate create-with-container gcloud sdk command, although you can pass 'machineImage' as part of the creation request.

Can one create a compute instance 'with-container' without using subprocess to call the gcloud sdk

OR

Can I convert my create-with-container instance into a machine image and then use the googleapi client?

1 Answers

Just for FYI, what I found from the log-http logging(I know it is not the right answer)

==== request start ====
uri: https://compute.googleapis.com/batch/compute/v1
method: POST
== headers start ==
b'authorization': --- Token Redacted ---
b'content-length': b'479'
b'content-type': b'multipart/mixed; boundary="===============34234234234234=="'
b'user-agent': b'google-cloud-sdk gcloud/329.0.0 command/gcloud.compute.instances.create-with-container invocation-id/0dd6a37ac0624ac4b00e30a44da environment/devshell environment-version/None interactive/False from-script/False python/3.7.3 term/screen (Linux 5.4.89+)'
== headers end ==

"create-with-container" is passed through the header, I did not find the body for the same.

Related