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 void Push(PushArgs pushArgs) { this.useV3 = pushArgs.V3; List <Application> manifestApps = new List <Application>(); List <Application> apps = new List <Application>(); if (!pushArgs.NoManifest) { string path; if (!string.IsNullOrWhiteSpace(pushArgs.Manifest)) { path = pushArgs.Manifest; } else { path = Directory.GetCurrentDirectory(); } try { Manifest manifest = ManifestDiskRepository.ReadManifest(path); manifestApps.AddRange(manifest.Applications().ToList()); } catch { if (!string.IsNullOrWhiteSpace(pushArgs.Manifest)) { throw new FileNotFoundException("Error reading manifest file."); } } } if (manifestApps.Count == 0) { var app = new Application() { Name = pushArgs.Name, Memory = pushArgs.Memory, Path = pushArgs.Dir, StackName = pushArgs.Stack }; app.Hosts.Add(pushArgs.Name); apps.Add(app); } else if (manifestApps.Count == 1) { Application app = manifestApps[0]; if (!string.IsNullOrWhiteSpace(pushArgs.Name)) { app.Name = pushArgs.Name; } if (pushArgs.Memory != 0) { app.Memory = pushArgs.Memory; } if (!string.IsNullOrWhiteSpace(pushArgs.Stack)) { app.StackName = pushArgs.Stack; } if (!string.IsNullOrWhiteSpace(pushArgs.Dir)) { app.Path = pushArgs.Dir; } if (!app.NoHostName && app.Hosts.Count == 0) { app.Hosts.Add(pushArgs.Name); } apps.Add(app); } else { if (!string.IsNullOrWhiteSpace(pushArgs.Name)) { Application app = manifestApps.FirstOrDefault(a => a.Name == pushArgs.Name); if (app == null) { throw new ArgumentException(string.Format("Could not find app named '{0}' in manifest", pushArgs.Name)); } if (!string.IsNullOrWhiteSpace(pushArgs.Name)) { app.Name = pushArgs.Name; } if (pushArgs.Memory != 0) { app.Memory = pushArgs.Memory; } if (!string.IsNullOrWhiteSpace(pushArgs.Stack)) { app.StackName = pushArgs.Stack; } if (!string.IsNullOrWhiteSpace(pushArgs.Dir)) { app.Path = pushArgs.Dir; } apps.Add(app); } else { apps.AddRange(manifestApps); } } CloudFoundryClient client = SetTargetInfoFromFile(); foreach (Application app in apps) { if (!Directory.Exists(app.Path)) { throw new DirectoryNotFoundException(string.Format("Directory '{0}' not found", app.Path)); } // ======= GRAB FIRST SPACE AVAILABLE ======= new ConsoleString("Looking up spaces ...", ConsoleColor.Cyan).WriteLine(); var spaces = client.V2.Spaces.ListAllSpaces().Result; if (spaces.Count() == 0) { throw new InvalidOperationException("Couldn't find any spaces"); } CCV2.Data.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(); var stacks = client.V2.Stacks.ListAllStacks(new CCV2.RequestOptions() { Query = string.Format("name:{0}", app.StackName) }).Result; if (stacks.Count() == 0) { throw new InvalidOperationException(string.Format("Couldn't find the stack {0}", app.StackName)); } Guid appGuid = Guid.Empty; if (pushArgs.V3) { CreateAppRequest createAppRequest = new CreateAppRequest() { Name = app.Name, SpaceGuid = new Guid(space.EntityMetadata.Guid) }; CreateAppResponse appCreateResponse = client.Apps.CreateApp(createAppRequest).Result; appGuid = new Guid(appCreateResponse.Guid.ToString()); new ConsoleString(string.Format("Created app with guid '{0}'", appCreateResponse.Guid), ConsoleColor.Green).WriteLine(); } else { CCV2.Data.CreateAppRequest createAppRequest = new CCV2.Data.CreateAppRequest() { Name = app.Name, Memory = (int)app.Memory, StackGuid = new Guid(stacks.First().EntityMetadata.Guid), SpaceGuid = new Guid(space.EntityMetadata.Guid) }; CCV2.Data.CreateAppResponse appCreateResponse = client.V2.Apps.CreateApp(createAppRequest).Result; appGuid = appCreateResponse.EntityMetadata.Guid; new ConsoleString(string.Format("Created app with guid '{0}'", appCreateResponse.EntityMetadata.Guid), ConsoleColor.Green).WriteLine(); } // ======= PUSH THE APP ======= new ConsoleString("Pushing the app ...", ConsoleColor.Cyan).WriteLine(); if (pushArgs.V3) { 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(appGuid, app.Path, app.StackName, null, true).Wait(); } else { client.V2.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.V2.Apps.Push(appGuid, app.Path, true).Wait(); } // ======= HOOKUP LOGGING AND MONITOR APP ======= Guid appGuidV2 = Guid.Empty; if (this.useV3) { appGuidV2 = new Guid(client.Apps.ListAssociatedProcesses(appGuid).Result.First().Guid.ToString()); } CCV2.Data.GetV1InfoResponse v1Info = client.V2.Info.GetV1Info().Result; if (string.IsNullOrWhiteSpace(v1Info.AppLogEndpoint) == false) { this.GetLogsUsingLogyard(client, appGuidV2, v1Info); } else { CCV2.Data.GetInfoResponse detailedInfo = client.V2.Info.GetInfo().Result; if (string.IsNullOrWhiteSpace(detailedInfo.LoggingEndpoint) == false) { this.GetLogsUsingLoggregator(client, appGuidV2, detailedInfo); } else { throw new Exception("Could not retrieve application logs"); } } // ======= BIND SERVICES var allServices = client.V2.ServiceInstances.ListAllServiceInstances().Result; foreach (string service in app.Services) { new ConsoleString(string.Format("Binding service {0} to app {1} ...", service, app.Name), ConsoleColor.Cyan).WriteLine(); var currentService = allServices.FirstOrDefault(s => s.Name.ToUpperInvariant() == service.ToUpperInvariant()); if (currentService == null) { throw new InvalidOperationException(string.Format("Could not find service {0}", service)); } CCV2.Data.CreateServiceBindingRequest request = new CCV2.Data.CreateServiceBindingRequest() { AppGuid = appGuidV2, ServiceInstanceGuid = currentService.EntityMetadata.Guid }; client.V2.ServiceBindings.CreateServiceBinding(request).Wait(); } // ======= CREATE ROUTES ======= var allDomains = client.V2.SharedDomains.ListAllSharedDomains().Result; ArrayList domains = app.Domains; if (domains.Count == 0) { domains.AddRange(allDomains.Select(d => d.Name).ToList()); } foreach (string domain in domains) { new ConsoleString("Creating a route ...", ConsoleColor.Cyan).WriteLine(); if (allDomains.Count() == 0) { throw new InvalidOperationException("Could not find any shared domains"); } var currentDomain = allDomains.FirstOrDefault(d => d.Name.ToUpperInvariant() == domain.ToUpperInvariant()); if (currentDomain == null) { throw new InvalidOperationException(string.Format("Could not find domain {0}", domain)); } foreach (string host in app.Hosts) { string url = string.Format("{0}.{1}", host, domain); CCV2.Data.CreateRouteResponse createRouteResponse = client.V2.Routes.CreateRoute(new CCV2.Data.CreateRouteRequest() { DomainGuid = new Guid(currentDomain.EntityMetadata.Guid), Host = host, SpaceGuid = new Guid(space.EntityMetadata.Guid) }).Result; new ConsoleString(string.Format("Created route '{0}.{1}'", host, domain), ConsoleColor.Green).WriteLine(); // ======= BIND THE ROUTE ======= new ConsoleString("Associating the route ...", ConsoleColor.Cyan).WriteLine(); client.V2.Routes.AssociateAppWithRoute( new Guid(createRouteResponse.EntityMetadata.Guid), appGuidV2).Wait(); } } client.Apps.StoppingApp(appGuid, new StoppingAppRequest() { }).Wait(); client.Apps.StartingApp(appGuid, new StartingAppRequest() { }).Wait(); new ConsoleString(string.Format("App is running, done."), ConsoleColor.Green).WriteLine(); } }