ubuntu服务器端auto_make:
#!/bin/bash # this is a automatically compile script # add env source $HOME/.profile UPDATE_RESULT=FAILED MAKE_RESULT=FAILED UPDATE_LOG=update.log MAKE_LOG=make.log MAKE_LOG_TAIL=make_tail.log SSH_ENV_LOG=ssh_env.log HTTP_ALIAS=http://192.168.110.94/build HTTP_ALIAS_RELEASE=http://192.168.110.94/android-release HTTP_ALIAS_TARGET=$HOME/result PRJ_ABS_PATH= CP_LOG_FROM= CP_LOG_TO= CP_IMG_FROM= CP_IMG_TO= LOG_HTTP_URL= IMG_HTTP_URL= IMG_ZIP_NAME= RELEASE_PATH= dump() { echo "CP_LOG_FROM: $CP_LOG_FROM" echo "CP_LOG_TO: $CP_LOG_TO" echo "CP_IMG_FROM: $CP_IMG_FROM" echo "CP_IMG_TO: $CP_IMG_TO" echo "LOG_HTTP_URL: $LOG_HTTP_URL" echo "IMG_HTTP_URL: $IMG_HTTP_URL" echo "IMG_ZIP_NAME: $IMG_ZIP_NAME" } initialize() { prj_dir=$HOME/projects/$1 product=$2 is_valid_product= if [ -d $prj_dir ]; then cd $prj_dir ./makeMtk listp | grep -q "/<$product/>" && is_valid_product=true if [ ! $is_valid_product ]; then echo "invalid product: $product" exit 1 fi http_log_path=$1/build_log/ http_img_path=$1/img CP_LOG_FROM=$prj_dir/out/target/product CP_LOG_TO=$HTTP_ALIAS_TARGET/$http_log_path if [ "$product" = "emulator" ]; then CP_IMG_FROM=$prj_dir/out/target/product/generic CP_IMG_TO=$HTTP_ALIAS_TARGET/$http_img_path/generic else CP_IMG_FROM=$prj_dir/out/target/product/$product CP_IMG_TO=$HTTP_ALIAS_TARGET/$http_img_path/$product fi LOG_HTTP_URL=$HTTP_ALIAS/$http_log_path IMG_HTTP_URL=$HTTP_ALIAS/$http_img_path/ PRJ_ABS_PATH=$prj_dir IMG_ZIP_NAME=$1-$product.zip RELEASE_PATH=$HOME/release/$1 #dump else echo "Not found project directory: $prj_dir" exit 1 fi # remove old log file if [ -f $MAKE_LOG ]; then rm $MAKE_LOG fi if [ -f $MAKE_LOG_TAIL ]; then rm $MAKE_LOG_TAIL fi } update_prj() { has_changed=1 echo "1. SSH_AUTH_SOCK: $SSH_AUTH_SOCK, SSH_AGENT_PID: $SSH_AGENT_PID" >$SSH_ENV_LOG /usr/bin/keychain id_rsa source ~/.keychain/$HOSTNAME-sh echo "2 after keychain. SSH_AUTH_SOCK: $SSH_AUTH_SOCK, SSH_AGENT_PID: $SSH_AGENT_PID" >>$SSH_ENV_LOG #eval $(ssh-agent) #SSH_AUTH_SOCK="$(find /tmp/ssh-*/ -perm 0600 -type s -user builder -group builder -name 'agent*' | head -n 1)" if /home/builder/bin/auto_ssh_add;then # update projects if /usr/local/bin/rrdt update -f 1>$UPDATE_LOG 2>&1;then if [ "$(tail -n 1 $UPDATE_LOG)" = "This project has changed" ];then has_changed=0 # 0 is true elif [ "$(tail -n 1 $UPDATE_LOG)" = "This project has conflict in updating" ];then # reset to HEAD because of some files modified in last time building /usr/local/bin/rrdt forall git reset --hard HEAD 1>>$UPDATE_LOG 2>&1 /usr/local/bin/rrdt update 1>>$UPDATE_LOG 2>&1 if [ "$(tail -n 1 $UPDATE_LOG)" = "This project has changed" ];then has_changed=0 # 0 is true fi fi echo "OK: rrdt update success at $(date)" >>$UPDATE_LOG else echo "ERROR: rrdt update failed at $(date)" >>$UPDATE_LOG fi else echo "ssh-add failed at $(date)" >$UPDATE_LOG fi # clean the intermediate files of building /usr/local/bin/rrdt forall git clean -fd 1>>$UPDATE_LOG 2>&1 return $has_changed } # killed to avoid too many ssh-agent process # [ -n "$SSH_AGENT_PID" ] && kill "$SSH_AGENT_PID" do_make() { # setup env and do config for compile source env_setup ./makeMtk $1 new 1>>$MAKE_LOG 2>&1 && MAKE_RESULT=SUCCESS if [ "$MAKE_RESULT" = "SUCCESS" ]; then ./makeMtk $1 otapackage fi tail -n 100 $MAKE_LOG > $MAKE_LOG_TAIL } do_copy() { [ -d "$CP_LOG_TO" ] || mkdir -p $CP_LOG_TO ls $CP_LOG_FROM/*.log 1>/dev/null 2>&1 && cp $CP_LOG_FROM/*.log $CP_LOG_TO ls $CP_LOG_FROM/*.log_err 1>/dev/null 2>&1 && cp $CP_LOG_FROM/*.log_err $CP_LOG_TO [ -d "$CP_IMG_TO" ] || mkdir -p $CP_IMG_TO # remove old files rm $CP_IMG_TO/* ls $CP_IMG_FROM/*.img 1>/dev/null 2>&1 && cp $CP_IMG_FROM/*.img $CP_IMG_TO/ ls $CP_IMG_FROM/*.bin 1>/dev/null 2>&1 && cp $CP_IMG_FROM/*.bin $CP_IMG_TO/ ls $CP_IMG_FROM/*.txt 1>/dev/null 2>&1 && cp $CP_IMG_FROM/*.txt $CP_IMG_TO/ ls $CP_IMG_FROM/*.zip 1>/dev/null 2>&1 && cp $CP_IMG_FROM/*.zip $CP_IMG_TO/ if [ -f "$CP_IMG_FROM/kernel" ]; then cp $CP_IMG_FROM/kernel $CP_IMG_TO/ fi # copy AP database if [ -f "$PRJ_ABS_PATH/mtk/src/cgen/APDB_MT6516_S01_10YW1048MP_W11.13" ]; then cp $PRJ_ABS_PATH/mtk/src/cgen/APDB_MT6516_S01_10YW1048MP_W11.13 $CP_IMG_TO/ fi # copy BP database if [ -f "$PRJ_ABS_PATH/mtk/src/custom/common/modem/e1k16_ad6548_gprs_gemini/BPLGUInfoCustomApp_MT6516_S01_MAUI_10A_W10_48" ]; then cp $PRJ_ABS_PATH/mtk/src/custom/common/modem/e1k16_ad6548_gprs_gemini/BPLGUInfoCustomApp_MT6516_S01_MAUI_10A_W10_48 $CP_IMG_TO/ fi chmod a+r $CP_IMG_TO/* cd $CP_IMG_TO /usr/bin/zip $IMG_ZIP_NAME *.txt *.bin *.img APDB_MT6516* BPLGUInfoCustomApp_MT6516* chmod a+r $IMG_ZIP_NAME cd - } do_release() { version=`grep -r "ro.build.display.id" $CP_IMG_FROM/system/build.prop` # cut "ro.build.display.id=" version=${version#ro.build.display.id=} # cut date such as "-2011-06-03_00:16:06" version=`echo $version | sed 's/-201[1-9]-[0-1][0-9]-[0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9]//g'` release_imgs=$version.zip release_otapackage=$version-update.zip echo $release_imgs echo $release_otapackage # if directory not exists, create [ -d "$RELEASE_PATH" ] || mkdir -p $RELEASE_PATH # if not a new version, do nothing if [ -f "$RELEASE_PATH/$release_imgs" ]; then echo "Existed version, ignore" return fi product=$2 cp $CP_IMG_TO/$IMG_ZIP_NAME $RELEASE_PATH/$release_imgs cp $CP_IMG_TO/$product-ota-*.zip $RELEASE_PATH/$release_otapackage send_release_mail $1 $2 } send_build_mail() { ~/bin/send_make_email $1 $2 $MAKE_RESULT $LOG_HTTP_URL $IMG_HTTP_URL } send_release_mail() { ~/bin/send_make_email $1 $2 RELEASE $LOG_HTTP_URL $HTTP_ALIAS_RELEASE/$1 } init_prj_cfg() { prj_name=$1 script=`pwd`/build/mtkcp/$prj_name if [ -x $script ]; then echo "execute script before building" $script else echo "script not exist, ignore" fi } #------------------------------------------------------------ #------------------------- Main Loop ------------------------ #------------------------------------------------------------ if [ -n "$1" -a -n "$2" ]; then initialize $1 $2 if update_prj; then init_prj_cfg $1 do_make $2 do_copy send_build_mail $1 $2 do_release $1 $2 fi else echo "Usage: ./auto_make <project_dir> [<product>], such as ./auto_make 6000 emulator" echo "<project_dir>: directory name of project which to be built" echo "<product>: target product which must in output by ./makeMK -listp" exit 1 fi
缺点:在cron任务中执行这个脚本之前需要ssh-agent已经存在。还没有找到办法解决这个问题。
在cron任务中执行ssh-agent和ssh-add之后,虽然创建了ssh-agent进程,但随后的命令需要用到ssh时,仍然不能通过ssh鉴权
auto_make用到的send_mail脚本内容:
#!/bin/sh # send_make_email: send mail for compile project # # generate_email() { # --- Arguments project=$1 product=$2 result=$3 generate_email_header $project $product $result generate_email_body $project $product $result generate_email_footer $project } generate_email_header() { # --- Email (all stdout will be the email) # Generate header project=$1 product=$2 result=$3 cat <<-EOF To: $RECIPIENTS Subject: make [$project] [$product] [$result] Content-Type: text/plain; charset=utf-8 EOF } generate_email_footer() { project=$1 SPACE=" " cat <<-EOF --${SPACE} $project EOF } generate_email_body() { project=$1 product=$2 result=$3 echo "/n---------------------------------------------------/n" echo "This is an automated email for $project building." if [ "$result" = "FAILED" -a -n "$MAKE_LOG_URL" ]; then echo "" echo "There is some error in building, please check result from followed url:" echo $MAKE_LOG_URL fi if [ "$result" = "SUCCESS" -a -n "$MAKE_IMG_URL" ]; then echo "" if [ "$product" = "emulator" ];then echo "You can get imgs from followed url:" else echo "You can get imgs and kernel from followed url:" fi echo $MAKE_IMG_URL fi if [ "$result" = "RELEASE" -a -n "$MAKE_IMG_URL" ]; then echo "" echo "You can get new version from followed url:" echo $MAKE_IMG_URL fi echo "/n---------------------------------------------------" } send_mail() { /usr/bin/msmtp -t } # ---------------------------- main() # --- Constants LOGFILE=~/send_make_email.log MAKE_LOG_URL= MAKE_IMG_URL= DATE=$(date) echo "/n$DATE:" >> $LOGFILE # --- Main loop if [ -n "$1" -a -n "$2" -a -n "$3" ]; then if [ "$3" = "RELEASE" ]; then RECIPIENTS=$(git config hooks.mailinglistrelease) else RECIPIENTS=$(git config hooks.mailinglist) fi if [ -z $RECIPIENTS ]; then echo "Failed: PROJECT: $1. CAUSE: recipients is null!" >> $LOGFILE exit 1 fi if [ -n "$4" ]; then MAKE_LOG_URL=$4 fi if [ -n "$5" ]; then MAKE_IMG_URL=$5 fi generate_email $1 $2 $3 | send_mail echo "send to $RECIPIENTS" >>$LOGFILE echo "send finished" >>$LOGFILE exit 0 else echo "Usage: ./send_make_email <project> <product> <result> [<log_url> <img_url>]" echo "<project>: name of project which has done compile" echo "<product>: which product of project" echo "<result>: result of compile, must be one of 'SUCCESS', 'FAILED', 'RELEASE'" echo "<log_url>: url of make log files" echo "<img_url>: url of img files" echo "Failed: CAUSE: invalid arguments" >> $LOGFILE exit 1 fi
auto_make用到的auto_ssh_add:
#!/usr/bin/expect spawn ssh-add # spawn /usr/bin/keychain id_rsa expect "Enter passphrase*.ssh/id_rsa:" send "builder/r" interact
ubuntu本地:
#!/bin/bash # this is a automatically compile script # add env source $HOME/.profile UPDATE_RESULT=FAILED MAKE_RESULT=FAILED UPDATE_LOG=update.log MAKE_LOG=make.log MAKE_LOG_TAIL=make_tail.log SSH_ENV_LOG=ssh_env.log HTTP_ALIAS=http://192.168.110.94/build HTTP_ALIAS_TARGET=/home/builder/result CP_LOG_FROM= CP_LOG_TO= CP_IMG_FROM= CP_IMG_TO= LOG_HTTP_URL= IMG_HTTP_URL= dump() { echo "CP_LOG_FROM: $CP_LOG_FROM" echo "CP_LOG_TO: $CP_LOG_TO" echo "CP_IMG_FROM: $CP_IMG_FROM" echo "CP_IMG_TO: $CP_IMG_TO" echo "LOG_HTTP_URL: $LOG_HTTP_URL" echo "IMG_HTTP_URL: $IMG_HTTP_URL" } initialize() { prj_dir=$HOME/work/projects/$1 product=$2 is_valid_product= if [ -d $prj_dir ]; then cd $prj_dir ./makeMtk listp | grep -q "/<$product/>" && is_valid_product=true if [ ! $is_valid_product ]; then echo "invalid product: $product" exit 1 fi # http_log_path=$1/build_log/ # http_img_path=$1/img # CP_LOG_FROM=$prj_dir/out/target/product # CP_LOG_TO=$HTTP_ALIAS_TARGET/$http_log_path # if [ "$product" = "emulator" ]; then # CP_IMG_FROM=$prj_dir/out/target/product/generic # CP_IMG_TO=$HTTP_ALIAS_TARGET/$http_img_path/generic # else # CP_IMG_FROM=$prj_dir/out/target/product/$product # CP_IMG_TO=$HTTP_ALIAS_TARGET/$http_img_path/$product # fi # LOG_HTTP_URL=$HTTP_ALIAS/$http_log_path # IMG_HTTP_URL=$HTTP_ALIAS/$http_img_path/ #dump else echo "Not found project directory: $prj_dir" exit 1 fi # remove old log file if [ -f $MAKE_LOG ]; then rm $MAKE_LOG fi if [ -f $MAKE_LOG_TAIL ]; then rm $MAKE_LOG_TAIL fi } update_prj() { has_changed=1 echo "1. SSH_AUTH_SOCK: $SSH_AUTH_SOCK, SSH_AGENT_PID: $SSH_AGENT_PID" >$SSH_ENV_LOG /usr/bin/keychain id_rsa source ~/.keychain/$HOSTNAME-sh echo "2 after keychain. SSH_AUTH_SOCK: $SSH_AUTH_SOCK, SSH_AGENT_PID: $SSH_AGENT_PID" >>$SSH_ENV_LOG if $HOME/work/code/script_study/tools/auto_ssh_add;then # update projects if /usr/local/bin/rrdt update 1>$UPDATE_LOG 2>&1;then if [ "$(tail -n 1 $UPDATE_LOG)" = "This project has changed" ];then has_changed=0 # 0 is true fi echo "OK: rrdt update success at $(date)" >>$UPDATE_LOG else echo "ERROR: rrdt update failed at $(date)" >>$UPDATE_LOG fi else echo "ssh-add failed at $(date)" >$UPDATE_LOG fi return $has_changed } do_make() { # clean the intermediate files of building /usr/local/bin/rrdt forall git clean -fd 1>>$UPDATE_LOG 2>&1 # setup env and do config for compile source env_setup ./makeMtk $1 new 1>>$MAKE_LOG 2>&1 && MAKE_RESULT=SUCCESS ./makeMtk $1 otapackage tail -n 100 $MAKE_LOG > $MAKE_LOG_TAIL } #------------------------------------------------------------ #------------------------- Main Loop ------------------------ #------------------------------------------------------------ if [ -n "$1" -a -n "$2" ]; then initialize $1 $2 if [ -n "$3" -a "$3" = "true" ]; then update_prj fi do_make $2 else echo "Usage: ./auto_make <project_dir> [<product>] [<update>], such as ./auto_make 6000 emulator" echo "<project_dir>: directory name of project which to be built" echo "<product>: target product which must in output by ./makeMK -listp" echo "<update>: true: will update this project, else false" exit 1 fi