public void CleansUpWhenItFails()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                ContainerHostService.StartContainerHost(null, null, null, null)
                .ThrowsForAnyArgs(new Exception());

                try
                {
                    Service.CreateContainer(spec);
                }
                catch (Exception)
                {
                    // Expect this exception.
                }

                // Created and deleted the user
                UserManager.Received(1).CreateUser(Arg.Any <string>());
                UserManager.Received(1).DeleteUser(Arg.Any <string>());
                UserManager.Received(1).CreateProfile(Arg.Any <string>());
                UserManager.Received(1).DeleteProfile(Arg.Any <string>());

                containerDirectory.Received().Destroy();
            }
            public WithContainer()
            {
                Handle = "KnownHandle";
                var spec = new ContainerSpec
                {
                    Handle = Handle,
                };

                Container = Service.CreateContainer(spec);
            }
            public void GenerateDiskQuotaControlUsingTheContainerDirectory()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);
                diskQuotaManager.Received().CreateDiskQuotaControl(containerDirectory, sid);
            }
            public void UsesProvidedHandle()
            {
                var spec = new ContainerSpec
                {
                    Handle = "container-handle",
                };

                var container = Service.CreateContainer(spec);

                Assert.Equal("container-handle", container.Handle);
            }
            public void CreatesContainerSpecificDirectory()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                containerDirectory.Received().CreateSubdirectories(Arg.Any <IContainerUser>());
            }
            public void CreatesContainerSpecificUser()
            {
                UserManager.CreateUser("").ReturnsForAnyArgs(new NetworkCredential());
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                UserManager.Received(1).CreateUser("c_DEADBEEF");
            }
            public void GeneratesIdFromHandle()
            {
                HandleHelper.GenerateId("handle").Returns("derived-id");
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                var container = Service.CreateContainer(spec);

                Assert.Equal("derived-id", container.Id);
            }
            public void CreatesContainerSpecificHost()
            {
                var expectedCredentials = new NetworkCredential();

                UserManager.CreateUser("").ReturnsForAnyArgs(expectedCredentials);
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                ContainerHostService.Received(1).StartContainerHost(Arg.Any <string>(), Arg.Any <IContainerDirectory>(), Arg.Any <JobObject>(), expectedCredentials);
            }
            public void CreatesBindMounts()
            {
                var bindMounts = new[]
                {
                    new BindMount()
                };
                var spec = new ContainerSpec
                {
                    BindMounts = bindMounts
                };

                Service.CreateContainer(spec);
                containerDirectory.Received().CreateBindMounts(bindMounts, Arg.Any <IContainerUser>());
            }
            public void WhenHandleIsNotProvided_GeneratesHandle(string handle)
            {
                var expectedHandle = Guid.NewGuid().ToString("N");

                HandleHelper.GenerateHandle().Returns(expectedHandle);
                var spec = new ContainerSpec
                {
                    Handle = handle,
                };

                var container = Service.CreateContainer(spec);

                Assert.NotEqual(handle, container.Handle);
                Assert.Equal(expectedHandle, container.Handle);
            }
                public void ContainerRemainsInContainerListForRemoveOnException()
                {
                    var badSpec = new ContainerSpec
                    {
                        Handle = "KnownBadHandle",
                    };

                    var badContainer = Service.CreateContainer(badSpec);

                    Assert.NotNull(Service.GetContainerByHandle(badContainer.Handle));
                    try {
                        Service.DestroyContainer(badContainer.Handle);
                    } catch (Exception) { }
                    Assert.NotNull(Service.GetContainerByHandleIncludingDestroyed(badContainer.Handle));
                }
                public void ContainerIsRemovedFromContainerListOnException()
                {
                    var badSpec = new ContainerSpec
                    {
                        Handle = "KnownBadHandle",
                    };

                    var badContainer = Service.CreateContainer(badSpec);

                    Assert.NotNull(Service.GetContainerByHandle(badContainer.Handle));
                    try
                    {
                        Service.DestroyContainer(badContainer.Handle);
                    } catch (Exception) { }
                    Assert.Null(Service.GetContainerByHandle(badContainer.Handle));
                }
            public void SetsProperties()
            {
                var spec = new ContainerSpec
                {
                    Handle     = "handle",
                    Properties = new Dictionary <string, string>
                    {
                        { "name1", "value1" },
                        { "name2", "value2" },
                    },
                };

                var container = Service.CreateContainer(spec);

                ContainerPropertiesService.Received(1).SetProperties(container, spec.Properties);
            }
                public void IncludesDestroyingContainersInGetContainers()
                {
                    var badSpec = new ContainerSpec
                    {
                        Handle = "KnownBadHandle",
                    };

                    var badContainer = Service.CreateContainer(badSpec);

                    Assert.True(Service.GetContainers().Contains(badContainer));

                    try
                    {
                        Service.DestroyContainer(badContainer.Handle);
                    } catch (Exception) { }

                    Assert.True(Service.GetContainers().Contains(badContainer));
                }
        public CreateResponse Create(ContainerSpecApiModel spec)
        {
            if (spec.Env == null)
            {
                spec.Env = new List<string>();
            }

            var containerSpec = new ContainerSpec
            {
                Handle = spec.Handle,
                Properties = spec.Properties,
                Environment = ContainerService.EnvsFromList(spec.Env)
            };

            try
            {
                var container = containerService.CreateContainer(containerSpec);
                container.SetActiveProcessLimit(CONTAINER_ACTIVE_PROCESS_LIMIT);
                container.SetPriorityClass(ProcessPriorityClass.BelowNormal);
                if (spec.Limits.MemoryLimits.LimitInBytes != null)
                    container.LimitMemory(spec.Limits.MemoryLimits.LimitInBytes.Value);
                if (spec.Limits.CpuLimits.Weight != null)
                    container.LimitCpu(spec.Limits.CpuLimits.Weight.Value);
                if (spec.Limits.DiskLimits.ByteHard != null)
                    container.LimitDisk(spec.Limits.DiskLimits.ByteHard.Value);

                return new CreateResponse
                {
                    Handle = container.Handle
                };
            }
            catch (PrincipalExistsException)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Conflict,
                    string.Format("handle already exists: {0}", spec.Handle)));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
        public CreateResponse Create(ContainerSpecApiModel spec)
        {
            if (spec.Env == null)
            {
                spec.Env = new List<string>();
            }

            var containerSpec = new ContainerSpec
            {
                Handle = spec.Handle,
                Properties = spec.Properties,
                Environment = ContainerService.EnvsFromList(spec.Env),
                BindMounts = spec.BindMounts.Select(b => b.ToIronFrame()).ToArray()
            };

            try
            {
                var container = containerService.CreateContainer(containerSpec);
                container.SetActiveProcessLimit(CONTAINER_ACTIVE_PROCESS_LIMIT);
                container.SetPriorityClass(ProcessPriorityClass.BelowNormal);
                container.LimitCpu(CONTAINER_DEFAULT_CPU_WEIGHT);
                if (spec.Limits.MemoryLimits.LimitInBytes != null)
                    container.LimitMemory(spec.Limits.MemoryLimits.LimitInBytes.Value);
                if (spec.Limits.DiskLimits.ByteHard != null)
                    container.LimitDisk(spec.Limits.DiskLimits.ByteHard.Value);
                if (spec.GraceTime.HasValue)
                    container.SetProperty("GraceTime", spec.GraceTime.Value.ToString());

                return new CreateResponse
                {
                    Handle = container.Handle
                };
            }
            catch (PrincipalExistsException)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Conflict,
                    string.Format("handle already exists: {0}", spec.Handle)));
            }
        }
        public IContainer CreateContainer(ContainerSpec containerSpec)
        {
            Guard.NotNull(containerSpec, "containerSpec");

            UndoStack undoStack = new UndoStack();
            IContainer container;

            try
            {
                var handle = containerSpec.Handle;
                if (String.IsNullOrEmpty(handle))
                    handle = handleHelper.GenerateHandle();

                var id = handleHelper.GenerateId(handle);

                var user = ContainerUser.Create(userManager, id);
                undoStack.Push(() => user.Delete());

                var directory = directoryFactory.Create(fileSystem, containerBasePath, id);
                directory.CreateSubdirectories(user);
                undoStack.Push(directory.Destroy);

                directory.CreateBindMounts(containerSpec.BindMounts, user);

                var jobObject = new JobObject(id);
                undoStack.Push(() => jobObject.Dispose());

                var containerHostClient = containerHostService.StartContainerHost(id, directory, jobObject, user.GetCredential());
                undoStack.Push(() => containerHostClient.Shutdown());

                var constrainedProcessRunner = new ConstrainedProcessRunner(containerHostClient);
                undoStack.Push(() => constrainedProcessRunner.Dispose());

                var processHelper = new ProcessHelper();
                var dependencyHelper = new ContainerHostDependencyHelper();

                var diskQuotaControl = diskQuotaManager.CreateDiskQuotaControl(directory);

                container = new Container(
                    id,
                    handle,
                    user,
                    directory,
                    containerPropertiesService,
                    tcpPortManager,
                    jobObject,
                    diskQuotaControl,
                    processRunner,
                    constrainedProcessRunner,
                    processHelper,
                    containerSpec.Environment,
                    dependencyHelper);

                containerPropertiesService.SetProperties(container, containerSpec.Properties);
                lock (containers)
                {
                    containers.Add(container);
                }
            }
            catch (Exception e)
            {
                try
                {
                    undoStack.UndoAll();
                    throw;
                }
                catch (AggregateException undoException)
                {
                    throw new AggregateException(new[] { e, undoException });
                }
            }

            return container;
        }
            public WithContainer()
            {
                Handle = "KnownHandle";
                var spec = new ContainerSpec
                {
                    Handle = Handle,
                };

                Container = Service.CreateContainer(spec);
            }
            public void WhenHandleIsNotProvided_GeneratesHandle(string handle)
            {
                var expectedHandle = Guid.NewGuid().ToString("N");
                HandleHelper.GenerateHandle().Returns(expectedHandle);
                var spec = new ContainerSpec
                {
                    Handle = handle,
                };

                var container = Service.CreateContainer(spec);

                Assert.NotEqual(handle, container.Handle);
                Assert.Equal(expectedHandle, container.Handle);
            }
            public void UsesProvidedHandle()
            {
                var spec = new ContainerSpec
                {
                    Handle = "container-handle",
                };

                var container = Service.CreateContainer(spec);

                Assert.Equal("container-handle", container.Handle);
            }
            public void SetsProperties()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                    Properties = new Dictionary<string,string>
                    {
                        { "name1", "value1" },
                        { "name2", "value2" },
                    },
                };

                var container = Service.CreateContainer(spec);

                ContainerPropertiesService.Received(1).SetProperties(container, spec.Properties);
            }
            public void GeneratesIdFromHandle()
            {
                HandleHelper.GenerateId("handle").Returns("derived-id");
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                var container = Service.CreateContainer(spec);

                Assert.Equal("derived-id", container.Id);
            }
            public void GenerateDiskQuotaControlUsingTheContainerDirectory()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);
                diskQuotaManager.Received().CreateDiskQuotaControl(containerDirectory);
            }
            public void CreatesContainerSpecificUser()
            {
                UserManager.CreateUser("").ReturnsForAnyArgs(new NetworkCredential());
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                UserManager.Received(1).CreateUser("c_DEADBEEF");
            }
            public void CreatesContainerSpecificHost()
            {
                var expectedCredentials = new NetworkCredential();
                UserManager.CreateUser("").ReturnsForAnyArgs(expectedCredentials);
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                ContainerHostService.Received(1).StartContainerHost(Arg.Any<string>(), Arg.Any<IContainerDirectory>(), Arg.Any<JobObject>(), expectedCredentials);
            }
            public void CreatesContainerSpecificDirectory()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                containerDirectory.Received().CreateSubdirectories(Arg.Any<IContainerUser>());
            }
 public void CreatesBindMounts()
 {
     var bindMounts = new[]
     {
         new BindMount()
     };
     var spec = new ContainerSpec
     {
         BindMounts = bindMounts
     };
     Service.CreateContainer(spec);
     containerDirectory.Received().CreateBindMounts(bindMounts, Arg.Any<IContainerUser>());
 }
            public void CleansUpWhenItFails()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                ContainerHostService.StartContainerHost(null, null, null, null)
                    .ThrowsForAnyArgs(new Exception());

                try
                {
                    Service.CreateContainer(spec);
                }
                catch (Exception)
                {
                    // Expect this exception.
                }

                // Created and deleted the user
                UserManager.Received(1).CreateUser(Arg.Any<string>());
                UserManager.Received(1).DeleteUser(Arg.Any<string>());

                containerDirectory.Received().Destroy();
            }
