This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_r
executable file
·167 lines (135 loc) · 5.23 KB
/
build_r
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
#!/bin/bash
# fail fast
set -e
# debug
# set -x
# for checking which version of the build script was used, output to STDOUT
# so that it gets included in the build log output file
echo "#======================================================================"
echo "# $0"
cat $0
echo "#======================================================================"
echo "# Environment"
set
echo "#======================================================================"
R_VERSION="${1:-3.4.0}"
BUILD_NO="${2:-`date +%Y%m%d-%H%M`}"
STACK="${3:-cedar-14}"
# check arguments
if ([ -z "$R_VERSION" ] || [ -z "$BUILD_NO" ] || [ -z "$STACK" ]); then
echo
echo "USAGE: $0 VERSION [BUILD_NO] [STACK]"
echo
echo " R_VERSION Optionally, the R version for apt-get."
echo " Defaults to 3.3.1-1trusty0"
echo " Use \"apt-cache policy r-base\" to obtain valid versions."
echo
echo " BUILD_NO Optionally, the build number."
echo " Defaults to todays date and current time, in the form \"YYYYMMDD-HHMM\"."
echo
echo " STACK Optionally, the Heroku stack."
echo " Defaults to cedar-14"
echo
exit 1
fi
# helper functions
function topic() {
echo "-----> $*"
}
function indent() {
c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}
ARCHIVE="R-$R_VERSION-binaries-$BUILD_NO.tar.gz"
DEBOOTSTRAP_VER=1.0.59
FAKECHROOT_VER=2.18
APP_DIR="/app"
TOOLS_DIR="$APP_DIR/.tools"
CHROOT_DIR="$APP_DIR/.root"
DEBOOTSTRAP_BASE_DIR="$TOOLS_DIR/debootstrap"
DEBOOTSTRAP_DIR="$DEBOOTSTRAP_BASE_DIR/usr/share/debootstrap"
FAKECHROOT_DIR="$TOOLS_DIR/fakechroot"
PATH="$DEBOOTSTRAP_BASE_DIR/usr/sbin:$FAKECHROOT_DIR/sbin:$FAKECHROOT_DIR/bin:$PATH"
# export DEBIAN_FRONTEND=noninteractive
# export TERM=linux
export DEBOOTSTRAP_DIR
export PATH
mkdir -p $APP_DIR
pushd $APP_DIR > /dev/null
#======================================================================
topic 'Installing debootstrap'
mkdir -p $DEBOOTSTRAP_BASE_DIR
curl -L https://launchpad.net/ubuntu/+archive/primary/+files/debootstrap_$DEBOOTSTRAP_VER.tar.xz -o debootstrap.tar.xz
tar xvf debootstrap.tar.xz
pushd debootstrap-$DEBOOTSTRAP_VER > /dev/null
DESTDIR=$DEBOOTSTRAP_BASE_DIR fakeroot make
DESTDIR=$DEBOOTSTRAP_BASE_DIR fakeroot make install
popd > /dev/null
#======================================================================
topic 'Installing fakechroot'
mkdir -p $FAKECHROOT_DIR
git clone -b "$FAKECHROOT_VER" --single-branch --depth 1 https://github.com/dex4er/fakechroot.git
pushd fakechroot > /dev/null
./autogen.sh
./configure --prefix=$FAKECHROOT_DIR
make
make install
popd > /dev/null
#======================================================================
topic 'Installing debootstrap linux'
mkdir -p $CHROOT_DIR
fakechroot fakeroot debootstrap --variant=fakechroot --arch=amd64 trusty $CHROOT_DIR
#======================================================================
topic 'Configuring Linux and installing R $R_VERSION'
BASH_RC_FILE="$CHROOT_DIR/root/.bashrc"
# uncomment line
sed -i -e s/#force_color_prompt=yes/force_color_prompt=yes/ $BASH_RC_FILE
# configure apt
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'echo "deb http://archive.ubuntu.com/ubuntu trusty main" > /etc/apt/sources.list'
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'echo "deb http://archive.ubuntu.com/ubuntu trusty-updates main" >> /etc/apt/sources.list'
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'echo "deb http://archive.ubuntu.com/ubuntu trusty universe" >> /etc/apt/sources.list'
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'echo "deb http://archive.ubuntu.com/ubuntu trusty-updates universe" >> /etc/apt/sources.list'
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'
# add cran key
fakechroot fakeroot chroot $CHROOT_DIR \
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'gpg --export E084DAB9 > /var/tmp/E084DAB9 && apt-key add /var/tmp/E084DAB9 && rm /var/tmp/E084DAB9'
fakechroot fakeroot chroot $CHROOT_DIR \
apt-get update
# prevent warnings about man-db when installing packages
export LD_LIBRARY_PATH="/usr/lib/man-db:$LD_LIBRARY_PATH"
fakechroot fakeroot chroot $CHROOT_DIR \
apt-get install -y \
build-essential \
gfortran \
libcairo2-dev \
libcurl4-openssl-dev \
libgsl0-dev \
libssl-dev \
libxt-dev \
pkg-config \
r-base-dev=${R_VERSION}* \
r-recommended=${R_VERSION}*
# install pandoc
fakechroot fakeroot chroot $CHROOT_DIR \
/bin/sh -c 'curl -L https://github.com/jgm/pandoc/releases/download/1.15.1/pandoc-1.15.1-1-amd64.deb -o pandoc.deb && dpkg -i pandoc.deb && rm pandoc.deb'
fakechroot fakeroot chroot $CHROOT_DIR \
apt-get clean
# TODO: configure default CRAN mirror (0-cloud)
# echo 'options(repos = c(CRAN = "http://cran.revolutionanalytics.com/"), download.file.method = "libcurl")' >> $CHROOT_DIR/etc/R/Rprofile.site
# install shiny (as it's the most used on Heroku)
fakechroot fakeroot chroot $CHROOT_DIR \
/usr/bin/R -e "install.packages('shiny', repos='http://cran.rstudio.com/')"
# check
fakechroot fakeroot chroot $CHROOT_DIR \
/usr/bin/R -e "capabilities()"
exit 0