Example #1
0
    def register(self, initial_status="STARTING"):
        data_center_info = {
            'name': self.data_center,
            '@class': 'com.netflix.appinfo.DataCenterInfo'
        }
        if self.data_center == "Amazon":
            data_center_info['metadata'] = {
                'ami-launch-index': ec2metadata.get('ami-launch-index'),
                'local-hostname': ec2metadata.get('local-hostname'),
                'availability-zone': ec2metadata.get('availability-zone'),
                'instance-id': ec2metadata.get('instance-id'),
                'public-ipv4': ec2metadata.get('public-ipv4'),
                'public-hostname': ec2metadata.get('public-hostname'),
                'ami-manifest-path': ec2metadata.get('ami-manifest-path'),
                'local-ipv4': ec2metadata.get('local-ipv4'),
                'ami-id': ec2metadata.get('ami-id'),
                'instance-type': ec2metadata.get('instance-type'),
            }
        instance_data = {
            'instance': {
                'hostName': self.host_name,
                'app': self.app_name,
                'ipAddr': self.ip_address,
                'vipAddress': self.vip_address or '',
                'secureVipAddress': self.secure_vip_address or '',
                'status': initial_status,
                'dataCenterInfo': data_center_info,
                'healthCheckUrl': self.health_check_url,
                'secureHealthCheckUrl': self.secure_health_check_url,
                'appGroupName': self.app_group_name,
                'asgName': self.asg_name,
                'metadata': self.metadata,
            }
        }
        if self.port:
            instance_data['instance']['port'] = {
                "$": self.port,
                "@enabled": True
            }
        if self.secure_port:
            instance_data['instance']['securePort'] = {
                "$": self.secure_port,
                "@enabled": True
            }

        success = False
        for eureka_url in self.eureka_urls:
            try:
                r = requests.post(urljoin(eureka_url, "apps/%s" % self.app_name), json.dumps(instance_data),
                                  headers={'Content-Type': 'application/json'})
                r.raise_for_status()
                success = True
                break
            except (EurekaHTTPException, URLError):
                pass
        if not success:
            raise EurekaRegistrationFailedException("Did not receive correct reply from any instances"), None, sys.exc_info()[2]
Example #2
0
 def __init__(self, app_name, eureka_url=None, eureka_domain_name=None, host_name=None, data_center="Amazon",
              vip_address=None, secure_vip_address=None, port=None, secure_port=None, use_dns=True, region=None,
              prefer_same_zone=True, context="eureka/v2", eureka_port=None):
     super(EurekaClient, self).__init__()
     self.app_name = app_name
     self.eureka_url = eureka_url
     self.data_center = data_center
     if not host_name and data_center == "Amazon":
         self.host_name = ec2metadata.get("public-hostname")
     else:
         self.host_name = host_name
     # Virtual host name by which the clients identifies this service
     self.vip_address = vip_address
     self.secure_vip_address = secure_vip_address
     self.port = port
     self.secure_port = secure_port
     self.use_dns = use_dns
     # Region where eureka is deployed - For AWS specify one of the AWS regions, for other datacenters specify a
     # arbitrary string indicating the region.
     self.region = region
     # Prefer a eureka server in same zone or not
     self.prefer_same_zone = prefer_same_zone
     # Domain name, if using DNS
     self.eureka_domain_name = eureka_domain_name
     #if eureka runs on a port that is not 80, this will go into the urls to eureka
     self.eureka_port = eureka_port
     # Relative URL to eureka
     self.context = context
     self.eureka_urls = self.get_eureka_urls()
Example #3
0
 def register(self, initial_status="STARTING"):
     data_center_info = {
         'name': self.data_center
     }
     if self.data_center == "Amazon":
         data_center_info['metadata'] = {
             'ami-launch-index': ec2metadata.get('ami-launch-index'),
             'local-hostname': ec2metadata.get('local-hostname'),
             'availability-zone': ec2metadata.get('availability-zone'),
             'instance-id': ec2metadata.get('instance-id'),
             'public-ipv4': ec2metadata.get('public-ipv4'),
             'public-hostname': ec2metadata.get('public-hostname'),
             'ami-manifest-path': ec2metadata.get('ami-manifest-path'),
             'local-ipv4': ec2metadata.get('local-ipv4'),
             'ami-id': ec2metadata.get('ami-id'),
             'instance-type': ec2metadata.get('instance-type'),
         }
     instance_data = {
         'instance': {
             'hostName': self.host_name,
             'app': self.app_name,
             'vipAddr': self.vip_address or '',
             'secureVipAddr': self.secure_vip_address or '',
             'status': initial_status,
             'port': self.port,
             'securePort': self.secure_port,
             'dataCenterInfo': data_center_info
         }
     }
     success = False
     for eureka_url in self.eureka_urls:
         try:
             r = requests.post(urljoin(eureka_url, "apps/%s" % self.app_name), json.dumps(instance_data),
                               headers={'Content-Type': 'application/json'})
             r.raise_for_status()
             success = True
             break
         except (EurekaHTTPException, URLError) as e:
             pass
     if not success:
         raise EurekaRegistrationFailedException("Did not receive correct reply from any instances")
