Exemple #1
0
        /// <inheritdoc/>
        public override DockerShimInfo Shim(DockerShim shim)
        {
            Program.LogPath = null;

            var commandLine = shim.CommandLine;

            if (commandLine.Arguments.Length > 2)
            {
                switch (commandLine[1])
                {
                case "write":

                    // We need handle any [key=@file] arguments specially by adding
                    // them to the shim.

                    foreach (var arg in commandLine.Arguments.Skip(2))
                    {
                        var pos = arg.IndexOf("=@");

                        if (pos != -1)
                        {
                            var shimFile = shim.AddFile(arg.Substring(pos + 2), dontReplace: true);

                            shim.ReplaceItem(arg, arg.Substring(0, pos + 2) + shimFile);
                        }
                    }
                    break;

                case "policy-write":

                    // The last command line item is either:
                    //
                    //      * A "-", indicating that the content should come from standard input.
                    //      * A file name prefixed by "@"
                    //      * A string holding JSON or HCL

                    if (commandLine.Arguments.LastOrDefault() == "-")
                    {
                        shim.AddStdin();
                    }
                    else
                    {
                        var lastArg = commandLine.Arguments.LastOrDefault();

                        if (lastArg.StartsWith("@"))
                        {
                            var shimFile = shim.AddFile(lastArg.Substring(1), dontReplace: true);

                            shim.ReplaceItem(lastArg, "@" + shimFile);
                        }
                    }
                    break;
                }
            }

            return(new DockerShimInfo(shimability: DockerShimability.Optional, ensureConnection: true));
        }
Exemple #2
0
        /// <inheritdoc/>
        public override DockerShimInfo Shim(DockerShim shim)
        {
            Program.LogPath = null;

            var commandLine = shim.CommandLine;

            // Handle the case where we need to pipe the standard input to
            // to the container.

            if (commandLine.Items.LastOrDefault() == "-")
            {
                shim.AddStdin();
            }
            else if (commandLine.StartsWithArgs("consul", "kv", "put") && commandLine.Arguments.Length == 5 && commandLine.Arguments[4].StartsWith("@"))
            {
                // We're going to special case PUT when saving a file
                // whose name is prefixed with "@".

                var fileArg  = commandLine.Arguments[4];
                var filePath = fileArg.Substring(1);
                var shimFile = shim.AddFile(filePath, dontReplace: true);

                shim.ReplaceItem(fileArg, "@" + shimFile);
            }

            return(new DockerShimInfo(shimability: DockerShimability.Optional, ensureConnection: true));
        }
Exemple #3
0
        /// <inheritdoc/>
        public override DockerShimInfo Shim(DockerShim shim)
        {
            var commandLine = shim.CommandLine;

            if (commandLine.Arguments.Length >= 3)
            {
                // We need to have the container command write the target file
                // to the [/shim] folder and then add a post action that copies
                // the target file to the specified location on the operator's
                // workstation.

                var externalTarget = commandLine.Arguments[2];
                var internalTarget = "__target";

                shim.ReplaceItem(externalTarget, internalTarget);

                shim.SetPostAction(
                    exitCode =>
                {
                    if (exitCode == 0)
                    {
                        File.Copy(Path.Combine(shim.ShimExternalFolder, internalTarget), externalTarget);
                    }
                });
            }

            return(new DockerShimInfo(shimability: DockerShimability.Optional, ensureConnection: true));
        }
