git+python+ Management machine for configuration file version control

Source: Internet
Author: User

With the company's business more and more, the grouping of business is more and more, like Nginx,haproxy,lvs and so will also distinguish the multi-group configuration, so I want to write a simple set of support for multi-person operation/version control/Fast rollback of a set of management solutions.
A brief description of my design ideas
Through a single springboard to git pull the Nginx configuration file for each business, in the use of the Board modified after the implementation of the script to implement the grammar test, the test through the direct update of the line, such as the update found that the bug implementation of fast rollback, all the operation through the script execution, operations aware of the existence of Git

Now write the results for the above purpose

A:10.0.4.12 Configuration Management Center
b:10.5.8.19 on-line tengine (can be multiple)

1, log on to the Profile Management Server, the user logged on as a personal user to modify the configuration operation
2. Update the latest configuration file

Will download the configuration to their home directory, in case there are multiple simultaneous operation problems
The script content is posted here

#!/bin/shpid= ' PS EF | grep commit.sh| Grep-v grep | Wc-l ' If [$PID-eq 2]then continueelse echo-e "\033[41;37;1m already someone usering \033[0m" Exit 4fiparamet  Er1= ' echo ' $ ' |sed s/[[:space:]]//g ' parameter2= ' echo ' $ |sed s/[[:space:]]//g ' if [$#-eq 1]then if [$parameter 1 = = ' pull ' and then cd/home/' WhoAmI '/deploy/git fetch--all >/dev/null 2>&1 git reset-- Hard Origin/master >/dev/null 2>&1 git pull exit 3 Fifiif [$#-ne 2]then echo-e "\033[41; 37;1m Update configs----->\c\033[0m "echo-e" \033[32;1m commit.sh pull \033[0m "echo-e" \033[41;37;1m Delete f Ile----->\c\033[0m "ECHO-E" \033[32;1m commit.sh rm FILENAME \033[0m "ECHO-E" \033[41;37;1m commit configs--- -->\c\033[0m "Echo-e" \033[32;1m commit.sh changeinf APPNAME \033[0m "ECHO-E" \033[41;37;1m Stop action-----&G t;\c\033[0m "Echo-e" \033[32;1m commit.sh APPNAME stop \033[0m "Exit 2fiif [$parameter2 = = ' Stop ']then sudo python/data/config/change_run.py-s $parameter 1-a stop exit 7figit config--global user.em Ail ' whoami ' @meicai. cn git config--global user.name ' whoami ' cd/home/' whoami '/deploy/git Add. Git statusgit diff masteri f [$parameter 1 = = ' rm ']then cd/home/' whoami '/deploy git rm $parameter 2 if [$?-eq 0] then ECHO-E        "\033[32;1m Delete is complated \033[0m" Exit 2 else echo-e "\033[41;37;1m Delete error \033[0m" Exit 3 Fifiecho-e "\033[32;1m" Confirm your change Y (commit) or N (interrupt): \033[0m "Read-p" "ynif [$YN = ="     Y "-o $YN = =" Y "]then sudo rsync-av--delete--progress--exclude. git//home/' WhoAmI '/deploy//data/config/deploy/        If [$?-eq 0] then sudo python/data/config/sync.py-s $parameter 2 return_status=$? If [$return _status-eq 0] then git commit-m $parameter 1 git push if [$?-eq 0               ] Then Echo-e "\033[32;1m $parameter 1 push git server is complate \033[0m" Else Echo-e "\033[41;37;1 M push error \033[0m "fi elif [$return _status-eq 3];then exit 7 Else Ech        O-E "\033[41;37;1m Reload error \033[0m" ECHO-E "\033[41;37;1m already roll-back \033[0m" Exit 6 Fi else echo-e "\033[41;37;1m sync error \033[0m" Exit 5 Fielse Exit 3fipython/data/config/ Change_run.py-s $parameter 2-a Start

3, through the tools such as vim to modify the configuration file, note that if there are deleted files, after RM is also executed, commit.sh RM FILENAME (relative path)
4, after the modification of the latest configuration
sh commit.sh changeinfo PRODUCTNAME
Here are two parameters to do the following
Changeinfo: This operation you modified what
PRODUCTNAME: You modified the configuration of which project

