{"id":2784,"date":"2015-10-14T17:25:48","date_gmt":"2015-10-14T09:25:48","guid":{"rendered":"https:\/\/drho.club\/?p=353"},"modified":"2015-10-14T17:25:48","modified_gmt":"2015-10-14T09:25:48","slug":"python-api-switch-all-vm-on-off","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2015\/10\/14\/python-api-switch-all-vm-on-off\/","title":{"rendered":"\u4f7f\u7528Python API\u4e00\u500b\u6307\u4ee4\u6253\u958b\u6216\u95dc\u9589\u6240\u6709\u7684VM"},"content":{"rendered":"<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u5728\u64cd\u4f5cVM\u7684\u904e\u7a0b\uff0c\u7d93\u5e38\u6709\u9700\u8981\u628a\u6240\u6709\u672c\u5730\u7aef\u4e3b\u6a5f\u4e0a\u7684\u6240\u6709 VM\u90fd\u6253\u958b\u6216\u662f\u95dc\u6a5f\uff0c\u6709\u4e86Python API\uff0c\u6211\u5011\u53ef\u4ee5\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u7a0b\u5f0f\uff0c\u4f7f\u7528listAllDomains\u628a\u6240\u6709\u7684domain\u90fd\u627e\u51fa\u4f86\u653e\u5728\u4e00\u500b\u4e32\u5217\u4e2d\uff0c\u7136\u5f8c\u5206\u5225\u4ee5create()\u548cshutdown()\u9019\u5169\u500b\u65b9\u6cd5\u4f86\u6253\u958b\u6216\u662f\u95dc\u9589\u6bcf\u4e00\u500bdomain\uff08\u865b\u64ec\u6a5f\uff09\u3002<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n#! \/usr\/bin\/python<br \/>\nimport libvirt<br \/>\nimport sys<\/p>\n<p>if len(sys.argv)&lt;2:<br \/>\n  print &quot;Usage: vmturn on\/off&quot;<br \/>\n  exit(1)<br \/>\ncommand = sys.argv[1]<br \/>\nconn = libvirt.open(&quot;qemu:\/\/\/system&quot;)<br \/>\nallvm = conn.listAllDomains()<br \/>\nfor vm in allvm:<br \/>\n  if command == &#8216;on&#8217;:<br \/>\n    try:      vm.create()<br \/>\n    except:   continue<br \/>\n  if command == &#8216;off&#8217;:<br \/>\n    try:    vm.shutdown()<br \/>\n    except:  continue<br \/>\nconn.close()<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u4f60\u53ef\u4ee5\u628a\u9019\u500b\u7a0b\u5f0f\u547d\u540d\u70ba vmturn\uff08\u4e0d\u7528\u526f\u6a94\u540d\uff09\uff0c\u7136\u5f8c\u8a2d\u5b9a\u6210\u53ef\u57f7\u884c\u6a94\uff0c\u8907\u88fd\u5230\/usr\/sbin\u4e2d\uff0c\u4ee5\u5f8c\u53ea\u8981\u60f3\u4e00\u53e3\u6c23\u6253\u958b\u6240\u6709\u7684VM\uff0c\u53ea\u8981\u57f7\u884c vmturn on\u5c31\u53ef\u4ee5\u4e86\uff0c\u7576\u7136\uff0c\u8981\u95dc\u9589\u6240\u6709\u7684VM\uff0c\u4e5f\u662f\u53ea\u8981\u4f7f\u7528 vmturn off\u3002\u8f38\u5165\u5176\u5b83\u7684\u53c3\u6578\u90fd\u4e0d\u6703\u6709\u4efb\u4f55\u7684\u4f5c\u7528\u3002<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u9664\u4e86create()\u548cshutdown()\u9019\u5169\u500b\u65b9\u6cd5\u51fd\u6578\u4e4b\u5916\uff0c\u53ef\u4ee5\u64cd\u4f5cdomain\u7684\u65b9\u6cd5\u9084\u6709\u5982\u4e0b\u6240\u793a\u7684\u9019\u9ebc\u591a\u7a2e\uff0c\u6709\u8208\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u81ea\u5df1\u8a66\u8a66\u770b\u5594\uff1a<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">&#8216;abortJob&#8217;, &#8216;attachDevice&#8217;, &#8216;attachDeviceFlags&#8217;, &#8216;autostart&#8217;, &#8216;blkioParameters&#8217;, &#8216;blockCommit&#8217;, &#8216;blockCopy&#8217;, &#8216;blockInfo&#8217;, &#8216;blockIoTune&#8217;, &#8216;blockJobAbort&#8217;, &#8216;blockJobInfo&#8217;, &#8216;blockJobSetSpeed&#8217;, &#8216;blockPeek&#8217;, &#8216;blockPull&#8217;, &#8216;blockRebase&#8217;, &#8216;blockResize&#8217;, &#8216;blockStats&#8217;, &#8216;blockStatsFlags&#8217;, &#8216;connect&#8217;, &#8216;controlInfo&#8217;, &#8216;coreDump&#8217;, &#8216;coreDumpWithFormat&#8217;, &#8216;create&#8217;, &#8216;createWithFiles&#8217;, &#8216;createWithFlags&#8217;, &#8216;destroy&#8217;, &#8216;destroyFlags&#8217;, &#8216;detachDevice&#8217;, &#8216;detachDeviceFlags&#8217;, &#8216;diskErrors&#8217;, &#8217;emulatorPinInfo&#8217;, &#8216;fSTrim&#8217;, &#8216;fsFreeze&#8217;, &#8216;fsThaw&#8217;, &#8216;getCPUStats&#8217;, &#8216;getTime&#8217;, &#8216;hasCurrentSnapshot&#8217;, &#8216;hasManagedSaveImage&#8217;, &#8216;hostname&#8217;, &#8216;info&#8217;, &#8216;injectNMI&#8217;, &#8216;interfaceParameters&#8217;, &#8216;interfaceStats&#8217;, &#8216;isActive&#8217;, &#8216;isPersistent&#8217;, &#8216;isUpdated&#8217;, &#8216;jobInfo&#8217;, &#8216;jobStats&#8217;, &#8216;listAllSnapshots&#8217;, &#8216;managedSave&#8217;, &#8216;managedSaveRemove&#8217;, &#8216;maxMemory&#8217;, &#8216;maxVcpus&#8217;, &#8216;memoryParameters&#8217;, &#8216;memoryPeek&#8217;, &#8216;memoryStats&#8217;, &#8216;metadata&#8217;, &#8216;migrate&#8217;, &#8216;migrate2&#8217;, &#8216;migrate3&#8217;, &#8216;migrateGetCompressionCache&#8217;, &#8216;migrateGetMaxSpeed&#8217;, &#8216;migrateSetCompressionCache&#8217;, &#8216;migrateSetMaxDowntime&#8217;, &#8216;migrateSetMaxSpeed&#8217;, &#8216;migrateToURI&#8217;, &#8216;migrateToURI2&#8217;, &#8216;migrateToURI3&#8217;, &#8216;name&#8217;, &#8216;numaParameters&#8217;, &#8216;openChannel&#8217;, &#8216;openConsole&#8217;, &#8216;openGraphics&#8217;, &#8216;openGraphicsFD&#8217;, &#8216;pMSuspendForDuration&#8217;, &#8216;pMWakeup&#8217;, &#8216;pinEmulator&#8217;, &#8216;pinVcpu&#8217;, &#8216;pinVcpuFlags&#8217;, &#8216;reboot&#8217;, &#8216;reset&#8217;, &#8216;resume&#8217;, &#8216;revertToSnapshot&#8217;, &#8216;save&#8217;, &#8216;saveFlags&#8217;, &#8216;schedulerParameters&#8217;, &#8216;schedulerParametersFlags&#8217;, &#8216;schedulerType&#8217;, &#8216;screenshot&#8217;, &#8216;securityLabel&#8217;, &#8216;securityLabelList&#8217;, &#8216;sendKey&#8217;, &#8216;sendProcessSignal&#8217;, &#8216;setAutostart&#8217;, &#8216;setBlkioParameters&#8217;, &#8216;setBlockIoTune&#8217;, &#8216;setInterfaceParameters&#8217;, &#8216;setMaxMemory&#8217;, &#8216;setMemory&#8217;, &#8216;setMemoryFlags&#8217;, &#8216;setMemoryParameters&#8217;, &#8216;setMemoryStatsPeriod&#8217;, &#8216;setMetadata&#8217;, &#8216;setNumaParameters&#8217;, &#8216;setSchedulerParameters&#8217;, &#8216;setSchedulerParametersFlags&#8217;, &#8216;setTime&#8217;, &#8216;setVcpus&#8217;, &#8216;setVcpusFlags&#8217;, &#8216;shutdown&#8217;, &#8216;shutdownFlags&#8217;, &#8216;snapshotCreateXML&#8217;, &#8216;snapshotCurrent&#8217;, &#8216;snapshotListNames&#8217;, &#8216;snapshotLookupByName&#8217;, &#8216;snapshotNum&#8217;, &#8216;state&#8217;, &#8216;suspend&#8217;, &#8216;undefine&#8217;, &#8216;undefineFlags&#8217;, &#8216;updateDeviceFlags&#8217;, &#8216;vcpuPinInfo&#8217;, &#8216;vcpus&#8217;, &#8216;vcpusFlags&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u64cd\u4f5cVM\u7684\u904e\u7a0b\uff0c\u7d93\u5e38\u6709\u9700\u8981\u628a\u6240\u6709\u672c\u5730\u7aef\u4e3b\u6a5f\u4e0a\u7684\u6240\u6709 VM\u90fd\u6253\u958b\u6216\u662f\u95dc\u6a5f\uff0c\u6709\u4e86Python API\uff0c\u6211\u5011\u53ef\u4ee5\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u7a0b\u5f0f\uff0c\u4f7f\u7528listAllDomains\u628a\u6240\u6709\u7684domain\u90fd\u627e\u51fa\u4f86\u653e\u5728\u4e00\u500b\u4e32\u5217\u4e2d\uff0c\u7136\u5f8c\u5206\u5225\u4ee5create()\u548cshutdown()\u9019\u5169\u500b\u65b9\u6cd5\u4f86\u6253\u958b\u6216\u662f\u95dc\u9589\u6bcf\u4e00\u500bdomain\uff08\u865b\u64ec\u6a5f\uff09\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97,15],"tags":[162,193,194],"class_list":["post-2784","post","type-post","status-publish","format-standard","hentry","category-kvm-libvirt","category-python","tag-python-libvirt-api","tag-vm-create","tag-vm-shutdown"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2784"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2784\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}