Esempio n. 1
0
        public IRunspaceInfo[] List()
        {
            List <IRunspaceInfo> result = new List <IRunspaceInfo>();

            try {
                var runningRunspaceContainerList = _containerApi.ContainerList();
                foreach (var runspaceContainer in runningRunspaceContainerList)
                {
                    if (runspaceContainer != null &&
                        runspaceContainer.Image == _runspaceContainerCreateSpec.ImageName &&
                        runspaceContainer.State.ToLower() == "running")
                    {
                        var containerInspectResponse = _containerApi.ContainerInspect(runspaceContainer.Id);

                        result.Add(DockerRunspaceInfo.FromContainerInspectResponse(
                                       containerInspectResponse,
                                       _runspaceContainerCreateSpec.NetworkName));
                    }
                }
            } catch (ApiException dockerApiException) {
                throw new RunspaceProviderException(
                          Resources.Resources.DockerRunspaceProvider_Get_ContainerDockerAPIFail,
                          dockerApiException);
            }

            // Populate Result from ContainerInspectResponse
            return(result.ToArray());
        }
Esempio n. 2
0
        /// <summary>
        /// Instantiates, Starts, and Gets Container details through Docker API
        /// </summary>
        /// <returns>Instance of <see cref="IRunspaceInfo"/> for the started Runspace Container.</returns>
        public IRunspaceInfo StartCreate()
        {
            IRunspaceInfo result          = null;
            const long    CPUsCount       = 1;
            const long    BytesInMegaByte = 1048576;
            var           containerConfig = new ContainerConfig(
                image: _runspaceContainerCreateSpec.ImageName,
                hostConfig: new HostConfig(
                    networkMode: _runspaceContainerCreateSpec.NetworkName,
                    cpuCount: CPUsCount,
                    restartPolicy: new RestartPolicy(RestartPolicy.NameEnum.Empty),
                    memory: BytesInMegaByte * _runspaceContainerCreateSpec.RunspaceContainerMemoryLimitMB));

            ContainerCreateResponse runspaceContainerInstance;

            try {
                // Create Runspace Container Instance
                runspaceContainerInstance = _containerApi.ContainerCreate(containerConfig);

                // Start Runspace Container
                _containerApi.ContainerStart(runspaceContainerInstance.Id);

                result = Get(runspaceContainerInstance.Id);
            } catch (ApiException dockerApiException) {
                result = DockerRunspaceInfo.FromRunspaceProviderError(
                    new RunspaceProviderException(
                        Resources.Resources.DockerRunspaceProvider_Create_StartContainerDockerAPIFail,
                        dockerApiException));
            }

            try {
                if (_testConnectionToContainerOnCreate)
                {
                    // Ensure Container is accessible over the network after creation
                    EnsureRunspaceEndpointIsAccessible(result);
                }
            } catch (RunspaceProviderException exception) {
                // Kill the container that is not accessible, otherwise it will leak
                try {
                    if (result.Id != null)
                    {
                        Kill(result.Id);
                    }
                } catch (RunspaceProviderException) {}

                result = DockerRunspaceInfo.FromRunspaceProviderError(exception);
            }

            return(result);
        }
Esempio n. 3
0
        public IRunspaceInfo Get(string id)
        {
            ContainerInspectResponse containerInspectResponse;

            try {
                // Get Container Details
                containerInspectResponse = _containerApi.ContainerInspect(id);
            } catch (ApiException dockerApiException) {
                throw new RunspaceProviderException(
                          Resources.Resources.DockerRunspaceProvider_Get_ContainerDockerAPIFail,
                          dockerApiException);
            }

            // Populate Result from ContainerInspectResponse
            return(containerInspectResponse.State.Status == ContainerInspectResponseState.StatusEnum.Running ?
                   DockerRunspaceInfo.FromContainerInspectResponse(
                       containerInspectResponse,
                       _runspaceContainerCreateSpec.NetworkName) :
                   null);
        }