aboutsummaryrefslogtreecommitdiffstats
path: root/bin/install-as-user
blob: 297efc1f394aaa09a221bfaec78452f8ab49570d (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
#!/bin/sh

set -e
error_msg() { printf "\033[31m%s\033[0m\n" "$*"; }
notice_msg() { printf "\033[33m%s\033[0m " "$*"; }
done_msg() { printf "\033[32m%s\033[0m\n" "$*"; }
DONE_MSG=$(done_msg done)

if [ $# -ne 3 ]
then
    cat >&2 <<EOUSAGE
Usage: $0 <UNIX-USER> <HOST> <INSTALLATION-DIRECTORY>
EOUSAGE
    exit 1
fi

UNIX_USER="$1"
HOST="$2"
DIRECTORY="$3"

misuse() {
  echo The variable $1 was not defined, and it should be.
  echo This script should not be run directly.
  exit 1
}

[ -z "$DEVELOPMENT_INSTALL" ] && misuse DEVELOPMENT_INSTALL

DB_NAME="fixmystreet"

# Check that the arguments we've been passed are sensible:

IP_ADDRESS_FOR_HOST="$(dig +short $HOST)"

if [ x = x"$IP_ADDRESS_FOR_HOST" ]
then
    error_msg "The hostname $HOST didn't resolve to an IP address"
    exit 1
fi

if ! id "$UNIX_USER" 2> /dev/null > /dev/null
then
    error_msg "The user '$UNIX_USER' didn't exist."
    exit 1
fi

if [ "$(whoami)" != "$UNIX_USER" ]
then
    error_msg "This script should be run by the user '$UNIX_USER'."
    exit 1
fi

REPOSITORY="$DIRECTORY/fixmystreet"
LINK_DESTINATION="$HOME/fixmystreet"

ln -sfn "$REPOSITORY" $LINK_DESTINATION
cd "$REPOSITORY"

if [ ! "$DEVELOPMENT_INSTALL" = true ]; then
    echo -n "Adding crontab... "
    # Add regularly scheduled tasks to cron:
    TEMPORARY_CRONTAB=$(mktemp)
    echo crontab file is $TEMPORARY_CRONTAB
    cp "$REPOSITORY"/conf/crontab-example "$TEMPORARY_CRONTAB"
    sed -i \
        -e 's,$FMS,'"$REPOSITORY,g" \
        -e 's,$LOCK_DIR,'"$DIRECTORY,g" \
        -e 's,$UNIX_USER,'"$UNIX_USER,g" \
        "$TEMPORARY_CRONTAB"
    crontab $TEMPORARY_CRONTAB
    echo $DONE_MSG
fi

# Install the compass gem locally - it's required for generating the
# CSS. Don't trust the bundled bundler in e.g. precise.
echo "Setting up CSS... "
gem1.9.1 install --user-install --no-ri --no-rdoc bundler
FMS_GEMPATH="$DIRECTORY/gem-bin"
FMS_GEMPATH="$FMS_GEMPATH:$(ruby1.9.1 -rubygems -e 'puts Gem.user_dir')/bin"
export PATH="$FMS_GEMPATH:$PATH"

if ! grep -q 'Set up local PATH for FixMyStreet' $HOME/.bashrc; then
    cat >>$HOME/.bashrc <<EOBRC

# Set up local PATH for FixMyStreet
export PATH="$FMS_GEMPATH:\$PATH"
EOBRC
fi

bundle install --deployment --path "$DIRECTORY/gems" --binstubs "$DIRECTORY/gem-bin"

# Use compass to generate the CSS, if it doesn't seem to already
# exist:
if [ ! -f web/cobrands/default/base.css ]
then
    bin/make_css
fi
echo $DONE_MSG

# Write sensible values into the config file, if it doesn't already exist
if [ ! -f conf/general.yml ]; then
    echo -n "Setting up default conf/general.yml file... "
    sed -r \
        -e "s,^( *FMS_DB_HOST:).*,\\1 ''," \
        -e "s,^( *FMS_DB_NAME:).*,\\1 '$DB_NAME'," \
        -e "s,^( *FMS_DB_USER:).*,\\1 '$UNIX_USER'," \
        -e "s,^( *BASE_URL:).*,\\1 'http://$HOST'," \
        -e "s,^( *EMAIL_DOMAIN:).*,\\1 '$HOST'," \
        -e "s,^( *CONTACT_EMAIL:).*,\\1 'help@$HOST'," \
        -e "s,^( *DO_NOT_REPLY_EMAIL:).*,\\1 'help@$HOST'," \
        conf/general.yml-example > conf/general.yml
    echo $DONE_MSG
fi

echo "Installing required Perl modules - this may take some time"
cd "$REPOSITORY"
bin/install_perl_modules
echo $DONE_MSG

# Create the database if it doesn't exist:
echo -n "Setting up database... "
if ! psql -l | egrep "^ *$DB_NAME *\|" > /dev/null
then
    createdb --owner "$UNIX_USER" "$DB_NAME"
    echo 'CREATE LANGUAGE plpgsql;' | psql -U "$UNIX_USER" "$DB_NAME" || true
    psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/schema.sql
    psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/alert_types.sql
    psql -U "$UNIX_USER" "$DB_NAME" < "$REPOSITORY"/db/generate_secret.sql
else
    bin/update-schema --commit
fi
echo $DONE_MSG

# Generate po and mo files (these invocations taken from Kagee's script):
echo "Creating locale .mo files"
commonlib/bin/gettext-makemo FixMyStreet
echo $DONE_MSG

bin/update-all-reports