Merge pull request #1785 from bendiy/4_6_x
[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.8.26
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 postgresql-9.1 postgresql-server-dev-9.1 postgresql-contrib-9.1 postgresql-9.1-plv8 2>&1 | tee -a $LOG_FILE
143
144   if [ ! -d "/usr/local/nvm" ]; then
145     sudo rm -f /usr/local/bin/nvm
146     sudo mkdir /usr/local/nvm
147     sudo git clone https://github.com/xtuple/nvm.git /usr/local/nvm
148     sudo ln -s /usr/local/nvm/nvm_bin.sh /usr/local/bin/nvm
149     sudo chmod +x /usr/local/bin/nvm
150   fi
151   sudo nvm install $NODE_VERSION
152   sudo nvm use $NODE_VERSION
153   sudo nvm alias default $NODE_VERSION
154   sudo nvm alias xtuple $NODE_VERSION
155
156         # npm no longer supports its self-signed certificates
157         log "telling npm to use known registrars..."
158         npm config set ca ""
159
160   log "installing npm modules..."
161   npm install --unsafe-perm 2>&1 | tee -a $LOG_FILE
162 }
163
164 # Use only if running from a debian package install for the first time
165 user_init() {
166         if [ "$USER" = "root" ]
167         then
168                 echo "Run this as a normal user"
169                 return 1
170         fi
171         echo "WARNING: This will wipe clean the xtuple folder in your home directory."
172         echo "Hit ctrl-c to cancel."
173         read PAUSE
174         read -p "Github username: " USERNAME ERRS
175         rm -rf ~/xtuple
176
177         git clone git://github.com/$USERNAME/xtuple.git
178         git remote add xtuple git://github.com/xtuple/xtuple.git
179 }
180
181 # Configure postgres and initialize postgres databases
182
183 setup_postgres() {
184         sudo mkdir -p $BASEDIR/postgres
185         if [ $? -ne 0 ]
186         then
187                 return 1
188         fi
189
190         PGDIR=/etc/postgresql/9.1/main
191
192   log "copying configs..."
193         sudo cp $PGDIR/postgresql.conf $PGDIR/postgresql.conf.default
194         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
195         sudo chown postgres $PGDIR/postgresql.conf
196
197         sudo cp $PGDIR/pg_hba.conf $PGDIR/pg_hba.conf.default
198         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
199         sudo chown postgres $PGDIR/pg_hba.conf
200
201   log "restarting postgres..."
202         sudo service postgresql restart
203
204   log "dropping existing db, if any..."
205         sudo -u postgres dropdb $DATABASE || true
206
207         cdir $BASEDIR/postgres
208
209   log "Setup database"
210     sudo wget -qO init.sql http://sourceforge.net/projects/postbooks/files/03%20PostBooks-databases/4.2.1/init.sql/download
211         sudo -u postgres psql -q -f 'init.sql' 2>&1 | tee -a $LOG_FILE
212 }
213
214 init_everythings() {
215         log "Setting properties of admin user"
216
217         cdir $XT_DIR/node-datasource
218
219         cat sample_config.js | sed "s/testDatabase: \"\"/testDatabase: '$DATABASE'/" > config.js
220         log "Configured node-datasource"
221         log "The database is now set up..."
222
223         mkdir -p $XT_DIR/node-datasource/lib/private
224         cdir $XT_DIR/node-datasource/lib/private
225         cat /dev/urandom | tr -dc '0-9a-zA-Z!@#$%^&*_+-'| head -c 64 > salt.txt
226         log "Created salt"
227         cat /dev/urandom | tr -dc '0-9a-zA-Z!@#$%^&*_+-'| head -c 64 > encryption_key.txt
228         log "Created encryption key"
229         openssl genrsa -des3 -out server.key -passout pass:xtuple 1024 2>&1 | tee -a $LOG_FILE
230         openssl rsa -in server.key -passin pass:xtuple -out key.pem -passout pass:xtuple 2>&1 | tee -a $LOG_FILE
231         openssl req -batch -new -key key.pem -out server.csr -subj '/CN='$(hostname) 2>&1 | tee -a $LOG_FILE
232         openssl x509 -req -days 365 -in server.csr -signkey key.pem -out server.crt 2>&1 | tee -a $LOG_FILE
233         if [ $? -ne 0 ]
234         then
235                 log "Failed to generate server certificate in $XT_DIR/node-datasource/lib/private"
236                 return 3
237         fi
238
239         cdir $XT_DIR/test/lib
240   cat sample_login_data.js | sed "s/org: \'dev\'/org: \'$DATABASE\'/" > login_data.js
241         log "Created testing login_data.js"
242
243         cdir $XT_DIR
244         npm run-script test-build 2>&1 | tee -a $LOG_FILE
245
246         log "You can login to the database and mobile client with:"
247         log "  username: admin"
248         log "  password: admin"
249         log "Installation now finished."
250         log "Run the following commands to start the datasource:"
251         if [ $USERNAME ]
252         then
253                 log "cd node-datasource"
254                 log "node main.js"
255         else
256                 log "cd /usr/local/src/xtuple/node-datasource/"
257                 log "node main.js"
258         fi
259 }
260
261 if [ $USERINIT ]
262 then
263         user_init
264 fi
265
266 if [ $INSTALL ]
267 then
268   log "install_packages()"
269         install_packages
270         if [ $? -ne 0 ]
271         then
272                 log "package installation failed."
273                 exit 1
274         fi
275 fi
276
277 if [ $POSTGRES ]
278 then
279   log "setup_postgres()"
280         setup_postgres
281         if [ $? -ne 0 ]
282         then
283                 exit 4
284         fi
285 fi
286 if [ $INIT ]
287 then
288   log "init_everythings()"
289         init_everythings
290         if [ $? -ne 0 ]
291         then
292                 log "init_everythings failed"
293         fi
294 fi
295
296 log "All Done!"