-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (104 loc) · 4.39 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Will take the active revision of the specified Cloud Run service and check if the GTM image sha
* is the same as the latest stable version of the GTM image.
* In case of mismatch a new revision will be created copying the variables and resources from the last active revision.
*
* @param req Post request keys required: project_id, region, service_name
* @returns The status of the currently deployed Cloud Run service
*/
const functions = require('@google-cloud/functions-framework');
const {RevisionsClient,ServicesClient} = require('@google-cloud/run').v2;
const fetch = require('node-fetch')
const runClient = new RevisionsClient();
const service_client = new ServicesClient();
functions.http('check_cloud_run', (req, res) => {
if(req.body.project_id && req.body.region && req.body.service_name){
let params = {
...req.body
}
console.log(params)
check_cloud_run(params);
}
else{
res.status(500).json({"success":"false","message":"Wrong / Missing request body"});
}
async function check_cloud_run(params) {
const parent = `projects/${params.project_id}/locations/${params.region}/services/${params.service_name}`
const request = {
parent,
};
let revisions_list = [];
let active_revision;
const iterable = await runClient.listRevisionsAsync(request);
for await (const response of iterable) {
revisions_list.push(response)
}
for (const revision of revisions_list) {
for (const condition of revision.conditions) {
if(condition.type == "Active" && condition.state == "CONDITION_SUCCEEDED") {
active_revision = revision;
}
}
}
let current_image_key = active_revision.containers[0].image.split(":")[1];
console.log(`Currently deployed GTM image version sha: ${current_image_key}`);
const url = 'https://gcr.io/v2/cloud-tagging-10302018/gtm-cloud-image/tags/list';
const response = await fetch(url, {
method: 'GET',
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
let image_manifest = await response.json();
image_manifest = image_manifest.manifest;
let stable_image_key = null;
let stable_image_tags = null;
for (const release in image_manifest) {
if (Array.isArray(image_manifest[release].tag) && image_manifest[release].tag.includes('stable')) {
stable_image_key = release.split(":")[1];
stable_image_tags = image_manifest[release].tag;
break;
}
}
console.log(`Stable GTM image sha: ${stable_image_key} and tags ${stable_image_tags}`);
if(stable_image_key != current_image_key) {
console.log(`Versions are different: Deploying a new revision to catch latest stable image: ${stable_image_key}`);
console.log(active_revision);
const service = {
"name": `projects/${params.project_id}/locations/${params.region}/services/${params.service_name}`,
"template":{
"containers":[
{
"image": "gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable",
"env": active_revision.containers[0].env,
"resources": active_revision.containers[0].resources,
"livenessProbe": active_revision.containers[0].livenessProbe,
"startupProbe": active_revision.containers[0].startupProbe,
}
],
scaling:{
"minInstanceCount":active_revision.scaling.minInstanceCount,
"maxInstanceCount":active_revision.scaling.maxInstanceCount
},
"serviceAccount": active_revision.serviceAccount
}
}
const revision_request = {
service,
};
const [operation] = await service_client.updateService(revision_request);
const [revision_response] = await operation.promise();
console.log(revision_response);
return res.status(200).json({
"status":"updated successfully",
"gtm-version": current_image_key,
"latest-image-version": stable_image_key
});
}
return res.status(200).json({
"status":"no update needed",
"gtm-version": current_image_key,
"latest-image-version": stable_image_key
});
}
});