Example #29
0
        public IContainer CreateContainer(ContainerSpec containerSpec)
        {
            Guard.NotNull(containerSpec, "containerSpec");

            UndoStack  undoStack = new UndoStack();
            IContainer container;

            try
            {
                var handle = containerSpec.Handle;
                if (String.IsNullOrEmpty(handle))
                {
                    handle = handleHelper.GenerateHandle();
                }

                var id = handleHelper.GenerateId(handle);

                var user = ContainerUser.Create(userManager, id);
                undoStack.Push(() => user.Delete());

                var directory = directoryFactory.Create(fileSystem, containerBasePath, id);
                directory.CreateSubdirectories(user);
                undoStack.Push(directory.Destroy);

                directory.CreateBindMounts(containerSpec.BindMounts, user);

                var jobObject = new JobObject(id);
                undoStack.Push(() => jobObject.Dispose());

                var containerHostClient = containerHostService.StartContainerHost(id, directory, jobObject, user.GetCredential());
                undoStack.Push(() => containerHostClient.Shutdown());

                var constrainedProcessRunner = new ConstrainedProcessRunner(containerHostClient);
                undoStack.Push(() => constrainedProcessRunner.Dispose());

                var processHelper    = new ProcessHelper();
                var dependencyHelper = new ContainerHostDependencyHelper();

                var diskQuotaControl = diskQuotaManager.CreateDiskQuotaControl(directory);

                container = new Container(
                    id,
                    handle,
                    user,
                    directory,
                    containerPropertiesService,
                    tcpPortManager,
                    jobObject,
                    diskQuotaControl,
                    processRunner,
                    constrainedProcessRunner,
                    processHelper,
                    containerSpec.Environment,
                    dependencyHelper);

                containerPropertiesService.SetProperties(container, containerSpec.Properties);
                lock (containers)
                {
                    containers.Add(container);
                }
            }
            catch (Exception e)
            {
                try
                {
                    undoStack.UndoAll();
                    throw;
                }
                catch (AggregateException undoException)
                {
                    throw new AggregateException(new[] { e, undoException });
                }
            }

            return(container);
        }