Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 450 lines (353 sloc) 11.184 kb
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
#!/bin/bash

###
# MAKE ME BETTER
###

: '
see https://github.com/jamiew/git-friendly for more ideas

maybe this should be two files
one which includes cloning diaspora/diaspora, and one that assumes you already cloned it yourself
maybe one script just calls another?


other ideas what we could do

1. check that you have ruby installed, if not, point to wiki page and exit
2. check to see if we need sudo (generally, if it is a system ruby you need sudo, which you can check
if which ruby is /usr/bin/ruby, or does not have rvm in the path)
3. check if you have bundle installed and install it, and install with/without sudo if you need it

check if you have mysql and/or postgres installed, point to wiki page if neither is found.
(maybe even switch database.yml if this is the case?)

make it work if you have just cloned diapsora and want a quick setup, or
support magic install, like this http://docs.meteor.com/#quickstart
'

#### ####
# #
# DEFAULT VARS #
# #
#### ####

# required programs
declare -A BINARIES
BINARIES["git"]="git"
BINARIES["ruby"]="ruby"
BINARIES["rubygems"]="gem"
BINARIES["bundler"]="bundle"
BINARIES["sed"]="sed"
BINARIES["mktemp"]="mktemp"

D_GIT_CLONE_PATH="/srv/diaspora" # path for diaspora

D_REMOTE_REPO_URL="https://github.com/diaspora/diaspora.git"

D_INSTALL_SCRIPT_URL="https://raw.github.com/diaspora/diaspora/master/script/install.sh"

D_WIKI_URL="https://github.com/diaspora/diaspora/wiki"

D_IRC_URL="irc://freenode.net/diaspora"

D_DB="mysql"

D_DB_CONFIG_FILE="config/database.yml"

D_DB_HOST="localhost"

D_DB_USER="diaspora"

D_DB_PASS="diaspora"

# TODO: read this from ./script/env/ruby_env
D_RUBY_VERSION="1.9.3-p194"

#### INTERNAL VARS ####

RVM_DETECTED=false
JS_RUNTIME_DETECTED=false

#### ####
# #
# FUNCTIONS, etc. #
# #
#### ####

#... could be put in a separate file and sourced here

# heredoc for variables - very readable, http://stackoverflow.com/a/8088167
# use like this:
# define VAR <<'EOF'
# somecontent
# EOF
define(){ IFS='\n' read -r -d '' ${1}; }

# expand aliases in this script
shopt -s expand_aliases

# alias echo to alway print \newlines
alias echo='echo -e'

# run a command or print the error
run_or_error() {
  eval "$1"
  if [ $? -ne 0 ]; then
    error "executing '$1' failed."
  fi
}

# nicely output error messages and quit
error() {
  echo "\n"
  echo "[ERROR] -- $1"
  echo " --"
  echo " -- have a look at our wiki: $D_WIKI_URL"
  echo " -- or join us on IRC: $D_IRC_URL"
  exit 1
}

# check for functions
fn_exists() {
  type -t $1 | grep -q 'function'
}

# shell interactive or not
interactive_check() {
  fd=0 #stdin
  if [[ -t "$fd" || -p /dev/stdin ]]; then
    # all is well
    echo ""
  else
    # non-interactive
    TMPFILE=`mktemp`
    curl -s -o "$TMPFILE" "$D_INSTALL_SCRIPT_URL"
    chmod +x "$TMPFILE"
    exec 0< /dev/tty
    bash -i "$TMPFILE"
    rm "$TMPFILE"
    exit 0
  fi
}

# check if all necessary binaries are available
binaries_check() {
  for exe in "${!BINARIES[@]}"; do
    echo -n "checking for $exe... "
    which "${BINARIES[$exe]}"
    if [ $? -ne 0 ]; then
      error "you are missing the '${BINARIES[$exe]}' command, please install '$exe'";
    fi
  done
  echo ""
}

# check for rvm
define RVM_MSG <<'EOT'
RVM was not found on your system (or it isn't working properly).
It is higly recommended to use it, since it's making it extremely easy
to install, manage and work with multiple ruby environments.

For more details check out https://rvm.io//
EOT
rvm_check() {
  echo -n "checking for rvm... "
  fn_exists rvm
  if [ $? -eq 0 ] ; then
    RVM_DETECTED=true

  # seems we don't have it loaded, try to do so
  elif [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
    source "$HOME/.rvm/scripts/rvm" >/dev/null 2>&1
    RVM_DETECTED=true
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
    source "/usr/local/rvm/scripts/rvm" >/dev/null 2>&1
    RVM_DETECTED=true
fi

  if $RVM_DETECTED ; then
    echo "found"
  else
    echo "not found"
    echo "$RVM_MSG"
    read -p "Press [Enter] to continue without RVM or abort this script and install RVM..."
  fi
  echo ""
}

# prepare ruby with rvm
install_or_use_ruby() {
  if ! $RVM_DETECTED ; then
    return
  fi

  # make sure we have the correct ruby version available
  echo -n "checking your ruby version... "
  rvm use $D_RUBY_VERSION >/dev/null 2>&1
  if [ $? -ne 0 ] ; then
    echo "not ok"
    rvm --force install $D_RUBY_VERSION
  else
    echo "ok"
  fi

  echo ""
}

# trust and load rvmrc
# do this in a directory that has a .rvmrc, only :)
load_rvmrc() {
  if ! $RVM_DETECTED || [[ ! -s ".rvmrc" ]] ; then
    return
  fi

  # trust rvmrc
  rvm rvmrc is_trusted
  if [ $? -ne 0 ] ; then
    rvm rvmrc trust
  fi

  # load .rvmrc
  echo -n "loading .rvmrc ... "
  source ".rvmrc"
  #rvm rvmrc load
  if [ $? -eq 0 ] ; then
    echo "ok"
  else
    echo "not ok"
  fi
  echo ""
}

# rvm doesn't need sudo, otherwise we do have to use it :(
rvm_or_sudo() {
  if $RVM_DETECTED ; then
    run_or_error "$1"
  else
    eval "$1"
    if [ $? -ne 0 ] ; then
      echo "\nrunning '$1' didn't succeed, trying again with sudo...\n"
      run_or_error "sudo $1"
    fi
  fi
}

# we need a valid js runtime...
define JS_RT_MSG <<'EOT'
This script was unable to find a JavaScript runtime compatible to ExecJS on
your system. We recommend you install either Node.js or TheRubyRacer, since
those have been proven to work.

Node.js -- http://nodejs.org/
TheRubyRacer -- https://github.com/cowboyd/therubyracer

For more information on ExecJS, visit
-- https://github.com/sstephenson/execjs
EOT
js_runtime_check() {
  echo -n "checking for a JavaScript runtime... "

  # Node.js
  which node >/dev/null 2>&1
  if [ $? -eq 0 ] ; then
    JS_RUNTIME_DETECTED=true
fi

  # TheRubyRacer
  (echo "require 'v8'" | ruby) >/dev/null 2>&1
  if [ $? -eq 0 ] ; then
    JS_RUNTIME_DETECTED=true
fi

  ##
  # add a check for your favourite js runtime here...
  ##

  if $JS_RUNTIME_DETECTED ; then
    echo "ok"
  else
    echo "not ok"
    echo "$JS_RT_MSG"
    error "can't continue without a JS runtime"
  fi
  echo ""
}

# make ourselves comfy
prepare_install_env() {
  install_or_use_ruby
  load_rvmrc
  js_runtime_check

  rvm_or_sudo "gem install bundler"
}

# do some sanity checking
sane_environment_check() {
  binaries_check
  rvm_check
}

# find or set up a working git environment
git_stuff_check() {
  echo "Where would you like to put the git clone, or, where is your existing git clone?"
  echo "(please use a full path, not '~' or '.')"
  read -e -p "-> " D_GIT_CLONE_PATH
  echo ""

  test -d "$D_GIT_CLONE_PATH" \
    && cd "$D_GIT_CLONE_PATH" \
    && git status # folder exists? go there. is a good git clone?
  if [ $? -ne 0 ]; then
    # not a git repo, create it?
    echo "the folder you specified does not exist or doesn't contain a git repo"
    read -p "Press [Enter] to create it... "
    run_or_error "mkdir -p -v \"$D_GIT_CLONE_PATH\"" # only if it doesn't exist
    run_or_error "git clone \"$D_REMOTE_REPO_URL\" \"$D_GIT_CLONE_PATH\""
  else
    run_or_error "git checkout master"
    run_or_error "git pull"
  fi
  echo ""
}

# handle database decision
database_question() {
  echo "Which database type are you using?"
  select choice in "MySQL" "PgSQL"; do
    case $choice in
      MySQL )
        D_DB="mysql"
        # we're done, mysql is default
        break
        ;;
      PgSQL )
        D_DB="postgres"
        # replace default with postgres
        run_or_error "sed -i'' -e 's/\(<<: \*mysql\)/#\1/g' \"$D_DB_CONFIG_FILE\""
        run_or_error "sed -i'' -e 's/\(#\(<<: \*postgres\)\)/\2/g' \"$D_DB_CONFIG_FILE\""
        break
        ;;
    esac
  done
}

