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 pong_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 service_ip
= params
['service_ip']
41 if 'service_port' in params
:
42 service_port
= params
['service_port']
47 config_cmd
= 'curl -D /dev/stdout -H "Accept: application/json" ' \
48 '-H "Content-Type: application/json" ' \
49 '-X POST -d "{{\\"ip\\":\\"{service_ip}\\", \\"port\\":{service_port}}}" ' \
50 'http://{mgmt_ip}:{mgmt_port}/api/v1/pong/server'. \
54 service_ip
=service_ip
,
55 service_port
=service_port
)
57 logger
.debug("Executing cmd: %s", config_cmd
)
65 proc
= subprocess
.Popen(config_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
99 def main(argv
=sys
.argv
[1:]):
101 parser
= argparse
.ArgumentParser()
102 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
103 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
104 args
= parser
.parse_args()
106 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
107 if not os
.path
.exists(run_dir
):
109 log_file
= "{}/pong_setup-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
111 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
112 logger
= logging
.getLogger('pong-setup')
113 logger
.setLevel(logging
.DEBUG
)
115 fh
= logging
.FileHandler(log_file
)
116 fh
.setLevel(logging
.DEBUG
)
118 ch
= logging
.StreamHandler()
120 ch
.setLevel(logging
.DEBUG
)
122 ch
.setLevel(logging
.INFO
)
124 # create formatter and add it to the handlers
125 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
126 fh
.setFormatter(formatter
)
127 ch
.setFormatter(formatter
)
128 logger
.addHandler(fh
)
129 logger
.addHandler(ch
)
131 except Exception as e
:
132 logger
.exception("Exception in {}: {}".format(__file__
, e
))
136 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
137 yaml_str
= args
.yaml_cfg_file
.read()
138 yaml_cfg
= yaml
.load(yaml_str
)
139 logger
.debug("Input YAML: {}".format(yaml_cfg
))
141 rc
= pong_setup(yaml_cfg
, logger
)
142 logger
.info("Return code: {}".format(rc
))
145 except Exception as e
:
146 logger
.exception("Exception in {}: {}".format(__file__
, e
))
149 if __name__
== "__main__":