码迷,mamicode.com
首页 > 其他好文 > 详细

SSH自动部署

时间:2014-09-20 21:32:19      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   strong   for   

我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上。写了个脚本能够自动上传文件和执行部署任务。完成这个任务需要的条件包括SSH配置和一个执行脚本。

准备

1. 本地环境配置

    install scp

    install cygwin

2. SSH setting

   step1: install openSSH

    find the cygwin installation application like setup-x86_64.exe and double click to install the openSSH, searc the openSSH

    bubuko.com,布布扣

 and you will find the openSSH package and click next to setup the package

bubuko.com,布布扣

   step2: produce a ssh key

   open the cygwin window to generate and ssh key using

ssh-keygen

  also, you can append with some params to generate a ssh key but it‘s ok for us now.

  you can find an hiden file in the current folder called .ssh and there are two files id_rsa  id_rsa.pub

step 3: create connection between local and the remote server

  here  we‘ll login to the remote server without password using

ssh-copy-id username@ipaddress

  explanation:

    username  is what you login with to the remote server

    ipadress    is the target remote server‘s ip

answering yes to the questions and finally you can try to login the remote server with comman like

ssh username@ipadress -p port

you can omit the port if you are using the default port number 22

step4: create an exec script for deployment

If you are not familiar with shell, so you have to read basic about this. I‘ll give an demonstrator without explaining. here‘s the code of deploy_test file

#! /bin/bash

tests=(ip1 ip2 ip3)
length=${#tests[@]}
for ((i=0; i<$length; i++))
do
node=${tests[$i]}
echo $node
# copy file to test server
scp /cygdrive/d/git/test.zip $node:~
# operation to mv the etcd file and restart the etcd service

#first step to backup the previous version of test
ssh $node "sudo mv /opt/app/testrepo/test.zip /opt/app/etcdrepo/testbak"

#stop the etcd service
ssh $node  "sudo mv /home/op2/test.zip /opt/app/testrepo "
done

step5: make script work out

exec the script using comman

./deploy_test

 

Congratulations! You make it. Great!

 

SSH自动部署

标签:des   style   blog   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/ilawrence/p/3983587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!