0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pyvmomiでServiceInstanceを取得する処理の抽象化メモ

0
Posted at

pyvmomiのログイン処理(ServiceInstance取得)を直感的にしたくて抽象化したclassのメモです。

# !/usr/bin/env python3
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
from getpass import getpass
import ssl
import atexit
import argparse

class login:
    """
    ログイン処理を抽象化したクラス
    """
    def __init__(self):
        self.username = ""
        self.password = ""
        self.host = ""

    def get_service_instance(self):
        """
        ServiceInstanceを取得するメソッド

        :rtype: class
        :return: pyVmomi.VmomiSupport.vim.ServiceInstance
        """
        # SSL証明書対策
        context = None
        if hasattr(ssl, '_create_unverified_context'):
            context = ssl._create_unverified_context()

        # ServiceInstanceを取得
        si = SmartConnect(host = self.host,
                          user = self.username,
                          pwd = self.password,
                          sslContext = context)

        # 処理完了時にvCenterから切断
        atexit.register(Disconnect, si)

        return si

def options():
    """ コマンドラインオプション設定

    :rtype: class
    :return: argparse.Namespace
    """
    parser = argparse.ArgumentParser(prog='',
                                     add_help=True,
                                     description='')
    parser.add_argument('--host', '-vc',
                        type=str, required=True,
                        help='vCenterのIP又はホスト名')
    parser.add_argument('--username', '-u',
                        type=str, default='administrator@vsphere.local',
                        help='vCenterのログインユーザー名(default:administrator@vsphere.local)')
    parser.add_argument('--password', '-p',
                        type=str,
                        help='vCenterのログインユーザーパスワード')
    args = parser.parse_args()

    if(not(args.password)):
        args.password = getpass()

    return args

if __name__ == '__main__':
    # オプションを取得
    args = options()

    # ログイン情報を設定
    login = login()
    login.username = args.username
    login.password = args.password
    login.host = args.host

    # ServiceInstanceを取得
    si = login.get_service_instance()

    # 処理を書いていく

例えば、VM一覧の名前を表示する場合は、こんな感じ。

    # 処理を書いていく
    sc = si.content
    vms = sc.viewManager.CreateContainerView(sc.rootFolder,
                                             [vim.VirtualMachine],
                                             True)
    for vm in vms.view:
        print(vm.name)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?