public void AppTest() { //Arrange GetAppSummaryResponse appResponse = new GetAppSummaryResponse() { Buildpack = "testBuildpack", DetectedBuildpack = "testDetectBuildpack", PackageUpdatedAt = "testCreation", Instances = 2, Memory = 256, Name = "testAppName", RunningInstances = 2 }; appResponse.Name = "testAppName"; appResponse.State = "STARTED"; CloudFoundryClient appClient = new CloudFoundryClient(new Uri("http://test.app.xip.io"), new System.Threading.CancellationToken()); //Act App testApp = new App(appResponse, appClient); //Assert Assert.IsTrue(testApp.Actions.Count > 0); Assert.IsTrue(testApp.Buildpack == "testBuildpack"); Assert.IsTrue(testApp.CreationDate == "testCreation"); Assert.IsTrue(testApp.DetectedBuildpack == "testDetectBuildpack"); Assert.IsNotNull(testApp.Icon); Assert.IsTrue(testApp.Instances > 0); Assert.IsTrue(testApp.Memory > 0); Assert.IsTrue(testApp.Name == "testAppName"); Assert.IsTrue(testApp.RunningInstances > 0); Assert.IsTrue(testApp.Text == "testAppName"); }
public App(GetAppSummaryResponse app, bool?diego, CloudFoundryClient client) : base(CloudItemType.App) { this.client = client; this.app = app; isDiego = diego; }
public AppFolder(string fileName, string filePath, int instanceNumber, GetAppSummaryResponse app, CloudFoundryClient client) : base(CloudItemType.AppFolder) { this.fileName = fileName; this.filePath = filePath; this.client = client; this.app = app; this.instanceNumber = instanceNumber; }
public void Application_test() { CreateAppResponse newApp = null; GetAppSummaryResponse readApp = null; UpdateAppResponse updateApp = null; CreateAppRequest app = new CreateAppRequest(); app.Name = Guid.NewGuid().ToString(); app.SpaceGuid = spaceGuid; app.Instances = 1; app.Memory = 256; app.StackGuid = stackGuid; try { newApp = client.Apps.CreateApp(app).Result; } catch (Exception ex) { Assert.Fail("Error creating app: {0}", ex.ToString()); } Assert.IsNotNull(newApp); try { readApp = client.Apps.GetAppSummary(newApp.EntityMetadata.Guid).Result; } catch (Exception ex) { Assert.Fail("Error reading app: {0}", ex.ToString()); } Assert.IsNotNull(readApp); Assert.AreEqual(app.Name, readApp.Name); UpdateAppRequest updateAppRequest = new UpdateAppRequest(); updateAppRequest.Memory = 512; try { updateApp = client.Apps.UpdateApp(newApp.EntityMetadata.Guid, updateAppRequest).Result; } catch (Exception ex) { Assert.Fail("Error updating app: {0}", ex.ToString()); } Assert.IsNotNull(updateApp); Assert.AreEqual(updateAppRequest.Memory, updateApp.Memory); try { client.Apps.DeleteApp(newApp.EntityMetadata.Guid).Wait(); } catch (Exception ex) { Assert.Fail("Error deleting app: {0}", ex.ToString()); } }
private void MonitorApp(CloudFoundryClient client, Guid?appGuid) { GetAppSummaryResponse response = client.Apps.GetAppSummary(appGuid.Value).Result; if (response.State != "STOPPED") { UpdateAppRequest stopReq = new UpdateAppRequest(); stopReq.State = "STOPPED"; client.Apps.UpdateApp(appGuid.Value, stopReq).Wait(); } UpdateAppRequest startReq = new UpdateAppRequest(); startReq.State = "STARTED"; client.Apps.UpdateApp(appGuid.Value, startReq).Wait(); // ======= WAIT FOR APP TO COME ONLINE ======= while (true) { if (this.CancelToken.IsCancellationRequested == true) { break; } GetAppSummaryResponse appSummary = client.Apps.GetAppSummary(appGuid.Value).Result; if (appSummary.RunningInstances > 0) { break; } if (appSummary.PackageState == "FAILED") { throw new InvalidOperationException("App staging failed"); } 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); } }
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 App(GetAppSummaryResponse app, CloudFoundryClient client) : base(CloudItemType.App) { this.client = client; this.app = app; }
public AppInstances(GetAppSummaryResponse app, int instanceNumber, CloudFoundryClient client) : base(CloudItemType.InstancesCollection) { this.app = app; this.instanceNumber = instanceNumber; this.client = client; }