Skip to content

Instantly share code, notes, and snippets.

@mAster-rAdio
Last active November 1, 2019 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mAster-rAdio/53a60e1fd3b2448b44ac949d41392f43 to your computer and use it in GitHub Desktop.
Save mAster-rAdio/53a60e1fd3b2448b44ac949d41392f43 to your computer and use it in GitHub Desktop.
Install anyenv + pyenv + direnv, and certbot by using them, based on CentOS/RHEL.
#!/bin/bash
########################################
# functions
## function: add strings into file unress 'any-words'
addstr_unless () {
_subject="${1}"
_add_string="${2}"
_target_file="${3}"
test "${#}" -ne 3 && return 1
grep -Ev "^[[:blank:]]*[#]" "${_target_file}" 2> /dev/null | grep -Eq "${_subject}" || echo "${_add_string}" >> "${_target_file}"
}
## function: git-update
git-update () {
_uri="${1}"
_dir="${2}"; test -z "${_dir}" && _dir=`pwd`
test "${#}" -lt 1 && return 1
if [ -f "${_dir}/.git/index" ]; then
( cd "${_dir}"; git pull )
else
git clone "${_uri}" "${_dir}"
fi
}
########################################
########################################
# backup current & initial general setting
## backup
/bin/cp -a "${HOME}/.bash_profile" "${HOME}/.bash_profile.`date +'%Y%m%d-%H%M%S'`.bak"
/bin/cp -a "${HOME}/.bashrc" "${HOME}/.bashrc.`date +'%Y%m%d-%H%M%S'`.bak"
## read .bashrc
addstr_unless '\.bashrc' 'test -f "${HOME}/.bashrc" && . "${HOME}/.bashrc"' "${HOME}/.bash_profile"
## add function to .bashrc
path_merge=`cat << 'EOM'
## function: merge dir-path
path_merge () {
pathes=""
while [ -n "${1}" ]; do
_add=`printf "${1}" | sed -E -e 's%/+$%%'`
test -n "${pathes}" && pathes="${pathes}:"
pathes="${pathes}${_add}"
shift
done
pathes=`printf "${pathes}" | tr ':' '\n'`
_PATH=""
while [ -n "${pathes}" ]; do
_dir=`printf "${pathes}" | head -1 | sed -E -e 's%/+$%%'`
test -n "${_PATH}" && _PATH="${_PATH}:"
_PATH="${_PATH}${_dir}"
pathes=`printf "${pathes}" | grep -Ev "^${_dir}$" | grep -Ev "^$"`
done
printf "${_PATH}"; unset _PATH
}
EOM
`
addstr_unless 'path_merge \(\)' "${path_merge}" "${HOME}/.bashrc"
########################################
# install: direnv (not supported by anyenv)
cat << 'EOM' > "${HOME}/.direnv.bash_profile"
export PATH=`path_merge "${PATH}" "${HOME}/bin"`
direnv-update () {
direnv_latest=`curl -kLRs https://api.github.com/repos/direnv/direnv/releases/latest | grep browser_download_url | grep linux-amd64 | awk '{print($NF);}' | tr -d '"'`
test -d "${HOME}/bin" || mkdir "${HOME}/bin"
if [ -f "${HOME}/bin/direnv" ]; then
direnv_bak="${HOME}/bin/direnv.`date +'%Y%m%d-%H%M%S'`.bak"
mv "${HOME}/bin/direnv" "${direnv_bak}"
chmod -x "${direnv_bak}"
fi
curl -kRLs "${direnv_latest}" -o "${HOME}/bin/direnv" && chmod +x "${HOME}/bin/direnv"
diff -q "${HOME}/bin/direnv" "${direnv_bak}" &> /dev/null && /bin/rm -f "${direnv_bak}" 2> /dev/null
DIRENV_CONFIG=`direnv status | grep 'DIRENV_CONFIG' | cut -d' ' -f2-`
test -d "${DIRENV_CONFIG}" || mkdir -p "${DIRENV_CONFIG}"
}
if command -v direnv > /dev/null; then
eval "`direnv hook bash`"
fi
EOM
addstr_unless '\.direnv' 'test -f "${HOME}/.direnv.bash_profile" && . "${HOME}/.direnv.bash_profile"' "${HOME}/.bash_profile"
. "${HOME}/.bash_profile"
direnv-update
########################################
# install: anyenv
ANYENV_ROOT="${HOME}/.anyenv"
git-update https://github.com/anyenv/anyenv "${ANYENV_ROOT}"
git-update https://github.com/znz/anyenv-update "${ANYENV_ROOT}/plugins/anyenv-update"
git-update https://github.com/znz/anyenv-git "${ANYENV_ROOT}/plugins/anyenv-git"
${ANYENV_ROOT}/bin/anyenv init
cat << 'EOM' > "${HOME}/.anyenv.bash_profile"
export ANYENV_ROOT="${HOME}/.anyenv"
export PATH=`path_merge "${ANYENV_ROOT}/bin" "${PATH}"`
if command -v anyenv > /dev/null; then
eval "`anyenv init -`"
fi
EOM
addstr_unless '\.anyenv' 'test -f "${HOME}/.anyenv.bash_profile" && . "${HOME}/.anyenv.bash_profile"' "${HOME}/.bash_profile"
. "${HOME}/.bash_profile"
anyenv install --force-init
. "${HOME}/.bash_profile"
########################################
# anyenv install: pyenv
addstr_unless 'xxenv-latest' 'install_plugin xxenv-latest https://github.com/momo-lab/xxenv-latest.git' "${HOME}/.config/anyenv/anyenv-install/pyenv"
anyenv install pyenv
## pyenv setting: for direnv
### cf.) https://github.com/direnv/direnv/wiki/Python#pyenv
curl -kLRs https://raw.githubusercontent.com/wiki/direnv/direnv/Python.md | sed -n '/^##\s*pyenv\s*$/,/^```\s*$/p' | sed -n '/^```/,/^```/p' | sed '1d;$d' > "${HOME}/.config/direnv/pyenv.direnvrc"
addstr_unless 'pyenv' '. ${HOME}/.config/direnv/pyenv.direnvrc' "${HOME}/.config/direnv/direnvrc"
########################################
# enable: direnv & pyenv
. "${HOME}/.bash_profile"
#!/bin/bash
########################################
# setting: certbot (& python)
mkdir -p "${HOME}/local/certbot"
cd "${HOME}/local/certbot"
## install: python-latest (& dependency)
yum -y groupinstall "Development Tools"
yum -y install gcc gcc-c++ zlib zlib-devel libffi libffi-devel bzip2 bzip2-devel bzip2-libs readline readline-devel readline-static sqlite sqlite-devel sqlite-tcl
pyenv latest install
python_latest=`pyenv latest --print`
## install: pip & pipenv
pyenv shell "${python_latest}"
pyenv exec pip install --upgrade pip pipenv
## install: certbot
export WORKON_HOME="${PYENV_ROOT}/share/virtualenvs"
export PIPENV_VENV_IN_PROJECT=true
pyenv exec pipenv install --python="${python_latest}" --verbose certbot certbot-dns-route53 certbot-dns-sakuracloud
## setting: direnv: .envrc for certbot
addstr_unless '\.anyenv' 'test -f "${HOME}/.anyenv.bash_profile" && . "${HOME}/.anyenv.bash_profile"' ./.envrc
cat << EOM >> ./.envrc
export PIPENV_IGNORE_VIRTUALENVS=1
use pyenv "${python_latest}"
EOM
cat << 'EOM' >> ./.envrc
layout pipenv
export WORKON_HOME="${PYENV_ROOT}/share/virtualenvs"
export PIPENV_VENV_IN_PROJECT=true
if command -v pyenv > /dev/null; then if command -v pipenv > /dev/null; then
eval "`pyenv exec pipenv --completion`"
fi; fi
. .venv/bin/activate
EOM
direnv allow
########################################
cd "${HOME}"
pyenv shell system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment