コード例 #1
0
        public void DeleteApp(DeleteArgs deleteArgs)
        {
            CloudFoundryClient client = SetTargetInfoFromFile();

            PagedResponseCollection <ListAllAppsResponse> apps = client.Apps.ListAllApps(new RequestOptions()
            {
                Query = string.Format("name:{0}", deleteArgs.Name)
            }).Result;


            if (apps.Count() == 0)
            {
                throw new InvalidOperationException(string.Format("Couldn't find an app with name {0}", deleteArgs.Name));
            }

            client.Apps.DeleteApp(new Guid(apps.First().EntityMetadata.Guid)).Wait();

            new ConsoleString(string.Format("Deleted app with id {0}", apps.First().EntityMetadata.Guid), ConsoleColor.Green).WriteLine();
        }
コード例 #2
0
        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();
        }