-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm_setTag.py
executable file
·99 lines (72 loc) · 2.24 KB
/
vm_setTag.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Saul Bertuccio
# Date: gio 1 febbraio 2017
# Imposta i tag per la vm
from __future__ import print_function
# Custom libs
from libs.datacenter import Datacenter
from libs.utils import checkDate, getBaseArgs
from configs.vcenterparams import VcenterParams
from pyVim.task import WaitForTasks
import argparse
import time
import getpass
import sys
import copy
import json
import csv
import yaml
from datetime import datetime
from pprint import pprint
# DEBUG
from pprint import pprint
def GetArgs():
"""
Gestione degli argomenti
"""
PARS=VcenterParams()
parser = getBaseArgs(PARS)
parser.add_argument('-n', '--vmname', required=True, action='store',
help='Nome della VM per la quale impostare tag e categorie.')
# type = lambda s : s.upper()
parser.add_argument('-t', '--tags', required=True, nargs='+', action='store',
help='Elenco dei tag che devono essere associati alla macchina virtuale.')
args = parser.parse_args()
if args.user == 'sysadmin':
args.password = PARS.SYSADMINPASSWORD
return args
def main():
# Parsing argomenti
args = GetArgs()
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Inserire la password per l\'host %s e l\'utente %s: ' % (args.host,args.user))
# Connessione al vcenter e oggetto DataCenter
try:
vmdtc = Datacenter(host=args.host,
user=args.user,
password=password,
port=int(args.port),
datacenter = args.datacenter)
except Exception, e:
sys.stderr.write(str(e))
sys.exit(1)
try:
vms = vmdtc.getVirtualMachineByName(name=args.vmname)
except Exception, e:
sys.stderr.write(str(e))
sys.exit(1)
if not vms:
sys.stderr.write("Attenzione: nessuna Macchina virtuale trovata nella cartella %s\n" % args.folder)
sys.exit(1)
# ottengo tutti i tags dal nome
tags = []
for t in args.tags:
tag = vmdtc.getTagByName(t)
vms.setTag(tag, vmdtc)
#
# Avvio l'applicazione
if __name__ == "__main__":
main()