Ejemplo n.º 1
0
        private void RefreshDiff()
        {
            Data.NewConfigLines = VyattaConfigUtil.WriteToStringLines(Data.ConfigRoot);

            var Diff       = new Menees.Diffs.TextDiff(HashType.HashCode, false, false, 0, false);
            var EditScript = Diff.Execute(Data.OldConfigLines, Data.NewConfigLines);

            ConfigDiff.SetData(Data.OldConfigLines, Data.NewConfigLines, EditScript, "Existing router config", "New router config", false, true, false);
            ConfigDiff.GoToFirstDiff();
        }
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Connecting to SSH...", 0);

            using (VyattaShell Shell = new VyattaShell(Data.Address, Data.Username, Data.Password))
            {
                if (ShouldCancel())
                {
                    return(false);
                }

                //Verify that we can identify the device

                SetStatus("Identifying device...", 5);

                var Version = Shell.RunCommand("cat /proc/version");
                if (!Version.Contains("edgeos"))
                {
                    throw new Exception("The device is not running EdgeOS and is not supported. Device ");
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                //Enter configure mode

                SetStatus("Processing routing interfaces...", 8);

                Dictionary <string, string> Gateways = IPRoute.GetDefaultGateways(Shell);

                SetStatus("Processing interface list...", 16);
                string ShowInterfaces = Shell.RunCommand("show interfaces");

                Regex ParseInterfaces = new Regex(@"([\w\.]+)\s+([0-9.\-]+(:?\/[0-9]+)?)\s+(\w\/\w)\s+(\w+)?");

                Data.Interfaces = new ObservableCollection <InterfaceMapping>();

                string[] InterfaceLines = ShowInterfaces.Split(new char[] { '\n' });
                foreach (string Line in InterfaceLines)
                {
                    Match Match = ParseInterfaces.Match(Line);
                    if (Match.Success)
                    {
                        InterfaceMapping Mapping = new InterfaceMapping();

                        Mapping.Interface   = Match.Groups[1].Value;
                        Mapping.IPAddress   = Match.Groups[2].Value == "-" ? "" : Match.Groups[2].Value;
                        Mapping.Codes       = Match.Groups[4].Value;
                        Mapping.Description = Match.Groups[5].Value;

                        string Gateway;
                        if (Gateways.TryGetValue(Mapping.Interface, out Gateway))
                        {
                            Mapping.Gateway = Gateway;
                        }

                        Data.Interfaces.Add(Mapping);
                    }
                }
            }

            SetStatus("Connecting over SCP...", 60);

            if (ShouldCancel())
            {
                return(false);
            }

            using (ScpClient Client = new ScpClient(Data.Address, Data.Username, Data.Password))
            {
                Client.Connect();

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Downloading config...", 80);

                using (Stream tempFile = new FileStream(TempPath, FileMode.CreateNew))
                {
                    Client.Download("/config/config.boot", tempFile);
                }

                SetStatus("Parsing existing config...", 85);

                string Errors = "";
                Data.OldConfigLines = File.ReadAllLines(TempPath);
                Data.ConfigRoot     = VyattaConfigUtil.ReadFromFile(TempPath, ref Errors);
                if (Errors.Length > 0)
                {
                    throw new Exception(Errors);
                }

                SetStatus("Downloading current template...", 90);

                try
                {
                    using (Stream tempTemplateFile = new FileStream(TempTemplatePath, FileMode.CreateNew))
                    {
                        Client.Download("/config/vcu/current.vcu", tempTemplateFile);
                    }

                    SetStatus("Parsing current template...", 95);

                    Errors            = "";
                    Data.TemplateRoot = VyattaConfigUtil.ReadFromFile(TempTemplatePath, ref Errors);
                    if (Errors.Length > 0)
                    {
                        throw new Exception(Errors);
                    }
                }
                catch (SshException e)
                {
                    if (!e.Message.Contains("No such file or directory"))
                    {
                        throw e;
                    }

                    //It's quite okay to fail here, it means the user hasn't uploaded
                    //a config with the tool yet.

                    Data.TemplateRoot = new VyattaConfigObject(null);
                }

                if (ShouldCancel())
                {
                    return(false);
                }

                SetStatus("Disconnecting...", 98);

                Client.Disconnect();
            }

            SetStatus("Completed.", 100);

            return(true);
        }
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            SetStatus("Connecting to SSH...", 0);

            using (VyattaShell Shell = new VyattaShell(Data.Address, Data.Username, Data.Password))
            {
                SetStatus("Creating backup...", 5);
                Shell.RunCommand("mkdir /config/vcu");
                Shell.RunCommand("cp /config/config.boot /config/vcu/previous.boot");
                Shell.RunCommand("cp /config/current.vcu /config/vcu/previous.vcu");
                Shell.RunCommand(string.Format("cp /config/config.boot /config/vcu/history_{0}.boot", DateTime.Now.ToString(VyattaConfigUtil.SortableDateFormat)));
                Shell.RunCommand(string.Format("cp /config/current.vcu /config/vcu/history_{0}.vcu", DateTime.Now.ToString(VyattaConfigUtil.SortableDateFormat)));

                SetStatus("Entering configure mode...", 10);
                Shell.RunCommand("configure");

                if (!ShouldCancel())
                {
                    using (ScpClient ScpClient = new ScpClient(Data.Address, Data.Username, Data.Password))
                    {
                        string TempConfigPath = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                        Data.NewConfigLines = VyattaConfigUtil.WriteToStringLines(Data.ConfigRoot);
                        VyattaConfigUtil.WriteToFile(Data.ConfigRoot, TempConfigPath);

                        string TempTemplatePath = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString());
                        VyattaConfigUtil.WriteToFile(Data.TemplateRoot, TempTemplatePath);

                        ScpClient.Connect();

                        SetStatus("Uploading new config...", 15);

                        if (!ShouldCancel())
                        {
                            using (Stream configFile = new FileStream(TempConfigPath, FileMode.Open))
                            {
                                ScpClient.Upload(configFile, "/config/config.boot");
                            }
                        }

                        //Probably shouldn't cancel after this point it might leave things in a weird state...

                        using (Stream templateFile = new FileStream(TempTemplatePath, FileMode.Open))
                        {
                            ScpClient.Upload(templateFile, "/config/current.vcu");
                        }

                        SetStatus("Disconnecting SCP...", 20);

                        ScpClient.Disconnect();
                    }
                }

                SetStatus("Loading new config...", 25);

                Shell.RunCommand("load");

                SetStatus("Comparing config...", 35);
                Shell.RunCommand("compare");

                SetStatus("Committing new config (this will take a while)...", 45);
                string CommitResult = Shell.RunCommand("commit");

                if (CommitResult.Contains("Commit failed"))
                {
                    //Restore the backup
                    Shell.RunCommand("cp /config/vcu/previous.boot /config/config.boot");

                    Shell.RunCommand("load");
                    Shell.RunCommand("commit");

                    Shell.RunCommand("exit");

                    throw new Exception(CommitResult);
                }

                SetStatus("Committing new config (this will take a while)...", 95);
                Shell.RunCommand("exit");

                SetStatus("Disconnecting from SSH...", 98);
            }

            SetStatus("Completed.", 100);

            return(true);
        }
