-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (72 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var gcloud = require('gcloud');
module.exports = {
scaleUpDown: function(context, data) {
var instanceGroup = data['instance-group'];
var scaleTo = data['scale-to'];
if (!instanceGroup) {
context.failure(
'Instance group not provided. Make sure you have a \'instance-grouo\' property in ' +
'your request');
return;
}
if (!scaleTo) {
context.failure(
'Scale index not provided. Make sure you have a \'scale-to\' property in ' +
'your request');
return;
}
console.log('instance-group:' + instanceGroup);
console.log('scale-to:' + scaleTo);
// Create a gce client.
var gce = gcloud.compute({
// We're using the API from the same project as the Cloud Function.
projectId: process.env.GCP_PROJECT,
});
//get a zone object
var zone = gce.zone('us-east1-b');
//get all the autoscalers
zone.getAutoscalers(function(err, autoscalers) {
// autoscalers is an array of `Autoscaler` objects
if (!err){
for (var i=0; i < autoscalers.length; i++){
var target = autoscalers[i].metadata['target'];
console.log('value of target = ' + target);
if (target.indexOf(instanceGroup) > -1){
console.log('Found it!!!');
//set min instances to scaleTo value
var oldValue = autoscalers[i].metadata.autoscalingPolicy['minNumReplicas'];
console.log('old value = ' + oldValue);
autoscalers[i].metadata.autoscalingPolicy['minNumReplicas'] = scaleTo;
if (oldValue != scaleTo) {
console.log('new value = ' + autoscalers[i].metadata.autoscalingPolicy['minNumReplicas']);
autoscalers[i].setMetadata(autoscalers[i].metadata, function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
operation.on('complete', function(metadata) {
// The operation is complete.
console.log('Modified metadata set!');
context.success('Success! Instance group scaled to minimum of ' + scaleTo + '. [Original value was ' + oldValue + '.]')
});
operation.on('error', function(err) {
// An error occurred during the operation.
console.log('error: ' + err);
context.failure('An error occured. Scaling not set.');
});
});
}else{
console.log('ScaleTo matched existing minReplicas. No need to set.');
context.success('Success! ScaleTo value already set on instance group.');
};
};
//console.log('Autoscaler [' + i + ']');
//console.log('name = ' + autoscalers[i].name);
//console.log('target = ' + autoscalers[i].metadata['target']);
//console.log('metadata = ' + Object.keys(autoscalers[i].metadata));
//console.log('policy = ' + Object.keys(autoscalers[i].metadata.autoscalingPolicy));
}
}else{
context.failure('No autoscalers found in us-east1-b zone!');
}
});
},
};