wait_for 2.46 KB
Newer Older
Jeroen Hoekx committed
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# (c) 2012, Jeroen Hoekx <jeroen@hoekx.be>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

import socket
import datetime
import time
import sys

def main():

    module = AnsibleModule(
        argument_spec = dict(
30
            host=dict(default='127.0.0.1'),
Jeroen Hoekx committed
31
            timeout=dict(default=300),
32 33 34
            delay=dict(default=0),
            port=dict(required=True),
            state=dict(default='started', choices=['started', 'stopped']),
Jeroen Hoekx committed
35 36 37 38 39
        ),
    )

    params = module.params

40
    host = params['host']
Jeroen Hoekx committed
41
    timeout = int(params['timeout'])
42
    delay = int(params['delay'])
Jeroen Hoekx committed
43
    port = int(params['port'])
44
    state = params['state']
Jeroen Hoekx committed
45

46 47 48 49
    if delay:
        time.sleep(delay)

    if state is 'stopped':
50 51
        ### first wait for the host to go down
        end = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
Jeroen Hoekx committed
52

53 54 55 56 57 58 59 60 61 62 63
        while datetime.datetime.now() < end:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(5)
            try:
                s.connect( (host, port) )
                s.close()
                time.sleep(1)
            except:
                break
        else:
            module.fail_json(msg="Timeout when waiting for %s to stop."%(host))
Jeroen Hoekx committed
64

65
    if state is 'started':
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        ### wait for the host to come up
        end = datetime.datetime.now() + datetime.timedelta(seconds=timeout)

        while datetime.datetime.now() < end:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                s.connect( (host, port) )
                s.close()
                break
            except:
                time.sleep(1)
        else:
            module.fail_json(msg="Timeout when waiting for %s"%(host))

80
    module.exit_json(state=state, port=port)
Jeroen Hoekx committed
81 82 83 84

# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()