-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
199 lines (168 loc) · 4.51 KB
/
index.js
File metadata and controls
199 lines (168 loc) · 4.51 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const http = require('http');
const https = require('https');
const SOURCE_KEY = process.env.SOURCE_KEY;
const SOURCE_URL = process.env.SOURCE_URL;
const STREAM_URL = process.env.STREAM_URL;
const WEBHOOK_URL = process.env.WEBHOOK_URL;
const PREPEND_MESSAGE = ':arrows_counterclockwise:';
// JSON files that contains the body to POST to streammachine when creating streams and sources
const streamsJson = require('./streams.json');
const sourceJson = require('./source.json');
/* Start: helper functions */
const notify = function(message) {
const data = JSON.stringify({
text: `${PREPEND_MESSAGE} ${message}`
});
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(WEBHOOK_URL, options, (resp) => {
});
req.on('error', (error) => {
console.error("Error posting to slack:");
console.error(error);
});
req.write(data);
req.end();
};
const createStream = function(streamKey) {
const config = JSON.stringify(streamsJson[streamKey]);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': config.length
}
};
const req = http.request(STREAM_URL, options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(data)
});
});
req.on('error', (error) => {
console.error("Error creating to stream: " + streamKey);
console.error(error);
});
req.write(config);
req.end();
};
const deleteStream = function(streamKey) {
console.log(`deleting stream: ${streamKey}`);
const deleteUrl = `${STREAM_URL}/${streamKey}`;
const options = {
method: 'DELETE'
};
const req = http.request(deleteUrl, options, (resp) => {
resp.on('data', () => {
console.log(`deleted stream: ${streamKey}`);
});
});
req.end();
};
const createSource = function(sourceKey) {
const config = JSON.stringify(sourceJson);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': config.length
}
};
const req = http.request(SOURCE_URL, options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(data)
});
});
req.on('error', (error) => {
console.error("Error creating source: " + sourceKey);
console.error(error);
});
req.write(config);
req.end();
};
const recreateSource = function(sourceKey) {
console.log(`deleting source: ${sourceKey}`);
const deleteUrl = `${SOURCE_URL}/${sourceKey}`;
const options = {
method: 'DELETE'
};
const req = http.request(deleteUrl, options, (resp) => {
resp.on('data', () => {
console.log(`deleted source: ${sourceKey}`);
createSource(sourceKey);
});
});
req.end();
};
/* End: helper functions */
// Check if we have the required environment variables
if (SOURCE_KEY === undefined) {
console.error("No SOURCE_KEY");
return;
} else if (SOURCE_URL === undefined) {
console.error("No SOURCE_URL");
return;
} else if (STREAM_URL === undefined) {
console.error("No STREAM_URL");
return;
} else if (WEBHOOK_URL === undefined) {
console.error("No WEBHOOK_URL");
return;
}
// Collect all the streams and begin recreating them
http.get(SOURCE_URL, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
let notify_body = [];
const body = JSON.parse(data);
if (body.length === 0) {
notify_body.push("No streams found.");
return;
}
// Delete the streams
body.forEach((stream) => {
const source = stream.source;
if (source === undefined) {
notify_body.push(`No source for: ${stream.key}`);
return;
}
if (streamsJson[stream.key] !== undefined) {
deleteStream(stream.key);
}
});
// Recreate the source mount
recreateSource(SOURCE_KEY);
// Create the streams
body.forEach((stream) => {
const source = stream.source;
if (source === undefined) {
notify_body.push(`No source for: ${stream.key}`);
return;
}
if (streamsJson[stream.key] !== undefined) {
createStream(stream.key);
notify_body.push(`Stream \`${stream.key}\` has been automatically recreated.`);
}
});
if (notify_body.length > 0) {
notify(notify_body.join("\n"));
}
});
}).on("error", (err) => {
notify(err.message);
});