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