def register(self, initial_status="STARTING"):
        data_center_info = {           
            '@class':'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',     
            '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, "@enabled": "true"},
                "securePort": {"$": self.secure_port, "@enabled": "true"},
                "dataCenterInfo": data_center_info,
                "healthCheckUrl": self.health_check_url or ""
            }
        }

        success = False
        last_e = None
        
        
        temp= json.dumps(instance_data)
        print temp
        for eureka_url in self.eureka_urls:
            
            try:
             
                r = requests.post(urljoin(eureka_url, "apps/%s" % self.app_name), temp,
                                 headers={'Content-Type': 'application/json'})
                print"request"
                print r
                r.raise_for_status()
                success = True
                break
            except (EurekaHTTPException, URLError) as e:
                last_e = e
                print e
        if not success:
            raise EurekaRegistrationFailedException("Did not receive correct reply from any instances, last error: " + str(e))
Exemple #2
0
 def register(self, initial_status="STARTING"):
     data_center_info = {
         '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
         '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,
             'ipAddr': self.ip_addr,
             'vipAddr': self.vip_address or '',
             'secureVipAddr': self.secure_vip_address or '',
             'status': initial_status,
             'port': {'$': self.port, '@enabled': 'true'},
             'securePort': {'$': self.secure_port, '@enabled': 'false'},
             'dataCenterInfo': data_center_info,
             'healthCheckUrl': self.health_check_url or '',
             'instanceId': self.get_instance_id(),
             'homePageUrl': self.home_page_url,
             'vipAddress': self.host_name,
             'secureVipAddress': self.host_name
         }
     }
     success = False
     last_e = None
     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:
             last_e = e
     if not success:
         raise EurekaRegistrationFailedException("Did not receive correct reply from any instances, last error: " + str(last_e))
Exemple #3
0
 def register(self, initial_status="STARTING"):
     data_center_info = {
         'name': self.data_center,
         '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo'
     }
     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': {
             'instanceId': self.host_name + ":" + self.app_name + ":" + str(self.port),
             'hostName': self.host_name,
             'app': self.app_name,
             'ipAddr': self.ip_address,
             'status': initial_status,
             'port': self.composed_port,
             'securePort': self.composed_secure_port,
             'dataCenterInfo': data_center_info,
             "leaseInfo": self.lease_info,
             "homePageUrl":  "http://" + self.host_name + ":" + str(self.port) + "/",
             "statusPageUrl": "http://" + self.host_name + ":" + str(self.port) + "/info",
             "healthCheckUrl": "http://" + self.host_name + ":" + str(self.port) + "/health",
             'vipAddress': self.vip_address or '',
             'secureVipAddress': self.secure_vip_address or ''
         }
     }
     success = False
     last_e = None
     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:
             last_e = e
     if not success:
         raise EurekaRegistrationFailedException("Did not receive correct reply from any instances, last error: " + str(e))
Exemple #4
0
 def register(self, initial_status="STARTING"):
     data_center_info = {
         '@class': 'com.netflix.appinfo.MyDataCenterInfo',
         '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': {
             'instanceId' : self.get_instance_id(),
             'hostName': self.host_name,
             'app': self.app_name,
             'vipAddress': self.vip_address or '',
             'secureVipAddress': self.secure_vip_address or '',
             'status': initial_status,
             'ipAddr' : self.ip_address,
             'port': {'$': self.port, '@enabled': self.port is not None},
             'securePort': {'$': self.secure_port, '@enabled': self.secure_port is not None},
             'dataCenterInfo': data_center_info,
             'healthCheckUrl': self.health_check_url,
             'statusPageUrl' : self.status_page_url
         }
     }
     success = False
     last_e = None
     for eureka_url in self.eureka_urls:
         try:
             print (instance_data)
             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 (requests.exceptions.HTTPError, URLError) as e:
             last_e = e
     if not success:
         raise EurekaRegistrationFailedException("Did not receive correct reply from any instances, last error: " + str(last_e))
Exemple #5
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,
                 health_check_url=None, status_page_url=None, ip_address=None):
        super(EurekaClient, self).__init__()
        self.app_name = app_name.upper()
        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.ip_address = ip_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.health_check_url = health_check_url
        self.status_page_url = status_page_url

        self.eureka_urls = self.get_eureka_urls()
 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,
              health_check_url=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.health_check_url = health_check_url
     self.eureka_urls = self.get_eureka_urls()
