forked from aosedge/aos_servicemanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
173 lines (148 loc) · 6.69 KB
/
config.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-License-Identifier: Apache-2.0
//
// Copyright (C) 2021 Renesas Electronics Corporation.
// Copyright (C) 2021 EPAM Systems, Inc.
//
// 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 config provides set of API to provide aos configuration
package config
import (
"encoding/json"
"io/ioutil"
"path"
"time"
"github.com/aoscloud/aos_common/aoserrors"
"github.com/aoscloud/aos_common/aostypes"
"github.com/aoscloud/aos_common/journalalerts"
"github.com/aoscloud/aos_common/resourcemonitor"
log "github.com/sirupsen/logrus"
)
/***********************************************************************************************************************
* Consts
**********************************************************************************************************************/
const (
defaultServiceAlertPriority = 4
defaultSystemAlertPriority = 3
maxAlertPriorityLevel = 7
minAlertPriorityLevel = 0
)
/***********************************************************************************************************************
* Types
**********************************************************************************************************************/
// Logging configuration for system and service logging.
type Logging struct {
MaxPartSize uint64 `json:"maxPartSize"`
MaxPartCount uint64 `json:"maxPartCount"`
}
// Migration struct represents path for db migration.
type Migration struct {
MigrationPath string `json:"migrationPath"`
MergedMigrationPath string `json:"mergedMigrationPath"`
}
// Config instance.
type Config struct {
CACert string `json:"caCert"`
CertStorage string `json:"certStorage"`
CMServerURL string `json:"cmServerUrl"`
IAMProtectedServerURL string `json:"iamProtectedServerUrl"`
IAMPublicServerURL string `json:"iamPublicServerUrl"`
WorkingDir string `json:"workingDir"`
StorageDir string `json:"storageDir"`
StateDir string `json:"stateDir"`
ServicesDir string `json:"servicesDir"`
ServicesPartLimit uint `json:"servicesPartLimit"`
LayersDir string `json:"layersDir"`
LayersPartLimit uint `json:"layersPartLimit"`
DownloadDir string `json:"downloadDir"`
ExtractDir string `json:"extractDir"`
RemoteNode bool `json:"remoteNode"`
RunnerFeatures []string `json:"runnerFeatures"`
UnitConfigFile string `json:"unitConfigFile"`
ServiceTTLDays uint64 `json:"serviceTtlDays"`
LayerTTLDays uint64 `json:"layerTtlDays"`
ServiceHealthCheckTimeout aostypes.Duration `json:"serviceHealthCheckTimeout"`
Monitoring resourcemonitor.Config `json:"monitoring"`
Logging Logging `json:"logging"`
JournalAlerts journalalerts.Config `json:"journalAlerts,omitempty"`
HostBinds []string `json:"hostBinds"`
Hosts []aostypes.Host `json:"hosts,omitempty"`
Migration Migration `json:"migration"`
}
/***********************************************************************************************************************
* Public
**********************************************************************************************************************/
// New creates new config object.
func New(fileName string) (config *Config, err error) {
raw, err := ioutil.ReadFile(fileName)
if err != nil {
return config, aoserrors.Wrap(err)
}
config = &Config{
ServiceTTLDays: 30, // nolint:gomnd
LayerTTLDays: 30, // nolint:gomnd
ServiceHealthCheckTimeout: aostypes.Duration{Duration: 35 * time.Second}, // nolint:gomnd
Monitoring: resourcemonitor.Config{
SendPeriod: aostypes.Duration{Duration: 1 * time.Minute},
PollPeriod: aostypes.Duration{Duration: 10 * time.Second},
},
Logging: Logging{
MaxPartSize: 524288, // nolint:gomnd
MaxPartCount: 20, // nolint:gomnd
},
JournalAlerts: journalalerts.Config{
SystemAlertPriority: defaultSystemAlertPriority,
ServiceAlertPriority: defaultServiceAlertPriority,
},
}
if err = json.Unmarshal(raw, &config); err != nil {
return config, aoserrors.Wrap(err)
}
if config.CertStorage == "" {
config.CertStorage = "/var/aos/crypt/sm/"
}
if config.StorageDir == "" {
config.StorageDir = path.Join(config.WorkingDir, "storages")
}
if config.LayersDir == "" {
config.LayersDir = path.Join(config.WorkingDir, "layers")
}
if config.ServicesDir == "" {
config.ServicesDir = path.Join(config.WorkingDir, "services")
}
if config.DownloadDir == "" {
config.DownloadDir = path.Join(config.WorkingDir, "download")
}
if config.ExtractDir == "" {
config.ExtractDir = path.Join(config.WorkingDir, "extract")
}
if config.UnitConfigFile == "" {
config.UnitConfigFile = path.Join(config.WorkingDir, "aos_unit.cfg")
}
if config.Migration.MigrationPath == "" {
config.Migration.MigrationPath = "/usr/share/aos/servicemanager/migration"
}
if config.Migration.MergedMigrationPath == "" {
config.Migration.MergedMigrationPath = path.Join(config.WorkingDir, "mergedMigration")
}
if config.JournalAlerts.ServiceAlertPriority > maxAlertPriorityLevel ||
config.JournalAlerts.ServiceAlertPriority < minAlertPriorityLevel {
log.Warnf("Default value %d for service alert priority is assigned", defaultServiceAlertPriority)
config.JournalAlerts.ServiceAlertPriority = defaultServiceAlertPriority
}
if config.JournalAlerts.SystemAlertPriority > maxAlertPriorityLevel ||
config.JournalAlerts.SystemAlertPriority < minAlertPriorityLevel {
log.Warnf("Default value %d for system alert priority is assigned", defaultSystemAlertPriority)
config.JournalAlerts.SystemAlertPriority = defaultSystemAlertPriority
}
return config, nil
}