This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (103 loc) · 4.11 KB
/
index.js
File metadata and controls
116 lines (103 loc) · 4.11 KB
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
'use strict'
var { ClientSecretCredential } = require("@azure/identity");
var { ContainerInstanceManagementClient } = require("@azure/arm-containerinstance");
var clientId = process.env.client_id,
secret = process.env.client_secret,
domain = process.env.tenant,
subscriptionId = process.env.subscription_id,
db_connection = process.env.CUSTOMCONNSTR_CosmosDB,
resourceGroupName = process.env.resourceGroup;
var db_user = db_connection.slice(10, db_connection.indexOf(':',10));
var db_pwd = db_connection.slice(db_connection.indexOf(':',10) + 1, db_connection.indexOf('@'));
var db_uri = 'mongodb://' + db_connection.slice(db_connection.indexOf('@')+1) + '&ssl=true';
const db_name = "containerstate";
const IMAGE = "hubertsui/go-worker:latest";
const MongoClient = require('mongodb').MongoClient;
module.exports = function(context, mySbMsg) {
if ( !mySbMsg || mySbMsg.length == 0){
context.done('JavaScript ServiceBus queue message is empty');
return;
}
mySbMsg = mySbMsg.replace(/: u'/g,": '").replace(/'/g,"\"");
context.log.info('service bus message', mySbMsg);
var sbMsgObj = JSON.parse(mySbMsg);
if ( !sbMsgObj || !sbMsgObj.hasOwnProperty('name')){
let errMsg = 'JavaScript ServiceBus queue message has no container name.'
context.log.error(errMsg);
context.done(errMsg);
return;
}
context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);
try{
context.log.info("got credentials");
const credentials = new ClientSecretCredential(domain,clientId,secret);
let client = new ContainerInstanceManagementClient(credentials,subscriptionId);
let containerName = sbMsgObj.name;
let containerMsg = sbMsgObj.input;
let containerGroup = {
containers: [
{
name: containerName,
environmentVariables: [
{
name: "MESSAGE",
value: containerMsg
},
{
name: "CONTAINER_NAME",
value: containerName
},
{
name: "DATABASE_URI",
value: db_connection
}],
image: IMAGE,
ports: [
{
"port": 80
}
],
resources: {
requests: {
cpu: 1,
memoryInGB: 1.5
}
},
}
],
restartPolicy: "Never",
ipAddress: {
ports: [{ port: 80 }]
},
osType: "Linux",
location: "East US"
};
context.log.info("creating container");
client.containerGroups.beginCreateOrUpdateAndWait(resourceGroupName, containerName, containerGroup)
.then( (cgroup) => {
context.log(cgroup);
context.done();
}).catch((err) => {
context.log('created container error', err);
MongoClient.connect(db_uri, { auth:{ user: db_user , password: db_pwd }}, function(dbError, client) {
if (dbError){
context.log(dbError);
context.done(err);
return;
}
context.log("Connected to cosmosdb server");
let db = client.db(db_name);
let col = db.collection(db_name);
col.updateMany({name: containerName }, {$set: { state: "Error", message: JSON.stringify(err) }}, function(dbUpdateError, r){
if (dbUpdateError){
context.log(dbUpdateError);
}
client.close();
context.done(err)
})
});
});
}catch(err){
console.log(err);
}
};