summaryrefslogtreecommitdiff
path: root/install/ishtar-install
blob: fcca1291f47ee0585237ccfd0f93aabac38a97f3 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/bash

set -e

cecho() {
  local code="\033["
  case "$1" in
    black  | bk) color="${code}0;30m";;
    red    |  r) color="${code}1;31m";;
    green  |  g) color="${code}1;32m";;
    yellow |  y) color="${code}1;33m";;
    blue   |  b) color="${code}1;34m";;
    purple |  p) color="${code}1;35m";;
    cyan   |  c) color="${code}1;36m";;
    gray   | gr) color="${code}0;37m";;
    *) local text="$1"
  esac
  [ -z "$text" ] && local text="$color$2${code}0m"
  echo -e "$text"
}

command_exists() {
    command -v "$@" > /dev/null 2>&1
}

# Check if this is a forked Linux distro
check_forked() {
    # Check for lsb_release command existence, it usually exists in forked distros
    if command_exists lsb_release; then
        # Check if the `-u` option is supported
        set +e
        lsb_release -a -u > /dev/null 2>&1
        lsb_release_exit_code=$?
        set -e

        # Check if the command has exited successfully, it means we're in a forked distro
        if [ "$lsb_release_exit_code" = "0" ]; then
            # Print info about current distro
            cat <<-EOF
            You're using '$lsb_dist' version '$dist_version'.
EOF

            # Get the upstream release info
            lsb_dist=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'id' | cut -d ':' -f 2 | tr -d '[[:space:]]')
            dist_version=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'codename' | cut -d ':' -f 2 | tr -d '[[:space:]]')

            # Print info about upstream distro
            cat <<-EOF
            Upstream release is '$lsb_dist' version '$dist_version'.
EOF
        fi
    fi
}

do_install() {

    echo ""
    cecho g "*******************************************************************************"
    cecho g "++++++                      Ishtar installation script                   ++++++"
    cecho g "*******************************************************************************"
    echo ""

    # check user
    user="$(id -un 2>/dev/null || true)"

    sh_c='sh -c'
    if [ "$user" != 'root' ]; then
        if command_exists sudo; then
            sh_c='sudo -E sh -c'
        elif command_exists su; then
            sh_c='su -c'
        else
            cecho r "  Error: this installer needs the ability to run commands as root."
            cecho r "  We are unable to find either "sudo" or "su" available to make this happen."
            exit 1
        fi
    fi

    # check distribution
    lsb_dist=''
    dist_version=''
    backports_activated=''
    if command_exists lsb_release; then
        lsb_dist="$(lsb_release -si)"
    fi
    if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then
        lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
    fi
    if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then
        lsb_dist='debian'
    fi
    if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then
        lsb_dist='fedora'
    fi
    if [ -z "$lsb_dist" ] && [ -r /etc/oracle-release ]; then
        lsb_dist='oracleserver'
    fi
    if [ -z "$lsb_dist" ]; then
        if [ -r /etc/centos-release ] || [ -r /etc/redhat-release ]; then
            lsb_dist='centos'
        fi
    fi
    if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then
        lsb_dist="$(. /etc/os-release && echo "$ID")"
    fi

    lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"

    case "$lsb_dist" in

        ubuntu)
            if command_exists lsb_release; then
                dist_version="$(lsb_release --codename | cut -f2)"
            fi
            if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
                dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
            fi
        ;;

        debian)
            dist_version="$(cat /etc/debian_version | sed 's/\/.*//' | sed 's/\..*//')"
            case "$dist_version" in
                9)
                    dist_version="stretch"
                ;;
                8)
                    dist_version="jessie"
                ;;
                7)
                    dist_version="wheezy"
                ;;
            esac
            set +e
            MAINBACKS=`cat /etc/apt/sources.list | grep $dist_version'-backports' |grep -v "^#"`
            ALLBACKS=''
            if [ "$(ls -A /etc/apt/sources.list.d/)" ]; then
                ALLBACKS=`cat /etc/apt/sources.list.d/* | grep $dist_version'-backports' |grep -v "^#"`
            fi
            set -e
            if [ "$ALLBACKS" != '' ] || [ "$MAINBACKS" != '' ]; then
                backports_activated='true';
            fi
        ;;

        oracleserver)
            # need to switch lsb_dist to match yum repo URL
            lsb_dist="oraclelinux"
            dist_version="$(rpm -q --whatprovides redhat-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//')"
        ;;

        fedora|centos)
            dist_version="$(rpm -q --whatprovides redhat-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//')"
        ;;

        *)
            if command_exists lsb_release; then
                dist_version="$(lsb_release --codename | cut -f2)"
            fi
            if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
                dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
            fi
        ;;


    esac

    # Check if this is a forked Linux distro
    check_forked

    case "$lsb_dist" in
        ubuntu|debian)
            ;;
        *)
            echo ""
            cecho r " Sorry. Either your platform is not easily detectable or not supported by"
            cecho r " this installer."
            echo ""
            exit 1
    esac

    default_db=''
    cat >&2 <<-'EOF'

