問題の声明
問題の説明
Xenは、HVM仮想化の作業で積極的に使用されています。 多くの場合、仮想マシンのコンソールにアクセスする必要があります。これには、Xenを使用して北部にアクセスできないユーザーも含まれます。 このため、Xenaには仮想マシンごとにVNCコンソールを作成する機能がありますが、毎回VNCを介して手動で接続するのは不便です。
挑戦する
実行中の仮想マシンのリストとその中に埋め込まれたVNCアプレットを含むWebページを作成します。これはリンクをクリックして開くことができます。 PythonからXenを操作する方法を理解する途中。
どうした
実行中のdomUのリスト

VNCコンソール自体

どうでしたか
Pythonと軽量のCherrPyWebフレームワークを使用しました。
何で構成されていますか
vm_console.py # static # static/table.htm # domU, static/vnc.js # javascript VNC- static/vnc.htm # VNC-, static/tightvnc-jviewer.jar # java- VNC, http://www.tightvnc.com/ static/styles.css # domU, http://veerle-v2.duoh.com/blog/comments/a_css_styled_table/ static/images # static/images/bg_header.jpg static/images/bullet1.gif
彼自身skrpit
スクリプトのロジックは次のとおりです。
- domU VNC- java VNC-
必要なモジュールをインポートします:cherrypy自体、XENサーバーとやり取りするためのモジュール、およびVNCサーバーのアドレスとポートに一致する正規表現モジュール:
import cherrypy from xen.util.xmlrpcclient import ServerProxy import re
実行中のdomUのリストを取得し、テーブルを形成するためのクラス:
class xen(): """Communicates with xen via rpc""" @staticmethod def get_domains(): """Gets a dictionary with 'Domain Name':'VNC host:port' structure""" server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock') domains = server.xend.domains_with_state(True, 'all', 1) domain_list = {} for first_level in domains: # iterate through first level parameters for second_level in first_level: # iterate through second level parameters if second_level[0] == "name" and second_level[1] != "Domain-0": domain_list_current = second_level[1] if second_level[0] == "device": for third_level in second_level[1]: # iterate through third level subparameters if third_level[0] == "location" and re.match("\d+\.\d+\.\d+\.\d+\:\d\d\d\d", third_level[1]): domain_list[domain_list_current] = str(third_level[1]) return domain_list @staticmethod def create_domain_table(): """Creates table from domain list""" table = """<table id='mytable' cellspacing='0'><caption>List of active domUs</caption> <th scope="col" class="nobg">domU</th> <th scope="col">VNC host:port</th> """ domain_list = xen.get_domains() for k, v in domain_list.iteritems(): link = v.split(':', 1)[1] table += """ <tr> <th class="spec" scope="row">%s</th> <td><a href="javascript:vnc_console('/vnc/%s')" target='_blank'>%s</a></td> </tr> """ % (k, link, v) table += "</table>" return table
ソケットを介してXENサーバーに接続し、そこから多くの属性を持つ実行中のdomUのリストを引き出します。 次に、これらの属性から次のものが取り出されます。domU名とアドレス:VNCサーバーポート。 その後、実行中のdomUのリストを含むテーブルが形成されます。
xenに付属しているxmユーティリティで接続方法を見ました。
/usr/lib64/python2.7/site-packages/xen/util/xmlrpcclient.py
おそらく将来、時間があれば、XenApiを介してより正確な方法でXENサーバーへの接続をやり直します。
Webページを表示するためのクラス:
class listvm(object): def index(self, port=None): """Just a stub""" return """<h1><a href="/vnc/">vnc</a></h1>""" index.exposed = True def vnc(self, port=None): """Show running vm's or open vnc applet""" if port: template = open("static/vnc.htm", 'r').read() return template % port else: template = open("static/table.htm", 'r').read() return template % xen.create_domain_table() vnc.exposed = True
vnc関数に興味があります。 に行けば
スラッシュを介して目的のポートを渡すと、実行中のdomUのリストが表示されます。たとえば、< >/vnc
< >/vnc/5900
目的のコンソールが起動します。 コンソールは、Proxmox VEから引き裂かれたjavascript関数を使用して起動されます。 アプレットを開いた後、Proxmox VEの別の関数が新しいウィンドウのサイズを必要に応じて調整します。
cherrypy webサーバーを設定します。ここではすべてが簡単です:
server_config = { 'server.socket_host': '127.0.0.1', 'server.socket_port': 80, 'tools.staticdir.root': "/usr/local/vm_console", 'tools.staticdir.debug': True, } cherrypy.config.update(server_config) listvm_config = { '/static': {'tools.staticdir.on': True, 'tools.staticdir.dir': "static", } } cherrypy.tree.mount(listvm(), '/', config=listvm_config) cherrypy.engine.start() cherrypy.engine.block()
突きたい
すべてをgithubに投稿しました。ここで入手できます: github.com/sistemshik/vm_console