~canonical-ci-engineering/ubuntu-ci-services-itself/ansible

« back to all changes in this revision

Viewing changes to library/wait_for

  • Committer: Package Import Robot
  • Author(s): Michael Vogt, Harlan Lieberman-Berg, Michael Vogt
  • Date: 2013-11-01 09:40:59 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20131101094059-6w580ocxzqyqzuu3
Tags: 1.3.4+dfsg-1
[ Harlan Lieberman-Berg ]
* New upstream release (Closes: #717777).
  Fixes CVE-2013-2233 (Closes: #714822).
  Fixes CVE-2013-4259 (Closes: #721766).
* Drop fix-ansible-cfg patch.
* Change docsite generation to not expect docs as part of a wordpress install.
* Add trivial patch to fix lintian error with rpm-key script.
* Add patch header information to fix-html-makefile.

[ Michael Vogt ]
* add myself to uploader
* build/ship the module manpages for ansible in the ansible package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
# (c) 2012, Jeroen Hoekx <jeroen@hoekx.be>
5
 
#
6
 
# This file is part of Ansible
7
 
#
8
 
# Ansible is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License as published by
10
 
# the Free Software Foundation, either version 3 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# Ansible is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
# GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License
19
 
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
20
 
 
21
 
import socket
22
 
import datetime
23
 
import time
24
 
import sys
25
 
 
26
 
DOCUMENTATION = '''
27
 
---
28
 
module: wait_for
29
 
short_description: Waits for a given port to become accessible on a server.
30
 
description:
31
 
     - This is useful for when services are not immediately available after
32
 
       their init scripts return - which is true of certain Java application
33
 
       servers. It is also useful when starting guests with the M(virt) module and
34
 
       needing to pause until they are ready.
35
 
version_added: "0.7"
36
 
options:
37
 
  host:
38
 
    description:
39
 
      - hostname or IP address to wait for
40
 
    required: false
41
 
    default: "127.0.0.1"
42
 
    aliases: []
43
 
  timeout:
44
 
    description:
45
 
      - maximum number of seconds to wait for
46
 
    required: false
47
 
    default: 300
48
 
  delay:
49
 
    description:
50
 
      - number of seconds to wait before starting to poll
51
 
    required: false
52
 
    default: 0
53
 
  port:
54
 
    description:
55
 
      - port number to poll
56
 
    required: true
57
 
  state:
58
 
    description:
59
 
      - either C(started), or C(stopped) depending on whether the module should 
60
 
        poll for the port being open or closed.
61
 
    choices: [ "started", "stopped" ]
62
 
    default: "started"
63
 
examples:
64
 
   - code: "wait_for: port=8000 delay=10"
65
 
     description: "Example from Ansible Playbooks"
66
 
notes: []
67
 
requirements: null
68
 
author: Jeroen Hoekx
69
 
'''
70
 
 
71
 
def main():
72
 
 
73
 
    module = AnsibleModule(
74
 
        argument_spec = dict(
75
 
            host=dict(default='127.0.0.1'),
76
 
            timeout=dict(default=300),
77
 
            connect_timeout=dict(default=5),
78
 
            delay=dict(default=0),
79
 
            port=dict(required=True),
80
 
            state=dict(default='started', choices=['started', 'stopped']),
81
 
        ),
82
 
    )
83
 
 
84
 
    params = module.params
85
 
 
86
 
    host = params['host']
87
 
    timeout = int(params['timeout'])
88
 
    connect_timeout = int(params['connect_timeout'])
89
 
    delay = int(params['delay'])
90
 
    port = int(params['port'])
91
 
    state = params['state']
92
 
 
93
 
    if delay:
94
 
        time.sleep(delay)
95
 
 
96
 
    start = datetime.datetime.now()
97
 
 
98
 
    if state == 'stopped':
99
 
        ### first wait for the host to go down
100
 
        end = start + datetime.timedelta(seconds=timeout)
101
 
 
102
 
        while datetime.datetime.now() < end:
103
 
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
104
 
            s.settimeout(connect_timeout)
105
 
            try:
106
 
                s.connect( (host, port) )
107
 
                s.shutdown(socket.SHUT_RDWR)
108
 
                s.close()
109
 
                time.sleep(1)
110
 
            except:
111
 
                break
112
 
        else:
113
 
            elapsed = datetime.datetime.now() - start
114
 
            module.fail_json(msg="Timeout when waiting for %s:%s to stop." % (host, port), elapsed=elapsed.seconds)
115
 
 
116
 
    elif state == 'started':
117
 
        ### wait for the host to come up
118
 
        end = start + datetime.timedelta(seconds=timeout)
119
 
 
120
 
        while datetime.datetime.now() < end:
121
 
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
122
 
            s.settimeout(connect_timeout)
123
 
            try:
124
 
                s.connect( (host, port) )
125
 
                s.shutdown(socket.SHUT_RDWR)
126
 
                s.close()
127
 
                break
128
 
            except:
129
 
                time.sleep(1)
130
 
                pass
131
 
        else:
132
 
            elapsed = datetime.datetime.now() - start
133
 
            module.fail_json(msg="Timeout when waiting for %s:%s" % (host, port), elapsed=elapsed.seconds)
134
 
 
135
 
    elapsed = datetime.datetime.now() - start
136
 
    module.exit_json(state=state, port=port, elapsed=elapsed.seconds)
137
 
 
138
 
# this is magic, see lib/ansible/module_common.py
139
 
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
140
 
main()