Created
September 2, 2012 14:37
-
-
Save chzealot/3599669 to your computer and use it in GitHub Desktop.
fabfile to deploy autossh for sock5 proxy over ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Author: Zealot Ke <[email protected]> | |
# Copyright (C) losthit.com 2012 | |
import os | |
import sys | |
import time | |
from fabric.api import * | |
import autopath | |
from lib.zeus import apt | |
from lib.zeus import monit | |
env.user = 'zeus' | |
APP_DIR = '/home/z/share/autossh' | |
BASE_PATH = os.path.dirname(os.path.abspath(__file__)) | |
@task | |
def setup(): | |
''' build and setup autossh | |
''' | |
args = { | |
'app_dir': APP_DIR, | |
'local_dir': BASE_PATH, | |
} | |
with settings(warn_only=True): | |
run('hostname') | |
apt.require('build-essential') | |
run('if [[ ! -d %(app_dir)s ]]; then mkdir -p %(app_dir)s; fi' % args) | |
run(('if [[ ! -e %(app_dir)s/autossh-1.4c.tgz ]]; then ' | |
'wget -q --no-check-certificate "http://www.harding.motd.ca/autossh/autossh-1.4c.tgz" -O %(app_dir)s/autossh-1.4c.tgz; ' | |
'fi') % args) | |
run(('if [[ ! -d %(app_dir)s/autossh-1.4c ]]; then ' | |
'cd %(app_dir)s && tar zxvf autossh-1.4c.tgz; ' | |
'fi') % args) | |
run(('cd %(app_dir)s/autossh-1.4c && ' | |
'./configure --prefix=%(app_dir)s && ' | |
'make && ' | |
'make install' | |
) % args) | |
deploy() | |
clean() | |
#the end | |
@task | |
def deploy(): | |
args = { | |
'local_dir': BASE_PATH, | |
'app_dir': APP_DIR, | |
} | |
for subdir in ('bin', 'conf', 'log', 'run',): | |
dir_path = os.path.abspath(os.path.join(APP_DIR, subdir)) | |
run('if [[ ! -d %s ]]; then mkdir -p %s; fi' % (dir_path, dir_path)) | |
put('%(local_dir)s/bin/autossh-ctl' % args, | |
'%(app_dir)s/bin/autossh-ctl' % args, mode=0755) | |
monit.register('%(local_dir)s/monit/autossh.conf' % args) | |
restart() | |
@task | |
def restart(): | |
args = { | |
'local_dir': BASE_PATH, | |
'app_dir': APP_DIR, | |
} | |
with settings(warn_only=True): | |
run('%(app_dir)s/bin/autossh-ctl restart' % args) | |
@task | |
def start(): | |
args = { | |
'local_dir': BASE_PATH, | |
'app_dir': APP_DIR, | |
} | |
with settings(warn_only=True): | |
run('%(app_dir)s/bin/autossh-ctl start' % args) | |
@task | |
def stop(): | |
args = { | |
'local_dir': BASE_PATH, | |
'app_dir': APP_DIR, | |
} | |
with settings(warn_only=True): | |
run('%(app_dir)s/bin/autossh-ctl stop' % args) | |
@task | |
def clean(): | |
args = { | |
'local_dir': BASE_PATH, | |
'app_dir': APP_DIR, | |
} | |
with settings(warn_only=True): | |
run('rm -rf %(app_dir)s/autossh-1.4c.tgz' % args) | |
run('rm -rf %(app_dir)s/autossh-1.4c' % args) | |
run('rm -rf %(app_dir)s/man' % args) | |
run('rm -rf %(app_dir)s/share' % args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#=============================================================================== | |
# FILE: autossh-ctl.sh | |
# | |
# USAGE: ./autossh-ctl.sh | |
# | |
# DESCRIPTION: | |
# | |
# OPTIONS: --- | |
# REQUIREMENTS: --- | |
# BUGS: --- | |
# NOTES: --- | |
# AUTHOR: Zealot Ke (Zealot), [email protected] | |
# COMPANY: losthit.com | |
# VERSION: 1.0 | |
# CREATED: 09/02/2012 11:16:46 CST | |
# REVISION: --- | |
#=============================================================================== | |
BASEPATH=$(cd "$(dirname "$0")/.."; pwd) | |
cd $BASEPATH | |
PID_FILE=$BASEPATH/run/autossh.pid | |
CONF_FILE=$BASEPATH/conf/autossh.conf | |
LOG_FILE=$BASEPATH/log/autossh.log | |
AUTOSSH_BIN=$BASEPATH/bin/autossh | |
AUTOSSH_OPT="-M 0 -f -q -N -D 0.0.0.0:7070 [email protected]" | |
RED="\033[1;31m" | |
GREEN="\033[1;32m" | |
COLOR_END="\033[0;0m" | |
export AUTOSSH_LOGFILE=$LOG_FILE | |
export AUTOSSH_LOGLEVEL=7 | |
export AUTOSSH_PIDFILE=$PID_FILE | |
export AUTOSSH_POLL=600 | |
action_start() { | |
if [ -e $PID_FILE ]; then | |
PID=`cat $PID_FILE` | |
if [ `ps aux|awk '{print $2}'|grep $PID` ]; then | |
exit 0 | |
else | |
rm -f $PID_FILE | |
fi | |
fi | |
$AUTOSSH_BIN $AUTOSSH_OPT &>/dev/null | |
sleep 1 | |
exist; status=$? | |
if [[ $status = 1 ]]; then | |
printf "starting autossh ... $GREEN%s$COLOR_END\n" "OK" | |
else | |
printf "starting autossh ... $RED%s$COLOR_END\n" "FAILED" | |
fi | |
} | |
action_check() { | |
action_start | |
} | |
exist() { | |
if [ -f $PID_FILE ]; then | |
pid=`cat $PID_FILE` | |
if [[ `ps aux | awk '{print $2}' | /bin/grep "\<$pid\>"` ]]; then | |
return 1 | |
else | |
return 0 | |
fi | |
fi | |
return 0 | |
} | |
action_stop() { | |
if [ -e $PID_FILE ]; then | |
PID=`cat $PID_FILE` | |
if [ `ps aux|awk '{print $2}'|grep $PID` ]; then | |
kill -9 $PID | |
rm -f $PID_FILE | |
else | |
rm -f $PID_FILE | |
fi | |
fi | |
sleep 1 | |
exist; status=$? | |
if [[ status = 1 ]]; then | |
printf "stoping autossh ... $RED%s$COLOR_END\n" "FAILED" | |
else | |
printf "stoping autossh ... $GREEN%s$COLOR_END\n" "OK" | |
fi | |
} | |
case $1 in | |
start) | |
action_start | |
;; | |
stop) | |
action_stop | |
;; | |
restart) | |
action_stop | |
action_start | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart}" | |
;; | |
esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
check process autossh with pidfile /home/z/share/autossh/run/autossh.pid | |
group proxy | |
start program = "/home/z/share/autossh/bin/autossh-ctl start" | |
as uid zeus and gid zeus | |
stop program = "/home/z/share/autossh/bin/autossh-ctl stop" | |
as uid zeus and gid zeus | |
if failed host 127.0.0.1 port 7070 then restart |
配合我修改过的fabric和keychain就可以不用输入ssh登陆、sudo、配置应用密码(比如mysql登陆密码):https://github.com/chzealot/fabric/commit/6f59bd89d031addaad3f36557a4911951b98cbad
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以上是一个使用fabric部署应用的例子(使用场景说明见:http://blog.losthit.com/archives/manage-person-linux-host/)