# ask for database credentials
database_credentials() {
  read -e -p "hostname: " D_DB_HOST
  read -e -p "username: " D_DB_USER
  read -e -p "password: " D_DB_PASS

  run_or_error "sed -i'' -e \"s/\(host:\)[^\n]*/\1 $D_DB_HOST/g\" \"$D_DB_CONFIG_FILE\""
  run_or_error "sed -i'' -e \"s/\(username:\)[^\n]*/\1 $D_DB_USER/g\" \"$D_DB_CONFIG_FILE\""
  run_or_error "sed -i'' -e \"s/\(password:\)[^\n]*/\1 $D_DB_PASS/g\" \"$D_DB_CONFIG_FILE\""
}

# setup database
# (assume we are in the Diaspora directory)
database_setup() {
  echo "Database setup"
  run_or_error "cp config/database.yml.example \"$D_DB_CONFIG_FILE\""
  database_question
  database_credentials
  echo ""
}

# install all the gems with bundler
# (assume we are in the Diaspora directory)
prepare_gem_bundle() {
  echo "installing all required gems..."
  rvm_or_sudo "bundle install"
  echo ""
}


#### ####
# #
# START #
# #
#### ####

#interactive_check


# display a nice welcome message
define WELCOME_MSG <<'EOT'
#####################################################################

DIASPORA* INSTALL SCRIPT

----

This script will guide you through the basic steps
to get a DEVELOPMENT setup of Diaspora* up and running

For a PRODUCTION installation, please do *not* use this script!
Follow the guide in our wiki, instead:
-- https://github.com/diaspora/diaspora/wiki/Installation-Guides

#####################################################################

EOT
echo "$WELCOME_MSG"
read -p "Press [Enter] to continue... "


# check if we have everything we need
sane_environment_check


# check git stuff and pull if necessary
git_stuff_check


# goto working directory
run_or_error "cd \"$D_GIT_CLONE_PATH\""
prepare_install_env


# configure database setup
database_setup


echo "copying diaspora.yml.example to diaspora.yml"
run_or_error "cp config/diaspora.yml.example config/diaspora.yml"
echo ""


# bundle gems
prepare_gem_bundle


echo "creating the default database specified in config/database.yml. please wait..."
run_or_error "bundle exec rake db:schema:load_if_ruby --trace"
echo ""

define GOODBYE_MSG <<EOT
#####################################################################

It worked! :)

Now, you should have a look at

- config/database.yml and
- config/diaspora.yml

and change them to your liking. Then you should be able to
start Diaspora* in development mode with:

`rails s`


For further information read the wiki at $D_WIKI_URL
or join us on IRC $D_IRC_URL

EOT
echo "$GOODBYE_MSG"


exit 0
Something went wrong with that request. Please try again.