re-refactor code
This commit is contained in:
48
cmd/root.go
48
cmd/root.go
@@ -137,17 +137,15 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
"gid": config.Get().System.User.Gid,
|
||||
}).Info("configured system user successfully")
|
||||
|
||||
panelClient := remote.CreateClient(
|
||||
pclient := remote.CreateClient(
|
||||
config.Get().PanelLocation,
|
||||
config.Get().AuthenticationTokenId,
|
||||
config.Get().AuthenticationToken,
|
||||
remote.WithTimeout(time.Second*time.Duration(config.Get().RemoteQuery.Timeout)),
|
||||
)
|
||||
_ = panelClient
|
||||
|
||||
serverManager := server.NewManager(panelClient)
|
||||
|
||||
if err := serverManager.Initialize(int(c.RemoteQuery.BootServersPerPage)); err != nil {
|
||||
manager, err := server.NewManager(cmd.Context(), pclient)
|
||||
if err != nil {
|
||||
log.WithField("error", err).Fatal("failed to load server configurations")
|
||||
}
|
||||
|
||||
@@ -160,20 +158,38 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
}
|
||||
|
||||
// Just for some nice log output.
|
||||
for _, s := range serverManager.GetAll() {
|
||||
log.WithField("server", s.Id()).Info("loaded configuration for server")
|
||||
for _, s := range manager.All() {
|
||||
log.WithField("server", s.Id()).Info("finished loading configuration for server")
|
||||
}
|
||||
|
||||
states, err := server.CachedServerStates()
|
||||
states, err := manager.ReadStates()
|
||||
if err != nil {
|
||||
log.WithField("error", err).Error("failed to retrieve locally cached server states from disk, assuming all servers in offline state")
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
// Every minute, write the current server states to the disk to allow for a more
|
||||
// seamless hard-reboot process in which wings will re-sync server states based
|
||||
// on it's last tracked state.
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if err := manager.PersistStates(); err != nil {
|
||||
log.WithField("error", err).Warn("failed to persist server states to disk")
|
||||
}
|
||||
case <-cmd.Context().Done():
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Create a new workerpool that limits us to 4 servers being bootstrapped at a time
|
||||
// on Wings. This allows us to ensure the environment exists, write configurations,
|
||||
// and reboot processes without causing a slow-down due to sequential booting.
|
||||
pool := workerpool.New(4)
|
||||
for _, serv := range serverManager.GetAll() {
|
||||
for _, serv := range manager.All() {
|
||||
s := serv
|
||||
|
||||
// For each server we encounter make sure the root data directory exists.
|
||||
@@ -184,7 +200,6 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
|
||||
pool.Submit(func() {
|
||||
s.Log().Info("configuring server environment and restoring to previous state")
|
||||
|
||||
var st string
|
||||
if state, exists := states[s.Id()]; exists {
|
||||
st = state
|
||||
@@ -236,14 +251,14 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
defer func() {
|
||||
// Cancel the context on all of the running servers at this point, even though the
|
||||
// program is just shutting down.
|
||||
for _, s := range server.GetServers().All() {
|
||||
for _, s := range manager.All() {
|
||||
s.CtxCancel()
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// Run the SFTP server.
|
||||
if err := sftp.New().Run(); err != nil {
|
||||
if err := sftp.New(manager).Run(); err != nil {
|
||||
log.WithError(err).Fatal("failed to initialize the sftp server")
|
||||
return
|
||||
}
|
||||
@@ -278,7 +293,7 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
// and external clients.
|
||||
s := &http.Server{
|
||||
Addr: api.Host + ":" + strconv.Itoa(api.Port),
|
||||
Handler: router.Configure(serverManager),
|
||||
Handler: router.Configure(manager),
|
||||
TLSConfig: config.DefaultTLSConfig,
|
||||
}
|
||||
|
||||
@@ -323,13 +338,6 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cancel the context on all of the running servers at this point, even though the
|
||||
// program is just shutting down.
|
||||
for _, s := range serverManager.GetAll() {
|
||||
s.CtxCancel()
|
||||
}
|
||||
}
|
||||
|
||||
// Reads the configuration from the disk and then sets up the global singleton
|
||||
// with all of the configuration values.
|
||||
func initConfig() {
|
||||
|
||||
Reference in New Issue
Block a user