simplify testing with diff postgres version (still need to sync this with vagrant_set...
[xtuple] / scripts / install_xtuple.sh
1 #!/bin/bash
2
3 set -e
4
5 echo -n "Checking for sudo..."
6 if ! which sudo ;
7 then
8   echo "Please install sudo and grant yourself access to sudo:"
9   echo
10   echo "   # apt-get install sudo"
11   echo "   # addgroup $USER sudo"
12   echo
13   exit 1
14 fi
15
16 alias sudo='sudo env PATH=$PATH $@'
17
18 # Make sure we have all the essential tools we need
19 sudo apt-get update
20 sudo apt-get -q -y install \
21   git \
22   curl \
23   python-software-properties \
24   software-properties-common
25
26 NODE_VERSION=0.10.31
27
28 DEBDIST=`lsb_release -c -s`
29 echo "Trying to install xTuple for platform ${DEBDIST}"
30
31 RUN_DIR=$(pwd)
32 LOG_FILE=$RUN_DIR/install.log
33 cp $LOG_FILE $LOG_FILE.old 2>&1 &> /dev/null || true
34 log() {
35         echo "xtuple >> $@"
36         echo $@ >> $LOG_FILE
37 }
38
39 varlog() {
40   log $(eval "echo $1 = \$$1")
41 }
42
43 cdir() {
44         cd $1
45         log "Changing directory to $1"
46 }
47
48 PG_VERSION=9.1
49 DATABASE=dev
50 RUNALL=true
51 XT_VERSION=
52 BASEDIR=/usr/local/src
53 LIBS_ONLY=
54 XT_DIR=$RUN_DIR
55 XTUPLE_REPO='http://sourceforge.net/projects/postbooks/files/mobile-debian'
56
57 while getopts ":d:ipnhmx-:" opt; do
58   case $opt in
59     d)
60       PG_VERSION=$OPTARG
61       echo $PG_VERSION
62       ;;
63     i)
64       # Install packages
65       RUNALL=
66       INSTALL=true
67       ;;
68     p)
69       # Configure postgress
70       RUNALL=
71       POSTGRES=true
72       ;;
73     n)
74       # iNitialize the databases and stuff
75       RUNALL=
76       INIT=true
77       ;;
78     m)
79       RUNALL=
80       NPM_INSTALL=true
81       ;;
82     x)
83       # Checkout a specific version of the xTuple repo
84       XT_VERSION=$OPTARG
85       ;;
86     init)
87       # only for initializing a fresh debian package install
88       RUNALL=
89       USERINIT=true
90       ;;
91     node)
92       # select the version to use for nodejs
93       NODE_VERSION=$OPTARG
94       varlog NODE_VERSION
95       ;;
96     h)
97       echo "Usage: install_xtuple [OPTION]"
98          echo "Build the full xTuple Mobile Development Environment."
99          echo ""
100          echo "To install everything, run bash /scripts/install_xtuple.sh"
101          echo ""
102          echo -e "  -h\t\t"
103          echo -e "  -i\t\t"
104          echo -e "  -p\t\t"
105          echo -e "  -n\t\t"
106          echo -e "  -m\t\t"
107          echo -e "  -x\t\t"
108    exit 0;
109       ;;
110   esac
111 done
112
113 if [ $RUNALL ]
114 then
115         INSTALL=true
116         POSTGRES=true
117         INIT=true
118 fi
119
120 if [ $USERINIT ]
121 then
122         INSTALL=
123         POSTGRES=
124         INIT=
125 fi
126
127 if [ -z "$NODE_VERSION" ]
128 then
129         varlog NODE_VERSION
130 fi
131
132 install_packages() {
133   log "installing debian packages..."
134   if [ "${DEBDIST}" = "wheezy" ];
135   then
136     # for Debian wheezy (7.x) we need some things from the wheezy-backports
137     sudo add-apt-repository -y "deb http://ftp.debian.org/debian wheezy-backports main"
138   fi
139   sudo add-apt-repository -y "deb http://apt.postgresql.org/pub/repos/apt/ ${DEBDIST}-pgdg main"
140   sudo wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
141   sudo apt-get -qq update 2>&1 | tee -a $LOG_FILE
142   sudo apt-get -q -y install curl build-essential libssl-dev \
143     postgresql-${PG_VERSION} postgresql-server-dev-${PG_VERSION} \
144     postgresql-contrib-${PG_VERSION} postgresql-${PG_VERSION}-plv8 2>&1 \
145     | tee -a $LOG_FILE
146
147   if [ ! -d "/usr/local/nvm" ]; then
148     sudo rm -f /usr/local/bin/nvm
149     sudo mkdir /usr/local/nvm
150     sudo git clone https://github.com/xtuple/nvm.git /usr/local/nvm
151     sudo ln -s /usr/local/nvm/nvm_bin.sh /usr/local/bin/nvm
152     sudo chmod +x /usr/local/bin/nvm
153   fi
154   sudo nvm install $NODE_VERSION
155   sudo nvm use $NODE_VERSION
156   sudo nvm alias default $NODE_VERSION
157   sudo nvm alias xtuple $NODE_VERSION
158
159   # use latest npm
160   sudo npm install -fg npm@1.4.25
161         # npm no longer supports its self-signed certificates
162         log "telling npm to use known registrars..."
163         npm config set ca ""
164         sudo chown -R $USER $HOME/.npm
165
166   log "installing npm modules..."
167   npm install --unsafe-perm 2>&1 | tee -a $LOG_FILE
168 }
169
170 # Use only if running from a debian package install for the first time
171 user_init() {
172         if [ "$USER" = "root" ]
173         then
174                 echo "Run this as a normal user"
175                 return 1
176         fi
177         echo "WARNING: This will wipe clean the xtuple folder in your home directory."
178         echo "Hit ctrl-c to cancel."
179         read PAUSE
180         read -p "Github username: " USERNAME ERRS
181         rm -rf ~/xtuple
182
183         git clone git://github.com/$USERNAME/xtuple.git
184         git remote add xtuple git://github.com/xtuple/xtuple.git
185 }
186
187 # Configure postgres and initialize postgres databases
188
189 setup_postgres() {
190         sudo mkdir -p $BASEDIR/postgres
191         if [ $? -ne 0 ]
192         then
193                 return 1
194         fi
195
196         PGDIR=/etc/postgresql/${PG_VERSION}/main
197
198   log "copying configs..."
199         sudo cp $PGDIR/postgresql.conf $PGDIR/postgresql.conf.default
200         sudo cat $PGDIR/postgresql.conf.default | sed "s/#listen_addresses = \S*/listen_addresses = \'*\'/" | sed "s/#custom_variable_classes = ''/custom_variable_classes = 'plv8'/" | sudo tee $PGDIR/postgresql.conf > /dev/null
201         sudo chown postgres $PGDIR/postgresql.conf
202
203         sudo cp $PGDIR/pg_hba.conf $PGDIR/pg_hba.conf.default
204         sudo cat $PGDIR/pg_hba.conf.default | sed "s/local\s*all\s*postgres.*/local\tall\tpostgres\ttrust/" | sed "s/local\s*all\s*all.*/local\tall\tall\ttrust/" | sed "s#host\s*all\s*all\s*127\.0\.0\.1.*#host\tall\tall\t127.0.0.1/32\ttrust#" | sudo tee $PGDIR/pg_hba.conf > /dev/null
205         sudo chown postgres $PGDIR/pg_hba.conf
206
207   log "restarting postgres..."
208         sudo service postgresql restart
209
210   log "dropping existing db, if any..."
211         sudo -u postgres dropdb $DATABASE || true
212
213         cdir $BASEDIR/postgres
214
215   log "Setup database"
216     sudo wget -qO init.sql http://sourceforge.net/projects/postbooks/files/03%20PostBooks-databases/4.2.1/init.sql/download
217         sudo -u postgres psql -q -f 'init.sql' 2>&1 | tee -a $LOG_FILE
218 }
219
220 init_everythings() {
221         log "Setting properties of admin user"
222
223         cdir $XT_DIR/node-datasource
224
225         cat sample_config.js | sed "s/testDatabase: \"\"/testDatabase: '$DATABASE'/" > config.js
226         log "Configured node-datasource"
227         log "The database is now set up..."
228
229         mkdir -p $XT_DIR/node-datasource/lib/private
230         cdir $XT_DIR/node-datasource/lib/private
231         cat /dev/urandom | tr -dc '0-9a-zA-Z!@#$%^&*_+-'| head -c 64 > salt.txt
232         log "Created salt"
233         cat /dev/urandom | tr -dc '0-9a-zA-Z!@#$%^&*_+-'| head -c 64 > encryption_key.txt
234         log "Created encryption key"
235         openssl genrsa -des3 -out server.key -passout pass:xtuple 1024 2>&1 | tee -a $LOG_FILE
236         openssl rsa -in server.key -passin pass:xtuple -out key.pem -passout pass:xtuple 2>&1 | tee -a $LOG_FILE
237         openssl req -batch -new -key key.pem -out server.csr -subj '/CN='$(hostname) 2>&1 | tee -a $LOG_FILE
238         openssl x509 -req -days 365 -in server.csr -signkey key.pem -out server.crt 2>&1 | tee -a $LOG_FILE
239         if [ $? -ne 0 ]
240         then
241                 log "Failed to generate server certificate in $XT_DIR/node-datasource/lib/private"
242                 return 3
243         fi
244
245         cdir $XT_DIR/test/lib
246   cat sample_login_data.js | sed "s/org: \'dev\'/org: \'$DATABASE\'/" > login_data.js
247         log "Created testing login_data.js"
248
249         cdir $XT_DIR
250         npm run-script test-build 2>&1 | tee -a $LOG_FILE
251
252         log "You can login to the database and mobile client with:"
253         log "  username: admin"
254         log "  password: admin"
255         log "Installation now finished."
256         log "Run the following commands to start the datasource:"
257         if [ $USERNAME ]
258         then
259                 log "cd node-datasource"
260                 log "node main.js"
261         else
262                 log "cd /usr/local/src/xtuple/node-datasource/"
263                 log "node main.js"
264         fi
265 }
266
267 if [ $USERINIT ]
268 then
269         user_init
270 fi
271
272 if [ $INSTALL ]
273 then
274   log "install_packages()"
275         install_packages
276         if [ $? -ne 0 ]
277         then
278                 log "package installation failed."
279                 exit 1
280         fi
281 fi
282
283 if [ $POSTGRES ]
284 then
285   log "setup_postgres()"
286         setup_postgres
287         if [ $? -ne 0 ]
288         then
289                 exit 4
290         fi
291 fi
292 if [ $INIT ]
293 then
294   log "init_everythings()"
295         init_everythings
296         if [ $? -ne 0 ]
297         then
298                 log "init_everythings failed"
299         fi
300 fi
301
302 log "All Done!"