Ejemplo n.º 1
0
        public AntdNameServiceModule()
        {
            Get["/nameservice"] = x => {
                var hosts    = CommandLauncher.Launch("cat-etc-hosts").ToArray();
                var networks = CommandLauncher.Launch("cat-etc-networks").ToArray();
                var resolv   = CommandLauncher.Launch("cat-etc-resolv").ToArray();
                var nsswitch = CommandLauncher.Launch("cat-etc-nsswitch").ToArray();
                var model    = new PageNameServiceModel {
                    Hostname  = CommandLauncher.Launch("cat-etc-hostname").JoinToString("<br />"),
                    DomainInt = HostConfiguration.Host.InternalDomain,
                    DomainExt = HostConfiguration.Host.ExternalDomain,
                    Hosts     = hosts.JoinToString("<br />"),
                    Networks  = networks.JoinToString("<br />"),
                    Resolv    = resolv.JoinToString("<br />"),
                    Nsswitch  = nsswitch.JoinToString("<br />")
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/nameservice/hosts"] = x => {
                string hosts = Request.Form.Hosts;
                HostConfiguration.SetNsHosts(hosts.Contains("\n")
                    ? hosts.SplitToList("\n").ToArray()
                    : hosts.SplitToList(Environment.NewLine).ToArray());
                HostConfiguration.ApplyNsHosts();
                return(HttpStatusCode.OK);
            };

            Post["/nameservice/networks"] = x => {
                string networks = Request.Form.Networks;
                HostConfiguration.SetNsNetworks(networks.Contains("\n")
                  ? networks.SplitToList("\n").ToArray()
                  : networks.SplitToList(Environment.NewLine).ToArray());
                HostConfiguration.ApplyNsNetworks();
                return(HttpStatusCode.OK);
            };

            Post["/nameservice/resolv"] = x => {
                string resolv = Request.Form.Resolv;
                HostConfiguration.SetNsResolv(resolv.Contains("\n")
                  ? resolv.SplitToList("\n").ToArray()
                  : resolv.SplitToList(Environment.NewLine).ToArray());
                HostConfiguration.ApplyNsResolv();
                return(HttpStatusCode.OK);
            };

            Post["/nameservice/switch"] = x => {
                string @switch = Request.Form.Switch;
                HostConfiguration.SetNsSwitch(@switch.Contains("\n")
                  ? @switch.SplitToList("\n").ToArray()
                  : @switch.SplitToList(Environment.NewLine).ToArray());
                HostConfiguration.ApplyNsSwitch();
                return(HttpStatusCode.OK);
            };

            Post["/host/int/domain"] = x => {
                string domain = Request.Form.Domain;
                HostConfiguration.SetInternalDomain(domain);
                return(HttpStatusCode.OK);
            };

            Post["/host/ext/domain"] = x => {
                string domain = Request.Form.Domain;
                HostConfiguration.SetExtenalDomain(domain);
                return(HttpStatusCode.OK);
            };
        }
Ejemplo n.º 2
0
        public WizardModule()
        {
            Get["/wizard/data"] = x => {
                var bash                        = new Bash();
                var timezones                   = bash.Execute("timedatectl list-timezones --no-pager").SplitBash();
                var networkConfiguration        = new NetworkConfiguration();
                var launcher                    = new CommandLauncher();
                var hostConfiguration           = new HostConfiguration();
                var hosts                       = launcher.Launch("cat-etc-hosts").ToArray();
                var networks                    = launcher.Launch("cat-etc-networks").ToArray();
                var resolv                      = launcher.Launch("cat-etc-resolv").ToArray();
                var nsswitch                    = launcher.Launch("cat-etc-nsswitch").ToArray();
                var hostnamectl                 = launcher.Launch("hostnamectl").ToList();
                const StringSplitOptions ssoree = StringSplitOptions.RemoveEmptyEntries;
                var model                       = new PageWizardModel {
                    Timezones            = timezones,
                    NetworkInterfaceList = networkConfiguration.InterfacePhysical,
                    DomainInt            = hostConfiguration.Host.InternalDomain,
                    DomainExt            = hostConfiguration.Host.ExternalDomain,
                    Hosts          = hosts.JoinToString(Environment.NewLine),
                    Networks       = networks.JoinToString(Environment.NewLine),
                    Resolv         = resolv.JoinToString(Environment.NewLine),
                    Nsswitch       = nsswitch.JoinToString(Environment.NewLine),
                    StaticHostname = hostnamectl.First(_ => _.Contains("Static hostname:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Chassis        = hostnamectl.First(_ => _.Contains("Chassis:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Deployment     = hostnamectl.First(_ => _.Contains("Deployment:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Location       = hostnamectl.First(_ => _.Contains("Location:")).Split(new[] { ":" }, 2, ssoree)[1],
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/wizard"] = x => {
                ConsoleLogger.Log("[wizard] start basic configuration");
                string password      = Request.Form.Password;
                var    masterManager = new ManageMaster();
                masterManager.ChangePassword(password);
                ConsoleLogger.Log("[wizard] changed master password");
                string hostname   = Request.Form.Hostname;
                string location   = Request.Form.Location;
                string chassis    = Request.Form.Chassis;
                string deployment = Request.Form.Deployment;
                ConsoleLogger.Log($"[wizard] host info:\t{hostname}");
                ConsoleLogger.Log($"[wizard] host info:\t{location}");
                ConsoleLogger.Log($"[wizard] host info:\t{chassis}");
                ConsoleLogger.Log($"[wizard] host info:\t{deployment}");
                var hostConfiguration = new HostConfiguration();
                hostConfiguration.SetHostInfoName(hostname);
                hostConfiguration.SetHostInfoChassis(chassis);
                hostConfiguration.SetHostInfoDeployment(deployment);
                hostConfiguration.SetHostInfoLocation(location);
                hostConfiguration.ApplyHostInfo();
                string timezone = Request.Form.Timezone;
                hostConfiguration.SetTimezone(timezone);
                hostConfiguration.ApplyTimezone();
                string ntpServer = Request.Form.NtpServer;
                hostConfiguration.SetNtpdate(ntpServer);
                hostConfiguration.ApplyNtpdate();
                string domainInt = Request.Form.DomainInt;
                string domainExt = Request.Form.DomainExt;
                string hosts     = Request.Form.Hosts;
                string networks  = Request.Form.Networks;
                string resolv    = Request.Form.Resolv;
                string nsswitch  = Request.Form.Nsswitch;
                hostConfiguration.SetNsHosts(hosts.Contains("\n")
                    ? hosts.SplitToList("\n").ToArray()
                    : hosts.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNsHosts();
                hostConfiguration.SetNsNetworks(networks.Contains("\n")
                    ? networks.SplitToList("\n").ToArray()
                    : networks.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNsNetworks();
                hostConfiguration.SetNsResolv(resolv.Contains("\n")
                  ? resolv.SplitToList("\n").ToArray()
                  : resolv.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNsResolv();
                hostConfiguration.SetNsSwitch(nsswitch.Contains("\n")
                  ? nsswitch.SplitToList("\n").ToArray()
                  : nsswitch.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNsSwitch();
                hostConfiguration.SetInternalDomain(domainInt);
                hostConfiguration.SetExtenalDomain(domainExt);
                ConsoleLogger.Log("[wizard] name services configured");
                string Interface            = Request.Form.Interface;
                string txqueuelen           = Request.Form.Txqueuelen;
                string mtu                  = Request.Form.Mtu;
                string mode                 = Request.Form.Mode;
                string staticAddress        = Request.Form.StaticAddress;
                string staticRange          = Request.Form.StaticRange;
                var    networkConfiguration = new NetworkConfiguration();
                var    model                = new NetworkInterfaceConfigurationModel {
                    Interface     = Interface,
                    Mode          = (NetworkInterfaceMode)Enum.Parse(typeof(NetworkInterfaceMode), mode),
                    Status        = NetworkInterfaceStatus.Up,
                    StaticAddress = staticAddress,
                    StaticRange   = staticRange,
                    Txqueuelen    = txqueuelen,
                    Mtu           = mtu,
                    Type          = NetworkInterfaceType.Physical
                };
                networkConfiguration.AddInterfaceSetting(model);
                networkConfiguration.ApplyInterfaceSetting(model);
                ConsoleLogger.Log($"[wizard] network configured at {Interface}");
                hostConfiguration.SetHostAsConfigured();
                ConsoleLogger.Log("[wizard] configuration complete");
                return(HttpStatusCode.OK);
            };
        }