File size: 3,426 Bytes
8d55ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
        print_function, unicode_literals)

from builtins import *
from future.builtins.disabled import *

import re

from ._common import *

class Customize(SubCommand):
    def __init__(self, *args, **kwargs):
        super(Customize, self).__init__(*args, **kwargs)

    @classmethod
    def addParser(self, cmdLineParser, subparsers):
        parser = cmdLineParser.getSubParser(
                "customize", subparsers,
                help="Customize VMs")
        parser.add_argument(
                "name",
                metavar="name",
                help="VM to clone from")
        parser.add_argument(
                "--csm", type=str,
                metavar="customization",
                help="Path to customication file")
        parser.add_argument(
                "--nic-add", nargs="+",
                default=[], type=cmdLineParser.nicAddType,
                metavar="<nic-add>",
                dest="nicAdd",
                help="Customize network interfaces.\n" \
                "[mac=,ip=x.x.x.x/mask,gw=]")

        parser.set_defaults(cloneArgs=["name", "csm", "nicAdd"])

    @export
    def customize(self, name=None, csm=None, nicAdd=[]):
        self._checkType(name, (str, vim.VirtualMachine))
        self._checkType(csm, (str, type(None)))
        self._checkType(nicAdd, list)
        for nc in nicAdd:
            assert "map" in nc
            assert "ip" in nc
            assert "mask" in nc
            assert "gw" in nc
            self._checkType(nc["map"], str)
            self._checkType(nc["ip"], str)
            self._checkType(nc["mask"], int)
            self._checkType(nc["gw"], str)


        regexps = [re.compile("^{}$".format(re.escape(name)))]
        vm = self.getRegisteredVms(regexps=regexps)[0]
        customSpec = vim.vm.customization.Specification()
        if csm:
            customSpec = self.getCSMByName(csm)

        for nicDesc in nicAdd:
            mac = nicDesc["map"]
            ip = nicDesc["ip"]
            mask = nicDesc["mask"]
            gw = nicDesc["gw"]
            adapterMap = vim.vm.customization.AdapterMapping()
            if mac:
                adapterMap.macAddress = mac
            if not ip:
                adapterMap.ip = vim.vm.customization.DhcpIpGenerator()
            else:
                adapterMap.ip = vim.vm.customization.FixedIp(ipAddress=ip)
                if mask:
                    adapterMap.subnetMask = mask
                if gw:
                    adapterMap.gateway = gw
                # adapterMap.dnsDomain
            csm.nicSettingsMap.append(adapterMap)
        # hostname/domain
        # ident = vim.vm.customization.LinuxPrep(hostName=hostname, domain=domain)
        # customSpec.identity = ident
        # windows ???

        # dnsServerList, dnsSuffixList
        # globalIPSettings = vim.vm.customization.GlobalIPSettings(dnsServerList=dnsServerList,
        #           dnsSuffixList=dnsSuffixList)
        # customSpec.globalIPSettings = globalIPSettings

        task = vm.CustomizeVM(customSpec)
        vcTask = VcTask(task)
        vcTask.waitTaskDone()
        if vcTask.isSuccess():
            self.logger.info("Success")
        else:
            msg = vcTask.error()
            self.logger.error("Failed {}".format(repr(msg)))
            raise RuntimeError("CMS failed")

        return 0