22import logging
33from collections import OrderedDict
44
5+ from compose .cli .docker_client import docker_client
6+
7+ import config
58import helper .backend_helper as BackendHelper
9+ import helper .cloud_link_helper as CloudLinkHelper
610import helper .config_helper as ConfigHelper
711import helper .frontend_helper as FrontendHelper
8- import helper .init_helper as InitHelper
12+ import helper .new_link_helper as NewLinkHelper
913import helper .ssl_helper as SslHelper
1014import helper .tcp_helper as TcpHelper
1115import helper .update_helper as UpdateHelper
1216from haproxy .config import *
13- from parser import Specs
17+ from haproxy . parser import LegacyLinkSpecs , NewLinkSpecs
1418from utils import fetch_remote_obj , prettify , save_to_file , get_service_attribute , get_bind_string
1519
1620logger = logging .getLogger ("haproxy" )
1721
1822
1923def run_haproxy (msg = None ):
20- haproxy = Haproxy (msg )
24+ haproxy = Haproxy (config . LINK_MODE , msg )
2125 haproxy .update ()
2226
2327
@@ -27,62 +31,78 @@ class Haproxy(object):
2731 cls_process = None
2832 cls_certs = []
2933
30- cls_service_name_match = re .compile (r"(.+)_\d+$" )
31-
32- LINKED_CONTAINER_CACHE = {}
33-
34- def __init__ (self , msg = "" ):
34+ def __init__ (self , link_mode = "" , msg = "" ):
3535 logger .info ("==========BEGIN==========" )
3636 if msg :
3737 logger .info (msg )
3838
39+ self .link_mode = link_mode
3940 self .ssl_bind_string = None
4041 self .ssl_updated = False
4142 self .routes_added = []
4243 self .require_default_route = False
44+ self .specs = None
4345
44- self ._initialize ()
45-
46- def _initialize (self ):
47- if HAPROXY_CONTAINER_URI and HAPROXY_SERVICE_URI and API_AUTH :
48- haproxy_container = fetch_remote_obj (HAPROXY_CONTAINER_URI )
46+ self .specs = self ._initialize (self .link_mode )
4947
50- haproxy_links = InitHelper .get_links_from_haproxy (haproxy_container .linked_to_container )
51- new_added_container_uris = InitHelper .get_new_added_link_uri (Haproxy .LINKED_CONTAINER_CACHE , haproxy_links )
52- new_added_containers = InitHelper .get_container_object_from_uri (new_added_container_uris )
53- InitHelper .update_container_cache (Haproxy .LINKED_CONTAINER_CACHE , new_added_container_uris ,
54- new_added_containers )
55- linked_containers = InitHelper .get_linked_containers (Haproxy .LINKED_CONTAINER_CACHE ,
56- haproxy_container .linked_to_container )
57- InitHelper .update_haproxy_links (haproxy_links , linked_containers )
58-
59- logger .info ("Service links: %s" , ", " .join (InitHelper .get_service_links_str (haproxy_links )))
60- logger .info ("Container links: %s" , ", " .join (InitHelper .get_container_links_str (haproxy_links )))
61-
62- Haproxy .cls_linked_services = InitHelper .get_linked_services (haproxy_links )
63- self .specs = Specs (haproxy_links )
48+ @staticmethod
49+ def _initialize (link_mode ):
50+ if link_mode == "cloud" :
51+ links = Haproxy ._init_cloud_links ()
52+ specs = NewLinkSpecs (links )
53+ elif link_mode == "new" :
54+ links = Haproxy ._init_new_links ()
55+ if links is None :
56+ specs = LegacyLinkSpecs ()
57+ else :
58+ specs = NewLinkSpecs (links )
6459 else :
65- logger .info ("Loading HAProxy definition from environment variables" )
66- Haproxy .cls_linked_services = None
67- Haproxy .specs = Specs ()
60+ specs = LegacyLinkSpecs ()
61+ return specs
6862
69- def update (self ):
70- self ._config_ssl ()
63+ @staticmethod
64+ def _init_cloud_links ():
65+ haproxy_container = fetch_remote_obj (HAPROXY_CONTAINER_URI )
66+ links = CloudLinkHelper .get_cloud_links (haproxy_container )
67+ Haproxy .cls_linked_services = CloudLinkHelper .get_linked_services (links )
68+ logger .info ("Linked service: %s" , ", " .join (CloudLinkHelper .get_service_links_str (links )))
69+ logger .info ("Linked container: %s" , ", " .join (CloudLinkHelper .get_container_links_str (links )))
70+ return links
7171
72- cfg_dict = OrderedDict ()
73- cfg_dict .update (self ._config_global_section ())
74- cfg_dict .update (self ._config_defaults_section ())
75- cfg_dict .update (self ._config_stats_section ())
76- cfg_dict .update (self ._config_userlist_section (HTTP_BASIC_AUTH ))
77- cfg_dict .update (self ._config_tcp_sections ())
78- cfg_dict .update (self ._config_frontend_sections ())
79- cfg_dict .update (self ._config_backend_sections ())
72+ @staticmethod
73+ def _init_new_links ():
74+ try :
75+ docker = docker_client ()
76+ docker .ping ()
77+ container_id = os .environ .get ("HOSTNAME" , "" )
78+ haproxy_container = docker .inspect_container (container_id )
79+ except Exception as e :
80+ logger .info ("Docker API error, regressing to legacy links mode: " , e )
81+ return None
82+ links , Haproxy .cls_linked_services = NewLinkHelper .get_new_links (docker , haproxy_container )
83+ logger .info ("Linked service: %s" , ", " .join (NewLinkHelper .get_service_links_str (links )))
84+ logger .info ("Linked container: %s" , ", " .join (NewLinkHelper .get_container_links_str (links )))
85+ return links
8086
81- cfg = prettify (cfg_dict )
82- self ._update_haproxy (cfg )
87+ def update (self ):
88+ if self .specs :
89+ self ._config_ssl ()
90+ cfg_dict = OrderedDict ()
91+ cfg_dict .update (self ._config_global_section ())
92+ cfg_dict .update (self ._config_defaults_section ())
93+ cfg_dict .update (self ._config_stats_section ())
94+ cfg_dict .update (self ._config_userlist_section (HTTP_BASIC_AUTH ))
95+ cfg_dict .update (self ._config_tcp_sections ())
96+ cfg_dict .update (self ._config_frontend_sections ())
97+ cfg_dict .update (self ._config_backend_sections ())
98+
99+ cfg = prettify (cfg_dict )
100+ self ._update_haproxy (cfg )
101+ else :
102+ logger .info ("Internal error: Specs is not initialized" )
83103
84104 def _update_haproxy (self , cfg ):
85- if HAPROXY_SERVICE_URI and HAPROXY_CONTAINER_URI and API_AUTH :
105+ if self . link_mode in [ "cloud" , "new" ] :
86106 if Haproxy .cls_cfg != cfg :
87107 logger .info ("HAProxy configuration:\n %s" % cfg )
88108 Haproxy .cls_cfg = cfg
@@ -94,10 +114,10 @@ def _update_haproxy(self, cfg):
94114 else :
95115 logger .info ("HAProxy configuration remains unchanged" )
96116 logger .info ("===========END===========" )
97- else :
117+ elif self . link_mode in [ "legacy" ] :
98118 logger .info ("HAProxy configuration:\n %s" % cfg )
99- save_to_file (HAPROXY_CONFIG_FILE , cfg )
100- UpdateHelper .run_once ()
119+ if save_to_file (HAPROXY_CONFIG_FILE , cfg ):
120+ UpdateHelper .run_once ()
101121
102122 def _config_ssl (self ):
103123 ssl_bind_string = ""
0 commit comments