Skip to content

Commit 573788f

Browse files
authored
Merge pull request #324 from smartdevicelink/bugfix/webengine-docker
Bugfix/webengine docker
2 parents 5495abe + 5526fec commit 573788f

File tree

2 files changed

+69
-54
lines changed

2 files changed

+69
-54
lines changed

docker/Dockerfile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Copyright (c) 2022, Livio, Inc.
2-
FROM node:12
2+
FROM debian:11.7
33

44
ARG VERSION=master
55

66
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
ca-certificates \
78
openssl \
89
curl \
10+
wget \
11+
xz-utils \
912
git
1013

1114
# Download SDL Server from github
@@ -14,10 +17,17 @@ WORKDIR /usr
1417
RUN mkdir /usr/policy
1518
RUN git clone https://github.com/smartdevicelink/sdl_server.git /usr/policy -b $VERSION --depth=1
1619

20+
# Install node + npm
21+
RUN wget https://nodejs.org/dist/v16.20.1/node-v16.20.1-linux-x64.tar.xz
22+
RUN tar xvf node-v16.20.1-linux-x64.tar.xz
23+
RUN chmod +rx node-v16.20.1-linux-x64/bin/node node-v16.20.1-linux-x64/bin/npm
24+
RUN ln -s /usr/node-v16.20.1-linux-x64/bin/node /usr/local/bin/node
25+
RUN ln -s /usr/node-v16.20.1-linux-x64/bin/npm /usr/local/bin/npm
26+
1727
WORKDIR /usr/policy
1828

19-
RUN npm install
20-
RUN npm install request aws-sdk node-stream-zip --save
29+
RUN npm install --legacy-peer-deps
30+
RUN npm install aws-sdk@2.1453.0 node-stream-zip@1.15.0 --save --legacy-peer-deps
2131

2232
COPY wait-for-it.sh wait-for-it.sh
2333
COPY keys customizable/ca

docker/webengine-bundle.js

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// skeleton function for customized downloading and extracting of package information
2-
const request = require('request');
2+
const http = require('http');
3+
const https = require('https');
34
const fs = require('fs');
45
const UUID = require('uuid');
56
const AWS = require('aws-sdk');
67
const StreamZip = require('node-stream-zip');
8+
// assumes the bucket already exists. make sure it is set up to allow writing objects to it from remote sources!
79
const BUCKET_NAME = process.env.BUCKET_NAME;
810

