-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgather-deps.sh
executable file
·147 lines (124 loc) · 5.36 KB
/
gather-deps.sh
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
#! /bin/bash
# Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
# Licensed under the MIT License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# This script gathers all of the binaries and libraries needed to run Meteor,
# but which aren't part of a normal Meteor bundle.
#
# We pull Node.js from the Meteor tools installation.
#
# We build a custom MongoDB binary using a special fork of Mongo which creates
# much smaller databases. This is necessary because Mongo by default
# pre-allocates many megabytes of space, but Sandstorm app instances should be
# individual documents and therefore small. (It would be nice to replace Mongo
# entirely with an in-process database; maybe based on Meteor's own
# "minimongo"...)
#
# We pull any needed libraries straight from the local system.
set -euo pipefail
if [ $# != 1 ]; then
echo "usage: $0 METEOR_VERSION" >&2
exit 1
fi
METEOR_RELEASE="METEOR@$1"
mkdir -p tmp
copyDep() {
# Copies a file from the system into the chroot.
local FILE=$1
local DST=bundle"${FILE/#\/usr\/local/\/usr}"
if [ -e "$DST" ]; then
# already copied
:
elif [[ "$FILE" == /etc/* ]]; then
# We'll want to copy configuration (e.g. for DNS) from the host at runtime.
if [ -f "$FILE" ]; then
echo "$FILE" >> tmp/etc.list
fi
elif [ -h "$FILE" ]; then
# Symbolic link.
# We copy over the target, and recreate the link.
# Currently we denormalize the link because I'm not sure how to follow
# one link at a time in bash (since readlink without -f gives a relative
# path and I'm not sure how to interpret that against the link's path).
# I'm sure there's a way, but whatever...
mkdir -p $(dirname "$DST")
local LINK=$(readlink -f "$FILE")
ln -sf "${LINK/#\/usr\/local/\/usr}" "$DST"
copyDep "$LINK"
elif [ -d "$FILE" ]; then
# Directory. Make it, but don't copy contents; we'll do that later.
mkdir -p "$DST"
elif [ -f "$FILE" ]; then
# Regular file. Copy it over.
mkdir -p $(dirname "$DST")
cp "$FILE" "$DST"
fi
}
copyDeps() {
# Reads filenames on stdin and copies them into the chroot.
while read FILE; do
copyDep "$FILE"
done
}
rm -rf bundle
mkdir bundle
METEOR_WAREHOUSE_DIR="${METEOR_WAREHOUSE_DIR:-$HOME/.meteor}"
echo -n "Finding meteor-tool installation (can take a few seconds)..." >&2
TOOL_VERSION=$(meteor show --ejson $METEOR_RELEASE | grep '^ *"tool":' |
sed -re 's/^.*"(meteor-tool@[^"]*)".*$/\1/g')
TOOLDIR=$(echo $TOOL_VERSION | tr @ /)
echo " $TOOL_VERSION" >&2
METEOR_DEV_BUNDLE=$(readlink -f $METEOR_WAREHOUSE_DIR/packages/$TOOLDIR/mt-os.linux.x86_64/dev_bundle)
cp start.js bundle/start.js
$METEOR_DEV_BUNDLE/bin/npm install
# Extract node-capnp build from installed Sandstorm and shove it into node_modules. This makes
# Npm.require('capnp') "just work" for Meteor Sandstorm apps, which is convenient since it can
# be tedious to build otherwise. We also copy in the standard .capnp imports that people will
# need on Sandstorm, so that you can do e.g. require("sandstorm/grain.capnp").
#
# Note that capnp.node and capnp.js have to got into programs/server/node_modules as Meteor will
# not search up the tree as of: https://github.com/meteor/meteor/pull/9095
mkdir -p bundle/programs/server/node_modules
cp /opt/sandstorm/latest/node_modules/{capnp.node,capnp.js} bundle/programs/server/node_modules
cp -r /opt/sandstorm/latest/usr/include/{capnp,sandstorm} bundle/programs/server/node_modules
mv node_modules bundle/node_modules
# Copy over key binaries.
mkdir -p bundle/bin
cp niscu/mongod bundle/bin/niscud
cp mongo/mongod bundle/bin/
cp $METEOR_DEV_BUNDLE/bin/node bundle/bin
# Binaries copied from Meteor aren't writable by default.
chmod u+w bundle/bin/*
# Copy over all necessary shared libraries.
(ldd bundle/bin/* $(find bundle -name '*.node') || true) | grep -o '[[:space:]]/[^ ]*' | copyDeps
# Mongo wants these localization files.
mkdir -p bundle/usr/lib
cp -r /usr/lib/locale bundle/usr/lib
mkdir -p bundle/usr/share/locale
cp /usr/share/locale/locale.alias bundle/usr/share/locale
# Due to a bug in Node 12, /proc/meminfo must exist otherwise the process will die at startup.
# However, it can be an empty file. See: https://github.com/nodejs/help/issues/2099
mkdir bundle/proc
touch bundle/proc/meminfo
# Make bundle smaller by stripping stuff.
strip bundle/bin/*
find bundle -name '*.so' | xargs strip
rm -rf meteor-spk.deps
mv bundle meteor-spk.deps