Example #1
0
        internal Application LoadAppFromManifest()
        {
            this.CFManifest = this.CFManifest.Trim();

            Manifest man = ManifestDiskRepository.ReadManifest(this.CFManifest);

            if (man.Applications().Length > 1)
            {
                throw new InvalidOperationException("More than one application specified in the manifest file");
            }

            return(man.Applications()[0]);
        }
        public void TestSave()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "manifest.yaml"));

            string tempFile = Path.GetTempFileName();

            Manifest.Save(man.Applications(), tempFile);

            Manifest tempManifest = ManifestDiskRepository.ReadManifest(tempFile);

            Application[] apps = tempManifest.Applications();
            Assert.AreEqual(1, apps.Length);
            Assert.AreEqual("app-name", apps[0].Name);
            Assert.AreEqual(128, apps[0].Memory);
            Assert.AreEqual(1, apps[0].InstanceCount);
            Assert.AreEqual(1, apps[0].Hosts.Count);
            Assert.AreEqual(1, apps[0].Domains.Count);
            Assert.AreEqual("home", apps[0].Hosts[0]);
            Assert.AreEqual("app.example.com", apps[0].Domains[0]);
            Assert.AreEqual(2, apps[0].EnvironmentVariables.Count);
            Assert.AreEqual("first", apps[0].EnvironmentVariables["env1"]);
            Assert.AreEqual("test-bp", apps[0].BuildpackUrl);
            Assert.AreEqual(2, apps[0].Services.Count);
            Assert.AreEqual(@"c:\path\to\app", apps[0].Path);
            Assert.AreEqual("win2012r2", apps[0].StackName);
            Assert.AreEqual(1024, apps[0].DiskQuota);
            Assert.IsFalse(apps[0].NoHostName);
            Assert.IsFalse(apps[0].NoRoute);
            Assert.IsFalse(apps[0].UseRandomHostName);
            Assert.AreNotEqual(-1, apps[0].Services.IndexOf("mysql"));

            File.Delete(tempFile);
        }
        public void TestRandomWord()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "random.yaml"));

            Application[] apps = man.Applications();
            Assert.AreEqual(1, apps.Length);
            Assert.AreNotEqual("${random-word}", apps[0].Name);
        }
        public void TestNoRoute()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "no_route.yaml"));

            Application[] apps = man.Applications();
            Assert.AreEqual(1, apps.Length);
            Assert.IsTrue(apps[0].NoRoute);
        }
        public void TestReadManifestInherit()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "inherit.yaml"));

            Assert.IsNotNull(man);
            Assert.IsNotNull(man.Data);
            Assert.IsNotNull(man.Path);
            Assert.IsNotNull(man.Applications());
        }
        public void TestReadManifestDirPath()
        {
            Manifest man = ManifestDiskRepository.ReadManifest("manifests");

            Assert.IsNotNull(man);
            Assert.IsNotNull(man.Data);
            Assert.IsNotNull(man.Path);
            Assert.IsNotNull(man.Applications());
        }
        public void TestInheritManifest()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "inherit.yaml"));

            Application[] apps = man.Applications();
            Assert.AreEqual(1, apps.Length);
            Assert.AreEqual("app-name", apps[0].Name);
            Assert.AreEqual(128, apps[0].Memory);
            Assert.AreEqual(1, apps[0].InstanceCount);
            Assert.AreEqual(1, apps[0].Hosts.Count);
            Assert.AreEqual(1, apps[0].Domains.Count);
            Assert.AreEqual("home", apps[0].Hosts[0]);
            Assert.AreEqual("app.example.com", apps[0].Domains[0]);
            Assert.AreEqual(@"c:\path\to\app", apps[0].Path);
            Assert.AreEqual(2, apps[0].EnvironmentVariables.Count);
            Assert.AreEqual("first", apps[0].EnvironmentVariables["env1"]);
        }
        public void TestLoadManifest()
        {
            Manifest man = ManifestDiskRepository.ReadManifest(Path.Combine("manifests", "manifest.yaml"));

            Application[] apps = man.Applications();
            Assert.AreEqual(1, apps.Length);
            Assert.AreEqual("app-name", apps[0].Name);
            Assert.AreEqual(128, apps[0].Memory);
            Assert.AreEqual(1, apps[0].InstanceCount);
            Assert.AreEqual(1, apps[0].Hosts.Count);
            Assert.AreEqual(1, apps[0].Domains.Count);
            Assert.AreEqual("home", apps[0].Hosts[0]);
            Assert.AreEqual("app.example.com", apps[0].Domains[0]);
            Assert.AreEqual(@"c:\path\to\app", apps[0].Path);
            Assert.AreEqual(2, apps[0].EnvironmentVariables.Count);
            Assert.AreEqual("first", apps[0].EnvironmentVariables["env1"]);
            Assert.AreEqual("test-bp", apps[0].BuildpackUrl);
            Assert.AreEqual(500, apps[0].HealthCheckTimeout);
            Assert.AreEqual("cmd", apps[0].Command);
            Assert.AreEqual(2, apps[0].Services.Count);
            Assert.AreEqual(1024, apps[0].DiskQuota);
            Assert.AreNotEqual(-1, apps[0].Services.IndexOf("mysql"));
        }
Example #9
0
        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();
            }
        }