911
if (process.env.AWS_REGION !== undefined && BUCKET_NAME !== undefined) {
@@ -32,52 +34,42 @@ exports.handleBundle = function (package_url, cb) {
3234
let bucketUrl = '';
3335
const TMP_FILE_NAME = `${UUID.v4()}.zip`;
3436

35-
// create a new bucket if it doesn't already exist
36-
new AWS.S3().createBucket({Bucket: BUCKET_NAME, ACL: 'public-read'}, err => {
37-
38-
// OperationAborted errors are expected, as we are potentially
39-
// calling this API multiple times simultaneously
40-
if (err && err.code !== 'OperationAborted') {
41-
console.log(err);
42-
return cb(err);
43-
}
44-
// read the URL and save it to a buffer variable
45-
readUrlToBuffer(package_url)
46-
.then(zipBuffer => { // submit the file contents to S3
47-
compressedSize = zipBuffer.length;
48-
const randomString = UUID.v4();
49-
const fileName = `${randomString}.zip`;
50-
bucketUrl = `https://${BUCKET_NAME}.s3.amazonaws.com/${fileName}`;
51-
// make the bundle publicly accessible
52-
const objectParams = {Bucket: BUCKET_NAME, ACL: 'public-read', Key: fileName, Body: zipBuffer};
53-
// Create object upload promise
54-
return new AWS.S3().putObject(objectParams).promise();
55-
})
56-
.then(() => { // unzip the contents of the bundle to get its uncompressed data information
57-
return streamUrlToTmpFile(bucketUrl, TMP_FILE_NAME);
58-
})
59-
.then(() => {
60-
return unzipAndGetUncompressedSize(TMP_FILE_NAME);
61-
})
62-
.then(uncompressedSize => {
63-
// delete the tmp zip file
64-
fs.unlink(TMP_FILE_NAME, () => {
65-
// all the information has been collected
66-
cb(null, {
67-
url: bucketUrl,
68-
size_compressed_bytes: compressedSize,
69-
size_decompressed_bytes: uncompressedSize
70-
});
71-
});
72-
})
73-
.catch(err => {
74-
console.log(err);
75-
// delete the tmp zip file
76-
fs.unlink(TMP_FILE_NAME, () => {
77-
cb(err);
37+
// read the URL and save it to a buffer variable
38+
readUrlToBuffer(package_url)
39+
.then(zipBuffer => { // submit the file contents to S3
40+
compressedSize = zipBuffer.length;
41+
const randomString = UUID.v4();
42+
const fileName = `${randomString}.zip`;
43+
bucketUrl = `https://${BUCKET_NAME}.s3.amazonaws.com/${fileName}`;
44+
// make the bundle publicly accessible
45+
const objectParams = {Bucket: BUCKET_NAME, ACL: 'public-read', Key: fileName, Body: zipBuffer};
46+
// Create object upload promise
47+
return new AWS.S3().putObject(objectParams).promise();
48+
})
49+
.then(() => { // unzip the contents of the bundle to get its uncompressed data information
50+
return streamUrlToTmpFile(bucketUrl, TMP_FILE_NAME);
51+
})
52+
.then(() => {
53+
return unzipAndGetUncompressedSize(TMP_FILE_NAME);
54+
})
55+
.then(uncompressedSize => {
56+
// delete the tmp zip file
57+
fs.unlink(TMP_FILE_NAME, () => {
58+
// all the information has been collected
59+
cb(null, {
60+
url: bucketUrl,
61+
size_compressed_bytes: compressedSize,
62+
size_decompressed_bytes: uncompressedSize
7863
});
7964
});
80-
});
65+
})
66+
.catch(err => {
67+
console.log(err);
68+
// delete the tmp zip file
69+
fs.unlink(TMP_FILE_NAME, () => {
70+
cb(err);
71+
});
72+
});
8173
}
8274

8375
function unzipAndGetUncompressedSize (fileName) {
@@ -109,24 +101,37 @@ function unzipAndGetUncompressedSize (fileName) {
109101
}
110102

111103
function streamUrlToTmpFile (url, fileName) {
104+
const urlObj = new URL(url);
112105
return new Promise((resolve, reject) => {
113-
request(url)
114-
.pipe(fs.createWriteStream(fileName))
115-
.on('close', resolve);
106+
function resCallback (res) {
107+
res.pipe(fs.createWriteStream(fileName)).on('close', resolve);
108+
}
109+
if (urlObj.protocol === "https:") {
110+
https.get(url, resCallback).end();
111+
} else {
112+
http.get(url, resCallback).end();
113+
}
116114
});
117115
}
118116

119117
function readUrlToBuffer (url) {
118+
const urlObj = new URL(url);
120119
return new Promise((resolve, reject) => {
121120
let zipBuffer = [];
122-
123-
request(url)
124-
.on('data', data => {
121+
function resCallback (res) {
122+
res.on('data', data => {
125123
zipBuffer.push(data);
126124
})
127125
.on('close', function () { // file fully downloaded
128126
// put the zip contents to a buffer
129127
resolve(Buffer.concat(zipBuffer));
130128
});
129+
}
130+
131+
if (urlObj.protocol === "https:") {
132+
https.get(url, resCallback).end();
133+
} else {
134+
http.get(url, resCallback).end();
135+
}
131136
})
132137
}

0 commit comments

Comments
 (0)