Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
曹牧年 committed Mar 22, 2016
0 parents commit b17f19d
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 0 deletions.
24 changes: 24 additions & 0 deletions add.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

read -p "Please enter your mis/relay account:" relay;
read -p "Please enter your mis password:" -s password;
echo ""
read -p "Please enter your applications' names separated by commas in order to config all servers that you want to add:" applications;

OLD_IFS="$IFS"
IFS=","
apparr=($applications)
for application in ${apparr[@]}
do
sleep 2
servers=`curl -s http://ops.sankuai.com/releng/app_to_hosts?app=$application -u $relay:$password | sed 's/.*\[//g' | sed 's/\]}//g' | sed 's/"//g' | sed 's/ //g'`
serverarr=($servers)
for server in ${serverarr[@]}
do
echo $server >> dest.config
echo $server >> $application.config
done
done
IFS="$OLD_IFS"

echo "Your server list is updated successfully."
15 changes: 15 additions & 0 deletions dest.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
relay.sankuai.com
/***************************************gct-information***************/
dx-gct-information-platform-staging01
dx-gct-information-platform01
dx-gct-information-platform02
dx-gct-information-platform03
/***************************************gct-groupbd*******************/
dx-gct-scp-groupbd-staging01
/***************************************mtproduct*********************/
dx-cos-product05
/***************************************mtcontent*i********************/
dx-cos-content-st01
/***************************************mtgroupbd*********************/
dx-cos-groupbd-st01

6 changes: 6 additions & 0 deletions dest.config.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
relay.sankuai.com
dx-gct-information-platform-staging01
dx-gct-information-platform01
dx-gct-information-platform02
dx-gct-information-platform03
dx-gct-scp-groupbd-staging01
55 changes: 55 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/sh

read -p "Please enter your mis/relay account:" relay;
read -p "Please enter your mis password:" -s password;
echo ""
read -p "Please enter your all application names separated by commas in order to config all servers:" applications;
dir=`pwd -P`
relay_config=$dir"/relay.config"
echo "relay="$relay > $relay_config

echo "Host relay.sankuai.com
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster auto" >> ~/.ssh/config

if [ -f "dest.config" ]; then
mv dest.config dest.config.bak
fi

echo "relay.sankuai.com" >> dest.config
echo "db24" >> dest.config
OLD_IFS="$IFS"
IFS=","
apparr=($applications)
for application in ${apparr[@]}
do
sleep 2
servers=`curl -s http://ops.sankuai.com/releng/app_to_hosts?app=$application -u $relay:$password | sed 's/.*\[//g' | sed 's/\]}//g' | sed 's/"//g' | sed 's/ //g'`
serverarr=($servers)
for server in ${serverarr[@]}
do
echo $server >> dest.config
echo $server >> $application.config
done
done

if [ -n "`grep 'zsh' $SHELL`" ]; then
profile=$HOME"/.zshrc"
elif [ -n "`grep 'bash' $SHELL `" ]; then
profile=$HOME"/.bash_profile"
else
echo "Please enter \"alias mgo='sh $dir/mgo.sh'\" into your shell profile"
fi

if [ ! -f "$profile" ]; then
echo "Fail to install Mgo... Please check your .bash_profile..."
else if [ -z "`grep 'alias mgo' $profile`" ]; then
echo "alias mgo='sh $dir/mgo.sh'" >> $profile
sleep 1
source $profile
fi
fi

IFS="OLD_IFS"

echo "MgO is installed successfully."
92 changes: 92 additions & 0 deletions mgo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

usage() {
echo
echo "Usage: mgo.sh [-r] [-d] [-h]"
echo
echo "-r - Get your depolyment server by the name of application."
echo "-d - Go to the depolyment server directly by the number of server list."
echo "-h - This help text."
echo
}

load_relay() {
while read line
do
eval $line
done < $1/relay.config
}

load_dest() {
if [ ! -f "$1/$2.config" ]; then
echo "Please check your $1/$2.config..."
exit 1
fi
unset dest
k=0;
while read line
do
dest[k++]=$line
done < $1/$2.config
}


path=`dirname $0`
load_relay $path
load_dest $path "dest"
host=$relay@relay.sankuai.com
PS3="MgO is short for Meituan Go... =====> "

while getopts "r:d:h" arg
do
case $arg in
r)
load_dest $path $OPTARG
;;
d)
n=`echo $OPTARG | grep "[^0-9]"`
if [ -n "$n" ]; then
echo "Please enter a number as param..."
exit 1
fi
if [[ "(($OPTARG-1))" -ge "${#dest[*]}" ]]; then
echo "Please enter a number which is in the range of destation array as param..."
exit 1
fi
direct=`echo ${dest[(($OPTARG-1))]} | sed 's/\[.*\]//g'`
if [[ "$direct" == "relay.sankuai.com" ]]; then
ssh $host
else
ssh -t $host ssh $direct
fi
exit 0
;;
h)
usage
exit 0
;;
?)
echo "ERROR: Unknow option..."
usage
exit 1
;;
esac
done

