aboutsummaryrefslogtreecommitdiffstats
path: root/install/deploy-helper.sh
blob: 487c4539c276176c4cab19dcffce1a569611c87a (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
#!/bin/bash -e

export BASEDIR=$PREFIX/var/www/stif-boiv

export RUN_USER=www-data
export RUN_GROUP=src

export SUDO=""

function setup() {
    mkdir -p $BASEDIR
    mkdir -p $BASEDIR/releases $BASEDIR/shared

    $SUDO mkdir -p $PREFIX/etc/stif-boiv
    ln -fs $PREFIX/etc/stif-boiv $BASEDIR/shared/config

    mkdir -p $BASEDIR/shared/config/environments

    mkdir -p $BASEDIR/shared/public
    mkdir -p $BASEDIR/shared/public/uploads
    mkdir -p $BASEDIR/shared/public/assets

    mkdir -p $BASEDIR/shared/tmp/uploads

    $SUDO chown $RUN_USER:$RUN_GROUP $BASEDIR/shared/public/uploads $BASEDIR/shared/tmp/uploads

    default_config
}

function default_config() {
    DATABASE_PASSWORD=${DATABASE_PASSWORD:-FIXME}
    DATABASE_HOST=${DATABASE_HOST:-"localhost"}

    cd $BASEDIR/shared/config

    if [ ! -f secrets.yml ]; then
        cat > secrets.yml <<EOF
production:
  secret_key_base: `echo $RANDOM | sha512sum | cut -f1 -d' '`
EOF
    fi

    if [ ! -f database.yml ]; then
        cat > database.yml <<EOF
production:
  adapter: postgresql
  encoding: unicode
  pool: 5

  host: $DATABASE_HOST
  database: stif-boiv

  username: stif-boiv
  password: $DATABASE_PASSWORD
EOF
    fi
}

function install() {
    tar_file=$1

    # stif-boiv-20160617154541.tar
    release_name=`echo $tar_file | sed 's/.*-\([0-9]*\)\.tar/\1/g'`

    RELEASE_PATH=$BASEDIR/releases/$release_name

    if [ -d $RELEASE_PATH ]; then
        echo "Release directory $RELEASE_PATH already exists"
        return
    fi

    mkdir -p $RELEASE_PATH

    tar -xf $tar_file -C $RELEASE_PATH

    cd $RELEASE_PATH

    mkdir -p tmp

    for directory in public/uploads tmp/uploads public/assets; do
        local_directory=$BASEDIR/shared/$directory
        release_directory=$directory

        rm -rf $release_directory
        ln -s $local_directory $release_directory
    done

    for file in secrets.yml database.yml environments/production.rb; do
        local_file=$BASEDIR/shared/config/$file
        release_file=config/$file

        rm $release_file && ln -fs $local_file $release_file
    done

    echo "Release installed into $RELEASE_PATH"
}

command=$1
shift

set -x
$command $@