Example #1
0
        public static ConsoleStream <string> Logs(this DockerUri host, string id,
                                                  CancellationToken cancellationToken = default(CancellationToken),
                                                  bool follow = false, bool showTimeStamps = false, DateTime?since = null, int?numLines = null,
                                                  ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            var options = string.Empty;

            if (follow)
            {
                options += " -f";
            }

            if (null != since)
            {
                options += $" --since {since}";
            }

            options += numLines.HasValue ? $" --tail={numLines}" : " --tail=all";

            if (showTimeStamps)
            {
                options += " -t";
            }

            return
                (new StreamProcessExecutor <StringMapper, string>(
                     "docker".ResolveBinary(),
                     $"{args} logs {options} {id}").Execute(cancellationToken));
        }
Example #2
0
        public static CommandResponse <IList <Diff> > Diff(this DockerUri host, string id, ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)}";

            return(new ProcessExecutor <ClientDiffResponseParser, IList <Diff> >("docker".ResolveBinary(),
                                                                                 $"{arg} diff {id}").Execute());
        }
Example #3
0
        public static CommandResponse <string> RemoveContainer(this DockerUri host, string id, bool force = false,
                                                               bool removeVolumes = false,
                                                               string removeLink  = null, ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)} rm";

            if (force)
            {
                arg += " --force";
            }

            if (removeVolumes)
            {
                arg += " --volumes";
            }

            if (!string.IsNullOrEmpty(removeLink))
            {
                arg += $" --link {removeLink}";
            }

            arg += $" {id}";

            return(new ProcessExecutor <SingleStringResponseParser, string>(
                       "docker".ResolveBinary(),
                       arg).Execute());
        }
Example #4
0
        public static CommandResponse <Processes> Top(this DockerUri host, string id, ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)} top {id}";

            return(new ProcessExecutor <ClientTopResponseParser, Processes>("docker".ResolveBinary(),
                                                                            arg).Execute());
        }
        public static void Initialize(TestContext ctx)
        {
            if (CommandExtensions.IsNative() || CommandExtensions.IsEmulatedNative())
            {
                return;
            }

            var machineName = "test-machine";

            var machines = Machine.Ls();

            if (machines.Success && machines.Data.Any(x => x.Name == "default"))
            {
                machineName = "default";
            }
            else
            {
                machineName.Create(1024, 20000, 1);
                _createdTestMachine = true;
            }

            machineName.Start();
            var inspect = machineName.Inspect().Data;

            _docker       = machineName.Uri();
            _certificates = new CertificatePaths
            {
                CaCertificate     = inspect.AuthConfig.CaCertPath,
                ClientCertificate = inspect.AuthConfig.ClientCertPath,
                ClientKey         = inspect.AuthConfig.ClientKeyPath
            };
        }
Example #6
0
        public static CommandResponse <string> Create(this DockerUri host, string image, string command = null,
                                                      string[] args = null, ContainerCreateParams prms = null, ICertificatePaths certificates = null)
        {
            var certArgs = host.RenderBaseArgs(certificates);

            var arg = $"{certArgs} create";

            if (null != prms)
            {
                arg += " " + prms;
            }

            arg += " " + image;

            if (!string.IsNullOrEmpty(command))
            {
                arg += $" {command}";
            }

            if (null != args && 0 != args.Length)
            {
                arg += " " + string.Join(" ", args);
            }

            return
                (new ProcessExecutor <SingleStringResponseParser, string>(
                     "docker".ResolveBinary(),
                     arg).Execute());
        }
Example #7
0
        private void MachineSetup(string name, bool throwOnNotRunning = false)
        {
            var info = name.Inspect().Data;

            RequireTls = info.RequireTls;
            if (info.DriverName == "hyperv")
            {
                // Workaround for Status and Url requires elevated process.
                Host  = new DockerUri($"tcp://{info.IpAddress}:2376");
                State = ServiceRunningState.Running;
            }
            else
            {
                State = name.Status();
                if (State != ServiceRunningState.Running)
                {
                    if (!throwOnNotRunning)
                    {
                        return;
                    }

                    throw new FluentDockerException(
                              $"Could not get status from machine '{name}' thus not possible to resolve host");
                }

                Host = name.Uri();
            }

            ResolveCertificatePaths(info);
        }
