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!

相關文章
相關標籤/搜索