forked from vmware/pyvmomi-community-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.py
executable file
·139 lines (115 loc) · 4.29 KB
/
renamer.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2013 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A Python script for changing the name of an object. Demonstrates the use
of tasks in an asynchronous way.
"""
import atexit
import argparse
import getpass
import sys
import time
from pyVim import connect
from pyVmomi import vim
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--host',
required=True,
action='store',
help='Remote host to connect to')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use when connecting to host')
parser.add_argument('-o', '--port',
required=False,
action='store',
help="port to use, default 443", default=443)
parser.add_argument('-n', '--name',
required=True,
action='store',
help='Name of the entity to look for.')
parser.add_argument('-r', '--new_name',
required=False,
action='store',
help='New name of the entity.')
args = parser.parse_args()
if args.password is None:
args.password = getpass.getpass(
prompt='Enter password for host %s and user %s: ' %
(args.host, args.user))
args = parser.parse_args()
return args
args = get_args()
# form a connection...
si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password,
port=args.port)
# doing this means you don't need to remember to disconnect your script/objects
atexit.register(connect.Disconnect, si)
# search the whole inventory tree recursively... a brutish but effective tactic
root_folder = si.content.rootFolder
entity_stack = root_folder.childEntity
name = args.name
obj = None
while entity_stack:
entity = entity_stack.pop()
if entity.name == name:
obj = entity
break
elif isinstance(entity, vim.Datacenter):
# add this vim.DataCenter's folders to our search
# we don't know the entity's type so we have to scan
# each potential folder...
entity_stack.append(entity.datastoreFolder)
entity_stack.append(entity.hostFolder)
entity_stack.append(entity.networkFolder)
entity_stack.append(entity.vmFolder)
elif isinstance(entity, vim.Folder):
# add all child entities from this folder to our search
entity_stack.extend(entity.childEntity)
if obj is None:
print "A object named %s could not be found" % args.name
exit()
if args.new_name:
new_name = args.new_name
else:
# just because we want the script to do *something*
new_name = args.name + "0"
print
print "name : %s" % obj.name
print
print " renaming from %s to %s" % (args.name, new_name)
print
# rename creates a task...
task = obj.Rename(new_name)
# Did you know that task objects in pyVmomi get updates automatically?
# Check this out... it's not super efficient but here's how you could
# have a script that looped waiting on a task but still had the
# chance to periodically check other things or do other actions...
print "rename task state:"
count = 0
state = task.info.state
while task.info.state != vim.TaskInfo.State.success:
sys.stdout.write("\r\t" + str(time.time()) + "\t: " + task.info.state)
sys.stdout.flush()
count += 1
print
print "rename finished"
print