设为首页 收藏本站
查看: 739|回复: 0

[经验分享] Jenkins Configuration(3)Shell Script

[复制链接]

尚未签到

发表于 2017-5-11 09:28:12 | 显示全部楼层 |阅读模式
Jenkins Configuration(3)Shell Script

Jenkins, we can make SVN, GIT, MAVEN, ANT working on that. But we need to restart JBOSS server, copy the package to working directory, remote access and sync files to other servers. So we need the help from Shell Script.



1. Basic Shell script and basic expect script
Here are the shell script, server-deploy.sh

#!/bin/sh



# copy the file to push directory

STORE_DIST=/android/work_space/push/projectname.war



rm -fr $STORE_DIST;



BUILD_TARGET=war

BUILD_WAR=$(ls $BUILD_TARGET/projectname-*-SNAPSHOT.war)



TMP=${BUILD_WAR%%.war}

unzip -q -d ${TMP} "$BUILD_WAR" ;



cp -r ${TMP} $STORE_DIST;



./server-autopush.exp



We need a exp file to remote access other server, because server-deploy.sh is running on the jenkins build server. We need to sync or rather say push the changes to our testing server, production server. Just note them as some other linux machine, and we need to remotely restart their JBOSS.



#!/usr/bin/expect -f

# December 26 2012

# This script can automatically push the files

#  to /opt/jboss-as/server/default/deploy/ on testing server.



# TIMEOUT

set timeout 20



# Login parameters

set server testing.server.com

set port 22

set user deploy

set rootpasswd 111111



# Logfile

log_file server-autopush-run.log



## Enable this and Disable the "spawn ssh ..." above if you are using ssh-key.

#spawn ssh -p $port $user@$server

spawn ssh -p $port -i /home/build/.ssh/id_rsa $user@$server



# Login as Root

expect "*]$\ " {send "su - root\r"}

expect "*assword:\ " {send "$rootpasswd\r"}



# Kill Jboss on testing server

expect "*]#\ " {send "kill -9 \`ps aux | grep /opt/jboss-as/bin/run.jar | grep -v grep | awk \'{print \$2}\'\`\r"}



# Sync the files from testing server to  

expect "*]#\ " {send "rsync -arz --delete --force -e \"ssh -i /home/rsync/.ssh/rsync_servers.ppk\" rsync@server.com:/android/work_space/push /opt/build/buildserver/\r"}



# Copy the files from /opt/build/buildserver/push to /opt/jboss-as/server/default/deploy on testing server

expect "*]#\ " {send "\\cp -rpa /opt/build/buildserver/push/* /opt/jboss-as/server/default/deploy/\r"}



# Remove direcotries data,log,work,tmp

expect "*]#\ " {send "\\rm -rf /opt/jboss-as/server/default/tmp\r"}

expect "*]#\ " {send "\\rm -rf /opt/jboss-as/server/default/work\r"}

expect "*]#\ " {send "\\rm -rf /opt/jboss-as/server/default/data\r"}



# Start Jboss on testing server

expect "*]#\ " {send "nohup /etc/init.d/jboss start > /tmp/nohup.out \r"}



# Exit Root

expect "*]#\ " {send "exit\r"}



# Exit User

expect "*]$\ " {send "exit\r"}



# Exit Expect

expect eof



But when I run this method on my server, I got these error messages

Error Message:
>log_file: command not found
>spawn: command not found
>expect: command not found
>expect: command not found
>expect: command not found
>expect: command not found
>expect: command not found  
>expect: command not found



That is only because I did not have expect on my machine.

Solution:
>yum update

>yum install tcl

>yum install expect



2. Improvement on SVN checkout process
One import improvement for the workspace working with SVN:

Repository URL: babababababalalalalalaal

Local module directory(optional) This is usually be ./

But I my project is a sub project, so I need some core codes standing there. So I make them svn update to one place



/opt/work_space

/opt/jenkins



So I change the ./ to mostly like this:

./../../../../work_space/branches/development/project/sub_projects/myproject



3. Take some parameters in shell script and expect script
3.1 Shell parameters
I made a test shell file like this test.sh

!/bin/sh

echo There are $# arguments to $0: $*

echo first argument: $1

echo second argument: $2

echo third argument: $3

echo fourth argument: $4

echo here they are again: $@



When I execute it with parameters

>./test.sh 1 2 3 4

There are 4 arguments to ./test.sh: 1 2 3 4

first argument: 1

second argument: 2

third argument: 3

fourth argument: 4

here they are again: 1 2 3 4



3.2 Expect parameters
And example in test.exp working with test.sh

append this line in test.sh

./test.exp h1 h2



test.exp content will be like this>

#!/usr/bin/expect -f



set arg1 [lindex $argv 0]

set arg2 [lindex $argv 1]



send $arg1\n

send $arg2\n



The result will be like this:

>./test.sh 1 2 3 4

There are 4 arguments to ./test.sh: 1 2 3 4

first argument: 1

second argument: 2

third argument: 3

fourth argument: 4

here they are again: 1 2 3 4

h1

h2



4. Some Tips about private/public key
Add one user named deploy

>/usr/sbin/useradd deploy



Give the user an password

>passwd deploy



Generate the key pair

>ssh-keygen -t rsa



Add the public key to authorize

>cd ~/.ssh

>vi authorized_keys

>chmod 711 ~/.ssh

>chmod 644 ~/.ssh/authorized_keys



Client Server tries to login to the remote server

>ssh -i /home/build/.ssh/id_rsa deploy@remote.server.com



Give the rights of directory to one user

>chown -R rsync:rsync /directory/name



rsync is the username, the second rsync is the group name.



Check the public key from private key
>ssh-keygen -f id_rsa -y



references:
http://s3tools.org/s3cmd

http://www.garron.me/mac/install-s3cmd-mac-os-x.html

http://wangyan.org/blog/s3cmd-how-to-use.html



http://osr507doc.sco.com/en/OSUserG/_Passing_to_shell_script.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-375749-1-1.html 上篇帖子: Template for Jenkins Jobs for PHP Projects----------http://jenkins-php.org/ 下篇帖子: Jenkins Configuration(4)Improve Shell Script Debug/Info Message
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表