Exemplo n.º 1
0
        public AntdHostModule()
        {
            Get["/host/info"] = x => {
                const StringSplitOptions ssoree = StringSplitOptions.RemoveEmptyEntries;
                var launcher    = new CommandLauncher();
                var hostnamectl = launcher.Launch("hostnamectl").ToList();
                var model       = new PageHostModel {
                    StaticHostname = hostnamectl.First(_ => _.Contains("Static hostname:")).Split(new[] { ":" }, 2, ssoree)[1],
                    IconName       = hostnamectl.First(_ => _.Contains("Icon name:")).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],
                    MachineId      = hostnamectl.First(_ => _.Contains("Machine ID:")).Split(new[] { ":" }, 2, ssoree)[1],
                    BootId         = hostnamectl.First(_ => _.Contains("Boot ID:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Virtualization = hostnamectl.First(_ => _.Contains("Virtualization:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Os             = hostnamectl.First(_ => _.Contains("Operating System:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Kernel         = hostnamectl.First(_ => _.Contains("Kernel:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Architecture   = hostnamectl.First(_ => _.Contains("Architecture:")).Split(new[] { ":" }, 2, ssoree)[1]
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/host/info/name"] = x => {
                string name = Request.Form.Name;
                if (string.IsNullOrEmpty(name))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SetHostInfoName(name);
                hostconfiguration.ApplyHostInfo();
                return(HttpStatusCode.OK);
            };

            Post["/host/info/chassis"] = x => {
                string chassis = Request.Form.Chassis;
                if (string.IsNullOrEmpty(chassis))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SetHostInfoChassis(chassis);
                hostconfiguration.ApplyHostInfo();
                return(HttpStatusCode.OK);
            };

            Post["/host/info/deployment"] = x => {
                string deployment = Request.Form.Deployment;
                if (string.IsNullOrEmpty(deployment))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SetHostInfoDeployment(deployment);
                hostconfiguration.ApplyHostInfo();
                return(HttpStatusCode.OK);
            };

            Post["/host/info/location"] = x => {
                string location = Request.Form.Location;
                if (string.IsNullOrEmpty(location))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SetHostInfoLocation(location);
                hostconfiguration.ApplyHostInfo();
                return(HttpStatusCode.OK);
            };

            Post["/host/info"] = x => {
                string name       = Request.Form.Name;
                string chassis    = Request.Form.Chassis;
                string deployment = Request.Form.Deployment;
                string location   = Request.Form.Location;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(chassis) || string.IsNullOrEmpty(deployment) || string.IsNullOrEmpty(location))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SetHostInfo(name, chassis, deployment, location);
                hostconfiguration.ApplyHostInfo();
                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);
            };
        }