-
Notifications
You must be signed in to change notification settings - Fork 163
/
oracle_asmvol
executable file
·279 lines (228 loc) · 8.14 KB
/
oracle_asmvol
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_asmvol
short_description: Manage Oracle ASMCMD Volumes
description:
- Manage Oracle advm Volumes
version_added: "2.1.0.0"
options:
name:
description:
- The name of the volume
required: True
default: None
size:
description:
- The size of the volume
default: None
column:
description:
- Number of columns in a stripe set
required: False
width:
description:
- Stripe width of a volume
required: False
diskgroup:
description:
- The diskgroup in which to create the volume
required: True
default: None
aliases: ['dg']
state:
description:
- The state of the volume.
default: present
choices: ['present','absent', 'status']
# oracle_sid:
# description:
# - The name of the ASM instance
# default: +ASM
# aliases: ['sid']
oracle_home:
description:
- The GI ORACLE_HOME
required: false
default: None
aliases: ['oh']
notes:
author: Mikael Sandström, [email protected], @oravirt
'''
EXAMPLES = '''
# Create an ASM volume
oracle_asmvol: name=acfsvol dg=acfsdg size=100G state=present oh=/u01/app/grid/12.1.0.2/grid
# Delete an ASM volume
oracle_asmvol: name=acfsvol dg=acfsdg state=absent oh=/u01/app/grid/12.1.0.2/grid
'''
import os
try:
import cx_Oracle
except ImportError:
cx_oracle_exists = False
else:
cx_oracle_exists = True
# Check if the volume exists
def check_vol_exists(cursor, module, msg, diskgroup, name):
sql = '''
select count (*) from v$asm_volume v,v$asm_diskgroup g
where v.group_number = g.group_number
and lower (g.name) = \'%s\'
and lower (v.volume_name) = \'%s\'
''' % (diskgroup.lower(),name.lower())
result = execute_sql_get(module, msg, cursor, sql)
# msg = 'Normal Result is: %s, [0] is: %s, [0][0] is: %s, len is: %s, type is: %s' % (result,result[0],result[0][0],len(result), type(result))
# module.exit_json(msg=msg)
if result[0][0] > 0:
return True
else:
return False
# command = '%s/bin/asmcmd volinfo -G %s %s' % (oracle_home, diskgroup, name)
# (rc, stdout, stderr) = module.run_command(command)
# if rc != 0:
# msg = 'Error, stdout: %s, stderr: %s, command is %s' % (stdout, stderr, command)
# module.fail_json(msg=msg, changed=False)
#
#
# if 'not found' in stdout:
# return False
# else:
# return True
def create_vol(cursor, module, msg, diskgroup, name, size):
sql = 'alter diskgroup %s ' % (diskgroup)
sql += 'add volume %s ' % (name)
sql += 'size %s ' % (size)
if execute_sql(module, msg, cursor, sql):
return True
else:
msg = 'error in exec sql create'
module.fail_json(msg=msg, changed=False)
# command = '%s/bin/asmcmd volcreate %s -G %s -s %s ' % (oracle_home, name, diskgroup, size)
#
# if column is not None:
# command += ' --column %s' % (column)
# if width is not None:
# command += ' --width %s' % (width)
# if redundancy is not None:
# command += ' --redundancy %s' % (redundancy)
#
# (rc, stdout, stderr) = module.run_command(command)
# if rc != 0:
# msg = 'Error, STDOUT: %s, STDERR: %s, command is: %s' % (stdout, stderr, command)
# module.fail_json(msg=msg, changed=False)
# else:
# return True #<-- all is well
def remove_vol(cursor, module, msg, diskgroup, name):
sql = 'alter diskgroup %s ' % (diskgroup)
sql += 'drop volume %s ' % (name)
if execute_sql(module, msg, cursor, sql):
return True
else:
msg = 'error in exec sql remove'
module.fail_json(msg=msg, changed=False)
def execute_sql_get(module, msg, cursor, sql):
#module.exit_json(msg="In execute_sql_get", changed=False)
try:
cursor.execute(sql)
result = (cursor.fetchall())
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Something went wrong while executing sql_get - %s sql: %s' % (error.message, sql)
module.fail_json(msg=msg, changed=False)
return False
return result
def execute_sql(module, msg, cursor, sql):
try:
cursor.execute(sql)
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Something went wrong while executing sql - %s sql: %s' % (error.message, sql)
module.fail_json(msg=msg, changed=False)
return False
return True
def main():
msg = ['']
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True, aliases = ['volume_name']),
diskgroup = dict(required=True, aliases = ['dg']),
size = dict(required=False),
column = dict(default=None),
width = dict(default=None),
redundancy = dict(default=None),
state = dict(default="present", choices = ["present", "absent"]),
user = dict(required=False, aliases = ['un','username']),
password = dict(required=False, no_log=True, aliases = ['pw']),
hostname = dict(required=False, default = 'localhost', aliases = ['host']),
port = dict(required=False, default = 1521),
service_name = dict(required=False, default = '+ASM', aliases = ['sn']),
oracle_home = dict(required=False, aliases = ['oh']),
),
)
name = module.params["name"]
diskgroup = module.params["diskgroup"]
size = module.params["size"]
column = module.params["column"]
width = module.params["width"]
redundancy = module.params["redundancy"]
state = module.params["state"]
user = module.params["user"]
password = module.params["password"]
hostname = module.params["hostname"]
port = module.params["port"]
service_name = module.params["service_name"]
oracle_home = module.params["oracle_home"]
# if oracle_home is not None:
# os.environ['ORACLE_HOME'] = oracle_home
# elif 'ORACLE_HOME' in os.environ:
# oracle_home = os.environ['ORACLE_HOME']
# else:
# msg = 'ORACLE_HOME variable not set. Please set it and re-run the command'
# module.fail_json(msg=msg, changed=False)
# if oracle_sid != '+ASM':
# os.environ['ORACLE_SID'] = oracle_sid
# elif 'ORACLE_SID' in os.environ:
# oracle_sid = os.environ['ORACLE_SID']
if not cx_oracle_exists:
msg = "The cx_Oracle module is required. 'pip install cx_Oracle' should do the trick. If cx_Oracle is installed, make sure ORACLE_HOME & LD_LIBRARY_PATH is set"
module.fail_json(msg=msg)
wallet_connect = '/@%s' % service_name
try:
if (not user and not password) : # If neither user or password is supplied, the use of an oracle wallet is assumed
connect = wallet_connect
conn = cx_Oracle.connect(wallet_connect, mode=cx_Oracle.SYSASM)
elif (user and password):
dsn = cx_Oracle.makedsn(host=hostname, port=port, service_name=service_name)
connect = dsn
conn = cx_Oracle.connect(user, password, dsn, mode=cx_Oracle.SYSASM)
elif (not(user) or not(password)):
module.fail_json(msg='Missing username or password for cx_Oracle')
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Could not connect to ASM: %s, connect descriptor: %s, username: %s, pass: %s' % (error.message, connect,user,password)
module.fail_json(msg=msg, changed=False)
cursor = conn.cursor()
if state == 'present' and not size:
msg = 'Missing argument: size. Please add and re-run the command'
module.fail_json(msg=msg, changed=False)
if state == 'present':
if not check_vol_exists(cursor, module, msg, diskgroup, name):
if create_vol(cursor, module, msg, diskgroup, name, size):
msg = 'Volume %s successfully created. Size: %s ' % (name.upper(), size)
module.exit_json(msg=msg, changed=True)
else:
msg = 'Volume %s already exists' % (name.upper())
module.exit_json(msgt=msg, changed=False)
elif state == 'absent' :
if check_vol_exists(cursor, module, msg, diskgroup, name):
if remove_vol(cursor, module, msg, diskgroup, name):
msg = 'Volume %s successfully removed' % (name.upper())
module.exit_json(msg=msg, changed=True)
else:
msg = 'Volume %s doesn\'t exist' % (name.upper())
module.exit_json(msg=msg, changed=False)
module.exit_json(msg="Unhandled exit", changed=False)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()