Example #8
0
        public DockerHostService(string name, bool isNative, bool stopWhenDisposed = false, string dockerUri = null,
                                 string certificatePath = null, bool isWindowsHost = false)
            : base(name)
        {
            _isWindowsHost    = isWindowsHost;
            _stopWhenDisposed = stopWhenDisposed;

            IsNative = isNative;
            if (IsNative)
            {
                var uri      = dockerUri ?? DockerUri.GetDockerHostEnvironmentPathOrDefault();
                var certPath = certificatePath ?? Environment.GetEnvironmentVariable(DockerCertPath);

                if (!string.IsNullOrEmpty(certPath))
                {
                    Certificates = new CertificatePaths
                    {
                        CaCertificate     = Path.Combine(certPath, DefaultCaCertName),
                        ClientCertificate = Path.Combine(certPath, DefaultClientCertName),
                        ClientKey         = Path.Combine(certPath, DefaultClientKeyName)
                    }
                }
                ;

                Host       = string.IsNullOrEmpty(uri) ? null : new DockerUri(uri);
                RequireTls = Environment.GetEnvironmentVariable(DockerTlsVerify) == "1";
                State      = ServiceRunningState.Running;
                return;
            }

            // Machine - do inspect & get url
            MachineSetup(name);
        }
Example #9
0
        public static CommandResponse <IList <string> > NetworkLs(this DockerUri host, bool dontTruncate = true,
                                                                  bool quiet = false,
                                                                  ICertificatePaths certificates = null, params string[] filters)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            var options = string.Empty;

            if (dontTruncate)
            {
                options += " --no-trunc";
            }

            if (quiet)
            {
                options += " -q";
            }

            if (null != filters && 0 != filters.Length)
            {
                foreach (var filter in filters)
                {
                    options += $"--filter={filter}";
                }
            }

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{args} network ls {options}").Execute());
        }
Example #10
0
        public static ConsoleStream <string> ComposeEvents(this DockerUri host, string altProjectName = null,
                                                           string composeFile = null, string[] services = null /*all*/,
                                                           CancellationToken cancellationToken = default(CancellationToken),
                                                           bool json = false,
                                                           ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            if (!string.IsNullOrEmpty(composeFile))
            {
                args += $" -f {composeFile}";
            }

            if (!string.IsNullOrEmpty(altProjectName))
            {
                args += $" -p {altProjectName}";
            }

            var options = string.Empty;

            if (json)
            {
                options += " --json";
            }

            if (null != services && 0 != services.Length)
            {
                options += " " + string.Join(" ", services);
            }

            return
                (new StreamProcessExecutor <StringMapper, string>(
                     "docker-compose".ResolveBinary(),
                     $"{args} events {options}").Execute(cancellationToken));
        }
Example #11
0
        public static ConsoleStream <string> Events(this DockerUri host,
                                                    CancellationToken cancellationToken = default(CancellationToken),
                                                    string [] filters = null, DateTime?since = null, DateTime?until = null,
                                                    ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            var options = string.Empty;

            if (null != since)
            {
                options += $" --since {since}";
            }

            if (null != until)
            {
                options += $" --since {until}";
            }

            if (null != filters && 0 != filters.Length)
            {
                foreach (var filter in filters)
                {
                    options += $" --filter={filter}";
                }
            }

            return
                (new StreamProcessExecutor <StringMapper, string>(
                     "docker".ResolveBinary(),
                     $"{args} events {options}").Execute(cancellationToken));
        }
Example #12
0
        public static CommandResponse <IList <string> > ComposeCreate(this DockerUri host,
                                                                      string altProjectName            = null,
                                                                      bool forceRecreate               = false, bool noRecreate = false, bool dontBuild = false,
                                                                      bool buildBeforeCreate           = false,
                                                                      string[] services                = null /*all*/,
                                                                      IDictionary <string, string> env = null,
                                                                      ICertificatePaths certificates   = null, params string[] composeFile)
        {
            var cwd  = WorkingDirectory(composeFile);
            var args = $"{host.RenderBaseArgs(certificates)}";

            if (null != composeFile && 0 != composeFile.Length)
            {
                foreach (var cf in composeFile)
                {
                    if (!string.IsNullOrEmpty(cf))
                    {
                        args += $" -f \"{cf}\"";
                    }
                }
            }

            if (!string.IsNullOrEmpty(altProjectName))
            {
                args += $" -p {altProjectName}";
            }

            var options = string.Empty;

            if (forceRecreate)
            {
                options += " --force-recreate";
            }

            if (noRecreate)
            {
                options += " --no-recreate";
            }

            if (dontBuild)
            {
                options += " --no-build";
            }

            if (buildBeforeCreate)
            {
                options += " --build";
            }

            if (null != services && 0 != services.Length)
            {
                options += " " + string.Join(" ", services);
            }

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker-compose".ResolveBinary(),
                     $"{args} create {options}", cwd.NeedCwd ? cwd.Cwd : null).ExecutionEnvironment(env).Execute());
        }
Example #13
0
        public static CommandResponse <string> Export(this DockerUri host, string id, string fqFilePath,
                                                      ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)} export";

            return(new ProcessExecutor <NoLineResponseParser, string>("docker".ResolveBinary(),
                                                                      $"{arg} -o {fqFilePath} {id}").Execute());
        }
