-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathformat.go
More file actions
152 lines (134 loc) · 4.27 KB
/
Copy pathformat.go
File metadata and controls
152 lines (134 loc) · 4.27 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
/*
* Copyright (c) 2021 NetEase 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.
*/
/*
* Project: CurveAdm
* Created Date: 2021-12-27
* Author: Jingli Chen (Wine93)
*/
package configure
import (
"strings"
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/viper"
)
const (
DEFAULT_CONTAINER_IMAGE = "opencurvedocker/curvebs:v1.2"
FORMAT_TYPE_DATA = "data"
FORMAT_TYPE_WAL = "wal"
)
/*
* host:
* - machine1
* - machine2
* - machine3
* disk:
* - data:/dev/sda:/data/chunkserver0:10 # fortmat_type:device:mount_path:format_percent
* - data:/dev/sdb:/data/chunkserver1:10
* - data:/dev/sdc:/data/chunkserver2:10
* - wal:/dev/nvme0n1p1:/data/wal/chunkserver0:10
* - wal:/dev/nvme0n1p2:/data/wal/chunkserver1:10
* - wal:/dev/nvme0n1p3:/data/wal/chunkserver2:10
*/
type (
FormatConfig struct {
ContainerIamge string
Host string
Type string
Device string
MountPoint string
FormtPercent int
FromDiskRecord bool
ServiceMountDevice bool
}
Format struct {
ContainerImage string `mapstructure:"container_image"`
Hosts []string `mapstructure:"host"`
Disks []string `mapstructure:"disk"`
}
)
func NewFormatConfig(containerImage, host, disk string) (*FormatConfig, error) {
items := strings.Split(disk, ":")
if len(items) != 4 {
return nil, errno.ERR_INVALID_DISK_FORMAT.S(disk)
}
formatType, device, mountPoint, percent := items[0], items[1], items[2], items[3]
if formatType != FORMAT_TYPE_DATA && formatType != FORMAT_TYPE_WAL {
return nil, errno.ERR_INVALID_FORMAT_TYPE.
F("formatType: %s", formatType)
} else if !strings.HasPrefix(device, "/") {
return nil, errno.ERR_INVALID_DEVICE.
F("device: %s", device)
} else if !strings.HasPrefix(mountPoint, "/") {
return nil, errno.ERR_MOUNT_POINT_REQUIRE_ABSOLUTE_PATH.
F("mountPoint: %s", mountPoint)
}
formatPercent, ok := utils.Str2Int(percent)
if !ok {
return nil, errno.ERR_FORMAT_PERCENT_REQUIRES_INTERGET.
F("percent: %s", percent)
} else if formatPercent <= 0 || formatPercent > 100 {
return nil, errno.ERR_FORMAT_PERCENT_MUST_BE_BETWEEN_1_AND_100.
F("percent: %s", percent)
}
return &FormatConfig{
ContainerIamge: containerImage,
Host: host,
Type: formatType,
Device: device,
MountPoint: mountPoint,
FormtPercent: formatPercent,
}, nil
}
func ParseFormat(filename string) ([]*FormatConfig, error) {
if !utils.PathExist(filename) {
return nil, errno.ERR_FORMAT_CONFIGURE_FILE_NOT_EXIST.
F("filepath: %s", filename)
}
parser := viper.New()
parser.SetConfigFile(filename)
parser.SetConfigType("yaml")
err := parser.ReadInConfig()
if err != nil {
return nil, errno.ERR_PARSE_FORMAT_CONFIGURE_FAILED.E(err)
}
format := &Format{}
err = parser.Unmarshal(format)
if err != nil {
return nil, errno.ERR_PARSE_FORMAT_CONFIGURE_FAILED.E(err)
}
containerImage := DEFAULT_CONTAINER_IMAGE
if len(format.ContainerImage) > 0 {
containerImage = format.ContainerImage
}
fcs := []*FormatConfig{}
for _, host := range format.Hosts {
for _, disk := range format.Disks {
fc, err := NewFormatConfig(containerImage, host, disk)
if err != nil {
return nil, err
}
fcs = append(fcs, fc)
}
}
return fcs, nil
}
func (fc *FormatConfig) GetContainerImage() string { return fc.ContainerIamge }
func (fc *FormatConfig) GetHost() string { return fc.Host }
func (fc *FormatConfig) GetFormatType() string { return fc.Type }
func (fc *FormatConfig) GetDevice() string { return fc.Device }
func (fc *FormatConfig) GetMountPoint() string { return fc.MountPoint }
func (fc *FormatConfig) GetFormatPercent() int { return fc.FormtPercent }