Exemple #4
0
        /// <summary>
        /// Handles the shim for the <b>docker deploy</b> and <b>docker stack deploy</b> commands.
        /// </summary>
        /// <param name="shim">The shim.</param>
        /// <param name="rightCommandLine">The right split of the command line.</param>
        private void ShimDeploy(DockerShim shim, CommandLine rightCommandLine)
        {
            string path;

            // We're going to shim the file specified by the first
            // [--bundle-file], [--compose-file], or [-c] option.

            for (int i = 0; i < rightCommandLine.Items.Length; i++)
            {
                switch (rightCommandLine.Items[i])
                {
                case "--bundle-file":
                case "--compose-file":
                case "-c":

                    path = rightCommandLine.Items.Skip(i + 1).FirstOrDefault();

                    if (path != null)
                    {
                        shim.AddFile(path);
                        return;
                    }
                    break;
                }
            }

            // If that didn't work, try looking for arguments like:
            //
            //      --bundle-file=PATH

            var patterns =
                new string[]
            {
                "--bundle-file=",
                "--compose-file=",
                "-c="
            };

            foreach (var item in rightCommandLine.Items)
            {
                foreach (var pattern in patterns)
                {
                    if (item.StartsWith(pattern))
                    {
                        path = item.Substring(pattern.Length);

                        var shimFile = shim.AddFile(path, dontReplace: true);

                        shim.ReplaceItem(pattern + path, pattern + shimFile);
                        return;
                    }
                }
            }
        }
Exemple #5
0
        /// <inheritdoc/>
        public override DockerShimInfo Shim(DockerShim shim)
        {
            var commandLine = shim.CommandLine;

            // Shim command: neon vpn ca HIVE-DEF CA-FOLDER
            //
            // We need to copy the [HIVE-DEF] and the contents of the
            // [CA-FOLDER] into a shim subfolder, update the command line
            // and execute it, and then copy the file contents back out
            // to the original folder when the command completes.

            if (commandLine.Arguments.Length == 4 &&
                commandLine.Arguments[0] == "vpn" &&
                commandLine.Arguments[1] == "ca")
            {
                var hiveDefPath     = commandLine.Arguments[2];
                var caFolderPath    = commandLine.Arguments[3];
                var shimmedCaFolder = Path.Combine(shim.ShimExternalFolder, "ca");

                shim.AddFile(hiveDefPath);

                foreach (var file in Directory.GetFiles(caFolderPath, "*.*", SearchOption.TopDirectoryOnly))
                {
                    File.Copy(file, Path.Combine(shimmedCaFolder, Path.GetFileName(file)));
                }

                shim.ReplaceItem(caFolderPath, $"{DockerShim.ShimInternalFolder}/ca");

                shim.SetPostAction(
                    exitCode =>
                {
                    if (exitCode == 0)
                    {
                        foreach (var file in Directory.GetFiles(shimmedCaFolder, "*.*", SearchOption.TopDirectoryOnly))
                        {
                            File.Copy(file, Path.Combine(caFolderPath, Path.GetFileName(file)));
                        }
                    }
                });

                return(new DockerShimInfo(shimability: DockerShimability.Required));
            }

            // Shim command: neon vpn cert user create USER
            //
            // We need to copy the new hive login file to the current directory
            // after the command runs in Docker.  Note that the shimmed command
            // writes the file name for the new login to [new-login.txt].

            if (commandLine.Arguments.Length == 4 &&
                commandLine.Arguments[0] == "vpn" &&
                commandLine.Arguments[1] == "user" &&
                commandLine.Arguments[2] == "create")
            {
                var username = commandLine.Arguments[3];

                shim.SetPostAction(
                    exitCode =>
                {
                    if (exitCode == 0)
                    {
                        var loginName          = File.ReadAllText(Path.Combine(shim.ShimExternalFolder, "new-login.txt"));
                        var generatedLoginPath = Path.Combine(shim.ShimExternalFolder, loginName);
                        var outputLoginPath    = Path.GetFullPath(loginName);
                        var generatedLoginText = File.ReadAllText(generatedLoginPath);

                        File.WriteAllText(outputLoginPath, generatedLoginText);
                        Console.WriteLine($"*** Created login: {outputLoginPath}");
                    }
                });

                return(new DockerShimInfo(shimability: DockerShimability.Required));
            }

            // Shim command: neon vpn user revoke [--restart-vpn] THUMBPRINT
            //
            // No special actions required.

            return(new DockerShimInfo(shimability: DockerShimability.Required));
        }