select selected in ${dest[@]};
do
d=`echo ${selected} | sed 's/\[.*\]//g'`
echo "\033[32mlogin to ${selected}\033[0m"
case $d in
relay.sankuai.com)
ssh $host
break
;;
$d)
ssh -t $host ssh $d
break
;;
*) echo "There is someting wrong with your enter."
;;
esac
done
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# MgO - 氧化镁

---

MgO,氧化镁,仅仅只是一个ssh登录的辅助工具。

## 背景
随着旅游业务的扩张,旅游rd小伙伴们的服务器也越来越多,小伙伴们在登录应用所在服务器所消耗的精力也越来越多。本工具旨在提高登录应用所在服务器的效率,处理问题更快捷。

## 效率提高
假设relay密码的长度为10,动态密码为6,所以登录relay跳板机,需要输入密码共敲16次键盘。
用氧化镁,可以为小伙伴们最多减少64次键盘敲击以及思考所登录服务器的hostname的时间(以张迎兵登录hotel-meilv-rhone-miniflow01为例),最少减少33次键盘敲击以及思考所登录服务器的hostname的时间(以葛伟登录db24为例)。

## 演示
### 没有氧化镁的时候,我们这么登录服务器
![没有氧化镁](http://ww1.sinaimg.cn/large/625dbb25gw1evjx0pcyhng20g50a20u0.gif)
### 有了氧化镁,我们这么登录服务器
![氧化镁](http://ww1.sinaimg.cn/large/625dbb25gw1evjx0ojh2fg20g50a2n2i.gif)

## 功能特点
1. 安装方便
2. 登录服务器无需再经过繁杂的输入过程
3. 方便的配置功能让你随意快速登录服务器
4. 每天只需要输入一次密码让你忘却糟心的登录过程

## 安装
1. clone本项目到本地,执行install.sh安装。
2. 输入relay/mis账号。注意不要带邮箱后缀。
3. 输入mis账号密码,用于访问ops接口同步部署服务器列表。
4. 输入你所需要登录的服务器上的应用名,如:meilv-online,多个应用名以英文逗号分隔。
5. 打开新的终端,输入mgo。

## 使用说明
### 1. 直接命令
输入mgo命令之后,会出现登录服务器列表,此列表由用户自主维护到mgo安装目录中的dest.config文件中。格式: `hostname[ip]`(ip可省略)
看到服务器列表之后,选择输入对应服务器的数字,即可登录。
### 2. 定制项目服务器列表,参数 -r
考虑到每个人涉及的项目过多,导致服务器列表过长的问题,增加了多服务器配置文件功能。配置你想要一个或多个服务器在一个名`name.config`的文件中,输入`mgo -r name`命令可显示改配置文件内的服务器列表。
### 3. 直达服务器,参数 -d
为了加快登录服务器速度,增加直达服务器参数-d。输入`mgo -d n`(n为数字),会直达dest.config中第n个服务器,无需查看服务器列表过程。
### 4. 追加服务器列表 add.sh
当有新的应用服务增加,可用过执行`sh add.sh`,通过脚本程序的提示,自动增加指定应用下的所有服务器。
### 5. 更新服务器列表 update.sh
当应用服务增加服务器时,无需手动添加服务器host至dest.config文件中。只需执行`sh update.sh`,通过脚本提示,自动增加服务器至dest.config。
# Mgo
1 change: 1 addition & 0 deletions relay.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
relay=caomunian
69 changes: 69 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/sh

load_dest() {
if [ ! -f "$1/$2.config" ]; then
echo "Please check your $1/$2.config..."
exit 1
fi
unset dest
k=0;
while read line
do
dest[k++]=$line
done < $1/$2.config
}

unset apparr
uniquify_app() {
flag=true
for appname in ${apparr[@]}
do
if [[ "$appname" == "$1" ]]; then
flag=false
fi
done
if [ "$flag" = true ]; then
length=${#apparr[*]}
apparr[length]=$1
fi
}
path=`dirname $0`
load_dest $path "dest"

read -p "Please enter your mis/relay account:" relay;
read -p "Please enter your mis password:" -s password;
echo ""

for server in ${dest[@]}
do
if [[ "$server" == "relay.sankuai.com" || "$server" == "db24" ]]; then
echo "vvvvvvvvvv"
continue
fi
sleep 2
servername=`echo ${server} | sed 's/\[.*\]//g'`
echo $servername
app=`curl -s http://ops.sankuai.com/releng/host_to_apps?host=$servername -u $relay:$password | sed 's/.*\[//g' | sed 's/\]}//g' | sed 's/"//g' | sed 's/ //g'| sed 's/,.*$//g'`
echo $app
uniquify_app $app
echo ${apparr[@]}
done

OLD_IFS="$IFS"
IFS=","
cat /dev/null > dest.config
echo "relay.sankuai.com" >> dest.config
echo "db24" >> dest.config
for application in ${apparr[@]}
do
sleep 2
echo "" > $application.config
servers=`curl -s http://ops.sankuai.com/releng/app_to_hosts?app=$application -u $relay:$password | sed 's/.*\[//g' | sed 's/\]}//g' | sed 's/"//g' | sed 's/ //g'`
serverarr=($servers)
for server in ${serverarr[@]}
do
echo $server >> dest.config
echo $server >> $application.config
done
done
IFS="$OLD_IFS"

0 comments on commit b17f19d

Please sign in to comment.