Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Script example of client network card status management of cloud server cluster computing node in python

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

The purpose of this article is to share with you the content of a script example of client Nic state management of CVM cluster computing nodes in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Calculate the node server code, respond to the client http request, and complete the state management of the virtual machine network card.

#! / usr/bin/pythonfrom wsgiref.simple_server import make_server import jsonimport os import signal import shlex import subprocess from eventlet.green import subprocess as green_subprocess from eventlet import greenthread reserved_ip = "10.38.88.10" # this service rejects any request except those from 10.38.88.10 reserved_http_method = "POST" listen_port = 9999 def create_process (cmd, root_helper=None, addl_env=None): if root_helper: cmd = shlex.split (root_helper) + cmd cmd = map (str) Cmd) env= os.environ.copy () if addl_env: env.update (addl_env) obj = subprocess_popen (cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) return obj Cmd def execute (cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): try: obj, cmd = create_process (cmd, root_helper=root_helper, addl_env=addl_env) _ stdout _ stderr = (process_input and obj.communicate (process_input) or obj.communicate ()) obj.stdin.close () m = ("\ nCommand:% (cmd) s\ nExit code:% (code) s\ nStdout:% (stdout) r\ n"Stderr:% (stderr) r")% {'cmd': cmd 'code': obj.returncode,' stdout': _ stdout, 'stderr': _ stderr} if obj.returncode and check_exit_code: raise RuntimeError (m) finally: greenthread.sleep (0) return return_stderr and (_ stdout) _ stderr) or _ stdoutdef _ subprocess_setup (): signal.signal (signal.SIGPIPE, signal.SIG_DFL) def subprocess_popen (args, stdin=None, stdout=None, stderr=None, shell=False, env=None): return green_subprocess.Popen (args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, preexec_fn=_subprocess_setup, close_fds=True, env=env) def app Start_response): source_ip = environ ['REMOTE_ADDR'] request_method = environ [' REQUEST_METHOD'] if source_ip! = reserved_ip: status = '400 Bad Request' # Not Allowed headers = [(' Content-type', 'application/json')] # HTTP Headers start_response (status Headers) return ['{"msg": "Not Acceptable"}'] if request_method! = reserved_http_method: status = '405 Method Not Allowed' # Not Allowed headers = [(' Content-type', 'application/json')] # HTTP Headers start_response (status Headers) return ['{"msg": "Method Not Allowed"}'] request_body_size = int (environ.get ('CONTENT_LENGTH') 0) request_body = environ ['wsgi.input'] .read (request_body_size) request_body = json.loads (request_body) param_vmid = request_body [' id'] param_status = request_body ['status'] hex_id = str (hex (param_vmid)) [2:] for i in xrange (8-len (hex_id)): Hex_id = "% d% s"% (0 Hex_id) instance_name = "instance-%s"% hex_id mgmt = "virsh domiflist% s"% instance_name cmd = mgmt.split () try: retv = execute (cmd,root_helper=None) except Exception as e: status = '400 Bad Request' # Not Allowed headers = [(' Content-type' 'application/json')] # HTTP Headers start_response (status Headers) return ['{"msg": "VM Not Found"}'] result = [] for line in retv.split ("\ n"): if "br-wan" in line: result.append (line) domif = result [0] .split () [0] .strip () if "-" = = Domif: status = '200 OK' # Not Allowed headers = [(' Content-type' 'application/json')] # HTTP Headers start_response (status, headers) return [' {"msg": "Success"}'] else: mgmt = "ip link set% s% s"% (domif,param_status) cmd = mgmt.split () retv = execute (cmd Root_helper=None) status = '200 OK' # Not Allowed headers = [(' Content-type', 'application/json')] # HTTP Headers start_response (status, headers) return [' {"msg": "Success"}'] httpd = make_server (', listen_port, app) httpd.serve_forever ()

The foreground program calculates the ip of the computing node (host) where the client is located, and then calls the http request to realize the function.

Def kvm_conn_mgmt (self,instance_name,conn_status): if connstrated statuses filled up' and connexual statusinstalled downloads: return (resultcode.BadRequest, {"msg": "Vps conn status must be up/down"}) if len (instance_name.strip ()) = 0: return (resultcode.BadRequest) {"msg": "Vps name should not be empty. "}) sql=" select I.idMagnedcn.hostkeeper IP from instances as iGrad computedexamples as cn where i.displayroomnameplates% s' and i.deleted=0 and i.host=cn.hypervisor_hostname "% instance_name result = db.execNovaSQL (sql) if len (result) = 0: return (resultcode.BadRequest, {" msg ":" Vps with name'% s' does Not Exist.) "% instance_name}) comp_host = result [0] [1] vmid = result [0] [0] msg = {" status ": conn_status," id ": vmid} f = urllib.urlopen (" http://%s:9999"%comp_host, " Json.dumps (msg)) content = f.read () if "Success" in content: return (resultcode.Success, {"msg": "Success"}) else: return (resultcode.BadRequest, {"msg": content})

The compute node runs the service listening port 9999 and restricts calls from 10.38.88.10

Iptables-An INPUT-p tcp-s 10.38.88.10-- dport 9999-j ACCEPT Thank you for reading! This is the end of this article on "A script example of client Nic state management on cloud server cluster computing nodes in python". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report