Exemple #7
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,
             'dataCenterInfo': data_center_info,
             'healthCheckUrl': self.health_check_url or ''
         }
     }
     if self.secure_port: # or Eureka POST call crashes if no secure port
         instance_data['instance']['securePort'] = self.secure_port
     success = False
     last_e = None
     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:
             last_e = e
     if not success:
         raise EurekaRegistrationFailedException("Did not receive correct reply from any instances, last error: " + str(e))
Exemple #8
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")
Exemple #9
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")
 def delete(self):
     instance_id = self.host_name
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     success = False
     last_e = None
     for eureka_url in self.eureka_urls:
         try:
             r=requests.delete(urljoin(eureka_url, "apps/%s/%s/" % (
                 self.app_name,
                 instance_id)), "", "")
             print r.raise_for_status()
      
             success = True
             break
         except (EurekaHTTPException, URLError) as e:
             last_e = e
     if not success:
         raise EurekaUpdateFailedException("Did not receive correct reply from any instances, last error: " + str(e))
Exemple #11
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
     last_e = None
     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:
             last_e = e
     if not success:
         raise EurekaUpdateFailedException("Did not receive correct reply from any instances, last error: " + str(e))
Exemple #12
0
 def update_status(self, new_status):
     instance_id = self.host_name + ":" + self.app_name + ":" + str(
         self.port)
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     success = False
     last_e = None
     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:
             last_e = e
     if not success:
         raise EurekaUpdateFailedException(
             "Did not receive correct reply from any instances, last error: "
             + str(e))
Exemple #13
0
 def get_instance_id(self):
     instance_id = "%s:%s:%d" % (self.host_name, self.app_name, self.port)
     if self.data_center == "Amazon":
         instance_id = ec2metadata.get('instance-id')
     return instance_id
Exemple #14
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)
Exemple #15
0
    def __init__(self,
                 app_name,
                 ip_address=None,
                 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,
                 health_check_url=None):
        super(EurekaClient, self).__init__()
        self.app_name = app_name
        self.ip_address = ip_address
        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
        if (port != None):
            self.composed_port = {"$": port, "@enabled": True}
        else:
            self.composed_port = {"$": 80, "@enabled": False}
        self.secure_port = secure_port
        if (secure_port != None):
            self.composed_secure_port = {"$": secure_port, "@enabled": True}
        else:
            self.composed_secure_port = {"$": 443, "@enabled": False}
        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.health_check_url = health_check_url
        self.eureka_urls = self.get_eureka_urls()

        self.lease_info = {
            "renewalIntervalInSecs": 5,
            "durationInSecs": 10,
            "registrationTimestamp": 0,
            "lastRenewalTimestamp": 0,
            "evictionTimestamp": 0,
            "serviceUpTimestamp": 0
        }
Exemple #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)
Exemple #17
0
    def __init__(self, app_name, ip_address=None, 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,
                 health_check_url=None):
        super(EurekaClient, self).__init__()
        self.app_name = app_name
        self.ip_address = ip_address
        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
        if (port != None):
            self.composed_port = {
                "$": port,
                "@enabled": True
            }
        else:
            self.composed_port = {
                "$": 80,
                "@enabled": False
            }
        self.secure_port = secure_port
        if (secure_port != None):
            self.composed_secure_port = {
                "$": secure_port,
                "@enabled": True
            }
        else:
            self.composed_secure_port = {
                "$": 443,
                "@enabled": False
            }
        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.health_check_url = health_check_url
        self.eureka_urls = self.get_eureka_urls()

        self.lease_info =  {
            "renewalIntervalInSecs": 5,
            "durationInSecs": 10,
            "registrationTimestamp": 0,
            "lastRenewalTimestamp": 0,
            "evictionTimestamp": 0,
            "serviceUpTimestamp": 0
        }
Exemple #18
0
 def get_instance_id(self):
     if self.data_center == "Amazon":
         return ec2metadata.get('instance-id')
     else:
         return ('%s:%s:%d' % (self.host_name, self.app_name, self.port))