Skip to content

Instantly share code, notes, and snippets.

@hiono
Last active November 10, 2021 03:57
Show Gist options
  • Save hiono/9636356 to your computer and use it in GitHub Desktop.
Save hiono/9636356 to your computer and use it in GitHub Desktop.
Shell Provisioner
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
# define checker
function _has_f () {
return 0;
}
# _is_true
# args: VARIABLE
# return: true OR false in VARIABLE
# # Usage:
# # -- cut start --
# _is_true VARIABLE
# # --- cut end ---
function _is_true () {
local val=$1
[[ "$val" == "true" ]] && return 0 || return 1
}
# _run
# args: commands
# return: command output
# environment variable: VERBOSE
# # Usage:
# # -- cut start --
# _run COMMAND [-OPTIONS] [ARGS..]
# # --- cut end ---
function _run () {
local scriptfile=$(mkfile script <<EOF
$@
EOF
)
chmod 0755 ${scriptfile}
if [[ -z ${VERBOSE} ]];then
${scriptfile} >/dev/null 2>&1
else
${scriptfile}
fi
[ $? -eq 0 ] && rm -f ${scriptfile}
}
# _run_sudo
# option: -a output_append_file
# args: commands
# return: command output
# environment variable: VERBOSE
# # Usage:
# # -- cut start --
# _run_sudo COMMAND [-OPTIONS] [ARGS..]
# # --- cut end ---
function _run_sudo () {
local append=""
if [[ $1 == '-a' ]];then
append=$2
shift 2
fi
local scriptfile=$(mkfile script <<EOF
$@
EOF
)
chmod 4755 ${scriptfile}
sudo chown root:root ${scriptfile}
if [[ x${append} != x"" ]];then
sudo ${scriptfile} | sudo tee -a ${append}
elif [[ -z ${VERBOSE} ]];then
sudo ${scriptfile} >/dev/null 2>&1
else
sudo ${scriptfile}
fi
[ $? -eq 0 ] && sudo rm -f ${scriptfile}
}
# replace
# arg1: sedscript_file
# arg2: input_file
# return: result of difference
# # Usage:
# # -- cut start --
# replace SEDSCRIPT input_file
# # --- cut end ---
function replace () {
local sedscript=$1
local inputfile=$2
local command="sed -i'.bak' -f ${sedscript} ${inputfile}"
_run "$command" && \
_run "diff -uwb ${inputfile}{.bak,}"
}
# replace_sudo
# arg1: sedscript_file
# arg2: input_file
# return: result of difference
# # Usage:
# # -- cut start --
# replace SEDSCRIPT input_file
# # --- cut end ---
function replace_sudo () {
local sedscript=$1
local inputfile=$2
local command="sed -i'.bak' -f ${sedscript} ${inputfile}"
_run "sudo $command" && \
_run "sudo diff -uwb ${inputfile}{.bak,}"
}
# repos_update
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# repos_update
# # --- cut end ---
function repos_update () {
log_action_begin_msg "repository update"
_run_sudo apt-get -q -y update
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# repos_upgrade
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# repos_upgrade
# # --- cut end ---
function repos_upgrade () {
log_action_begin_msg "repository upgrade"
_run_sudo apt-get -q -y upgrade
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# package_install
# arg: PACKAGE_NAME [PACKAGE_NAME...]
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# package_install PACKAGE_NAME [PACKAGE_NAME...]
# # --- cut end ---
function package_install () {
log_action_begin_msg "install $@"
_run_sudo apt-get -q -y install $@
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# package_remove
# arg: PACKAGE_NAME [PACKAGE_NAME...]
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# package_remove PACKAGE_NAME [PACKAGE_NAME...]
# # --- cut end ---
function package_remove () {
log_action_begin_msg "remove: $@"
_run_sudo apt-get -y remove $@
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# ppa_add
# arg: PPA
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# ppa_add PPA
# # --- cut end ---
function ppa_add () {
log_action_begin_msg "PPA add: $@"
_run_sudo add-apt-repository -y $@ && _run_sudo apt-get -q -y update
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# ppa_remove
# arg: PPA
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# ppa_remove PPA
# # --- cut end ---
function ppa_remove () {
log_action_begin_msg "PPA remove: $@"
_run_sudo add-apt-repository --remove $@
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# mkfile
# arg: See Uagse
# return: FILENAME
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/.functions)
# mkfile filename_hoge STRING_HOGE
# echo 123 | mkfile filename_hoge
# mkfile filename_hoge <<EOF
# ABCDEFG
# ...
# XYZ
# 01293456789
# EOF
# # --- cut end ---
function mkfile () {
local template=$1
local filename=$(mktemp -t ${template}_XXXXXXXX)
shift
# log_action_begin_msg "mkfile: $filename"
[[ ${1+_} || ! -t 0 ]] && printf '%s\n' "${*-$(<"/dev/fd/0")}" > ${filename}
echo "${filename}"
}
#!/usr/bin/env bash
##
# John C. Petrucci (http://johncpetrucci.com)
# 2013-10-19
# Allows your script to accept varying levels of verbosity flags and give appropriate feedback via file descriptors.
# Example usage: ./this [-v[v[v]]]
verbosity=2 #Start counting at 2 so that any increase to this will result in a minimum of file descriptor 3. You should leave this alone.
maxverbosity=5 #The highest verbosity we use / allow to be displayed. Feel free to adjust.
while getopts ":v" opt; do
case $opt in
v) (( verbosity=verbosity+1 ))
;;
esac
done
printf "%s %d\n" "Verbosity level set to:" "$verbosity"
for v in $(seq 3 $verbosity) #Start counting from 3 since 1 and 2 are standards (stdout/stderr).
do
(( "$v" <= "$maxverbosity" )) && echo This would display $v
(( "$v" <= "$maxverbosity" )) && eval exec "$v>&2" #Don't change anything higher than the maximum verbosity allowed.
done
for v in $(seq $(( verbosity+1 )) $maxverbosity ) #From the verbosity level one higher than requested, through the maximum;
do
(( "$v" > "2" )) && echo This would not display $v
(( "$v" > "2" )) && eval exec "$v>/dev/null" #Redirect these to bitbucket, provided that they don't match stdout and stderr.
done
# Some confirmations:
printf "%s\n" "This message is seen at verbosity level 3 and above." >&3
printf "%s\n" "This message is seen at verbosity level 4 and above." >&4
printf "%s\n" "This message is seen at verbosity level 5 and above." >&5

Shell Provisioning

Shell Provisioningを行うための一般化したtaskを定義していく

#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup anyenv
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/anyenv)
# function anyenv-command_install () {
# log_action_begin_msg "anyenv"
# anyenv_install
# anyenv_setup
# log_action_end_msg 0
# }
# anyenv-command_install
# AND additions
# rbenv_install
# pyenv_install
# plenv_install
# ndenv_install
# # -- cut end --
# anyenv_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# anyenv_install
# # --- cut end ---
function anyenv_install () {
log_action_cont_msg "Install anyenv"
[ ! -e $HOME/.anyenv ] && git clone https://github.com/riywo/anyenv $HOME/.anyenv && mkdir -p $HOME/.anyenv/plugins && git clone https://github.com/znz/anyenv-update.git $HOME/.anyenv/plugins/anyenv-update
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# anyenv_setup
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# anyenv_setup
# # --- cut end ---
function anyenv_setup () {
log_action_cont_msg "Setup anyenv"
[ ! -e $HOME/.bash_local.d ] && mkdir -p $HOME/.bash_local.d
if [ ! -e $HOME/.bash_local.d/anyenv ];then
cat > $HOME/.bash_local.d/anyenv <<\EOF
if [ -d $HOME/.anyenv ] ; then
addpath -p PATH $HOME/.anyenv/bin
eval "$(anyenv init -)"
fi
for D in `ls $HOME/.anyenv/envs`
do
addpath -p PATH $HOME/.anyenv/envs/$D/shims
done
EOF
fi
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# rbenv_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# rbenv_install
# # --- cut end ---
function rbenv_install () {
log_action_cont_msg "Install rbenv"
if [ -e $HOME/.bash_local.d/anyenv ];then
source $HOME/.bash_local.d/anyenv
anyenv install rbenv
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
else
log_failure_msg "failed"
fi
}
# pyenv_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# pyenv_install
# # --- cut end ---
function pyenv_install () {
log_action_cont_msg "Install pyenv"
if [ -e $HOME/.bash_local.d/anyenv ];then
source $HOME/.bash_local.d/anyenv
anyenv install pyenv
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
else
log_failure_msg "failed"
fi
}
# plenv_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# plenv_install
# # --- cut end ---
function plenv_install () {
log_action_cont_msg "Install plenv"
if [ -e $HOME/.bash_local.d/anyenv ];then
source $HOME/.bash_local.d/anyenv
anyenv install plenv
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
else
log_failure_msg "failed"
fi
}
# ndenv_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# ndenv_install
# # --- cut end ---
function ndenv_install () {
log_action_cont_msg "Install ndenv"
if [ -e $HOME/.bash_local.d/anyenv ];then
source $HOME/.bash_local.d/anyenv
anyenv install ndenv
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
else
log_failure_msg "failed"
fi
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup APT-CACHER-NG
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/apt-cacher-ng)
# function APT-CACHER-NG () {
# log_action_begin_msg "apt-cacher-ng"
# apt-cacher-ng_install
# apt-cacher-ng_setup
# log_action_end_msg 0
# # in client
# # apt-cacher-ng_install
# # apt-cacher-ng_client_setup
# }
# APT-CACHER-NG
# # -- cut end --
# apt-cacher-ng_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# apt-cacher-ng_install
# # --- cut end ---
function apt-cacher-ng_install () {
package_install apt-cacher-ng
}
# apt-cacher-ng_setup
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# apt-cacher-ng_setup
# # --- cut end ---
function apt-cacher-ng_setup () {
local config="/etc/apt-cacher-ng/acng.conf"
local IP=$(ip addr show dev lxcbr0 | grep -P -o "inet [\d.]+/" | grep -P -o "(\d+\.\d+\.\d+\.\d+)")
local sedscript_acng=$(mkfile acng <<EOF
/^[ \t]*BindAddress/{
iBindAddress: localhost ${IP}
d
}
EOF
)
log_action_cont_msg "Setup apt-cacher-ng"
local cnt=$(grep -c '^BindAddress' ${config} 2> /dev/null)
if [[ ${cnt} == 0 ]];then
local temp=$(mkfile bindaddress "BindAddress: localhost ${IP}")
_run_sudo -a ${config} "cat ${temp}"
_run_sudo service apt-cacher-ng restart
log_success_msg "added"
_run_sudo rm -f ${temp}
else
replace_sudo ${sedscript_acng} ${config}
if [[ $? != 0 ]];then
log_success_msg "changed"
_run_sudo service apt-cacher-ng restart
_run_sudo rm -f ${sedscript_acng}
else
log_failure_msg "not changed"
fi
fi
}
# apt-cacher-ng_client_setup
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# apt-cacher-ng_client_setup
# # --- cut end ---
function apt-cacher-ng_client_setup () {
local config="/etc/apt/apt.conf.d/02proxy.conf"
local IP=$(ip route show | grep default | grep -P -o "\d+.\d+.\d+.\d+")
log_action_cont_msg "Setup apt proxy"
local temp=$(mkfile aptconfig "Acquire::http::Proxy \"http://"${IP}":3142/\";")
_run_sudo cp ${temp} ${config}
_run_sudo chmod 0644 ${config}
log_success_msg "added"
_run_sudo rm -f ${temp}
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup cask
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/cask)
# function cask-command_install () {
# log_action_begin_msg "cask"
# _run "(source $HOME/.bash_local.d/anyenv;pyenv install 2.7.6)"
# _run "(source $HOME/.bash_local.d/anyenv;cd ;pyenv local 2.7.6)"
# _run "(source $HOME/.bash_local.d/anyenv;cd $PYENV_ROOT/plugins;git clone git://github.com/yyuu/pyenv-virtualenv.git)"
# cask_install
# cask_setup
# log_action_end_msg 0
# }
# cask-command_install
# # -- cut end --
# cask_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# cask_install
# # --- cut end ---
function cask_install () {
log_action_cont_msg "Install cask"
_run curl -fsSkL https://raw.github.com/cask/cask/master/go | python
git clone https://github.com/riywo/cask $HOME/.cask
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# cask_setup
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# cask_setup
# # --- cut end ---
function cask_setup () {
log_action_cont_msg "Setup cask"
if [ ! -e $HOME/.cask ];then
cask init
fi
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
#!/usr/bin/env bash
# set -e
# set -o errexit
set -o errtrace
set -o nounset
export LANG=C
# variables
declare VERBOSE=""
declare -r WORKDIR=${HOME}/.provision
[[ ! -d ${WORKDIR} ]] && mkdir -p ${WORKDIR}
cd ${WORKDIR}
# check options
verbosity=2 #Start counting at 2 so that any increase to this will result in a minimum of file descriptor 3. You should leave this alone.
maxverbosity=5 #The highest verbosity we use / allow to be displayed. Feel free to adjust.
while getopts ":v" opt; do
case $opt in
v) (( verbosity=verbosity+1 ))
;;
esac
done
for v in $(seq 3 $verbosity) #Start counting from 3 since 1 and 2 are standards (stdout/stderr).
do
(( "$v" <= "$maxverbosity" )) && eval exec "$v>&2"
done
for v in $(seq $(( verbosity+1 )) $maxverbosity ) #From the verbosity level one higher than requested, through the maximum;
do
(( "$v" > "2" )) && eval exec "$v>/dev/null"
done
declare -r SPACES=" "
if (( ${verbosity} == 3 ));then
PS4='\[\e[31m\]+ [\t] ${SPACES:0:$((${#BASH_SOURCE} > 24 ? 0 : 24 - ${#BASH_SOURCE}))}${BASH_SOURCE:$((${#BASH_SOURCE} < 24 ? 0 : -24))}:${SPACES:0:$((3-${#LINENO}))}$LINENO ${FUNCNAME:-${SPACES:0:20}}${FUNCNAME:+${SPACES:0:$((20 - ${#FUNCNAME}))}} $ \[\e[0m\]'
export VERBOSE=""
set -x
elif (( ${verbosity} >= 4 ));then
PS4='\[\e[31m\]+ [\t] ${SPACES:0:$((${#BASH_SOURCE} > 24 ? 0 : 24 - ${#BASH_SOURCE}))}${BASH_SOURCE:$((${#BASH_SOURCE} < 24 ? 0 : -24))}:${SPACES:0:$((3-${#LINENO}))}$LINENO ${FUNCNAME:-${SPACES:0:20}}${FUNCNAME:+${SPACES:0:$((20 - ${#FUNCNAME}))}} $ \[\e[0m\]'
export VERBOSE=1
set -x
fi
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup docker
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/docker)
# function docker () {
# log_action_begin_msg "docker"
# docker_install
# docker-compose_install
# log_action_end_msg 0
# }
# docker
# # -- cut end --
# docker_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# docker_install
# # --- cut end ---
function docker_install () {
local user=$(whoami)
log_action_cont_msg "docker insall"
_run_sudo wget -q -O docker_installer.sh https://get.docker.com/
_run_sudo bash docker_installer.sh
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
_run_sudo usermod -aG docker ${user}
_run_sudo gpasswd -a ${user} docker
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
_run_sudo grep "10.166.255.133" /etc/default/docker
if [[ $? != 0 ]];then
# _run_sudo -a /etc/default/docker echo 'DOCKER_OPTS="-H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock --dns=10.166.255.133 --dns=172.16.26.11"'
_run_sudo -a /etc/default/docker echo 'DOCKER_OPTS="--registry-mirror http://cpusys.mu.renesas.com:5000 --insecure-registry cpusys.mu.renesas.com:5000 -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock --dns 10.166.255.133 --dns 172.16.26.11"
fi
_run_sudo grep "localhost:4243" /etc/default/docker
if [[ $? != 0 ]];then
_run_sudo -a /etc/default/docker echo 'DOCKER_HOST="tcp://localhost:4243"'
fi
}
# docker-compose_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# docker-compose_install
# # --- cut end ---
function docker-compose_install () {
log_action_cont_msg "docker-compose insall"
VERSION=$(wget -q --max-redirect=1 -S -O - https://github.com/docker/compose/releases/latest 2>&1 | grep -e 'Location: ' | grep -o 'http.*' | grep 'tag' | sed -e 's!^http.*/tag/!!')
_run_sudo wget -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-`uname -s`-`uname -m`
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
_run_sudo chmod +x /usr/local/bin/docker-compose
_run_sudo wget -O /etc/bash_completion.d/docker-compose https://raw.githubusercontent.com/docker/compose/${VERSION}/contrib/completion/bash/docker-compose
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
unset VERSION
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup dropbox
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/dropbox)
# function dropbox () {
# log_action_begin_msg "dropbox"
# dropbox_package_install
# dropbox_command_install
# dropbox_connect
# log_action_end_msg 0
# }
# dropbox
# # -- cut end --
# dropbox_package_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# dropbox_package_install
# # --- cut end ---
function dropbox_package_install () {
package_install w3m python-gpgme
_run_sudo sysctl -w fs.inotify.max_user_watches=100000
_run_sudo sysctl -p
}
# dropbox_command_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# dropbox_command_install
# # --- cut end ---
function dropbox_command_install () {
_run mkdir -p ${HOME}/bin
curl -fsSkL "http://www.dropbox.com/download?dl=packages/dropbox.py" > ${HOME}/bin/dropbox.py
_run chmod 0755 ${HOME}/bin/dropbox.py
if [[ ! -d ${HOME}/.dropbox-dist ]];then
log_action_cont_msg "Download dropbox commands"
echo y | ${HOME}/bin/dropbox.py start -i
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
fi
}
# dropbox_connect
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# dropbox_connect
# # --- cut end ---
function dropbox_connect () {
while [[ "$(~/bin/dropbox.py status)" =~ Connecting* ]]
do
sleep 5
done
if [[ "$(~/bin/dropbox.py status)" =~ Waiting* ]];then
log_action_cont_msg "Connect dropbox"
${HOME}/bin/dropbox.py start
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
fi
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup fix_precise
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/fix_precise)
# function fix_precise () {
# log_action_begin_msg "fix_precise"
# _is_precise && upgrade_precise_kernel
# log_action_end_msg 0
# }
# fix_precise
# # -- cut end --
# _is_precise
# arg: void
# # Usage:
# # -- cut start --
# _is_precise
# # --- cut end ---
function _is_precise () {
[[ $(cat /etc/lsb-release | grep CODENAME | cut -d= -f2) == 'precise' ]] && return 0 || return 1
}
# upgrade_precise_kernel
# arg: void
# # Usage:
# # -- cut start --
# upgrade_precise_kernel
# # --- cut end ---
upgrade_precise_kernel () {
package_remove linux-generic-lts-raring linux-headers-generic-lts-raring linux-image-generic-lts-raring
_run_sudo apt-get autoremove
package_install linux-generic-lts-saucy linux-headers-generic-lts-saucy linux-image-generic-lts-saucy
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup git
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/git)
# function git-command_install () {
# log_action_begin_msg "git"
# git-core_install
# git-flow_install
# git-subtree_install
# log_action_end_msg 0
# }
# git-command_install
# # -- cut end --
# git-core_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# git-core_install
# # --- cut end ---
function git-core_install () {
package_install git-core git-man
}
# git-flow_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# git-flow_install
# # --- cut end ---
function git-flow_install () {
if [[ ! -e /tmp/gitflow-installer.sh ]];then
log_action_cont_msg "Install git-flow command"
_run echo 'cd /tmp && curl -fsSkL -o gitflow-installer.sh "https://raw.github.com/petervanderdoes/gitflow/develop/contrib/gitflow-installer.sh" && sudo bash gitflow-installer.sh install develop && sudo rm -rf gitflow gitflow-installer.sh && sudo curl -fsSkL -o /etc/bash_completion.d/git-flow-completion.bash "https://raw.githubusercontent.com/bobthecow/git-flow-completion/master/git-flow-completion.bash"'
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
fi
if [[ ! -d /tmp/gitflow-installer.sh ]];then
log_action_cont_msg "Install git-extras command"
_run 'cd /tmp && git clone "https://github.com/tj/git-extras.git" && cd git-extras && git checkout $(git describe --tags $(git rev-list --tags --max-count=1)) && sudo make install && sudo rm -rf git-extras'
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
fi
}
# git-subtree_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# git-subtree_install
# # --- cut end ---
function git-subtree_install () {
log_action_cont_msg "Install git-subtree command"
if [ ! -h /usr/lib/git-core/git-subtree ];then
local src=/usr/share/doc/git/contrib/subtree/git-subtree.sh
local dest=/usr/lib/git-core/git-subtree
_run_sudo ln -s ${src} ${dest} && \
_run_sudo chmod 755 ${dest}
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
fi
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup gitlocal
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/gitlocal)
# function gitlocal () {
# log_action_begin_msg "git-local"
# gitlocal_remove_section "user"
# gitlocal_add_value "user.name" "John Smith"
# gitlocal_add_value "user.email" "john.smith@example.com"
# gitlocal_remove_section "github"
# gitlocal_add_value "github.user" "hoge1"
# gitlocal_add_value "github.token" "12345667890abcdefghighlmnopqrstuvwxyzABC"
# gitlocal_add_value "github.password" "ABCDE0123456"
# gitlocal_remove_section "url.git@github.com:"
# gitlocal_add_value "url.git@github.com:.pushInsteadOf" "git://github.com/"
# gitlocal_add_value "url.git@github.com:.pushInsteadOf" "https://github.com/" "--add"
# gitlocal_add_value "diff.renames" "copies"
# gitlocal_add_value "init.templatedir" "${HOME}/.git_template"
# log_action_end_msg 0
# }
# gitlocal
# # -- cut end --
# gitlocal_add_value
# arg: NAME, VALUE, OPTION
# return: command output
# # Usage:
# # -- cut start --
# gitlocal_add_value "url.git@github.com:.pushInsteadOf" "https://github.com/" "--add"
# # --- cut end ---
function gitlocal_add_value () {
log_action_cont_msg "Add value: $1<=$2"
if (( $# == 3 ));then
_run git config --file ${HOME}/.gitconfig.local ${3-} $1 "'"$2"'"
else
_run git config --file ${HOME}/.gitconfig.local $1 "'"$2"'"
fi
}
# gitlocal_remove_section
# arg: SECTION_NAME
# return: command output
# # Usage:
# # -- cut start --
# gitlocal_remove_section "user"
# # --- cut end ---
function gitlocal_remove_section () {
log_action_cont_msg "Remove section: $1"
_run git config --file ${HOME}/.gitconfig.local --remove-section $1
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # homeshick
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/homeshick)
# function HOMESHICK () {
# homeshick_install
# homeshick_setup
# }
# HOMESHICK
# # -- cut end --
# homeshick_install
# arg1: void
# return: Last command result
function homeshick_install () {
log_action_begin_msg "Install homeshick"
if [ -e $HOME/.homesick/repos/homeshick ];then
$HOME/.homesick/repos/homeshick/bin/homeshick check -q dotfiles
else
_run git clone git://github.com/andsens/homeshick.git $HOME/.homesick/repos/homeshick
fi
[ ! -e $HOME/.bash_local.d ] && mkdir -p $HOME/.bash_local.d
[ ! -e $HOME/.bash_local.d/homeshick.sh ] && echo "source $HOME/.homesick/repos/homeshick/homeshick.sh" > $HOME/.bash_local.d/homeshick.sh
[ ! -e $HOME/.bash_local.d/homeshick-completion.bash ] && echo "source $HOME/.homesick/repos/homeshick/completions/homeshick-completion.bash" > $HOME/.bash_local.d/homeshick-completion.bash
log_action_end_msg 0
}
# homeshick_setup
# arg1: users_github_url/dotfiles.git (default:https://github.com/hiono/dotfiles.git)
# return: Last command result
function homeshick_setup () {
log_action_begin_msg "Setup homeshick"
source $HOME/.homesick/repos/homeshick/homeshick.sh
source $HOME/.homesick/repos/homeshick/completions/homeshick-completion.bash
homeshick check -q dotfiles
(( $? == 1 )) && (echo y | homeshick -f clone ${1:-https://github.com/hiono/dotfiles.git})
homeshick -f link dotfiles
log_action_end_msg 0
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup nas
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/nas)
# function nas () {
# log_action_begin_msg "nas"
# nas_install
# nas_add_hosts nas 192.168.0.3
# nas_connect
# log_action_end_msg 0
# }
# nas
# # -- cut end --
# nas_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# nas_install
# # --- cut end ---
function nas_install () {
package_install cifs-utils sshfs autofs smbclient fuse
if [ -e "/etc/fuseconf" ];
then
local sedscript=$(mkfile fuseconfig '/user_allow_other/{s/^#//}')
replace_sudo ${sedscript} /etc/fuseconf
fi
}
# nas_add_hosts
# arg: HOSTNAME IP_ADDRESS
# return: command output
# # Usage:
# # -- cut start --
# nas_add_hosts HOSTNAME IP_ADDRESS
# # --- cut end ---
function nas_add_hosts () {
_run grep "$1" /etc/hosts
if [[ $? != 0 ]];then
_run_sudo -a /etc/hosts echo "$2 $1"
fi
}
# nas_connect
# return: command output
# # Usage:
# # -- cut start --
# nas_connect
# # --- cut end ---
function nas_connect () {
local user=$(whoami)
log_action_cont_msg "nas_connect setup for ${user}"
_run_sudo service autofs stop
if [[ $(grep -c "/smb" /etc/auto.master 2> /dev/null) == 0 ]];then
_run_sudo -a /etc/auto.master echo /smb /etc/auto.smb --timeout=60 --ghost
fi
if [[ -e "/etc/auto.smb" ]];then
local sedscript=$(mkfile autosmb "/\"-fstype=cifs\"/{s/-fstype=cifs/-fstype=cifs,rw,iocharset=utf8,uid=${user},gid=${user}/}")
replace_sudo ${sedscript} /etc/auto.smb
fi
_run_sudo service autofs start
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup ntp
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/ntp)
# function ntp () {
# local sedscript_ntp=$(mkfile ntp <<EOF
# s/^server ntp.ubuntu.com/server ntp01.example.com/g
# s/^server 0.ubuntu.pool.ntp.org/server ntp01.example.com/g
# s/^server 1.ubuntu.pool.ntp.org/server ntp02.example.com/g
# s/^server 2.ubuntu.pool.ntp.org/server ntp03.example.com/g
# s/^server 3.ubuntu.pool.ntp.org/server ntp04.example.com/g
# EOF
# )
# local sedscript_ntpdate=$(mkfile ntpdate <<EOF
# s/^NTPSERVERS="ntp.ubuntu.com"/NTPSERVERS="ntp01.example.com"/g
# EOF
# )
# log_action_begin_msg "ntp"
# ntp_install
# ntp_config ${sedscript_ntp}
# ntpdate_config ${sedscript_ntpdate} ntp01.example.com
# log_action_end_msg 0
# rm -f ${sedscript_ntp} ${sedscript_ntp_date}
# }
# ntp
# # -- cut end --
# ntp_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# ntp_install
# # --- cut end ---
function ntp_install () {
package_install ntp ntpdate
}
# ntp_config
# arg: SEDSCRIPT_FILE
# return: command output
# # Usage:
# # -- cut start --
# ntp_config SEDSCRIPT_FILE
# # --- cut end ---
function ntp_config () {
log_action_cont_msg "ntp setup for your local environment"
replace_sudo $1 /etc/ntp.conf
if [[ $? != 0 ]];then
log_success_msg "changed"
_run_sudo service ntp restart
else
log_failure_msg "not changed"
fi
}
# ntpdate_config
# arg: sedscript_file, NTP_SERVER
# return: command output
# # Usage:
# # -- cut start --
# ntpdate_config SEDSCRIPT_FILE NTP_SERVER
# # --- cut end ---
function ntpdate_config () {
log_action_cont_msg "ntpdate setup for your local environment"
replace_sudo $1 /etc/default/ntpdate
if [[ $? != 0 ]];then
log_success_msg "changed"
_run_sudo service ntp stop
_run_sudo ntpdate $2
_run_sudo service ntp start
else
log_failure_msg "not changed"
fi
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup ssh_keepalive
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/ssh_keepalive)
# ssh_keepalive_config
# # -- cut end --
# ssh_keepalive_config
# arg: SEDSCRIPT_FILE
# return: command output
# # Usage:
# # -- cut start --
# ssh_keepalive_config
# # --- cut end ---
function ssh_keepalive_config () {
local config="/etc/ssh/ssh_config"
local sedscript_sshconfig=$(mkfile sshconfig <<\EOF
/^#?[ \t]+ProtocolKeepAlives/{
s/^#?[ \t]+ProtocolKeepAlives.*$//
a ProtocolKeepAlives 60
}
EOF
)
log_action_cont_msg "ssh keepalive setup for your local environment"
local cnt=$(grep -c 'ProtocolKeepAlives' ${config} 2> /dev/null)
if [[ ${cnt} == 0 ]];then
_run_sudo -a ${config} echo ' ProtocolKeepAlives 60'
log_success_msg "added"
_run_sudo service ssh restart
else
replace_sudo ${sedscript_sshconfig} ${config}
if [[ $? != 0 ]];then
log_success_msg "changed"
_run_sudo service ssh restart
_run_sudo rm -f ${sedscript_sshconfig}
else
log_failure_msg "not changed"
fi
fi
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup ssh_keygen
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/ssh_keygen)
# ssh_keygen_setup [PASSPHRASE]
# # -- cut end --
# ssh_keygen_setup
# arg: PASSPHRASE (default: hogehogehoge)
# return: command output
# # Usage:
# # -- cut start --
# ssh_keygen_setup [PASSPHRASE]
# # --- cut end ---
function ssh_keygen_setup () {
local config="/etc/ssh/ssh_config"
log_action_cont_msg "ssh-keygen setup for your local environment"
if [[ ! -e ${HOME}/.ssh/id_rsa ]];then
/usr/bin/ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "${1:-hogehogehoge}" && \
_run "(cat ${HOME}/.ssh/id_rsa.pub ${HOME}/.ssh/authorized_keys | sort -u -o ${HOME}/.ssh/authorized_keys )" && \
_run chmod 0600 ${HOME}/.ssh/authorized_keys
fi
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# sudoers_add_user
# arg: USER_NAME
# return: command output
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/sudoers)
# sudoers_add_user USER_NAME
# # --- cut end ---
function sudoers_add_user () {
log_action_cont_msg "sudoers setup for $1"
local sudoers_config=$(mkfile sudoers <<\EOF
$1 ALL=(ALL) NOPASSWD:ALL
EOF
)
local config="/etc/sudoers.d/$1"
_run_sudo chown root:root ${sudoers_config} && \
_run_sudo chmod 0440 ${sudoers_config} && \
_run_sudo mv ${sudoers_config} ${config}
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
#!/usr/bin/env bash
# load base functions
declare -F log_action_begin_msg > /dev/null
(( $? == 1 )) && source /lib/lsb/init-functions
declare -F _has_f > /dev/null
(( $? == 1 )) && source <(curl -fsSkL https://gist.githubusercontent.com/hiono/9636356/raw/.functions)
# # setup ufw
# # Usage:
# # -- cut start --
# source <(curl -fsSkL ${GIST}/ufw)
# function ufw-command_install () {
# log_action_begin_msg "ufw"
# ufw_install
# _run_sudo service ufw stop
# ufw_config
# ufw_setup
# _run_sudo service ufw start
# log_action_end_msg 0
# }
# ufw-command_install
# # -- cut end --
# ufw_install
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# ufw_install
# # --- cut end ---
function ufw_install () {
package_install ufw
}
# ufw_config
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# ufw_config
# # --- cut end ---
function ufw_config () {
log_action_cont_msg "Config ufw"
conf=/etc/default/ufw
if [[ $(grep -c '^DEFAULT_FORWARD_POLICY="DROP"' ${conf} 2> /dev/null) > 0 ]];then
local sedscript=$(mkfile ufw 's/^DEFAULT_FORWARD_POLICY="DROP"/DEFAULT_FORWARD_POLICY="ACCEPT"/g')
replace_sudo ${sedscript} ${conf}
else
if [[ $(grep -c 'DEFAULT_FORWARD_POLICY="ACCEPT"' ${conf} 2> /dev/null) == 0 ]];then
_run_sudo -a ${conf} echo 'DEFAULT_FORWARD_POLICY="ACCEPT"'
fi
fi
(( $? == 0 )) && log_success_msg "success" || log_failure_msg "failed"
}
# ufw_setup
# arg: void
# return: command output
# # Usage:
# # -- cut start --
# ufw_setup
# # --- cut end ---
function ufw_setup () {
log_action_cont_msg "Setup ufw"
script_ufw=$(mkfile ufw <<\EOF
ufw default deny
ufw allow in on lxcbr0
ufw allow in http
ufw allow in https
ufw allow ssh
ufw allow ftp
# dropbox
ufw allow in 17500
# apt-cacher-ng
ufw allow 3412
ufw --force enable
ufw reload
EOF
)
_run_sudo bash ${script_ufw}
log_action_end_msg 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment