forked from ibm-messaging/mq-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_manager.py
143 lines (123 loc) · 5.81 KB
/
queue_manager.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
140
141
142
143
#
# Copyright 2022 IBM Corp.
#
# 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.
#
from ansible.module_utils.basic import AnsibleModule
import os.path
def main():
qmgr_attributes = dict(
qmname=dict(type='list', required=True, max_len=48),
state=dict(type='str', required=True),
description=dict(type='str', required=False),
unit_test=dict(type='bool', default=False, required=False),
mqsc_file=dict(type='str', required=False)
)
module = AnsibleModule(
argument_spec=qmgr_attributes
)
result = dict(
rc=0,
msg='',
state='',
output=''
)
result['state'] = module.params['state']
if module.params['state'] != 'absent' and module.params['mqsc_file'] is not None:
result['msg'] = 'runmqsc command ran successfully'
result['rc'] = 0
for qmname in module.params['qmname']:
if module.params['unit_test'] is False:
rc, stdout, stderr = module.run_command(['dspmq', '-m', qmname])
if rc == 72:
rc, stdout, stderr = module.run_command(['crtmqm', qmname])
if module.params['state'] == 'running':
rc, stdout, stderr = module.run_command(['strmqm', qmname])
rc, stdout, stderr = module.run_command(['dspmq', '-m', qmname])
if stdout is not None:
if 'Running' in stdout:
exists = os.path.isfile(module.params['mqsc_file'])
if exists is True:
rc_runmqsc, stdout_runmqsc, stderr_runmqsc = module.run_command(["runmqsc", qmname, "-f", module.params['mqsc_file']])
result['rc'] = rc_runmqsc
result['output'] = stdout_runmqsc + stderr_runmqsc
if rc == 0:
result['msg'] = 'MQSC configuration successfully applied to Queue Manager'
else:
result['rc'] = 16
result['msg'] = 'MQSC file could not be found'
else:
result['msg'] = 'Queue Manager Needs to be running to apply MQSC configuration.'
result['rc'] = 8
else:
result['msg'] = 'Queue Manager does not exist.'
result['rc'] = 8
elif module.params['state'] == 'running':
for qmname in module.params['qmname']:
result['msg'] = 'IBM MQ queue manager \'' + str(qmname) + '\' started'
if module.params['unit_test'] is False:
rc, stdout, stderr = module.run_command(['dspmq', '-m', qmname])
if rc == 72:
rc, stdout, stderr = module.run_command(['crtmqm', qmname])
rc, stdout, stderr = module.run_command(['strmqm', qmname])
result['rc'] = rc
if rc == 5:
module.exit_json(skipped=True, state='running', msg='IBM MQ queue manager running')
elif rc > 0:
module.fail_json(**result)
elif module.params['state'] == 'present':
result['msg'] = 'IBM MQ Queue Manager Created'
for qmname in module.params['qmname']:
if module.params['unit_test'] is False:
rc, stdout, stderr = module.run_command(['crtmqm', qmname])
result['rc'] = rc
if rc == 8:
module.exit_json(skipped=True, state='present', msg='IBM MQ Queue Manager already exists')
elif rc > 0:
module.fail_json(**result)
elif module.params['state'] == 'absent':
result['msg'] = 'IBM MQ queue manager deleted.'
for qmname in module.params['qmname']:
if module.params['unit_test'] is False:
rc, stdout, stderr = module.run_command(['dltmqm', qmname])
result['msg'] = stdout + stderr
result['rc'] = rc
if rc == 5:
module.exit_json(skipped=True, state='running', msg='IBM MQ queue manager running.')
elif rc == 16:
# Queue Manager does not exist
module.exit_json(skipped=True, state='absent', msg='AMQ8118E: IBM MQ queue manager does not exist.')
elif rc > 0:
module.fail_json(**result)
elif module.params['state'] == 'stopped':
result['msg'] = 'Quiesce request accepted. The queue manager will stop when all outstanding work\nis complete.\n'
for qmname in module.params['qmname']:
if module.params['unit_test'] is False:
rc, stdout, stderr = module.run_command(['endmqm', qmname])
result['msg'] = stdout + stderr
if rc == 16:
result['state'] = 'absent'
module.fail_json(**result)
elif rc == 40:
module.exit_json(skipped=True, state='present', msg=stdout + stderr)
elif rc > 0:
module.fail_json(**result)
else:
# State Unrecognized, fail_json
result['state'] = ''
result['msg'] = 'Unrecognised State'
result['rc'] = 16
module.fail_json(**result)
module.exit_json(**result)
if __name__ == '__main__':
main()