Exemplo n.º 1
0
        private void extractTar(InterProcessMessage msg)
        {
            var args = msg.Args;

            if (!args.TryGetJsonString(PathKey, out var path) || !args.TryGetJsonString(TargetKey, out var target))
            {
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(SucceededKey, false)
                }
                                                                     )));
            }
            else
            {
                Process.Start("mkdir", $"-p {target.Value}").WaitForExit();

                var startInfo = new ProcessStartInfo("tar", $"-x -f {path.Value} -C {target.Value}")
                {
                    RedirectStandardOutput = true, RedirectStandardError = true
                };
                var process = Process.Start(startInfo);
                process.WaitForExit();

                new StreamWriter(Console.OpenStandardError()).Write(process.StandardError.ReadToEnd());

                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(SucceededKey, process.ExitCode == 0)
                }
                                                                     )));
            }
        }
Exemplo n.º 2
0
        private void deleteDir(InterProcessMessage msg)
        {
            var args = msg.Args;

            string error = null;

            if (!args.TryGetJsonString(PathKey, out var path))
            {
                error = "invalid args";
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(PathKey, new JsonNull()),
                    new JsonObjectKeyValuePair(ErrorKey, error)
                }
                                                                     )));
            }
            else
            {
                try {
                    Directory.Delete(path, true);
                } catch (Exception) {
                    ; // if any file don't want to leave, let it go
                }
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(PathKey, path)
                }
                                                                     )));
            }
        }
Exemplo n.º 3
0
        private void moveDir(InterProcessMessage msg)
        {
            var args = msg.Args;

            string error = null;

            if (!args.TryGetJsonString(PathKey, out var path) || !args.TryGetJsonString(TargetKey, out var target))
            {
                error = "invalid args";
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(PathKey, new JsonNull()),
                    new JsonObjectKeyValuePair(TargetKey, new JsonNull()),
                    new JsonObjectKeyValuePair(ErrorKey, error)
                }
                                                                     )));
            }
            else
            {
                new DirectoryInfo(path).CopyTo(new DirectoryInfo(target));
                try {
                    Directory.Delete(path, true);
                } catch (Exception) {
                    ;
                }
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(PathKey, path),
                    new JsonObjectKeyValuePair(TargetKey, target)
                }
                                                                     )));
            }
        }
Exemplo n.º 4
0
        private void ensuredir(InterProcessMessage msg)
        {
            var args = msg.Args;

            string error = null;

            if (!args.TryGetJsonObject(RootKey, out var root))
            {
                error = "invalid args";
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                     new[] {
                    new JsonObjectKeyValuePair(PathKey, new JsonNull()),
                    new JsonObjectKeyValuePair(ErrorKey, error)
                }
                                                                     )));

                return;
            }

            string rv = ensuredirNode(".", root);

            SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                 new[] {
                new JsonObjectKeyValuePair(PathKey, rv)
            }
                                                                 )));
        }
Exemplo n.º 5
0
        private void dockerPsall(InterProcessMessage msg)
        {
            var p = new Process {
                StartInfo = getStartInfo("ps -a --no-trunc")
            };

            p.Start();
            p.WaitForExit();

            if (p.ExitCode == 0)
            {
                var cids = p.StandardOutput
                           .ReadToEnd()
                           .Split('\n')
                           .Skip(1)
                           .Select(line => line.Trim())
                           .Where(line => line.Length > 0)
                           .Select(line => line.Split(' ')[0])
                           .Where(line => line.Length == 64)
                           .Select(cid => new JsonString(cid));

                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair("cid", new JsonArray(cids)) })));
            }
            else
            {
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair("cid", new JsonNull()), new JsonObjectKeyValuePair("error", p.StandardError.ReadToEnd()) })));
            }
        }
Exemplo n.º 6
0
 private void dockerContainerOpWrapper(string command, string args, int token)
 {
     SendMessage(InterProcessMessage.GetResultMessage(ModuleName, token, new JsonObject(
                                                          dockerWrapper(command, args, out var rv)
             ? new[] { new JsonObjectKeyValuePair(CidKey, rv) }
             : new[] { new JsonObjectKeyValuePair(CidKey, new JsonNull()), new JsonObjectKeyValuePair(ErrorKey, rv) }
                                                          )));
 }
Exemplo n.º 7
0
        private void dockerKillMany(InterProcessMessage msg)
        {
            var args = msg.Args;

            var list = new List <string>();

            if (!args.TryGetJsonArray(CidsKey, out var cids))
            {
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair(ErrorKey, "invalid args") })));
                return;
            }

            foreach (var i in cids)
            {
                if (i is JsonString str)
                {
                    list.Add(str);
                }
            }

            if (list.Count == 0)
            {
                SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair(ErrorKey, new JsonArray()) })));
            }
            else
            {
                var p = new Process {
                    StartInfo = getStartInfo($"kill {list.JoinBy(" ")}")
                };

                p.Start();
                p.WaitForExit();

                var rcids = p.StandardOutput
                            .ReadToEnd()
                            .Split('\n')
                            .Select(line => line.Trim())
                            .Where(line => line.Length == 64)
                            .Select(cid => new JsonString(cid));

                if (p.ExitCode == 0)
                {
                    SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair(CidsKey, new JsonArray(rcids)) })));
                }
                else
                {
                    SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(new[] { new JsonObjectKeyValuePair(CidsKey, new JsonArray(rcids)), new JsonObjectKeyValuePair(ErrorKey, p.StandardError.ReadToEnd()) })));
                }
            }
        }
Exemplo n.º 8
0
        // all path here should be absolute!!!
        private void instantiate(InterProcessMessage msg)
        {
            var       args   = msg.Args;
            JsonValue dirmap = new JsonNull();
            JsonValue error  = new JsonNull();

            if (!args.TryGetJsonObject(RootKey, out var root) || !args.TryGetJsonString(TargetKey, out var target))
            {
                error = "invalid params";
            }
            else
            {
                dirmap = new JsonArray(instantiateNode(root, new DirectoryInfo(target), ".").Select(m => new JsonObject(new[] { new JsonObjectKeyValuePair(DockerKey, m.Docker), new JsonObjectKeyValuePair(HostKey, m.Host), new JsonObjectKeyValuePair(ReadonlyKey, m.Readonly) })));
            }

            SendMessage(InterProcessMessage.GetResultMessage(ModuleName, msg.Token, new JsonObject(
                                                                 dirmap is JsonNull
                ? new[] { new JsonObjectKeyValuePair(DirMapKey, dirmap), new JsonObjectKeyValuePair(ErrorKey, error) }
                : new[] { new JsonObjectKeyValuePair(DirMapKey, dirmap) }
                                                                 )));
        }