here the script will actually call sync.py such a synchronization script
Paste the details here

#-*-Coding:utf-8-*-import osimport argparseimport configparserimport ansible.runnerimport jsonimport sysimport Timeim Port Getpass parser = Argparse. Argumentparser (description= "Useage:python sync.py-s APPNAME") parser.add_argument ('-s ', help= "Service name", REQUIRED=TRUE) def Sync (): Runner_sync = Ansible.runner.Runner (module_name= ' Synchronize ', module_args= "de        Lete=yes "+" + "src=" + app_src + "+" dest= "+ app_dest +" "+" dest_port= "+ Dest_port, PATTERN=ARGS.S, forks=10) datastructure = Runner_sync.run () data = Datastructure.get (' contacted ') return datadef Tar_ ZCVF (): Runner_command = Ansible.runner.Runner (module_name= ' Shell ', module_args= "TAR-ZCVF" + "+"/tm p/"+ ARGS.S +". tar.gz "+" + app_dest, PATTERN=ARGS.S, forks=10) Command_runner = Runner_c      Ommand.run () return command_runnerdef command (): Runner_command = Ansible.runner.Runner (module_name= ' shell ',   Module_args=res_service, PATTERN=ARGS.S, forks=10) Command_runner = Runner_command.run () re Turn Command_runnerdef roll_back (): Runner_command = Ansible.runner.Runner (module_name= ' Shell ', module_a        rgs= "CD/" + ";" + "RM-RF" + "" + App_dest + ";" + "TAR-XVF" + "+"/tmp/"+ ARGS.S +". tar.gz "+" + "-C" + "/", PATTERN=ARGS.S, forks=10) Command_runner = Runner_command.run () return Command_runnerif __n ame__ = = "__main__": Config=configparser.configparser () config.read (U '/data/config/app_dir.conf ') args = PARSER.P Arse_args () try:app_src = Config.get (args.s, ' src ') app_dest = Config.get (ARGS.S, ' dest ') Res_servi CE = config.get (args.s, ' res_service ') Dest_port = Config.get (ARGS.S, ' dest_port ') running = Config.get (ARGS.S , ' running ') Current_User = Config.get (ARGS.S, ' Current_User ') except ConfigParser.NoSectionError:print ' W        Ithout this app 'Sys.exit (5) if int (running) = = 0:TAR_ZCVF () for k,v in Sync (). Items (): If V.get (' failed '):                    Print K,v.get (' msg ') exit (6) Else:if v.get (' changed ') = = False:                 Print (' \033[1;31;40m ') print K + ": Is not changed" print (' \033[0m ')                    Else:print (' \033[1;32;40m ') print K + ": Change complated" Print (' \033[0m ') #time. Sleep (5) cmd = Command (). Get (' contacted '). Get (k). Get (' cmd ') stdout = command (). Get (' contacted '). Get (k). Get (' stdout ') stderr = Command ().                        Get (' contacted '). Get (k). Get (' stderr ') if Stdout:print (' \033[1;32;40m ')                    print "cmd:" + cmd print stdout print (' \033[0m ')            Else            Print stderr roll_back () sys.exit (1) else:print (' \033 [1;32;40m ') Print Current_User + "" + "UserD" Print (' \033[0m ') sys.exit (3)

I just added a file. Here I get the correct syntax for the configuration by hostname command (directly take the test on the line without nginx-t)

Finally, there will be output indicating that the latest configuration has been uploaded to the git server

5, to be completed after the execution of commit.sh Tengine_mall stop this operation, convenient for others to make changes, because I do the operation of protection, once there are 1 people are performing the modification, the other people are forbidden to operate
Format of Last Post configuration file
/data/config/app_dir.conf
[Tengine_mall]
src =/data/config/deploy/tengine/tengine_mall/nginx
Dest =/tmp/test
Res_service = "hostname"
Dest_port = 50022
Running = 0
Current_User = Zhangsan

git+python+ Management machine for configuration file version control

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.