Exemplo n.º 1
0
        public AntdTimeModule()
        {
            Get["/time/info"] = x => {
                const StringSplitOptions ssoree = StringSplitOptions.RemoveEmptyEntries;
                var bash              = new Bash();
                var launcher          = new CommandLauncher();
                var hostConfiguration = new HostConfiguration();
                var timezones         = bash.Execute("timedatectl list-timezones --no-pager").SplitBash();
                var timedatectl       = launcher.Launch("timedatectl").ToList();
                var ntpd              = launcher.Launch("cat-etc-ntp").ToArray();
                var model             = new PageTimeModel {
                    Timezones = timezones,
                    LocalTime = timedatectl.First(_ => _.Contains("Local time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    UnivTime  = timedatectl.First(_ => _.Contains("Universal time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    RtcTime   = timedatectl.First(_ => _.Contains("RTC time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Timezone  = timedatectl.First(_ => _.Contains("Time zone:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Nettimeon = timedatectl.First(_ => _.Contains("Network time on:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Ntpsync   = timedatectl.First(_ => _.Contains("NTP synchronized:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Rtcintz   = timedatectl.First(_ => _.Contains("RTC in local TZ:")).Split(new[] { ":" }, 2, ssoree)[1],
                    NtpServer = hostConfiguration.Host.NtpdateServer.StoredValues["$server"],
                    Ntpd      = ntpd.JoinToString("<br />"),
                    NtpdEdit  = ntpd.JoinToString(Environment.NewLine)
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/host/timezone"] = x => {
                string timezone          = Request.Form.Timezone;
                var    hostconfiguration = new HostConfiguration();
                hostconfiguration.SetTimezone(timezone);
                hostconfiguration.ApplyTimezone();
                return(HttpStatusCode.OK);
            };

            Post["/host/synctime"] = x => {
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SyncClock();
                return(HttpStatusCode.OK);
            };

            Post["/host/ntpdate"] = x => {
                string ntpdate           = Request.Form.Ntpdate;
                var    hostconfiguration = new HostConfiguration();
                hostconfiguration.SetNtpdate(ntpdate);
                hostconfiguration.ApplyNtpdate();
                return(HttpStatusCode.OK);
            };

            Post["/host/ntpd"] = x => {
                string ntpd = Request.Form.Ntpd;
                var    hostConfiguration = new HostConfiguration();
                hostConfiguration.SetNtpd(ntpd.Contains("\n")
                  ? ntpd.SplitToList("\n").ToArray()
                  : ntpd.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNtpd();
                return(HttpStatusCode.OK);
            };
        }
Exemplo 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);
            };
        }