3 ############################################################################
4 # Copyright 2016 RIFT.IO Inc #
6 # Licensed under the Apache License, Version 2.0 (the "License"); #
7 # you may not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 ############################################################################
30 def ping_setup(yaml_cfg
, logger
):
31 '''Use curl to configure ping and set the ping rate'''
33 # Get the required and optional parameters
34 params
= yaml_cfg
['parameters']
35 mgmt_ip
= params
['mgmt_ip']
37 if 'mgmt_port' in params
:
38 mgmt_port
= params
['mgmt_port']
39 pong_ip
= params
['pong_ip']
41 if 'pong_port' in params
:
42 pong_port
= params
['pong_port']
47 cmd
= 'curl -D /dev/stdout -H "Accept: application/json" ' \
48 '-H "Content-Type: application/json" ' \
49 '-X POST -d "{{\\"ip\\":\\"{pong_ip}\\", \\"port\\":{pong_port}}}" ' \
50 'http://{mgmt_ip}:{mgmt_port}/api/v1/ping/server'. \
57 logger
.debug("Executing cmd: %s", cmd
)
65 proc
= subprocess
.Popen(cmd
, shell
=True,
66 stdout
=subprocess
.PIPE
,
67 stderr
=subprocess
.PIPE
)
70 logger
.debug("Process rc: {}".format(proc
.returncode
))
72 if proc
.returncode
== 0:
73 # Check if response is 200 OK
74 resp
= proc
.stdout
.read().decode()
75 if 'HTTP/1.1 200 OK' in resp
:
78 logger
.error("Got error response: {}".format(resp
))
82 elif proc
.returncode
== 7:
84 if count
>= max_tries
:
85 logger
.error("Connect failed for {}. Failing".format(count
))
91 #Exit the loop incase of errors other than connection timeout and response ok
92 err_resp
= proc
.stderr
.read().decode()
93 logger
.error("Got error response: {}".format(err_resp
))
94 return proc
.returncode
98 def main(argv
=sys
.argv
[1:]):
100 parser
= argparse
.ArgumentParser()
101 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
102 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
103 args
= parser
.parse_args()
105 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
106 if not os
.path
.exists(run_dir
):
108 log_file
= "{}/ping_setup-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
110 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
111 logger
= logging
.getLogger('ping-setup')
112 logger
.setLevel(logging
.DEBUG
)
114 fh
= logging
.FileHandler(log_file
)
115 fh
.setLevel(logging
.DEBUG
)
117 ch
= logging
.StreamHandler()
119 ch
.setLevel(logging
.DEBUG
)
121 ch
.setLevel(logging
.INFO
)
123 # create formatter and add it to the handlers
124 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
125 fh
.setFormatter(formatter
)
126 ch
.setFormatter(formatter
)
127 logger
.addHandler(fh
)
128 logger
.addHandler(ch
)
130 except Exception as e
:
131 logger
.exception("Exception in {}: {}".format(__file__
, e
))
135 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
136 yaml_str
= args
.yaml_cfg_file
.read()
137 yaml_cfg
= yaml
.load(yaml_str
)
138 logger
.debug("Input YAML: {}".format(yaml_cfg
))
140 rc
= ping_setup(yaml_cfg
, logger
)
141 logger
.info("Return code: {}".format(rc
))
144 except Exception as e
:
145 logger
.exception("Exception in {}: {}".format(__file__
, e
))
148 if __name__
== "__main__":