예제 #1
0
        public void TryParse()
        {
            LinuxPermissions permissions;

            Assert.True(LinuxPermissions.TryParse("700", out permissions));
            Assert.True(permissions.OwnerRead);
            Assert.True(permissions.OwnerWrite);
            Assert.True(permissions.OwnerExecute);
            Assert.False(permissions.GroupRead);
            Assert.False(permissions.GroupWrite);
            Assert.False(permissions.GroupExecute);
            Assert.False(permissions.AllRead);
            Assert.False(permissions.AllWrite);
            Assert.False(permissions.AllExecute);
            Assert.Equal("700", permissions.ToString());

            Assert.False(LinuxPermissions.TryParse("70x", out permissions));
        }
예제 #2
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            var hiveLogin = Program.ConnectHive();

            // Process the command options.

            var isText      = false;
            var permissions = new LinuxPermissions("440");

            if (commandLine.GetOption("--text") != null)
            {
                isText = true;
            }

            var chmod = commandLine.GetOption("--chmod");

            if (!string.IsNullOrEmpty(chmod))
            {
                if (!LinuxPermissions.TryParse(chmod, out permissions))
                {
                    Console.Error.WriteLine("*** ERROR: Invalid Linux file permissions.");
                    Program.Exit(1);
                }
            }

            // Process the command arguments.

            List <NodeDefinition> nodeDefinitions = new List <NodeDefinition>();
            string source;
            string target;

            if (commandLine.Arguments.Length < 1)
            {
                Console.Error.WriteLine("*** ERROR: SOURCE file was not specified.");
                Program.Exit(1);
            }

            source = commandLine.Arguments[0];

            if (commandLine.Arguments.Length < 2)
            {
                Console.Error.WriteLine("*** ERROR: TARGET file was not specified.");
                Program.Exit(1);
            }

            target = commandLine.Arguments[1];

            if (commandLine.Arguments.Length == 2)
            {
                nodeDefinitions.Add(hiveLogin.Definition.Managers.First());
            }
            else if (commandLine.Arguments.Length == 3 && commandLine.Arguments[2] == "+")
            {
                foreach (var manager in hiveLogin.Definition.SortedManagers)
                {
                    nodeDefinitions.Add(manager);
                }

                foreach (var worker in hiveLogin.Definition.SortedWorkers)
                {
                    nodeDefinitions.Add(worker);
                }
            }
            else
            {
                foreach (var name in commandLine.Shift(2).Arguments)
                {
                    NodeDefinition node;

                    if (!hiveLogin.Definition.NodeDefinitions.TryGetValue(name, out node))
                    {
                        Console.Error.WriteLine($"*** ERROR: Node [{name}] is not present in the hive.");
                        Program.Exit(1);
                    }

                    nodeDefinitions.Add(node);
                }
            }

            if (!File.Exists(source))
            {
                Console.Error.WriteLine($"*** ERROR: File [{source}] does not exist.");
                Program.Exit(1);
            }

            // Perform the upload.

            var hive       = new HiveProxy(hiveLogin);
            var controller = new SetupController <NodeDefinition>(Program.SafeCommandLine, hive.Nodes.Where(n => nodeDefinitions.Exists(nd => nd.Name == n.Name)))
            {
                ShowStatus  = !Program.Quiet,
                MaxParallel = Program.MaxParallel
            };

            controller.SetDefaultRunOptions(RunOptions.FaultOnError);

            controller.AddWaitUntilOnlineStep();
            controller.AddStep("upload",
                               (node, stepDelay) =>
            {
                Thread.Sleep(stepDelay);

                node.Status = "uploading";

                if (isText)
                {
                    node.UploadText(target, File.ReadAllText(source, Encoding.UTF8), tabStop: 4, outputEncoding: Encoding.UTF8);
                }
                else
                {
                    using (var stream = new FileStream(source, FileMode.Open, FileAccess.Read))
                    {
                        node.Upload(target, stream);
                    }
                }

                node.Status = $"set permissions: {permissions}";
                node.SudoCommand("chmod", permissions, target);
            });

            if (!controller.Run())
            {
                Console.Error.WriteLine("*** ERROR: The upload to one or more nodes failed.");
                Program.Exit(1);
            }
        }