refactor install script

the script is now split up in parts that will be fetched from github
on demand.
fixes #4047
This commit is contained in:
Florian Staudacher 2013-04-08 17:28:42 +02:00
parent ea792246d0
commit 4005cd05ea
7 changed files with 590 additions and 455 deletions

View file

@ -106,6 +106,7 @@ everything is set up.
* Delete unnecessary javascript views. [#4059](https://github.com/diaspora/diaspora/pull/4059) * Delete unnecessary javascript views. [#4059](https://github.com/diaspora/diaspora/pull/4059)
* Cleanup of script/server * Cleanup of script/server
* Attempt to stabilize federation of attached photos (fix [#3033](https://github.com/diaspora/diaspora/issues/3033) [#3940](https://github.com/diaspora/diaspora/pull/3940) * Attempt to stabilize federation of attached photos (fix [#3033](https://github.com/diaspora/diaspora/issues/3033) [#3940](https://github.com/diaspora/diaspora/pull/3940)
* Refactor develop install script [#4111](https://github.com/diaspora/diaspora/pull/4111)
## Bug fixes ## Bug fixes

View file

@ -1,210 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env 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 # # minimal required functions to load the rest... #
# # # #
#### #### #### ####
# 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 # ... let's hope nobody hijacks githubs DNS while this runs :P
D_REMOTE_BASE_URL="https://raw.github.com/diaspora/diaspora/develop/"
D_REMOTE_REPO_URL="https://github.com/diaspora/diaspora.git" # ruby environment
D_REMOTE_ENV_PATH="script/env/ruby_env"
D_INSTALL_SCRIPT_URL="https://raw.github.com/diaspora/diaspora/master/script/install.sh" # installer files
D_INSTALL_SCRIPT="script/install.sh"
D_INSTALL_DEFAULTS_PATH="script/install/defaults"
D_INSTALL_REMOTE_VAR_READER_PATH="script/install/remote_var_reader"
D_INSTALL_PATH_SANITIZER_PATH="script/install/path_sanitizer"
D_INSTALL_FUNCTIONS_PATH="script/install/functions"
D_INSTALL_SETUP_PATH="script/install/setup"
D_WIKI_URL="https://github.com/diaspora/diaspora/wiki" # fetch a remote script containing functions and eval them into the local env
include_remote() {
_remote_include=$1
__TMP=$(curl -L $_remote_include)
eval "$__TMP"
}
D_IRC_URL="irc://freenode.net/diaspora"
D_DB="mysql" include_remote "$D_REMOTE_BASE_URL$D_INSTALL_DEFAULTS_PATH"
include_remote "$D_REMOTE_BASE_URL$D_INSTALL_REMOTE_VAR_READER_PATH"
include_remote "$D_REMOTE_BASE_URL$D_INSTALL_PATH_SANITIZER_PATH"
include_remote "$D_REMOTE_BASE_URL$D_INSTALL_FUNCTIONS_PATH"
include_remote "$D_REMOTE_BASE_URL$D_INSTALL_SETUP_PATH"
D_DB_CONFIG_FILE="config/database.yml" read_var_remote "ruby_version" "D_RUBY_VERSION"
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
ONE_UP="\e[1A"
#### #### #### ####
# # # #
# FUNCTIONS, etc. # # define some overly long message strings here... #
# # # #
#### #### #### ####
#... 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}; }
# add padding to the left of a given string to
# fill to a given amount of characters with a
# given char or space
# example:
# lpad 7 "test" "-"
lpad() {
LEN=$1
TXT=$2
CHR=$3
PAD=""
L_PAD=$(($LEN - ${#TXT}))
if [ $L_PAD -ne 0 ] ; then
PAD=$(printf "%*s" ${L_PAD} " ")
fi
if [ ${#CHR} -ne 0 ] ; then
PAD=$(printf "$PAD" | tr " " "$CHR")
fi
PAD="${PAD}${TXT}"
printf "%s" "$PAD"
}
# log function
# prints a given message with the given log level to STDOUT
logf() {
MSG=$1
LVL=$2
L_LEN=7
if [ ${#LVL} -ne 0 ] ; then
LVL="[$(lpad $(($L_LEN-2)) $LVL " ")]"
else
LVL=$(lpad $L_LEN "" "-")
fi
printf "%s -- %s\\n" "$LVL" "$MSG"
}
# short functions for various log levels
log_err() {
logf "$1" "error"
}
log_wrn() {
logf "$1" "warn"
}
log_dbg() {
logf "$1" "debug"
}
log_inf() {
logf "$1" "info"
}
# 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() {
log_err "$1"
logf "have a look at our wiki: $D_WIKI_URL"
logf "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
printf "\n"
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 this script is run as root
root_check() {
if [ `id -u` -eq 0 ] ; then
error "don't run this script as root!"
fi
}
# check if all necessary binaries are available
binaries_check() {
for exe in "${!BINARIES[@]}"; do
LOG_MSG="checking for $exe... "
log_inf "$LOG_MSG"
EXE_PATH=$(which "${BINARIES[$exe]}")
if [ $? -ne 0 ]; then
error "you are missing the '${BINARIES[$exe]}' command, please install '$exe'";
else
printf "$ONE_UP"
log_inf "$LOG_MSG found"
fi
done
printf "\n"
}
# check for rvm
define RVM_MSG <<'EOT' define RVM_MSG <<'EOT'
RVM was not found on your system (or it isn't working properly). 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 It is higly recommended to use it, since it's making it extremely easy
@ -212,98 +51,8 @@ to install, manage and work with multiple ruby environments.
For more details check out https://rvm.io// For more details check out https://rvm.io//
EOT EOT
rvm_check() {
LOG_MSG="checking for rvm... "
log_inf "$LOG_MSG"
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
printf "$ONE_UP"
log_inf "$LOG_MSG found"
else
log_wrn "not found"
logf "$RVM_MSG"
read -p "Press [Enter] to continue without RVM or abort this script and install RVM..."
fi
printf "\n"
}
# prepare ruby with rvm
install_or_use_ruby() {
if ! $RVM_DETECTED ; then
return
fi
# make sure we have the correct ruby version available
LOG_MSG="checking your ruby version... "
log_inf "$LOG_MSG"
rvm use $D_RUBY_VERSION >/dev/null 2>&1
if [ $? -ne 0 ] ; then
log_wrn "not ok"
rvm --force install $D_RUBY_VERSION
else
printf "$ONE_UP"
log_inf "$LOG_MSG ok"
fi
printf "\n"
}
# 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
LOG_MSG="loading .rvmrc ... "
log_inf "$LOG_MSG"
. ".rvmrc"
#rvm rvmrc load
if [ $? -eq 0 ] ; then
printf "$ONE_UP"
log_inf "$LOG_MSG ok"
else
log_wrn "not ok"
fi
printf "\n"
}
# 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
log_wrn "running '$1' didn't succeed, trying again with sudo..."
run_or_error "sudo $1"
fi
fi
}
# we need a valid js runtime...
define JS_RT_MSG <<'EOT' define JS_RT_MSG <<'EOT'
This script was unable to find a JavaScript runtime compatible to ExecJS on 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 your system. We recommend you install either Node.js or TheRubyRacer, since
@ -315,154 +64,16 @@ TheRubyRacer -- https://github.com/cowboyd/therubyracer
For more information on ExecJS, visit For more information on ExecJS, visit
-- https://github.com/sstephenson/execjs -- https://github.com/sstephenson/execjs
EOT EOT
js_runtime_check() {
LOG_MSG="checking for a JavaScript runtime... "
log_inf "$LOG_MSG"
# Node.js
which node >/dev/null 2>&1
if [ $? -eq 0 ] ; then
JS_RUNTIME_DETECTED=true
fi
# TheRubyRacer
(printf "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
printf "$ONE_UP"
log_inf "$LOG_MSG found"
else
log_err "not ok"
printf "$JS_RT_MSG"
error "can't continue without a JS runtime"
fi
printf "\n"
}
# make ourselves comfy
prepare_install_env() {
install_or_use_ruby
load_rvmrc
js_runtime_check
log_inf "making sure the 'bundler' gem is installed"
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() {
printf "Where would you like to put the git clone, or, where is your existing git clone?\n"
printf "(please use a full path, not '~' or '.')\n"
read -e -p "-> " D_GIT_CLONE_PATH
printf "\n"
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?
printf "the folder you specified does not exist or doesn't contain a git repo\n"
read -p "Press [Enter] to create it and continue... "
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
printf "\n"
}
# handle database decision
database_question() {
printf "Which database type are you using? [1|2]\n"
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
printf "\n"
}
# ask for database credentials
database_credentials() {
printf "Please specify the database credentials\n(the user must be existent and allowed to create new databases)\n"
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\""
printf "\n"
}
# setup database
# (assume we are in the Diaspora directory)
define DATABASE_CHK_MSG << 'EOT' define DATABASE_CHK_MSG << 'EOT'
you can now check the generated database config file in './config/database.yml' You can now check the generated database config file in './config/database.yml'
and see if the specified values are correct. and see if the specified values are correct.
Please make sure the database server is started and the credentials you
specified are working. This script will populate the database in a later step.
EOT EOT
database_setup() {
log_inf "Database setup"
run_or_error "cp config/database.yml.example \"$D_DB_CONFIG_FILE\""
printf "\n"
database_question
database_credentials
printf "$DATABASE_CHK_MSG"
read -p "Press [Enter] to continue... "
printf "\n"
}
# install all the gems with bundler
# (assume we are in the Diaspora directory)
prepare_gem_bundle() {
log_inf "installing all required gems..."
rvm_or_sudo "bundle install"
printf "\n"
}
#### ####
# #
# START #
# #
#### ####
#interactive_check
root_check
# display a nice welcome message
define WELCOME_MSG <<'EOT' define WELCOME_MSG <<'EOT'
##################################################################### #####################################################################
@ -480,40 +91,6 @@ Follow the guide in our wiki, instead:
##################################################################### #####################################################################
EOT EOT
printf "$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
# diaspora config
log_inf "copying diaspora.yml.example to diaspora.yml"
run_or_error "cp config/diaspora.yml.example config/diaspora.yml"
printf "\n"
# bundle gems
prepare_gem_bundle
log_inf "creating the default database specified in config/database.yml. please wait..."
run_or_error "bundle exec rake db:schema:load_if_ruby --trace"
printf "\n"
define GOODBYE_MSG <<EOT define GOODBYE_MSG <<EOT
##################################################################### #####################################################################
@ -528,15 +105,19 @@ Now, you should have a look at
and change them to your liking. Then you should be able to and change them to your liking. Then you should be able to
start Diaspora* in development mode with: start Diaspora* in development mode with:
`rails s` \`rails s\`
For further information read the wiki at $D_WIKI_URL For further information read the wiki at $D_WIKI_URL
or join us on IRC $D_IRC_URL or join us on IRC $D_IRC_URL
EOT EOT
printf "$GOODBYE_MSG"
exit 0 #### ####
# #
# do it! #
# #
#### ####
diaspora_setup

51
script/install/defaults Normal file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env bash
# to be included by install.sh
#### ####
# #
# DEFAULT VARS #
# #
#### ####
# list of required programs
declare -A BINARIES
BINARIES["git"]="git"
BINARIES["ruby"]="ruby"
BINARIES["rubygems"]="gem"
BINARIES["bundler"]="bundle"
BINARIES["sed"]="sed"
BINARIES["mktemp"]="mktemp"
BINARIES["dirname"]="dirname"
BINARIES["basename"]="basename"
BINARIES["realpath"]="realpath"
# local path for git clone, can be changed by the user interactively
D_GIT_CLONE_PATH="/srv/diaspora"
D_GIT_BRANCH="develop"
# a few important web addresses
D_REMOTE_REPO_URL="https://github.com/diaspora/diaspora.git"
D_WIKI_URL="https://github.com/diaspora/diaspora/wiki"
# irc url
D_IRC_URL="irc://freenode.net/diaspora"
# database config, can be changed by the user interactively
D_DB_CONFIG_FILE="config/database.yml"
D_DB="mysql"
D_DB_HOST="localhost"
D_DB_USER="diaspora"
D_DB_PASS="diaspora"
#### ####
# #
# INTERNAL VARS #
# #
#### ####
D_INSTALL_SCRIPT_URL="$D_REMOTE_BASE_URL$D_INSTALL_SCRIPT"
RVM_DETECTED=false
JS_RUNTIME_DETECTED=false
ONE_UP="\e[1A"

272
script/install/functions Normal file
View file

@ -0,0 +1,272 @@
#!/usr/bin/env bash
# to be included by install.sh
#### ####
# #
# FUNCTIONS, etc. #
# #
#### ####
# 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}; }
# add padding to the left of a given string to
# fill to a given amount of characters with a
# given char or space
# example:
# lpad 7 "test" "-"
lpad() {
LEN=$1
TXT=$2
CHR=$3
PAD=""
L_PAD=$(($LEN - ${#TXT}))
if [ $L_PAD -ne 0 ] ; then
PAD=$(printf "%*s" ${L_PAD} " ")
fi
if [ ${#CHR} -ne 0 ] ; then
PAD=$(printf "$PAD" | tr " " "$CHR")
fi
PAD="${PAD}${TXT}"
printf "%s" "$PAD"
}
# log function
# prints a given message with the given log level to STDOUT
logf() {
MSG=$1
LVL=$2
L_LEN=7
if [ ${#LVL} -ne 0 ] ; then
LVL="[$(lpad $(($L_LEN-2)) $LVL " ")]"
else
LVL=$(lpad $L_LEN "" "-")
fi
printf "%s -- %s\\n" "$LVL" "$MSG"
}
# short functions for various log levels
log_err() {
logf "$1" "error"
}
log_wrn() {
logf "$1" "warn"
}
log_dbg() {
logf "$1" "debug"
}
log_inf() {
logf "$1" "info"
}
# 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() {
log_err "$1"
logf "have a look at our wiki: $D_WIKI_URL"
logf "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
printf "\n"
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 this script is run as root
root_check() {
if [ `id -u` -eq 0 ] ; then
error "don't run this script as root!"
fi
}
# check if all necessary binaries are available
binaries_check() {
for exe in "${!BINARIES[@]}"; do
LOG_MSG="checking for $exe... "
log_inf "$LOG_MSG"
EXE_PATH=$(which "${BINARIES[$exe]}")
if [ $? -ne 0 ]; then
error "you are missing the '${BINARIES[$exe]}' command, please install '$exe'";
else
printf "$ONE_UP"
log_inf "$LOG_MSG found"
fi
done
printf "\n"
}
# check for rvm
rvm_check() {
LOG_MSG="checking for rvm... "
log_inf "$LOG_MSG"
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
printf "$ONE_UP"
log_inf "$LOG_MSG found"
else
log_wrn "not found"
logf "$RVM_MSG"
read -p "Press [Enter] to continue without RVM or abort this script and install RVM..."
fi
printf "\n"
}
# prepare ruby with rvm
install_or_use_ruby() {
if ! $RVM_DETECTED ; then
return
fi
# make sure we have the correct ruby version available
LOG_MSG="checking your ruby version... "
log_inf "$LOG_MSG"
rvm use $D_RUBY_VERSION >/dev/null 2>&1
if [ $? -ne 0 ] ; then
log_wrn "not ok"
rvm --force install $D_RUBY_VERSION
else
printf "$ONE_UP"
log_inf "$LOG_MSG ok"
fi
printf "\n"
}
# 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
LOG_MSG="loading .rvmrc ... "
log_inf "$LOG_MSG"
. ".rvmrc"
#rvm rvmrc load
if [ $? -eq 0 ] ; then
printf "$ONE_UP"
log_inf "$LOG_MSG ok"
else
log_wrn "not ok"
fi
printf "\n"
}
# 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
log_wrn "running '$1' didn't succeed, trying again with sudo..."
run_or_error "sudo $1"
fi
fi
}
# we need a valid js runtime...
js_runtime_check() {
LOG_MSG="checking for a JavaScript runtime... "
log_inf "$LOG_MSG"
# Node.js
which node >/dev/null 2>&1
if [ $? -eq 0 ] ; then
JS_RUNTIME_DETECTED=true
fi
# TheRubyRacer
(printf "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
printf "$ONE_UP"
log_inf "$LOG_MSG found"
else
log_err "not ok"
printf "$JS_RT_MSG"
error "can't continue without a JS runtime"
fi
printf "\n"
}

View file

@ -0,0 +1,40 @@
#!/usr/bin/env bash
# to be included by install.sh
# Deconstruct a given path string, applies bash expansion and remove all
# remaining relative fragments (e.g. "." or "..").
# Writes the result in the two given variable names, first is the portion
# with the existing path and the second contains the structure relative to the
# existing path, that'd have to be created.
# usage:
# sanitize_path "~/some/path/string" "EXISTING_VAR_NAME" "REL_NEW_PATH_NAME"
sanitize_path() {
# apply bash expansion
eval _path=$1
_existing_path_var=$2
_rel_new_segment_var=$3
_new_segment=""
_chk=1
_test_cmd='test -d "$_path" -a -n "$_path"'
$(eval $_test_cmd) && _chk=0
while [ $_chk -ne 0 ] ; do
# path doesn't exist, split it up
_segment="$(basename $_path)/$_segment"
_path="$(dirname $_path)"
$(eval $_test_cmd) && _chk=0
done
# remove relative fragments
_path="$(realpath $_path)/"
log_dbg "pt1 - existing path: $_path"
log_dbg "pt2 - new path: $_segment"
eval "$_existing_path_var=\"$_path\""
eval "$_rel_new_segment_var=\"$_segment\""
}

View file

@ -0,0 +1,13 @@
#!/usr/bin/env bash
# to be included by install.sh
# read a variable defined in the remote repository
# usage:
# read_remote_var "name_in_remote_script" "name_we_want_locally"
read_var_remote() {
_remote_name=$1
_local_name=$2
_remote_source="$D_REMOTE_BASE_URL$D_REMOTE_ENV_PATH"
eval "$_local_name=\"$(include_remote $_remote_source; echo ${!_remote_name})\""
}

177
script/install/setup Normal file
View file

@ -0,0 +1,177 @@
#!/usr/bin/env bash
# to be included by install.sh
#### ####
# #
# DIASPORA* development setup #
# #
#### ####
# make ourselves comfy
prepare_install_env() {
install_or_use_ruby
load_rvmrc
js_runtime_check
log_inf "making sure the 'bundler' gem is installed"
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() {
printf "Where would you like to put the git clone, or, where is your existing git clone?\n"
#printf "(please use a full path, not '~' or '.')\n"
read -e -p "-> " D_GIT_CLONE_PATH
printf "\n"
sanitize_path "$D_GIT_CLONE_PATH" "_D_GCLONE_PATH_EXISTING" "_D_GCLONE_PATH_NEW"
D_GIT_CLONE_PATH="$_D_GCLONE_PATH_EXISTING$_D_GCLONE_PATH_NEW"
if [ -n "$_D_GCLONE_PATH_NEW" ] ; then
# the path obviously doesn't exist yet
printf "the folder you specified does not exist.\n"
printf "create '$D_GIT_CLONE_PATH'?\n"
read -p "Press [Enter] to create it and continue... "
log_inf "creating '$D_GIT_CLONE_PATH' and cloning the git repo..."
run_or_error "mkdir -p -v \"$D_GIT_CLONE_PATH\""
_git_clone_diaspora_repo
elif ! (cd "$D_GIT_CLONE_PATH" && git status) ; then
# the path doesn't appear to contain a git clone
printf "the folder you specified does not contain a git repo\n"
read -p "Press [Enter] to create it and continue... "
log_inf "cloning the git repo..."
_git_clone_diaspora_repo
else
cd "$D_GIT_CLONE_PATH"
log_inf "setting your git clone to '$D_GIT_BRANCH' branch..."
run_or_error "git stash"
run_or_error "git checkout \"$D_GIT_BRANCH\""
run_or_error "git pull"
fi
printf "\n"
}
_git_clone_diaspora_repo() {
run_or_error "git clone \"$D_REMOTE_REPO_URL\" -b \"$D_GIT_BRANCH\" \"$D_GIT_CLONE_PATH\""
}
# handle database decision
database_question() {
printf "Which database type are you using? [1|2]\n"
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
printf "\n"
}
# ask for database credentials
database_credentials() {
printf "Please specify the database credentials\n(the user must be existent and allowed to create new databases)\n"
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\""
printf "\n"
}
# setup database
# (assume we are in the Diaspora directory)
database_setup() {
log_inf "Database setup"
run_or_error "cp config/database.yml.example \"$D_DB_CONFIG_FILE\""
printf "\n"
database_question
database_credentials
printf "$DATABASE_CHK_MSG"
read -p "Press [Enter] to continue... "
printf "\n"
}
# install all the gems with bundler
# (assume we are in the Diaspora directory)
prepare_gem_bundle() {
log_inf "installing all required gems..."
rvm_or_sudo "bundle install"
printf "\n"
}
# main setup function, entry point
# all other functions will be called from here
diaspora_setup() {
#interactive_check
root_check
# display a nice welcome message
printf "$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
# diaspora config
log_inf "copying diaspora.yml.example to diaspora.yml"
run_or_error "cp config/diaspora.yml.example config/diaspora.yml"
printf "\n"
# bundle gems
prepare_gem_bundle
log_inf "creating the default database specified in config/database.yml. please wait..."
run_or_error "bundle exec rake db:schema:load_if_ruby --trace"
printf "\n"
printf "$GOODBYE_MSG"
exit 0
}