Ejemplo n.º 4
0
        public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel)
        {
            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Deleting previous auto-generated rules...", 16);
            VyattaConfigRouting.DeleteGeneratedStaticRoutes(Data.ConfigRoot);

            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Generating Netflix static routing...", 32);
            //VyattaConfigRouting.AddStaticRoutesForOrganization( Data.ConfigRoot, "Netflix", Data, "Internet", "Netflix" );
            {
                StaticRoutingData NewRoute = new StaticRoutingData();
                NewRoute.Destination = "Netflix";
                NewRoute.Interface   = "eth0";
                NewRoute.Name        = "Netflix";
                NewRoute.Type        = RoutingType.Organisation;
                NewRoute.Action      = StaticRouteAction.ToInterface;

                Data.StaticRoutes.Add(NewRoute);
            }

            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Generating BBC static routing...", 48);
            //VyattaConfigRouting.AddStaticRoutesForOrganization( Data.ConfigRoot, "BBC", Data, "Internet", "BBC" );
            {
                StaticRoutingData NewRoute = new StaticRoutingData();
                NewRoute.Destination = "BBC";
                NewRoute.Interface   = "eth0";
                NewRoute.Name        = "BBC";
                NewRoute.Type        = RoutingType.Organisation;
                NewRoute.Action      = StaticRouteAction.ToInterface;

                Data.StaticRoutes.Add(NewRoute);
            }

            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Generating Valve static routing...", 56);
            //VyattaConfigRouting.AddStaticRoutesForOrganization( Data.ConfigRoot, "Valve", Data, "Internet", "Valve" );
            {
                StaticRoutingData NewRoute = new StaticRoutingData();
                NewRoute.Destination = "Valve";
                NewRoute.Interface   = "eth0";
                NewRoute.Name        = "Valve";
                NewRoute.Type        = RoutingType.Organisation;
                NewRoute.Action      = StaticRouteAction.ToInterface;

                Data.StaticRoutes.Add(NewRoute);
            }

            if (ShouldCancel())
            {
                return(false);
            }

            SetStatus("Generating Nest static routing...", 80);
            //VyattaConfigRouting.AddStaticRoutesForOrganization( Data.ConfigRoot, "Nest", Data, "Internet", "Nest" );
            {
                StaticRoutingData NewRoute = new StaticRoutingData();
                NewRoute.Destination = "Nest";
                NewRoute.Interface   = "eth0";
                NewRoute.Name        = "Nest";
                NewRoute.Type        = RoutingType.Organisation;
                NewRoute.Action      = StaticRouteAction.ToInterface;

                Data.StaticRoutes.Add(NewRoute);
            }

            SetStatus("Saving new config...", 90);
            Data.NewConfigLines = VyattaConfigUtil.WriteToStringLines(Data.ConfigRoot);

            return(true);
        }