private void GetLogsUsingLogyard(CloudFoundryClient client, Guid?appGuid, GetV1InfoResponse info) { using (LogyardLog logyard = new LogyardLog(new Uri(info.AppLogEndpoint), string.Format(CultureInfo.InvariantCulture, "bearer {0}", client.AuthorizationToken), null, CFSkipSslValidation)) { logyard.ErrorReceived += (sender, error) => { Logger.LogErrorFromException(error.Error); }; logyard.StreamOpened += (sender, args) => { Logger.LogMessage("Log stream opened."); }; logyard.StreamClosed += (sender, args) => { Logger.LogMessage("Log stream closed."); }; logyard.MessageReceived += (sender, message) => { Logger.LogMessage("[{0}] - {1}: {2}", message.Message.Value.Source, message.Message.Value.HumanTime, message.Message.Value.Text); }; logyard.StartLogStream(appGuid.Value.ToString(), 0, true); this.MonitorApp(client, appGuid); logyard.StopLogStream(); } }
public void Push(PushArgs pushArgs) { if (!Directory.Exists(pushArgs.Dir)) { throw new DirectoryNotFoundException(string.Format("Directory '{0}' not found", pushArgs.Dir)); } CloudFoundryClient client = SetTargetInfoFromFile(); // ======= GRAB FIRST SPACE AVAILABLE ======= new ConsoleString("Looking up spaces ...", ConsoleColor.Cyan).WriteLine(); PagedResponseCollection <ListAllSpacesResponse> spaces = client.Spaces.ListAllSpaces().Result; if (spaces.Count() == 0) { throw new InvalidOperationException("Couldn't find any spaces"); } ListAllSpacesResponse space = spaces.First(); new ConsoleString(string.Format("Will be using space {0}", space.Name), ConsoleColor.Green).WriteLine(); // ======= CREATE AN APPLICATION ======= new ConsoleString("Creating app ...", ConsoleColor.Cyan).WriteLine(); PagedResponseCollection <ListAllStacksResponse> stacks = client.Stacks.ListAllStacks(new RequestOptions() { Query = string.Format("name:{0}", pushArgs.Stack) }).Result; if (stacks.Count() == 0) { throw new InvalidOperationException(string.Format("Couldn't find the stack {0}", pushArgs.Stack)); } CreateAppRequest createAppRequest = new CreateAppRequest() { Name = pushArgs.Name, Memory = pushArgs.Memory, StackGuid = new Guid(stacks.First().EntityMetadata.Guid), SpaceGuid = new Guid(space.EntityMetadata.Guid) }; CreateAppResponse appCreateResponse = client.Apps.CreateApp(createAppRequest).Result; new ConsoleString(string.Format("Created app with guid '{0}'", appCreateResponse.EntityMetadata.Guid), ConsoleColor.Green).WriteLine(); // ======= CREATE A ROUTE ======= new ConsoleString("Creating a route ...", ConsoleColor.Cyan).WriteLine(); PagedResponseCollection <ListAllSharedDomainsResponse> allDomains = client.SharedDomains.ListAllSharedDomains().Result; if (allDomains.Count() == 0) { throw new InvalidOperationException("Could not find any shared domains"); } string url = string.Format("{0}.{1}", pushArgs.Name, allDomains.First().Name); CreateRouteResponse createRouteResponse = client.Routes.CreateRoute(new CreateRouteRequest() { DomainGuid = new Guid(allDomains.First().EntityMetadata.Guid), Host = pushArgs.Name, SpaceGuid = new Guid(space.EntityMetadata.Guid) }).Result; new ConsoleString(string.Format("Created route '{0}.{1}'", pushArgs.Name, allDomains.First().Name), ConsoleColor.Green).WriteLine(); // ======= BIND THE ROUTE ======= new ConsoleString("Associating the route ...", ConsoleColor.Cyan).WriteLine(); client.Routes.AssociateAppWithRoute( new Guid(createRouteResponse.EntityMetadata.Guid), new Guid(appCreateResponse.EntityMetadata.Guid)).Wait(); // ======= HOOKUP LOGGING ======= // TODO: detect logyard vs loggregator GetV1InfoResponse v1Info = client.Info.GetV1Info().Result; LogyardLog logyard = new LogyardLog(new Uri(v1Info.AppLogEndpoint), string.Format("bearer {0}", client.AuthorizationToken)); logyard.ErrorReceived += (sender, error) => { Program.PrintExceptionMessage(error.Error); }; logyard.StreamOpened += (sender, args) => { new ConsoleString("Log stream opened.", ConsoleColor.Cyan).WriteLine(); }; logyard.StreamClosed += (sender, args) => { new ConsoleString("Log stream closed.", ConsoleColor.Cyan).WriteLine(); }; logyard.MessageReceived += (sender, message) => { new ConsoleString( string.Format("[{0}] - {1}: {2}", message.Message.Value.Source, message.Message.Value.HumanTime, message.Message.Value.Text), ConsoleColor.White).WriteLine(); }; logyard.StartLogStream(appCreateResponse.EntityMetadata.Guid, 0, true); // ======= PUSH THE APP ======= new ConsoleString("Pushing the app ...", ConsoleColor.Cyan).WriteLine(); client.Apps.PushProgress += (sender, progress) => { new ConsoleString(string.Format("Push at {0}%", progress.Percent), ConsoleColor.Yellow).WriteLine(); new ConsoleString(string.Format("{0}", progress.Message), ConsoleColor.DarkYellow).WriteLine(); }; client.Apps.Push(new Guid(appCreateResponse.EntityMetadata.Guid), pushArgs.Dir, true).Wait(); // ======= WAIT FOR APP TO COME ONLINE ======= bool done = false; while (true) { GetAppSummaryResponse appSummary = client.Apps.GetAppSummary(new Guid(appCreateResponse.EntityMetadata.Guid)).Result; if (appSummary.RunningInstances > 0) { break; } if (appSummary.PackageState == "FAILED") { throw new Exception("App staging failed."); } else if (appSummary.PackageState == "PENDING") { new ConsoleString("[cfcmd] - App is staging ...", ConsoleColor.DarkCyan).WriteLine(); } else if (appSummary.PackageState == "STAGED") { new ConsoleString("[cfcmd] - App staged, waiting for it to come online ...", ConsoleColor.DarkCyan).WriteLine(); } Thread.Sleep(2000); } logyard.StopLogStream(); new ConsoleString(string.Format("App is running, done. You can browse it here: http://{0}.{1}", pushArgs.Name, allDomains.First().Name), ConsoleColor.Green).WriteLine(); }
public override bool Execute() { logger = new Microsoft.Build.Utilities.TaskLoggingHelper(this); logger.LogMessage("Restarting application {0}", CFAppGuid); try { if (CFAppGuid.Length == 0) { logger.LogError("Application Guid must be specified"); return(false); } CloudFoundryClient client = InitClient(); // ======= HOOKUP LOGGING ======= // TODO: detect logyard vs loggregator GetV1InfoResponse v1Info = client.Info.GetV1Info().Result; using (LogyardLog logyard = new LogyardLog(new Uri(v1Info.AppLogEndpoint), string.Format(CultureInfo.InvariantCulture, "bearer {0}", client.AuthorizationToken), null, CFSkipSslValidation)) { logyard.ErrorReceived += (sender, error) => { logger.LogErrorFromException(error.Error, true); }; logyard.StreamOpened += (sender, args) => { logger.LogMessage("Log stream opened."); }; logyard.StreamClosed += (sender, args) => { logger.LogMessage("Log stream closed."); }; logyard.MessageReceived += (sender, message) => { logger.LogMessage("[{0}] - {1}: {2}", message.Message.Value.Source, message.Message.Value.HumanTime, message.Message.Value.Text); }; logyard.StartLogStream(CFAppGuid, 0, true); GetAppSummaryResponse response = client.Apps.GetAppSummary(new Guid(CFAppGuid)).Result; if (response.State != "STOPPED") { UpdateAppRequest stopReq = new UpdateAppRequest(); stopReq.State = "STOPPED"; client.Apps.UpdateApp(new Guid(CFAppGuid), stopReq).Wait(); } UpdateAppRequest startReq = new UpdateAppRequest(); startReq.State = "STARTED"; client.Apps.UpdateApp(new Guid(CFAppGuid), startReq).Wait(); // ======= WAIT FOR APP TO COME ONLINE ======= while (true) { GetAppSummaryResponse appSummary = client.Apps.GetAppSummary(new Guid(CFAppGuid)).Result; if (appSummary.RunningInstances > 0) { break; } if (appSummary.PackageState == "FAILED") { logger.LogError("App staging failed."); return(false); } else if (appSummary.PackageState == "PENDING") { logger.LogMessage("App is staging ..."); } else if (appSummary.PackageState == "STAGED") { logger.LogMessage("App staged, waiting for it to come online ..."); } Thread.Sleep(3000); } logyard.StopLogStream(); } } catch (AggregateException exception) { List <string> messages = new List <string>(); ErrorFormatter.FormatExceptionMessage(exception, messages); this.logger.LogError(string.Join(Environment.NewLine, messages)); return(false); } return(true); }
public override bool Execute() { this.CFOrganization = this.CFOrganization.Trim(); this.CFSpace = this.CFSpace.Trim(); this.Logger = new TaskLogger(this); var app = LoadAppFromManifest(); try { CloudFoundryClient client = InitClient(); Guid?spaceGuid = null; if ((!string.IsNullOrWhiteSpace(this.CFSpace)) && (!string.IsNullOrWhiteSpace(this.CFOrganization))) { spaceGuid = Utils.GetSpaceGuid(client, this.Logger, this.CFOrganization, this.CFSpace); if (spaceGuid == null) { return(false); } } var appGuid = Utils.GetAppGuid(client, app.Name, spaceGuid.Value); if (appGuid.HasValue == false) { Logger.LogError("Application {0} not found", app.Name); return(false); } Logger.LogMessage("Restarting application {0}", app.Name); // ======= HOOKUP LOGGING ======= GetV1InfoResponse info = client.Info.GetV1Info().Result; if (string.IsNullOrWhiteSpace(info.AppLogEndpoint) == false) { this.GetLogsUsingLogyard(client, appGuid, info); } else { GetInfoResponse detailedInfo = client.Info.GetInfo().Result; if (string.IsNullOrWhiteSpace(detailedInfo.LoggingEndpoint) == false) { this.GetLogsUsingLoggregator(client, appGuid, detailedInfo); } else { this.Logger.LogError("Could not retrieve application logs"); } } } catch (Exception exception) { this.Logger.LogError("Restart App failed", exception); return(false); } return(true); }