-------------------------------------------------------------------------------
  A PostgreSQL database is needed to install Ishtar. If you do not plan to use
  a database host on another computer you need to install PostgreSQL.

EOF
    while [ "$default_db" == '' ]
    do
        cecho y '* Default PostgreSQL host? [localhost] '
        read choice
        if [ "$choice" == '' ]; then
            default_db='127.0.0.1'
        elif [ "$choice" == 'localhost' ]; then
            default_db='127.0.0.1'
        else
            default_db=$choice
        fi
    done

    webserver=''
    cat >&2 <<-'EOF'

-------------------------------------------------------------------------------
  A webserver is needed to make Ishtar available to the outside.
  Be carreful if another webserver is already configured, you'll have to serve
  your pages on a different port.

EOF
    MSG=""
    while [ "$webserver" == '' ]
    do
        cecho y '* Which webserver do you want to use? ([nginx]/none) '
        read choice
        case "$choice" in
            nginx ) webserver="nginx";;
            none ) webserver="none";;
            '' ) webserver="nginx";;
        esac
    done

    version=''
    cat >&2 <<-'EOF'

-------------------------------------------------------------------------------
  Two version are usually available for Ishtar: develop/master. Develop is the
  bleeding edge version and you can experience problems with this version.
  Master is the safest choice.

EOF
    while [ "$version" == '' ]
    do
        cecho y "* Which version would you like to use? ([develop]/master) "
        read choice
        case "$choice" in
            develop ) version="develop";;
            master ) version="master";;
            '' ) version="develop";;
        esac
    done

    etc_path="/etc/ishtar/"
    if [ -d "$etc_path" ]; then
        echo ""
        cecho r "ERROR: it seems that "$etc_path" already exists. If this is a remnant "
        cecho r "of an old installation please delete this path before installing."
        echo ""
        exit 1
    fi


    install_path=''
    cat >&2 <<-'EOF'

-------------------------------------------------------------------------------
  By default Ishtar base path is '/srv/'. With this base path Ishtar is
  installed in '/srv/ishtar/'.