Example #4
0
 def heartbeat(self):
     instance_id = self.host_name
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     success = False
     for eureka_url in self.eureka_urls:
         try:
             r = requests.put(urljoin(eureka_url, "apps/%s/%s" % (self.app_name, instance_id)))
             r.raise_for_status()
             success = True
             break
         except (EurekaHTTPException, URLError) as e:
             pass
     if not success:
         raise EurekaHeartbeatFailedException("Did not receive correct reply from any instances")
Example #5
0
 def update_status(self, new_status):
     instance_id = self.host_name
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     success = False
     for eureka_url in self.eureka_urls:
         try:
             r = requests.put(urljoin(eureka_url, "apps/%s/%s/status?value=%s" % (
                 self.app_name,
                 instance_id,
                 new_status
             )))
             r.raise_for_status()
             success = True
             break
         except (EurekaHTTPException, URLError) as e:
             pass
     if not success:
         raise EurekaUpdateFailedException("Did not receive correct reply from any instances"), None, sys.exc_info()[2]
Example #6
0
 def update_status(self, new_status):
     instance_id = get_instances(self)
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     success = False
     for Consul_url in self.Consul_urls:
         try:
             r = requests.put(
                 urljoin(
                     Consul_url, "apps/%s/%s/status?value=%s" %
                     (self.app_name, instance_id, new_status)))
             r.raise_for_status()
             success = True
             break
         except (ConsulHTTPException, URLError) as e:
             pass
     if not success:
         raise ConsulUpdateFailedException(
             "Did not receive correct reply from any instances"
         ), None, sys.exc_info()[2]
Example #7
0
 def __init__(self,
              app_name,
              eureka_url=None,
              eureka_domain_name=None,
              host_name=None,
              data_center="Amazon",
              vip_address=None,
              secure_vip_address=None,
              port=None,
              secure_port=None,
              use_dns=True,
              region=None,
              prefer_same_zone=True,
              context="eureka/v2",
              eureka_port=None):
     super(EurekaClient, self).__init__()
     self.app_name = app_name
     self.eureka_url = eureka_url
     self.data_center = data_center
     if not host_name and data_center == "Amazon":
         self.host_name = ec2metadata.get("public-hostname")
     else:
         self.host_name = host_name
     # Virtual host name by which the clients identifies this service
     self.vip_address = vip_address
     self.secure_vip_address = secure_vip_address
     self.port = port
     self.secure_port = secure_port
     self.use_dns = use_dns
     # Region where eureka is deployed - For AWS specify one of the AWS regions, for other datacenters specify a
     # arbitrary string indicating the region.
     self.region = region
     # Prefer a eureka server in same zone or not
     self.prefer_same_zone = prefer_same_zone
     # Domain name, if using DNS
     self.eureka_domain_name = eureka_domain_name
     #if eureka runs on a port that is not 80, this will go into the urls to eureka
     self.eureka_port = eureka_port
     # Relative URL to eureka
     self.context = context
     self.eureka_urls = self.get_eureka_urls()
Example #8
0
 def heartbeat(self, instance_id):
     while True:
         print 'run'
         if self.data_center == "Amazon":
             instance_id = ec2metadata.get('instance-id')
         success = False
         for i in xrange(len(self.Consul_urls)):
             try:
                 # instance_data["instance"]["healthCheckUrl"] =self.healthCheckUrls[i]
                 # instance_data["instance"]["statusPageUrl"] = self.statusPageUrls[i]
                 r = requests.put(
                     urljoin(self.Consul_urls[i],
                             "apps/%s/%s" % (self.app_name, instance_id)))
                 r[0].raise_for_status()
                 success = True
                 break
             except (ConsulHTTPException, URLError) as e:
                 pass
         if not success:
             raise ConsulHeartbeatFailedException(
                 "Did not receive correct reply from any instances"
             ), None, sys.exc_info()[2]
         time.sleep(int(self.heartbeatInterval))
Example #9
0
 def get_instance_zone(self):
     if self.data_center == "Amazon":
         return ec2metadata.get('availability-zone')
     else:
         raise NotImplementedError("%s does not implement DNS lookups" % self.data_center)
Example #10
0
def get_region():
    return ec2metadata.get('availability-zone')[0:-1]
Example #11
0
def get_zone():
    return ec2metadata.get('availability-zone')
Example #12
0
def get_instanceid():
    return ec2metadata.get('instance-id')
Example #13
0
def get_region():
    return ec2metadata.get('availability-zone')[0:-1]
Example #14
0
def get_zone():
    return ec2metadata.get('availability-zone')
Example #15
0
def get_instanceid():
    return ec2metadata.get('instance-id')
Example #16
0
 def get_instance_zone(self):
     if self.data_center == "Amazon":
         return ec2metadata.get('availability-zone')
     else:
         raise NotImplementedError("%s does not implement DNS lookups" %
                                   self.data_center)