-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.go
127 lines (110 loc) · 2.99 KB
/
main.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
package main
import (
"time"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/getsentry/sentry-go"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
"github.com/godbus/dbus/v5/prop"
"github.com/home-assistant/os-agent/apparmor"
"github.com/home-assistant/os-agent/boards"
"github.com/home-assistant/os-agent/cgroup"
"github.com/home-assistant/os-agent/config"
"github.com/home-assistant/os-agent/datadisk"
"github.com/home-assistant/os-agent/system"
logging "github.com/home-assistant/os-agent/utils/log"
)
const (
busName = "io.hass.os"
objectPath = "/io/hass/os"
sentryDsn = "https://c74e811a96e4413a95caaaa5ae05f851@o427061.ingest.sentry.io/5710878"
)
var (
// version and baord are set at link time via -X flag
version string = "dev"
board string = "unknown"
enableCapture bool = false
)
func main() {
logging.Info.Printf("Start OS-Agent %s", version)
// Sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
Release: version,
BeforeSend: filterSentry,
})
if err != nil {
logging.Critical.Fatalf("Sentry init: %s", err)
}
defer sentry.Flush(2 * time.Second)
defer sentry.Recover()
// Connect DBus
conn, err := dbus.SystemBus()
if err != nil {
logging.Critical.Fatalf("DBus connection: %s", err)
}
// Init Dbus io.hass.os
reply, err := conn.RequestName(busName, dbus.NameFlagDoNotQueue)
if err != nil {
logging.Critical.Panic(err)
}
if reply != dbus.RequestNameReplyPrimaryOwner {
logging.Critical.Fatalf("name already taken")
}
// Set base Property / functionality
InitializeDBus(conn)
logging.Info.Printf("Listening on service %s ...", busName)
datadisk.InitializeDBus(conn)
system.InitializeDBus(conn)
apparmor.InitializeDBus(conn)
cgroup.InitializeDBus(conn)
boards.InitializeDBus(conn, board)
config.InitializeDBus(conn)
_, err = daemon.SdNotify(false, daemon.SdNotifyReady)
if err != nil {
logging.Critical.Panic(err)
}
select {}
}
func InitializeDBus(conn *dbus.Conn) {
propsSpec := map[string]map[string]*prop.Prop{
busName: {
"Version": {
Value: version,
Writable: false,
Emit: prop.EmitInvalidates,
Callback: nil,
},
"Diagnostics": {
Value: enableCapture,
Writable: true,
Emit: prop.EmitTrue,
Callback: func(c *prop.Change) *dbus.Error {
logging.Info.Printf("Diagnostics is now %t", c.Value)
enableCapture = c.Value.(bool)
return nil
},
},
},
}
props, err := prop.Export(conn, objectPath, propsSpec)
if err != nil {
logging.Critical.Panic(err)
}
node := &introspect.Node{
Name: objectPath,
Interfaces: []introspect.Interface{
introspect.IntrospectData,
prop.IntrospectData,
{
Name: busName,
Properties: props.Introspection(busName),
},
},
}
err = conn.Export(introspect.NewIntrospectable(node), objectPath, "org.freedesktop.DBus.Introspectable")
if err != nil {
logging.Critical.Panic(err)
}
logging.Info.Printf("Exposing object %s with interface %s ...", objectPath, busName)
}