Skip to content

Commit c2b382f

Browse files
authored
Upload to s3 (#50)
Currently the docker build uses a shared volume (`/out`) to place the built files on the system. This change allows us to PUT the tarballs to any url via the environment. This allows us to leverage hyper.sh or AWS ECS Fargate (or any other docker service) to build and upload the files. This upgrades npm for docker and vagrant. It's much faster and uses less memory. For vagrant you should rebuild your vm to get the new script and npm.
1 parent 9d95303 commit c2b382f

9 files changed

+102
-23
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ before_install:
88
- docker build ./ -t t2-compiler:dev
99

1010
script:
11-
- docker run --rm -v `pwd`/out:/out t2-compiler:dev [email protected] /out 6.5.0 release
11+
- docker run --rm -v `pwd`/out:/out t2-compiler:dev [email protected] 6.5.0 release /out
1212
- test -f out/serialport-4.0.7-Release-node-v48-linux-mipsel.tgz

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ RUN ["/bin/bash", "-c", "curl -s -o - https://raw.githubusercontent.com/creation
1818
# Install node 6.5.0
1919
RUN ["/bin/bash", "-c", ". /root/.nvm/nvm.sh \
2020
&& nvm install 6.5.0 \
21+
&& npm install -g npm \
2122
&& npm install -g pre-gypify node-pre-gyp node-gyp \
2223
&& node-gyp install 6.5.0 \
2324
"]
2425

25-
COPY ./compile.sh /root/
26+
COPY compile.sh upload-files.js /root/
2627
ENTRYPOINT ["/root/compile.sh"]
2728
CMD ["--help"]

README.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Install vagrant
1919

2020
```
2121
vagrant up
22-
./compile-vagrant.sh serialport@2.0.5
22+
./compile-vagrant.sh serialport@6
2323
```
2424

2525
Look in the 'out' directory
@@ -29,8 +29,8 @@ Look in the 'out' directory
2929
If you want to use docker you can run;
3030

3131
```bash
32-
# puts the output in the `./out` directory (wont overwrite existing files)
33-
./compile-docker.sh serialport@4.0.0
32+
# puts the output in the `./out` directory (adds new files)
33+
./compile-docker.sh serialport@6
3434
```
3535

3636
To update to the latest t2-compiler from docker hub.
@@ -39,6 +39,13 @@ To update to the latest t2-compiler from docker hub.
3939
docker pull tessel/t2-compiler
4040
```
4141

42+
To output the build on the last line of docker output in JSON containing BASE64 encoded strings
43+
```bash
44+
docker run --rm tessel/t2-compiler $1 6.5.0 release JSON
45+
# build output followed by file contents
46+
# {"serialport-6.0.3-Release-node-v46-linux-mipsel.tgz":"H4s....."}
47+
```
48+
4249
#### Developing the compiler
4350

4451
To build your local Dockerfile
@@ -52,7 +59,9 @@ docker images
5259
# t2-compiler:dev latest 75f126974601 About a minute ago 1.281 GB
5360

5461
# Run the local image you've built
55-
docker run --rm -v `pwd`/out:/out t2-compiler:dev [email protected] /out
62+
docker run --rm -v `pwd`/out:/out t2-compiler:dev serialport 6.5.0 release /out
63+
# or
64+
docker run --rm t2-compiler:dev serialport 6.5.0 release JSON
5665
```
5766

5867
To get an interactive shell run

Vagrantfile

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Vagrant.configure(2) do |config|
3838
# Install node 6.5.0
3939
. /root/.nvm/nvm.sh \
4040
&& nvm install 6.5.0 \
41+
&& npm install -g npm \
4142
&& npm install -g pre-gypify node-pre-gyp node-gyp \
4243
&& node-gyp install 6.5.0
4344

compile-docker.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22
set -ex
3-
docker run --rm -v `pwd`/out:/out tessel/t2-compiler $1 /out 6.5.0 release
4-
docker run --rm -v `pwd`/out:/out tessel/t2-compiler $1 /out 6.5.0 debug
3+
docker run --rm -v `pwd`/out:/out tessel/t2-compiler $1 6.5.0 release /out
4+
docker run --rm -v `pwd`/out:/out tessel/t2-compiler $1 6.5.0 debug /out

compile-vagrant.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ set -e
33

44
PACKAGE_NAME=$1
55

6-
if [[ "$PACKAGE_NAME" == '' ]]; then
6+
if [[ $PACKAGE_NAME == '' ]]; then
77
echo 'Usage: compile.sh package-name'
88
exit 1
99
fi
1010

1111
cd $(dirname $0)
1212
mkdir -p ./out
13-
vagrant ssh -c "sudo su root /root/compile.sh $PACKAGE_NAME /root/out/ 6.5.0 release"
14-
vagrant ssh -c "sudo su root /root/compile.sh $PACKAGE_NAME /root/out/ 6.5.0 debug"
13+
vagrant ssh -c "sudo su root /root/compile.sh $PACKAGE_NAME 6.5.0 release /root/out/"
14+
vagrant ssh -c "sudo su root /root/compile.sh $PACKAGE_NAME 6.5.0 debug /root/out/"

compile.sh

+32-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
#!/bin/bash
22
set -e
3-
3+
echo "# compile.sh $1 $2 $3 $4"
44
PACKAGE_NAME=$1
5-
OUTPUT_DIR=$2
5+
6+
echo "loading nvm"
67
. /root/.nvm/nvm.sh
7-
[ -n "$3" ] && nvm use $3
8-
RELEASE_TYPE=$4
8+
9+
RELEASE_TYPE=$3
10+
OUTPUT_DIR=$4
11+
12+
if [ -n $2 ]; then
13+
echo "# installing node $2"
14+
nvm install $2
15+
fi
16+
917
NODE_VERSION=`node -p process.versions.node`
1018

1119
ARCH=mipsel
1220
export STAGING_DIR=/root
1321

14-
if [[ "$PACKAGE_NAME" == '--help' || "$PACKAGE_NAME" == '' ]]; then
22+
if [[ $PACKAGE_NAME == '--help' || $PACKAGE_NAME == '' ]]; then
1523
echo "Usage: "
16-
echo " $0 [package name]<@version> [output_dir/]"
24+
echo " $0 [package name]<@version> [nodeversion releaseType outputdir]"
1725
exit 1
1826
fi
1927

20-
if [ ! -d "$OUTPUT_DIR" ]; then
21-
(>&2 echo "ERROR: The output directory ${OUTPUT_DIR} doesn't exist")
22-
exit 1
28+
if [[ $OUTPUT_DIR != 'S3' ]]; then
29+
if [ ! -d "$OUTPUT_DIR" ]; then
30+
(>&2 echo "ERROR: The output directory ${OUTPUT_DIR} doesn't exist, must be a valid directory or JSON")
31+
exit 1
32+
fi
33+
else
34+
if [ ! -n $S3_RELEASE_URL ]; then
35+
(>&2 echo "ERROR: The S3_RELEASE_URL variable should be set for upload to S3")
36+
exit 1
37+
fi
2338
fi
2439

2540
cd $(dirname $0)
@@ -43,7 +58,7 @@ export PATH=$TOOLCHAIN_DIR/bin:$PATH
4358
export CPPPATH=$TARGET_DIR/usr/include
4459
export LIBPATH=$TARGET_DIR/usr/lib
4560

46-
#TODO: anything better than this hack?
61+
# TODO: anything better than this hack?
4762
OPTS="-I $SYSROOT/usr/include -L $TOOLCHAIN_DIR/lib -L $SYSROOT/usr/lib -L $SYSROOT/lib"
4863

4964
export CC="${TARGET_CROSS}gcc $OPTS"
@@ -70,10 +85,15 @@ if [[ $RELEASE_TYPE == "debug" ]]; then
7085
echo "Debug build"
7186
node-pre-gyp rebuild --target_platform=linux --target_arch=$ARCH --target=$NODE_VERSION --debug
7287
node-pre-gyp package --target_platform=linux --target_arch=$ARCH --target=$NODE_VERSION --debug
73-
mv -vn build/stage/*.tgz $OUTPUT_DIR
7488
else
7589
echo "Release build"
7690
node-pre-gyp rebuild --target_platform=linux --target_arch=$ARCH --target=$NODE_VERSION
7791
node-pre-gyp package --target_platform=linux --target_arch=$ARCH --target=$NODE_VERSION
78-
mv -vn build/stage/*.tgz $OUTPUT_DIR
92+
fi
93+
94+
cd $(dirname $0)
95+
if [[ $OUTPUT_DIR == 'S3' ]]; then
96+
node ./upload-files.js build/package/build/stage
97+
else
98+
mv -vn build/package/build/stage/*.tgz $OUTPUT_DIR
7999
fi

test-output-url.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const AWS = require('aws-sdk')
2+
const s3 = new AWS.S3()
3+
4+
const s3Bucket = 'packages.tessel.io'
5+
6+
const url = s3.getSignedUrl('putObject', {
7+
Key: 'test/' + process.argv[2],
8+
Bucket: s3Bucket,
9+
ACL: 'public-read'
10+
})
11+
12+
console.log(url)

upload-files.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env node
2+
const fs = require('fs')
3+
const https = require('https')
4+
const path = require('path')
5+
const url = require('url')
6+
const outputDir = process.argv[2]
7+
8+
const releaseURL = url.parse(process.env.S3_RELEASE_URL)
9+
10+
if (!outputDir) {
11+
console.log('Error no output dir')
12+
console.log(`Usage: ${__filename} OUTPUT_DIR`)
13+
process.exit(1)
14+
}
15+
16+
const files = fs.readdirSync(outputDir).filter(file => file.match(/\.tgz$/))
17+
18+
files.forEach(filename => {
19+
console.log(`Starting request to S3 for ${filename}: ${S3_RELEASE_URL}`)
20+
21+
const stream = fs.createReadStream(path.join(outputDir, filename))
22+
const request = https.request(
23+
Object.assign(
24+
releaseURL,
25+
{
26+
method: 'PUT',
27+
headers: {
28+
'Content-Length': fs.statSync(path.join(outputDir, filename)).size,
29+
},
30+
}
31+
),
32+
(response) => response.pipe(process.stdout),
33+
)
34+
35+
stream.pipe(request)
36+
})

0 commit comments

Comments
 (0)