-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_wait_for
87 lines (70 loc) · 2.95 KB
/
http_wait_for
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import generators
from __future__ import division
import httplib
import socket
import time
import datetime
from ansible.module_utils.basic import *
class HTTPWaitFor(object):
class HTTPClient(object):
def __init__(self, module):
self.params = module.params
def connect(self):
if self.params['ssl']:
return httplib.HTTPSConnection(self.params['host'], self.params['port'])
else:
return httplib.HTTPConnection(self.params['host'], self.params['port'], True)
class Progress(object):
def __init__(self):
self.start = datetime.datetime.now()
def elapsed(self):
return (datetime.datetime.now() - self.start).seconds
def wait(self, value):
time.sleep(value)
def __init__(self, AnsibleModule = AnsibleModule, HTTPClient = HTTPClient, Progress = Progress):
self.module = AnsibleModule(
argument_spec = dict(
host = dict(default='127.0.0.1', type='str'),
port = dict(type='int'),
path = dict(default='/', type='str'),
ssl = dict(default='no', type='bool'),
status = dict(default=200, type='int'),
interval = dict(default=3 , type='int'),
timeout = dict(default=60 , type='int'),
)
)
if 'port' not in self.module.params:
self.module.params['port'] = 443 if self.module.params['ssl'] else 80
self.HTTPClient = HTTPClient
self.Progress = Progress
def execute(self):
progress = self.Progress()
params = self.module.params
while True:
try:
connection = self.HTTPClient(self.module).connect()
connection.request('GET', params['path'])
response = connection.getresponse()
connection.close()
if response.status == params['status']:
self.module.exit_json(status = 1, elapsed = progress.elapsed())
break
except httplib.BadStatusLine:
connection.close()
self.module.fail_json(msg = 'Non HTTP server!!', status = 3, elapsed = progress.elapsed())
break
except socket.error:
connection.close()
elapsed = progress.elapsed()
if elapsed >= params['timeout']:
self.module.fail_json(msg = 'Timeout!! sec %d' % elapsed, status = 2, elapsed = elapsed)
break
else:
progress.wait(params['interval'])
continue
if __name__ == '__main__':
HTTPWaitFor().execute()