-
Notifications
You must be signed in to change notification settings - Fork 30
/
manager.go
157 lines (132 loc) · 5.1 KB
/
manager.go
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
// Package services mediates all the business logic of the application via a manager.
package services
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/accessibility"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/chown"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/gdm"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/keyboard"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/locale"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/privacy"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/pro"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/telemetry"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/timezone"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/services/user"
pb "github.com/canonical/ubuntu-desktop-provision/provd/protos"
"github.com/godbus/dbus/v5"
"github.com/ubuntu/decorate"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Manager mediates the whole business logic of the application.
type Manager struct {
userService user.Service
localeService locale.Service
keyboardSerivce keyboard.Service
privacyService privacy.Service
timezoneService timezone.Service
accessibilityService accessibility.Service
proService pro.Service
chownService chown.Service
telemetryService telemetry.Service
gdmService *gdm.Service
bus *dbus.Conn
}
// NewManager returns a new manager after creating all necessary items for our business logic.
func NewManager(ctx context.Context) (m *Manager, e error) {
defer decorate.OnError(&e, "can't create provd object")
var errs error
bus, err := dbus.ConnectSystemBus(
dbus.WithIncomingInterceptor(func(msg *dbus.Message) {
slog.Debug(fmt.Sprintf("DBUS: %s", msg))
}))
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to connect to system bus: %s", err))
}
userService, err := user.New(bus)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create user service: %s", err))
}
localeService, err := locale.New(bus)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create locale service: %s", err))
}
keyboardService, err := keyboard.New(bus)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create keyboard service: %s", err))
}
privacyService, err := privacy.New()
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create privacy service: %s", err))
}
timezoneService, err := timezone.New(bus)
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create timezone service: %s", err))
}
accessibilityService, err := accessibility.New()
if err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to create accessibility service: %s", err))
}
gdmService, err := gdm.New(bus)
if err != nil {
slog.Warn(fmt.Sprintf("GDM service failed to initiate: %s", err))
}
if errs != nil {
return nil, status.Errorf(codes.Internal, "%s", errs)
}
proService, err := pro.New()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create pro service: %s", err)
}
chownService, err := chown.New()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create chown service: %s", err)
}
telemetryService, err := telemetry.New()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create telemetry service: %s", err)
}
return &Manager{
userService: *userService,
localeService: *localeService,
keyboardSerivce: *keyboardService,
privacyService: *privacyService,
timezoneService: *timezoneService,
accessibilityService: *accessibilityService,
proService: *proService,
chownService: *chownService,
telemetryService: *telemetryService,
gdmService: gdmService,
bus: bus,
}, nil
}
// RegisterGRPCServices returns a new grpc Server.
func (m Manager) RegisterGRPCServices(ctx context.Context) *grpc.Server {
slog.Debug("Registering GRPC services")
grpcServer := grpc.NewServer()
pb.RegisterUserServiceServer(grpcServer, &m.userService)
pb.RegisterLocaleServiceServer(grpcServer, &m.localeService)
pb.RegisterKeyboardServiceServer(grpcServer, &m.keyboardSerivce)
pb.RegisterPrivacyServiceServer(grpcServer, &m.privacyService)
pb.RegisterTimezoneServiceServer(grpcServer, &m.timezoneService)
pb.RegisterAccessibilityServiceServer(grpcServer, &m.accessibilityService)
pb.RegisterProServiceServer(grpcServer, &m.proService)
pb.RegisterChownServiceServer(grpcServer, &m.chownService)
pb.RegisterTelemetryServiceServer(grpcServer, &m.telemetryService)
if m.gdmService != nil {
pb.RegisterGdmServiceServer(grpcServer, m.gdmService)
}
return grpcServer
}
// Stop closes the dbus connection.
func (m *Manager) Stop() error {
slog.Debug("Closing grpc manager and dbus connection")
if m.gdmService != nil {
m.gdmService.Close()
}
return m.bus.Close()
}