EOF
    while [ "$install_path" == '' ]
    do
        cecho y "* Which base install path for Ishtar? [/srv/]"
        read choice
        if [ -z "$choice" ]; then
            install_path='/srv'
        elif [ ! -d "$choice" ]; then
            echo 'Not a valid path.'
        else
            install_path=$choice
        fi
    done

    full_install_path=$install_path'/ishtar/'
    if [ -d "$full_install_path" ]; then
        echo ""
        cecho r "ERROR: it seems that "$full_install_path" already exists. If this is a "
        cecho r "remnant of an old installation please delete this directory before installing."
        echo ""
        exit 1
    fi

    echo ""
    cecho g "*******************************************************************************"
    echo ""

    # Run setup for each distro accordingly
    case "$lsb_dist" in
        ubuntu|debian)
            if [ "$dist_version" != "stretch" ] &&  [ "$dist_version" != "jessie" ]; then
                echo ""
                cecho r " Sorry this script cannot manage your version of Debian/Ubuntu."
                echo ""
                exit 1
            fi

            echo "-------------------------------------------------------------------------------";
            cecho y "Update debian packages cache";
            echo "";
            export DEBIAN_FRONTEND=noninteractive
            ( set -x; $sh_c 'sleep 3; apt-get update' )
            if ! command_exists git; then
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing git...";
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q git' )
            fi
            if ! command_exists apg; then
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing apg...";
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q apg' )
            fi
            if ! command_exists pip; then
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing pip...";
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q python-pip' )
            fi
            if [ "$webserver" == 'nginx' ]; then
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing nginx and uwsgi...";
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q  uwsgi uwsgi-plugin-python nginx' )
            fi

            if [ "$dist_version" == "jessie" ]; then
                if [ "$backports_activated" != 'true' ]; then
                    echo ""
                    cecho r "  In order to install Ishtar you have to activate Debian backports."
                    echo "  To do that:"
                    echo ""
                    echo "    echo 'deb http://ftp.debian.org/debian jessie-backports main contrib non-free' >> /etc/apt/sources.list"
                    echo ""
                    cecho p "  Run again Ishtar installation script after that."
                    exit 1
                fi

                if [ "$default_db" == '127.0.0.1' ]; then
                    echo "-------------------------------------------------------------------------------";
                    cecho y "Installing postgresql"
                    echo ""
                    POSTGIS=postgresql-9.4-postgis-2.3
                    ( set -x; $sh_c 'sleep 3; apt-get install -y -q postgresql '$POSTGIS )
                fi
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing Ishtar dependencies"
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -t jessie-backports -y -q python python-django\
                      python-django-registration python-cffi' )
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q \
                      python-bs4 libpangocairo-1.0-0 \
                      python-tidylib python-lxml python-imaging python-html5lib \
                      python-psycopg2 python-gdal gettext python-unicodecsv memcached \
                      python-django-extra-views python-memcache python-dbf python-markdown' )
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing django-simple-history"
                echo "";
                ( set -x; $sh_c 'pip install git+https://github.com/treyhunner/django-simple-history.git@1.8.2#egg=django-simple-history' )
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing python-secretary"
                echo "";
                ( set -x; $sh_c 'pip install secretary==0.2.14' )
                cecho y "Installing python-ajax-select (available version in Debian is not compatible with backported Django)"
                echo "";
                ( set -x; $sh_c 'pip install django-ajax-selects==1.4.3' )
                cecho y "Installing weasyprint"
                echo "";
                ( set -x; $sh_c 'pip install WeasyPrint==0.41' )
                cecho y "Installing django-formtools"
                echo "";
                ( set -x; $sh_c 'pip install django-formtools==2.1' )
                cecho y "Installing django-compressor"
                echo "";
                ( set -x; $sh_c 'pip install django-compressor==2.1' )

            fi

            if [ "$dist_version" == "stretch" ]; then
                if [ "$backports_activated" != 'true' ]; then
                    echo ""
                    cecho r "  In order to install Ishtar you have to activate Debian backports."
                    echo "  To do that:"
                    echo ""
                    echo "    echo 'deb http://ftp.debian.org/debian stretch-backports main contrib' >> /etc/apt/sources.list"
                    echo ""
                    cecho p "  Run again Ishtar installation script after that."
                    exit 1
                fi

                if [ "$default_db" == '127.0.0.1' ]; then
                    echo "-------------------------------------------------------------------------------";
                    cecho y "Installing postgresql"
                    echo ""
                    POSTGIS=postgresql-9.6-postgis-2.3
                    ( set -x; $sh_c 'sleep 3; apt-get install -y -q postgresql '$POSTGIS )
                fi
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing Ishtar dependencies"
                echo "";
                ( set -x; $sh_c 'sleep 3; apt-get install -t stretch-backports -y -q  python-django' )
                ( set -x; $sh_c 'sleep 3; apt-get install -y -q \
                      python-django-registration libpangocairo-1.0-0 \
                      python-bs4 python-cffi python-django-compressor \
                      python-tidylib python-lxml python-imaging python-html5lib \
                      python-psycopg2 python-gdal gettext python-unicodecsv memcached \
                      python-django-extra-views python-memcache python-dbf python-markdown \
                      python-reportlab django-ajax-selects python-django-extensions' )
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing django-simple-history"
                echo "";
                ( set -x; $sh_c 'pip install git+https://github.com/treyhunner/django-simple-history.git@1.8.2#egg=django-simple-history' )
                echo "-------------------------------------------------------------------------------";
                cecho y "Installing python-secretary"
                echo "";
                ( set -x; $sh_c 'pip install secretary==0.2.14' )
                cecho y "Installing weasyprint"
                echo "";
                ( set -x; $sh_c 'pip install WeasyPrint==0.41' )
                cecho y "Installing django-formtools - Debian version is too old..."
                echo "";
                ( set -x; $sh_c 'pip install django-formtools==2.1' )
            fi
            ;;

    esac
    echo "-------------------------------------------------------------------------------";
    cecho y "Installing Ishtar sources"
    echo "";

    cd $install_path
    if [ "$dist_version" == "wheezy" ]; then
        ( set -x; git clone https://nimn@gitlab.com/iggdrasil/oook_replace.git 2> /dev/null )
    fi
    ( set -x; git clone https://gitlab.com/iggdrasil/ishtar.git 2> /dev/null )
    cd ishtar
    git fetch 2> /dev/null
    git checkout $version 2> /dev/null

    mkdir -p $etc_path
    echo "ISHTAR_PATH="$full_install_path > $etc_path"config"
    echo "ISHTAR_DB="$default_db >> $etc_path"config"
    echo "ISHTAR_WEBSERVER="$webserver >> $etc_path"config"
    echo "# settings added to all instances" >> $etc_path"extra_settings.py"
    echo ""
    cecho g "*******************************************************************************"
    echo "";
    cecho g "Installation done."
    cecho g "Base configuration stored in: "$etc_path"config"
    echo ""
    echo "You can edit "$etc_path"extra_settings.py to add Django settings to all new"
    echo "instances.";
    echo "";
    cecho y "Next you will have to create an instance with: ./ishtar-prepare-instance"
    echo "";

}

do_install