Last active
August 13, 2019 23:38
-
-
Save mesmacosta/063aea59d3e9ada76198e234d7cfb26b to your computer and use it in GitHub Desktop.
Files used in the medium series
This file contains hidden or 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
| { | |
| "kind": "compute#instance", | |
| "name": "instance-1", | |
| "zone": "projects/your-project/zones/us-central1-a", | |
| "machineType": "projects/your-project/zones/us-central1-a/machineTypes/f1-micro", | |
| "displayDevice": { | |
| "enableDisplay": false | |
| }, | |
| "metadata": { | |
| "kind": "compute#metadata", | |
| "items": [ | |
| { | |
| "key": "startup-script", | |
| "value": "gcloud logging write batch-execution \"Hello world from $(hostname).\"\ngcp_zone=$(curl -H Metadata-Flavor:Google http://metadata.google.internal/computeMetadata/v1/instance/zone -s | cut -d/ -f4)\ngcloud compute instances delete $(hostname) --zone ${gcp_zone}" | |
| } | |
| ] | |
| }, | |
| "tags": { | |
| "items": [] | |
| }, | |
| "disks": [ | |
| { | |
| "kind": "compute#attachedDisk", | |
| "type": "PERSISTENT", | |
| "boot": true, | |
| "mode": "READ_WRITE", | |
| "autoDelete": true, | |
| "deviceName": "instance-1", | |
| "initializeParams": { | |
| "sourceImage": "projects/debian-cloud/global/images/debian-9-stretch-v20190729", | |
| "diskType": "projects/your-project/zones/us-central1-a/diskTypes/pd-standard", | |
| "diskSizeGb": "10" | |
| }, | |
| "diskEncryptionKey": {} | |
| } | |
| ], | |
| "canIpForward": false, | |
| "networkInterfaces": [ | |
| { | |
| "kind": "compute#networkInterface", | |
| "subnetwork": "projects/your-project/regions/us-central1/subnetworks/default", | |
| "accessConfigs": [ | |
| { | |
| "kind": "compute#accessConfig", | |
| "name": "External NAT", | |
| "type": "ONE_TO_ONE_NAT", | |
| "networkTier": "PREMIUM" | |
| } | |
| ], | |
| "aliasIpRanges": [] | |
| } | |
| ], | |
| "description": "", | |
| "labels": {}, | |
| "scheduling": { | |
| "preemptible": false, | |
| "onHostMaintenance": "MIGRATE", | |
| "automaticRestart": true, | |
| "nodeAffinities": [] | |
| }, | |
| "deletionProtection": false, | |
| "reservationAffinity": { | |
| "consumeReservationType": "ANY_RESERVATION" | |
| }, | |
| "serviceAccounts": [ | |
| { | |
| "email": "compute-execute-batch-job@your-project.iam.gserviceaccount.com", | |
| "scopes": [ | |
| "https://www.googleapis.com/auth/cloud-platform" | |
| ] | |
| } | |
| ] | |
| } |
This file contains hidden or 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
| const Buffer = require('safe-buffer').Buffer; | |
| const Compute = require('@google-cloud/compute'); | |
| const compute = new Compute(); | |
| const zone = 'us-central1-a'; | |
| // We are going to add our vm configuration in the next step | |
| // const vmConfig = ... | |
| exports.createInstance = (event, context) => { | |
| const vmName = 'batch-job-executor' + Date.now(); | |
| try { | |
| compute.zone(zone) | |
| .createVM(vmName, vmConfig) | |
| .then(data => { | |
| // Operation pending. | |
| const vm = data[0]; | |
| const operation = data[1]; | |
| console.log(`VM being created: ${vm.id}`); | |
| console.log(`Operation info: ${operation.id}`); | |
| return operation.promise(); | |
| }) | |
| .then(() => { | |
| const message = 'VM created with success, Cloud Function finished execution.'; | |
| console.log(message); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| }; |
This file contains hidden or 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
| const Buffer = require('safe-buffer').Buffer; | |
| const Compute = require('@google-cloud/compute'); | |
| const compute = new Compute(); | |
| const zone = 'us-central1-a'; | |
| const vmConfig = { | |
| kind: 'compute#instance', | |
| zone: 'projects/your-project/zones/us-central1-a', | |
| machineType: 'projects/your-project/zones/us-central1-a/machineTypes/f1-micro', | |
| displayDevice: { | |
| enableDisplay: false | |
| }, | |
| metadata: { | |
| kind: 'compute#metadata', | |
| items: [ | |
| { | |
| key: 'startup-script', | |
| value: 'gcloud logging write batch-execution \'Hello world from $(hostname).\'\ngcp_zone=$(curl -H Metadata-Flavor:Google http://metadata.google.internal/computeMetadata/v1/instance/zone -s | cut -d/ -f4)\ngcloud compute instances delete $(hostname) --zone ${gcp_zone}' | |
| } | |
| ] | |
| }, | |
| tags: { | |
| items: [] | |
| }, | |
| disks: [ | |
| { | |
| kind: 'compute#attachedDisk', | |
| type: 'PERSISTENT', | |
| boot: true, | |
| mode: 'READ_WRITE', | |
| autoDelete: true, | |
| deviceName: 'instance-1', | |
| initializeParams: { | |
| sourceImage: 'projects/debian-cloud/global/images/debian-9-stretch-v20190729', | |
| diskType: 'projects/your-project/zones/us-central1-a/diskTypes/pd-standard', | |
| diskSizeGb: '10' | |
| }, | |
| diskEncryptionKey: {} | |
| } | |
| ], | |
| canIpForward: false, | |
| networkInterfaces: [ | |
| { | |
| kind: 'compute#networkInterface', | |
| subnetwork: 'projects/your-project/regions/us-central1/subnetworks/default', | |
| accessConfigs: [ | |
| { | |
| kind: 'compute#accessConfig', | |
| name: 'External NAT', | |
| type: 'ONE_TO_ONE_NAT', | |
| networkTier: 'PREMIUM' | |
| } | |
| ], | |
| aliasIpRanges: [] | |
| } | |
| ], | |
| description: '', | |
| labels: {}, | |
| scheduling: { | |
| preemptible: false, | |
| onHostMaintenance: 'MIGRATE', | |
| automaticRestart: true, | |
| nodeAffinities: [] | |
| }, | |
| deletionProtection: false, | |
| reservationAffinity: { | |
| consumeReservationType: 'ANY_RESERVATION' | |
| }, | |
| serviceAccounts: [ | |
| { | |
| email: 'compute-execute-batch-job@your-project.iam.gserviceaccount.com', | |
| scopes: [ | |
| 'https://www.googleapis.com/auth/cloud-platform' | |
| ] | |
| } | |
| ] | |
| } | |
| exports.createInstance = (event, context) => { | |
| const vmName = 'batch-job-executor' + Date.now(); | |
| try { | |
| compute.zone(zone) | |
| .createVM(vmName, vmConfig) | |
| .then(data => { | |
| // Operation pending. | |
| const vm = data[0]; | |
| const operation = data[1]; | |
| console.log(`VM being created: ${vm.id}`); | |
| console.log(`Operation info: ${operation.id}`); | |
| return operation.promise(); | |
| }) | |
| .then(() => { | |
| const message = 'VM created with success, Cloud Function finished execution.'; | |
| console.log(message); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| }; |
This file contains hidden or 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
| { | |
| "name": "cloud-functions-create-batch-workload-vm", | |
| "version": "0.0.1", | |
| "private": true, | |
| "license": "Apache-2.0", | |
| "author": "Marcelo Costa", | |
| "main": "index.js", | |
| "engines": { | |
| "node": ">=8.13.0" | |
| }, | |
| "scripts": { | |
| "test": "mocha test/*.test.js --timeout=20000" | |
| }, | |
| "devDependencies": { | |
| "@google-cloud/nodejs-repo-tools": "^3.3.0", | |
| "mocha": "^6.0.0", | |
| "proxyquire": "^2.0.0", | |
| "sinon": "^7.0.0" | |
| }, | |
| "dependencies": { | |
| "@google-cloud/compute": "1.0.1", | |
| "safe-buffer": "^5.1.2" | |
| } | |
| } |
This file contains hidden or 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
| gcloud logging write batch-execution "Hello world from $(hostname)." | |
| gcp_zone=$(curl -H Metadata-Flavor:Google http://metadata.google.internal/computeMetadata/v1/instance/zone -s | cut -d/ -f4) | |
| gcloud compute instances delete $(hostname) --zone ${gcp_zone} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment