forked from lamw/vmware-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable_compression_dedupe_on_vsan.py
More file actions
executable file
·140 lines (121 loc) · 5.28 KB
/
enable_compression_dedupe_on_vsan.py
File metadata and controls
executable file
·140 lines (121 loc) · 5.28 KB
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
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Sample script leveraging the new VSAN Management 6.2 API
and VSAN Management SDK for Python to enable Compression/Dedupe capability
for a VSAN Cluster
"""
__author__ = 'William Lam'
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import atexit
import argparse
import getpass
import requests
import sys
import ssl
#import the VSAN API python bindings
import vsanmgmtObjects
import vsanapiutils
def GetArgs():
"""
Supports the command-line arguments listed below.
"""
parser = argparse.ArgumentParser(
description='Process args for VSAN SDK sample application')
parser.add_argument('-s', '--host', required=True, action='store',
help='Remote host to connect to')
parser.add_argument('-o', '--port', type=int, default=443, action='store',
help='Port to connect on')
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('--cluster', dest='clusterName', metavar="CLUSTER",
default='VSAN-Cluster')
args = parser.parse_args()
return args
def getClusterInstance(clusterName, serviceInstance):
content = serviceInstance.RetrieveContent()
searchIndex = content.searchIndex
datacenters = content.rootFolder.childEntity
for datacenter in datacenters:
cluster = searchIndex.FindChild(datacenter.hostFolder, clusterName)
if cluster is not None:
return cluster
return None
#Start program
def main():
args = GetArgs()
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Enter password for host %s and '
'user %s: ' % (args.host,args.user))
#For python 2.7.9 and later, the defaul SSL conext has more strict
#connection handshaking rule. We may need turn of the hostname checking
#and client side cert verification
context = None
if sys.version_info[:3] > (2,7,8):
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
# Disabling the annoying InsecureRequestWarning message
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
si = SmartConnect(host=args.host,
user=args.user,
pwd=password,
port=int(args.port),
sslContext=context)
atexit.register(Disconnect, si)
#for detecting whether the host is VC or ESXi
aboutInfo = si.content.about
if aboutInfo.apiType == 'VirtualCenter':
majorApiVersion = aboutInfo.apiVersion.split('.')[0]
if int(majorApiVersion) < 6:
print('The Virtual Center with version %s (lower than 6.0) is not supported.'
% aboutInfo.apiVersion)
return -1
#Here is an example of how to access VC side VSAN Health Service API
vcMos = vsanapiutils.GetVsanVcMos(si._stub, context=context)
# Get VSAN Cluster Config System
vccs = vcMos['vsan-cluster-config-system']
cluster = getClusterInstance(args.clusterName, si)
if cluster is None:
print("Cluster %s is not found for %s" % (args.clusterName, args.host))
return -1
# Check to see if Automatic Claiming is enabled, if so, we need to disable else we can continue
vsanCluster = vccs.VsanClusterGetConfig(cluster=cluster)
if(vsanCluster.defaultConfig.autoClaimStorage == True):
print ("Disabling Automatic Claiming on VSAN Cluster: %s" % args.clusterName)
vsanSpec=vim.VimVsanReconfigSpec(
vsanClusterConfig=vim.VsanClusterConfigInfo (
defaultConfig=vim.VsanClusterConfigInfoHostDefaultInfo(
autoClaimStorage=False
)
),
modify=True
)
vsanTask = vccs.VsanClusterReconfig(cluster=cluster,vsanReconfigSpec=vsanSpec)
vcTask = vsanapiutils.ConvertVsanTaskToVcTask(vsanTask,si._stub)
vsanapiutils.WaitForTasks([vcTask],si)
# Check to see if Dedupe & Compression is already enabled, if not, then we'll enable it
if(vsanCluster.dataEfficiencyConfig.compressionEnabled == False or vsanCluster.dataEfficiencyConfig.dedupEnabled == False):
print ("Enabling Compression/Dedupe capability on VSAN Cluster: %s" % args.clusterName)
# Create new VSAN Reconfig Spec, both Compression/Dedupe must be enabled together
vsanSpec = vim.VimVsanReconfigSpec(
dataEfficiencyConfig=vim.VsanDataEfficiencyConfig(
compressionEnabled=True,
dedupEnabled=True
),
modify=True
)
vsanTask = vccs.VsanClusterReconfig(cluster=cluster,vsanReconfigSpec=vsanSpec)
vcTask = vsanapiutils.ConvertVsanTaskToVcTask(vsanTask,si._stub)
vsanapiutils.WaitForTasks([vcTask],si)
else:
print ("Compression/Dedupe is already enabled on VSAN Cluster: %s" % args.clusterName)
# Start program
if __name__ == "__main__":
main()