Example #14
0
        public static CommandResponse <string> CopyFromContainer(this DockerUri host, string id, string containerPath,
                                                                 string hostPath, ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)}";

            return(new ProcessExecutor <IgnoreErrorResponseParser, string>("docker".ResolveBinary(),
                                                                           $"{arg} cp {id}:{containerPath} \"{hostPath}\"").Execute());
        }
Example #15
0
        public static CommandResponse <string> ExecuteCommandInContainer(this DockerUri host, string id, string options, string args, string command,
                                                                         ICertificatePaths certificates = null)
        {
            var arg = $"{host.RenderBaseArgs(certificates)}";

            return(new ProcessExecutor <GeneralResponseParser, string>("docker".ResolveBinary(),
                                                                       $"{arg} exec {options} {id} {args} \"{command}\"").Execute());
        }
Example #16
0
 public DockerNetworkService(string name, string id, DockerUri dockerHost, ICertificatePaths certificate,
                             bool removeOnDispose = false) :
     base(name)
 {
     _removeOnDispose = removeOnDispose;
     Id           = id;
     DockerHost   = dockerHost;
     Certificates = certificate;
 }
 public DockerContainerService(string name, string id, DockerUri docker, ServiceRunningState state,
                               ICertificatePaths certificates,
                               bool stopOnDispose             = true, bool removeOnDispose = true, bool removeMountOnDispose = false,
                               bool removeNamedMountOnDispose = false, bool isWindowsContainer = false, string instanceId    = null,
                               string project = null) :
     this(name, id, docker, state, certificates, null, stopOnDispose, removeOnDispose, removeMountOnDispose,
          removeNamedMountOnDispose, isWindowsContainer, instanceId, project)
 {
 }
Example #18
0
        public static CommandResponse <IList <string> > UnPause(this DockerUri host, ICertificatePaths certificates = null, params string[] containerIds)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{args} unpause {string.Join(" ", containerIds)}").Execute());
        }
Example #19
0
        public static CommandResponse <IList <string> > Start(this DockerUri host, string id, ICertificatePaths certificates = null)
        {
            var certArgs = host.RenderBaseArgs(certificates);

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{certArgs} start {id}").Execute());
        }
Example #20
0
        public static CommandResponse <IList <string> > Pull(this DockerUri host, string image, ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{args} pull {image}").Execute());
        }
 public DockerImageService(string name, string id, string tag, DockerUri dockerHost, ICertificatePaths certificate)
     : base(name)
 {
     Id           = id;
     Tag          = tag;
     Certificates = certificate;
     DockerHost   = dockerHost;
     State        = ServiceRunningState.Running;
 }
Example #22
0
        public static CommandResponse <IList <string> > NetworkRm(this DockerUri host,
                                                                  ICertificatePaths certificates = null, params string[] network)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{args} network rm {string.Join(" ", network)}").Execute());
        }
Example #23
0
        public static CommandResponse <IList <string> > NetworkCreate(this DockerUri host, string network,
                                                                      NetworkCreateParams prms = null, ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker".ResolveBinary(),
                     $"{args} network create {prms} {network}").Execute());
        }
Example #24
0
        /// <summary>
        /// Get the docker version both client and server version. It includes the server operating system (linux, windows).
        /// </summary>
        /// <param name="host">The docker daemon to contact.</param>
        /// <param name="certificates">Path to certificates if any.</param>
        /// <returns>A response with the versions if successful.</returns>
        public static CommandResponse <DockerInfoBase> Version(this DockerUri host, ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";
            var fmt  = "{{.Server.Version}};{{.Server.APIVersion}};{{.Client.Version}};{{.Client.APIVersion}};{{.Server.Os}}";

            return
                (new ProcessExecutor <BaseInfoResponseParser, DockerInfoBase>(
                     "docker".ResolveBinary(),
                     $"{args} version -f \"{fmt}\"").Execute());
        }
Example #25
0
 /// <summary>
 /// Creates a `IHostService` based on a _URI_.
 /// </summary>
 /// <param name="uri">The _URI_ to the docker daemon.</param>
 /// <param name="name">An optional name. If none is specified the _URI_ is the name.</param>
 /// <param name="isNative">If the docker daemon is native or not. Default to true.</param>
 /// <param name="stopWhenDisposed">If it should be stopped when disposed, default to false.</param>
 /// <param name="isWindowsHost">If it is a docker daemon that controls windows containers or not. Default false.</param>
 /// <param name="certificatePath">
 /// Optional path to where certificates are located in order to do TLS communication with docker daemon. If not provided,
 /// it will try to get it from the environment _DOCKER_CERT_PATH_.
 /// </param>
 /// <returns>Itself for fluent access.</returns>
 public HostBuilder FromUri(
     DockerUri uri,
     string name            = null,
     bool isNative          = true,
     bool stopWhenDisposed  = false,
     bool isWindowsHost     = false,
     string certificatePath = null)
 {
     this.customHostService = new Hosts().FromUri(uri, name, isNative, stopWhenDisposed, isWindowsHost, certificatePath);
     return(this);
 }
