SSH自动部署

个人是windows环境,目前开发的过程当中,有些项目须要一会儿部署到不少的linux服务器上。写了个脚本可以自动上传文件和执行部署任务。完成这个任务须要的条件包括SSH配置和一个执行脚本。node

准备linux

1. 本地环境配置git

    install scpshell

    install cygwinwindows

2. SSH settingbash

   step1: install openSSH服务器

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

   

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

   step2: produce a ssh keythis

   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 a hidden 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

    ipaddress    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@ipaddress -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 command

./deploy_test

 

Congratulations! You make it. Great!

相关文章
相关标签/搜索