1111# See the License for the specific language governing permissions and
1212# limitations under the License.
1313###############################################################################
14-
14+ import csv
1515import os
1616import subprocess
1717import tempfile
18+ from StringIO import StringIO
1819from contextlib import contextmanager
1920
21+ import time
2022from jinja2 import Template
23+ import requests
2124
2225from cloudify_rest_client import exceptions as rest_exceptions
2326from cloudify import ctx
@@ -58,18 +61,34 @@ def configure(subject=None):
5861
5962
6063def add_backend (port , maxconn , backend_address = None ):
64+ backends_before = len (_read_haproxy_stats ())
65+ ctx .logger .info ('backends before adding {} ({})' .format (backends_before , time .time ()))
66+
6167 with _backends_update () as backends :
6268 backends [ctx .source .instance .id ] = {
6369 'address' : backend_address or ctx .source .instance .host_ip ,
6470 'port' : port ,
6571 'maxconn' : maxconn
6672 }
6773
74+ time .sleep (10 )
75+
76+ backends_after = len (_read_haproxy_stats ())
77+ ctx .logger .info ('backends after adding {} ({})' .format (backends_after , time .time ()))
78+
6879
6980def remove_backend ():
81+ backends_before = len (_read_haproxy_stats ())
82+ ctx .logger .info ('backends before removing {} ({})' .format (backends_before , time .time ()))
83+
7084 with _backends_update () as backends :
7185 backends .pop (ctx .source .instance .id , None )
7286
87+ time .sleep (10 )
88+
89+ backends_after = len (_read_haproxy_stats ())
90+ ctx .logger .info ('backends after removing {} ({})' .format (backends_after , time .time ()))
91+
7392
7493@contextmanager
7594def _backends_update ():
@@ -94,6 +113,21 @@ def _backends_update():
94113 raise
95114
96115
116+ def _read_haproxy_stats ():
117+ csv_data = requests .get (
118+ 'http://localhost:9000/haproxy_stats;csv' ,
119+ auth = ('admin' , 'password' )).text
120+ buff = StringIO (csv_data )
121+ parsed_csv_data = list (csv .reader (buff ))
122+ headers = parsed_csv_data [0 ]
123+ structured_csv_data = [dict (zip (headers , row ))
124+ for row in parsed_csv_data ]
125+ return dict ([(struct ['svname' ], int (struct ['stot' ]))
126+ for struct in structured_csv_data
127+ if struct ['# pxname' ] == 'servers' and
128+ struct ['svname' ] != 'BACKEND' ])
129+
130+
97131def start ():
98132 _service ('start' )
99133
0 commit comments