Example #26
0
        public static CommandResponse <IList <string> > ComposeBuild(this DockerUri host,
                                                                     string altProjectName = null,
                                                                     bool forceRm          = false, bool dontUseCache = false,
                                                                     bool alwaysPull       = false, string[] services = null /*all*/, ICertificatePaths certificates = null,
                                                                     params string[] composeFile)
        {
            var cwd = WorkingDirectory(composeFile);

            var args = $"{host.RenderBaseArgs(certificates)}";

            if (null != composeFile && 0 != composeFile.Length)
            {
                foreach (var cf in composeFile)
                {
                    if (!string.IsNullOrEmpty(cf))
                    {
                        args += $" -f \"{cf}\"";
                    }
                }
            }

            if (!string.IsNullOrEmpty(altProjectName))
            {
                args += $" -p {altProjectName}";
            }

            var options = string.Empty;

            if (forceRm)
            {
                options += " --force-rm";
            }

            if (alwaysPull)
            {
                options += " --pull";
            }

            if (forceRm)
            {
                options += " --force-rm";
            }

            if (null != services && 0 != services.Length)
            {
                options += " " + string.Join(" ", services);
            }

            return
                (new ProcessExecutor <StringListResponseParser, IList <string> >(
                     "docker-compose".ResolveBinary(),
                     $"{args} build {options}", cwd.NeedCwd ? cwd.Cwd : null).Execute());
        }
Example #27
0
        public static CommandResponse <IList <Volume> > VolumeInspect(this DockerUri host,
                                                                      ICertificatePaths certificates = null, params string[] volume)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            var volumes = string.Join(" ", volume);

            return
                (new ProcessExecutor <VolumeInspectResponseParser, IList <Volume> >(
                     "docker".ResolveBinary(),
                     $"{args} volume inspect {volumes}").Execute());
        }
Example #28
0
        public static ConsoleStream <string> ComposeLogs(this DockerUri host, string altProjectName = null,
                                                         string composeFile = null, string[] services = null /*all*/,
                                                         CancellationToken cancellationToken = default(CancellationToken),
                                                         bool follow = false, bool showTimeStamps = false, DateTime?since = null, int?numLines = null, bool noColor = false,
                                                         ICertificatePaths certificates = null)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";

            if (!string.IsNullOrEmpty(composeFile))
            {
                args += $" -f {composeFile}";
            }

            if (!string.IsNullOrEmpty(altProjectName))
            {
                args += $" -p {altProjectName}";
            }

            var options = string.Empty;

            if (follow)
            {
                options += " -f";
            }

            if (null != since)
            {
                options += $" --since {since}";
            }

            options += numLines.HasValue ? $" --tail={numLines}" : " --tail=all";

            if (showTimeStamps)
            {
                options += " -t";
            }

            if (noColor)
            {
                options += " --no-color";
            }

            if (null != services && 0 != services.Length)
            {
                options += " " + string.Join(" ", services);
            }

            return
                (new StreamProcessExecutor <StringMapper, string>(
                     "docker-compose".ResolveBinary(),
                     $"{args} logs {options}").Execute(cancellationToken));
        }
Example #29
0
        public DockerContainerService(string name, string id, DockerUri docker, ServiceRunningState state,
                                      ICertificatePaths certificates,
                                      bool stopOnDispose = true, bool removeOnDispose = true)
        {
            _certificates    = certificates;
            _stopOnDispose   = stopOnDispose;
            _removeOnDispose = removeOnDispose;

            Name       = name;
            Id         = id;
            DockerHost = docker;
            State      = state;
        }
Example #30
0
        // TODO: Implement me!
        public static CommandResponse <IList <string> > ServiceCreate(this DockerUri host,
                                                                      Orchestrator orchestrator      = Orchestrator.All,
                                                                      string kubeConfigFile          = null,
                                                                      ICertificatePaths certificates = null, params string [] stacks)
        {
            var args = $"{host.RenderBaseArgs(certificates)}";
            var opts = $"--orchestrator={orchestrator}"; // TODO:

            return                                       // TODO:
                   (new ProcessExecutor <StringListResponseParser, IList <string> >(
                        "docker".ResolveBinary(),
                        $"{args} stack rm {opts} {string.Join(" ", stacks)}").Execute());
        }