forked from yunionio/cloudpods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost_services.go
More file actions
171 lines (148 loc) · 5.52 KB
/
Copy pathhost_services.go
File metadata and controls
171 lines (148 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hostman
import (
execlient "yunion.io/x/executor/client"
"yunion.io/x/log"
"yunion.io/x/pkg/util/signalutils"
"yunion.io/x/onecloud/pkg/appsrv"
app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
"yunion.io/x/onecloud/pkg/cloudcommon/cronman"
"yunion.io/x/onecloud/pkg/cloudcommon/service"
"yunion.io/x/onecloud/pkg/hostman/downloader"
"yunion.io/x/onecloud/pkg/hostman/guestman"
"yunion.io/x/onecloud/pkg/hostman/guestman/desc"
"yunion.io/x/onecloud/pkg/hostman/guestman/guesthandlers"
"yunion.io/x/onecloud/pkg/hostman/hostdeployer/deployclient"
"yunion.io/x/onecloud/pkg/hostman/hosthandler"
"yunion.io/x/onecloud/pkg/hostman/hostinfo"
"yunion.io/x/onecloud/pkg/hostman/hostmetrics"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/hostman/kubehandlers"
"yunion.io/x/onecloud/pkg/hostman/metadata"
"yunion.io/x/onecloud/pkg/hostman/options"
"yunion.io/x/onecloud/pkg/hostman/storageman"
"yunion.io/x/onecloud/pkg/hostman/storageman/diskhandlers"
"yunion.io/x/onecloud/pkg/hostman/storageman/storagehandler"
"yunion.io/x/onecloud/pkg/util/procutils"
"yunion.io/x/onecloud/pkg/util/sysutils"
)
type SHostService struct {
*service.SServiceBase
}
func (host *SHostService) InitService() {
options.Init()
isRoot := sysutils.IsRootPermission()
if !isRoot {
log.Fatalf("host service must running with root permissions")
}
if len(options.HostOptions.DeployServerSocketPath) == 0 {
log.Fatalf("missing deploy server socket path")
}
// options.HostOptions.EnableRbac = false // disable rbac
// init base option for pid file
host.SServiceBase.O = &options.HostOptions.BaseOptions
log.Infof("exec socket path: %s", options.HostOptions.ExecutorSocketPath)
if options.HostOptions.EnableRemoteExecutor {
execlient.Init(options.HostOptions.ExecutorSocketPath)
execlient.SetTimeoutSeconds(options.HostOptions.ExecutorConnectTimeoutSeconds)
procutils.SetRemoteExecutor()
}
}
func (host *SHostService) OnExitService() {}
func (host *SHostService) RunService() {
app := app_common.InitApp(&options.HostOptions.BaseOptions, false)
cronManager := cronman.InitCronJobManager(false, options.HostOptions.CronJobWorkerCount)
hostutils.Init()
hostInstance := hostinfo.Instance()
if err := hostInstance.Init(); err != nil {
log.Fatalf("Host instance init error: %v", err)
}
app_common.InitAuth(&options.HostOptions.CommonOptions, func() {
log.Infof("Auth complete!!")
})
deployclient.Init(options.HostOptions.DeployServerSocketPath)
if err := storageman.Init(hostInstance); err != nil {
log.Fatalf("Storage manager init error: %v", err)
}
var guestChan chan struct{}
if err := guestman.Init(hostInstance, options.HostOptions.ServersPath, options.HostOptions.DeployConcurrent); err != nil {
log.Fatalf("guest manager init error: %s", err)
}
guestman.GetGuestManager().InitQemuMaxCpus(
hostInstance.GetQemuMachineInfoList(), hostInstance.GetKVMMaxCpus(),
)
guestman.GetGuestManager().InitQemuMaxMems(uint(hostInstance.GetMemoryTotal()))
if err := guestman.GetGuestManager().InitPythonPath(); err != nil {
log.Fatalf("Guest manager init python path %s", err)
}
var err error
hostInstance.StartRegister(2, func() {
guestChan, err = guestman.GetGuestManager().Bootstrap()
if err != nil {
log.Fatalf("Guest manager Bootstrap %s", err)
}
// hostmetrics after guestmanager bootstrap
hostmetrics.Init()
hostmetrics.Start()
})
<-hostinfo.Instance().IsRegistered // wait host and guest init
host.initHandlers(app)
// Init Metadata handler
go metadata.Start(
app_common.InitApp(&options.HostOptions.BaseOptions, false),
&metadata.Service{
Address: options.HostOptions.Address,
Port: options.HostOptions.Port + 1000,
DescGetter: metadata.DescGetterFunc(func(ip string) *desc.SGuestDesc {
guestDesc, _ := guestman.GetGuestManager().GetGuestNicDesc("", ip, "", "", false)
return guestDesc
}),
},
)
{
cronManager.AddJobEveryFewDays(
"CleanRecycleDiskFiles", 1, 3, 0, 0, storageman.CleanRecycleDiskfiles, false)
cronManager.AddJobEveryFewDays(
"CleanImageCachefiles", 1, 3, 0, 0, storageman.CleanImageCachefiles, options.HostOptions.ImageCacheCleanupOnStartup)
cronManager.Start()
}
close(guestChan)
app_common.ServeForeverWithCleanup(app, &options.HostOptions.BaseOptions, func() {
hostinfo.Stop()
storageman.Stop()
hostmetrics.Stop()
guestman.Stop()
hostutils.GetWorkManager().Stop()
})
}
func (host *SHostService) initHandlers(app *appsrv.Application) {
guesthandlers.AddGuestTaskHandler("", app)
storagehandler.AddStorageHandler("", app)
diskhandlers.AddDiskHandler("", app)
downloader.AddDownloadHandler("", app)
kubehandlers.AddKubeAgentHandler("", app)
hosthandler.AddHostHandler("", app)
app_common.ExportOptionsHandler(app, &options.HostOptions)
}
func StartService() {
// register dump stack signal handler at first
signalutils.SetDumpStackSignal()
signalutils.StartTrap()
var srv = &SHostService{}
srv.SServiceBase = &service.SServiceBase{
Service: srv,
}
srv.StartService()
}