Simple Management of haproxy configuration files,

Source: Internet
Author: User
Tags haproxy

Simple Management of haproxy configuration files,

Version: python3
Function: simple query, add, and delete haproxy configuration files.
Procedure:
1. Select the appropriate options as prompted
2. After Entering the selected option, write the corresponding parameters as prompted.
3. the query function returns the query result. The add, delete, and rollback functions are not displayed.

Flowchart:

1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms10 timeout client 50000ms11 timeout server 50000ms12 option limit 14 listen stats: 888815 stats enable16 stats uri/admin17 stats auth admin: 123418 19 frontend oldboy. org20 bind 0.0.0.0: 8021 option httplog22 option httpclose23 option forwardfor24 log global25 acl www hdr_reg (host)-I www. oldboy. org26 use_backend www.oldboy.org if www27 28 backend www. oldboy. org29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 300030 31 backend buy. oldboy. oracle 32 server 100.1.7.90 100.1.7.90 weight 20 maxconn 300033 34 backend nb. oldboy. org35 server 100.1.7.9 100.1.7.9 weight 20 maxconn 300036 37 backend ttt. oldboy. org38 server 100.1.7.9 100.1.7.9 weight 20 maxconn 300039 40 backend www. oldboy. com41 server 192.168.1.1 192.168.1.1 weight 30 maxcoon 3000Haproxy configuration example file 1 import OS 2 import OS. path 3 import time 4 def query (back_name): # define the query function 5 flage = False # [find the row where the backend is located through the backend specified by the user, find the next row starting with backend and add the content in the middle of the two backend to the list] 6 query_list = [] 7 with open ('Ha ', 'R') as ha: # Open the configuration file 8 for line in ha: # Get the configuration file content 9 if line. strip () = 'backend % s' % (back_name): # obtain the content of the row where the backend is located. 10 flage = True # Use flag 11 continue 12 if line. strip (). startswith ('backend'): # determines whether the row is OK. Start with backend 13 flage = False 14 if flage and line. strip (): # determine whether flage is true 15 query_list.append (line. strip () # Add the matched content to the list separately. 16 return query_list 17 18 19 20 21 def add_backend (back_name, ip, weight, maxcoon ): # define the Add function 22 current_title = 'backend % s' % back_name # obtain the content of the row where the user input backend is located 23 current_record = 'server % s weight % s maxcoon % s' % (ip, ip, weight, maxcoon) # obtain the server 24 fetch_list = query (back_name) You want to add # obtain the specified B 25 if fetch_list: # if the added backend exists 26 if current_record in fetch_list: # if the added child item exists in backend, do not perform the 27 pass # ''' operation. [main idea: store the content under the backend to be added and the existing content to a list, write the content before 28 # backend to a new file, write the backend and the sever to be added to the list, and add the remaining content. ''' 29 else: 30 fetch_list.append (current_record) # If no backend exists, add 31 flage = False to the specified backend. # define the flag, it is used to determine the content to be written between backend and backend 32 has_write = False # defines the identifier bit to determine whether the content to be added already exists 3 3 with open ('Ha ', 'R') as ha, open ('ha. new', 'w') as new_ha: # Open the ha configuration file and create a new file 34 for line in ha: # iterate over the configuration file 35 if line. strip () = current_title: # determine whether the file contains backend 36 flage = True 37 new_ha.write (line) # Write backend to the new configuration file 38 continue 39 if flage and line. strip (). startswith ('backend'): 40 flage = False 41 if flage: 42 if not has_write: #43 for line in fetch_list: # Get the backend to be added, so server 44 temp = "% S \ n" % ("* 8, line) 45 new_ha.write (temp) # Write 46 has_write = True 47 else: 48 new_ha.write (line) to a new file) # Write the remaining content to the new file 49 else: # If the backend entered by the user does not exist in the configuration file 50 with open ('Ha ', 'R') as ha, open ('ha. new', 'w') as new_ha: 51 for line in ha: 52 new_ha.write (line) # Write the content of the configuration file into the new file 53 new_ha.write ('\ n' + current_title) # Write the backend entered by the user into the new file 54 tem = ''' * 8 + current_record + '\ n' 55 new_ha.write (' \ n' + tem) # Writing user-entered sever into a file 56 OS. rename ('ha', 'ha _ Bak') 57 OS. rename ('ha. new ', 'ha') 58 59 def quit_time (): # exit 60 for I in [, 1]: 61 print (' \ 033 [32; 1 MB query is complete. The main menu is being returned ..... \ 033 [1m ', I) 62 time. sleep (1) 63 64 def del_backend (back_name, ip, weight, maxcoon): # similar to adding, by saving the required server to a list, finally, write the remaining content and the content in the list to the new file 65 current_title = 'backend % s' % back_name # implement the function 66 current_record = 'server % s by renaming the file weight % s maxcoon % s' % (ip, ip, weight, Maxcoon) 67 fetch_list = query (back_name) # obtain the records under the specified backend 68 if fetch_list: # if the added backend has 69 if current_record in fetch_list: # If the added sub-item exists in backend, 70 fetch_list.remove (current_record) 71 else: 72 pass 73 flage = False 74 has_write = False 75 with open ('Ha ', 'R ') as ha, open ('ha. new', 'w') as new_ha: 76 for line in ha: 77 if line. strip () = current_title: 78 flage = True 79 new_ha.write (line) # Write the backend to the new configuration File 80 continue 81 if flage and line. strip (). startswith ('backend'): 82 flage = False 83 84 if flage: 85 if not has_write: 86 for line in fetch_list: 87 temp = "% s \ n" % ("* 8, line) 88 new_ha.write (temp) 89 has_write = True 90 else: 91 new_ha.write (line) 92 else: 93 with open ('Ha ', 'R') as ha, open ('ha. new', 'w') as new_ha: 94 for line in ha: 95 new_ha.write (line) 96 new_ha.write ('\ n' + current_title) 97 Tem = ''' * 8 + current_record + '\ n' 98 new_ha.write (' \ n' + tem) 99 if OS. path. isfile ('Ha _ Bak'): # to rename a file, you must determine whether the file exists. When the backup file does not exist for the first time, an error 100 OS will be reported. remove ('Ha _ Bak') 101 OS. rename ('ha', 'ha _ Bak') 102 OS. rename ('ha. new ', 'ha') 103 104 def fallback (): # roll back the code and rename the backup file and the current configuration file to implement the 105 OS function. rename ('ha', 'ha _ new') 106 OS. rename ('Ha _ Bak', 'ha ') 107 108 109 while True: 110 print (' \ 033 [34; 1 m ###################################### # \ 033 [1m ') # Print option 111 user_enter = input (''' \ 033 [32; 1 m 1. query backend backend112 2. add backend113. delete backend114 4. roll back to the previous version 115. exit \ 033 [1m116. Enter the operation you want to select: ''') 117 if user_enter = '1': 118 back_name = input ('\ 033 [32; 1 m enter the name of the backend to be queried, example: www.oldboy.org: \ 033 [1 M ') 119 result = query (back_name) # Call the query function 120 if result: 121 print ('\ 033 [33; 1 m query results: \ 033 [1 M') 122 for I in result: 123 print (I) 124 quit_time () 125 continue126 else: 127 p Rint ('\ 033 [34; 1 m query error, backend does not exist, the result is blank! \ 033 [1m ') 128 elif user_enter = '2': 129 back_name = input (' \ 033 [32; 1m 1m enter the name of the backend to be queried, example: www.oldboy.org: \ 033 [1m ') 130 ip = input (' \ 033 [33; 1 m, enter the serverIP address to be added, example192.168.1.1: \ 033 [1m ') 131 weight = input ('\ 033 [34; 1 m, enter the server's weight: \ 033 [1 M') 132 maxcoon = input ('\ 033 [35; 1 m, enter maxcoon: \ 033 [1 M ') 133 add_backend (back_name, ip, weight, maxcoon) 134 print ('add finished') 135 quit_time () 136 elif user_enter = '3': 137 Back_name = input ('\ 033 [32; 1 m enter the name of the backend to be deleted, example: www.oldboy.org: \ 033 [1 M') 138 ip = input ('\ 033 [33; 1 m enter the serverIP address to be added, example192.168.1.1: \ 033 [1 M ') 139 weight = input (' \ 033 [34; 1 m enter the server's weight: \ 033 [1m ') 140 maxcoon = input (' \ 033 [35; 1 m, enter maxcoon: \ 033 [1m ') 141 del_backend (back_name, ip, weight, maxcoon) 142 print ('deleted successfully') 143 quit_time () 144 elif user_enter = '4': 145 try: 146 fallback () 147 bytes t FileNo TFoundError: # error 148 print ('\ 033 [31; 1 m rollback failed when the backup file does not exist. It is currently the latest version and cannot be rolled back! \ 033 [1m ') 149 bytes t FileExistsError: # The user repeat 150 print ('already the latest version') 151 quit_time () 152 elif user_enter = '5 ': 153 quit_time () 154 break155 else: #156 print ('\ 033 [31; 1 m when illegal input, incorrect option. Please select again! \ 033 [1m ')Program code

 

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.