Exemplo n.º 1
0
        /// <summary>
        /// Method collects metadata from local AWS EC2 instance.
        /// </summary>
        /// <returns>Returns an instance of AmazonMetadataType class.</returns>
        public async Task <DataCenterMetadata> Collect()
        {
            DataCenterMetadata metadata = new DataCenterMetadata();
            HttpClient         client   = new HttpClient();

            metadata.AmiLaunchIndex = await GetParameter(client, "ami-launch-index");

            metadata.LocalHostName = await GetParameter(client, "local-hostname");

            metadata.AvailabilityZone = await GetParameter(client, "placement/availability-zone");

            metadata.InstanceId = await GetParameter(client, "instance-id");

            metadata.PublicIPv4 = await GetParameter(client, "public-ipv4");

            metadata.PublicHostName = await GetParameter(client, "public-hostname");

            metadata.AmiManifestPath = await GetParameter(client, "ami-manifest-path");

            metadata.LocalIPv4 = await GetParameter(client, "local-ipv4");

            metadata.HostName = await GetParameter(client, "hostname");

            metadata.AmiId = await GetParameter(client, "ami-id");

            metadata.InstanceType = await GetParameter(client, "instance-type");

            return(metadata);
        }
Exemplo n.º 2
0
        //Put instance back into service
        //PUT /eureka/v2/apps/appID/instanceID/status?value=UP
        //HTTP Code:
        // * 200 on success
        // * 500 on failure
        public async Task <bool> PutInstanceToService(DataCenterMetadata dcData)
        {
            string url = this.EurekaServiceRoot + "/" + dcData.InstanceId + "/status?value=UP";

            try
            {
                HttpClient client = this.httpClientFactory.CreateInstance();
                System.Net.Http.Formatting.MediaTypeFormatter frm = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
                HttpResponseMessage response = await client.PutAsync(url, "", frm, "application/json");

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    string tmp = await response.Content.ReadAsStringAsync();

                    Trace.TraceError("Error on TakeInstanceDown: " + tmp);
                    return(false);
                }
                else
                {
                    Trace.TraceInformation("TakeInstanceDown has been successfully submitted.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }
Exemplo n.º 3
0
        //Send application instance heartbeat
        //PUT /eureka/v2/apps/appID/instanceID
        //HTTP Code:
        // * 200 on success
        // * 404 if instanceID doesn’t exist
        public async Task <bool> SendHeartbeat(DataCenterMetadata dcData)
        {
            string url = this.EurekaServiceRoot + "/" + dcData.InstanceId;

            try
            {
                HttpClient client = this.httpClientFactory.CreateInstance();
                System.Net.Http.Formatting.MediaTypeFormatter frm = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
                HttpResponseMessage response = await client.PutAsync(url, "", frm, "application/json");

                //response.EnsureSuccessStatusCode();
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    string tmp = await response.Content.ReadAsStringAsync();

                    Trace.TraceError("Error on Heartbeat: " + tmp);
                    return(false);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    Trace.TraceWarning("Heartbeat failed. Instance does not exist.");
                    return(false);
                }
                else
                {
                    Trace.TraceInformation("Heartbeat has been successfully submitted.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }
Exemplo n.º 4
0
        //De-register application instance
        //DELETE /eureka/v2/apps/appID/instanceID
        //HTTP Code: 200 on success
        public async Task <bool> Unregister(DataCenterMetadata dcData)
        {
            string url = this.EurekaServiceRoot + "/" + dcData.InstanceId;

            try
            {
                HttpClient          client   = this.httpClientFactory.CreateInstance();
                HttpResponseMessage response = await client.DeleteAsync(url);

                //response.EnsureSuccessStatusCode();
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    string tmp = await response.Content.ReadAsStringAsync();

                    Trace.TraceError("Exception on Unregister: " + tmp);
                    return(false);
                }
                else
                {
                    Trace.TraceInformation("Unregistered successfully.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Method collects metadata from local machine.
        /// </summary>
        /// <returns>Returns an instance of AmazonMetadataType class.</returns>
        public Task <DataCenterMetadata> Collect()
        {
            var taskSource = new TaskCompletionSource <DataCenterMetadata>();
            DataCenterMetadata metadata = new DataCenterMetadata();

            metadata.DataCenterName = DataCenterType.MyOwn;
            KaryonConfig appConfig = KaryonConfig.Current;

            metadata.LocalIPv4  = appConfig.NonAmazonLocalIPv4;
            metadata.PublicIPv4 = appConfig.NonAmazonPublicIPv4;
            metadata.InstanceId = appConfig.NonAmazonInstanceId;
            taskSource.SetResult(metadata);
            return(taskSource.Task);
        }
Exemplo n.º 6
0
        //Register new application instance
        //POST /eureka/v2/apps/appID
        //Input: JSON/XML payload HTTP Code: 204 on success
        public async Task <bool> Register(DataCenterMetadata dcData)
        {
            EurekaRegistrationInfo instanceData = new EurekaRegistrationInfo();

            instanceData.Instance.DataCenterInfo.Name     = dcData.DataCenterName;
            instanceData.Instance.DataCenterInfo.Metadata = dcData;
            instanceData.Instance.HostName   = dcData.HostName;// "ec2-50-16-138-165.compute-1.amazonaws.com";
            instanceData.Instance.App        = this.ApplicationName;
            instanceData.Instance.Port       = this.ApplicationPort;
            instanceData.Instance.IPAddr     = dcData.LocalIPv4;
            instanceData.Instance.VipAddress = dcData.PublicIPv4;// "50.16.138.165";
            instanceData.Instance.SecurePort = this.ApplicationSecurePort;
            if (this.ApplicationSecurePort > 0)
            {
                instanceData.Instance.SecureVipAddress = dcData.PublicIPv4;
            }

            HttpClient client = null;
            string     url    = this.EurekaServiceRoot;

            try
            {
                System.Net.Http.Formatting.MediaTypeFormatter frm = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
                client = this.httpClientFactory.CreateInstance();
                HttpResponseMessage response = await client.PostAsync(url, instanceData, frm, "application/json");

                //response.EnsureSuccessStatusCode();
                if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
                {
                    string tmp = await response.Content.ReadAsStringAsync();

                    Trace.TraceError("Error on Register: " + tmp);
                    return(false);
                }
                else
                {
                    Trace.TraceInformation("Registration completed. Status: " + response.StatusCode.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }
 public DataCenterInfo()
 {
     this.Name     = DataCenterType.Amazon;
     this.Metadata = new DataCenterMetadata();
 }
Exemplo n.º 8
0
 public HeartbeatObserver(IEurekaClient eureka, DataCenterMetadata dcMetadata, IEnumerable <string> eurekaServiceUrls)
 {
     this.DataCenterMetadata = dcMetadata;
     this.eureka             = eureka;
     this.EurekaServiceUrls  = eurekaServiceUrls;
 }