Debian Codegraph @

type.go

raw symbols SCIP
1 // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
2 // Use of this source code is governed by a LGPLv2.1
3 // license that can be found in the LICENSE file.
4
5 //go:build linux && cgo
6 // +build linux,cgo
7
8 package lxc
9
10 // #include <lxc/lxccontainer.h>
11 import "C"
12
13 import (
14 "errors"
15 "fmt"
16 "strconv"
17 "strings"
18 "unicode"
19 )
20
21 // Verbosity type
22 type Verbosity int
23
24 const (
25 // Quiet makes some API calls not to write anything to stdout
26 Quiet Verbosity = 1 << iota
27 // Verbose makes some API calls write to stdout
28 Verbose
29 )
30
31 // BackendStore type specifies possible backend types.
32 type BackendStore int
33
34 const (
35 // Btrfs backendstore type
36 Btrfs BackendStore = iota + 1
37 // Directory backendstore type
38 Directory
39 // LVM backendstore type
40 LVM
41 // ZFS backendstore type
42 ZFS
43 // Aufs backendstore type
44 Aufs
45 // Overlayfs backendstore type
46 Overlayfs
47 // Loopback backendstore type
48 Loopback
49 // Best backendstore type
50 Best
51 )
52
53 // BackendStore as string
54 func (t BackendStore) String() string {
55 switch t {
56 case Directory:
57 return "dir"
58 case ZFS:
59 return "zfs"
60 case Btrfs:
61 return "btrfs"
62 case LVM:
63 return "lvm"
64 case Aufs:
65 return "aufs"
66 case Overlayfs:
67 return "overlayfs"
68 case Loopback:
69 return "loop"
70 case Best:
71 return "best"
72 }
73 return ""
74 }
75
76 var backendStoreMap = map[string]BackendStore{
77 "dir": Directory,
78 "zfs": ZFS,
79 "btrfs": Btrfs,
80 "lvm": LVM,
81 "aufs": Aufs,
82 "overlayfs": Overlayfs,
83 "loopback": Loopback,
84 "best": Best,
85 }
86
87 // Set is the method to set the flag value, part of the flag.Value interface.
88 func (t *BackendStore) Set(value string) error {
89 backend, ok := backendStoreMap[value]
90 if ok {
91 *t = backend
92 return nil
93 }
94 return ErrUnknownBackendStore
95 }
96
97 // State type specifies possible container states.
98 type State int
99
100 const (
101 // STOPPED means container is not running
102 STOPPED State = iota + 1
103 // STARTING means container is starting
104 STARTING
105 // RUNNING means container is running
106 RUNNING
107 // STOPPING means container is stopping
108 STOPPING
109 // ABORTING means container is aborting
110 ABORTING
111 // FREEZING means container is freezing
112 FREEZING
113 // FROZEN means containe is frozen
114 FROZEN
115 // THAWED means container is thawed
116 THAWED
117 )
118
119 // StateMap provides the mapping betweens the state names and states
120 var StateMap = map[string]State{
121 "STOPPED": STOPPED,
122 "STARTING": STARTING,
123 "RUNNING": RUNNING,
124 "STOPPING": STOPPING,
125 "ABORTING": ABORTING,
126 "FREEZING": FREEZING,
127 "FROZEN": FROZEN,
128 "THAWED": THAWED,
129 }
130
131 // State as string
132 func (t State) String() string {
133 switch t {
134 case STOPPED:
135 return "STOPPED"
136 case STARTING:
137 return "STARTING"
138 case RUNNING:
139 return "RUNNING"
140 case STOPPING:
141 return "STOPPING"
142 case ABORTING:
143 return "ABORTING"
144 case FREEZING:
145 return "FREEZING"
146 case FROZEN:
147 return "FROZEN"
148 case THAWED:
149 return "THAWED"
150 }
151 return ""
152 }
153
154 // Taken from http://golang.org/doc/effective_go.html#constants
155
156 // ByteSize type
157 type ByteSize float64
158
159 const (
160 // B - byte
161 B = iota
162
163 // KB - kilobyte
164 KB ByteSize = 1 << (10 * iota)
165
166 // MB - megabyte
167 MB
168
169 // GB - gigabyte
170 GB
171
172 // TB - terabyte
173 TB
174
175 // PB - petabyte
176 PB
177
178 // EB - exabyte
179 EB
180
181 // ZB - zettabyte
182 ZB
183
184 // YB - yottabyte
185 YB
186 )
187
188 func (b ByteSize) String() string {
189 switch {
190 case b >= YB:
191 return fmt.Sprintf("%.2fYB", b/YB)
192 case b >= ZB:
193 return fmt.Sprintf("%.2fZB", b/ZB)
194 case b >= EB:
195 return fmt.Sprintf("%.2fEB", b/EB)
196 case b >= PB:
197 return fmt.Sprintf("%.2fPB", b/PB)
198 case b >= TB:
199 return fmt.Sprintf("%.2fTB", b/TB)
200 case b >= GB:
201 return fmt.Sprintf("%.2fGB", b/GB)
202 case b >= MB:
203 return fmt.Sprintf("%.2fMB", b/MB)
204 case b >= KB:
205 return fmt.Sprintf("%.2fKB", b/KB)
206 }
207 return fmt.Sprintf("%.2fB", b)
208 }
209
210 // Used to convert user input to ByteSize
211 var unitMap = map[string]ByteSize{
212 "B": B,
213 "BYTE": B,
214 "BYTES": B,
215
216 "KB": KB,
217 "KILOBYTE": KB,
218 "KILOBYTES": KB,
219
220 "MB": MB,
221 "MEGABYTE": MB,
222 "MEGABYTES": MB,
223
224 "GB": GB,
225 "GIGABYTE": GB,
226 "GIGABYTES": GB,
227
228 "TB": TB,
229 "TERABYTE": TB,
230 "TERABYTES": TB,
231
232 "PB": PB,
233 "PETABYTE": PB,
234 "PETABYTES": PB,
235
236 "EB": EB,
237 "EXABYTE": EB,
238 "EXABYTES": EB,
239 }
240
241 // Inspired from https://github.com/inhies/go-bytesize
242
243 // ParseBytes parses a byte size string. A byte size string is a number followed by
244 // a unit suffix, such as "1024B" or "1 MB". Valid byte units are "B", "KB",
245 // "MB", "GB", "TB", "PB" and "EB". You can also use the long
246 // format of units, such as "kilobyte" or "kilobytes".
247 func ParseBytes(s string) (ByteSize, error) {
248 // Remove leading and trailing space
249 s = strings.TrimSpace(s)
250
251 split := make([]string, 0)
252 for i, r := range s {
253 if !unicode.IsDigit(r) {
254 // Split the string by digit and size designator, remove space
255 split = append(split, strings.TrimSpace(string(s[:i])))
256 split = append(split, strings.TrimSpace(string(s[i:])))
257 break
258 }
259 }
260
261 // Check to see if we split successfully
262 if len(split) != 2 {
263 return 0, errors.New("Unrecognized size suffix")
264 }
265
266 // Check for MB, MEGABYTE, and MEGABYTES
267 unit, ok := unitMap[strings.ToUpper(split[1])]
268 if !ok {
269 return 0, errors.New("Unrecognized size suffix " + split[1])
270
271 }
272
273 value, err := strconv.ParseFloat(split[0], 64)
274 if err != nil {
275 return 0, err
276 }
277
278 bytesize := ByteSize(value * float64(unit))
279 return bytesize, nil
280 }
281
282 // LogLevel type specifies possible log levels.
283 type LogLevel int
284
285 const (
286 // TRACE priority
287 TRACE LogLevel = iota
288 // DEBUG priority
289 DEBUG
290 // INFO priority
291 INFO
292 // NOTICE priority
293 NOTICE
294 // WARN priority
295 WARN
296 // ERROR priority
297 ERROR
298 // CRIT priority
299 CRIT
300 // ALERT priority
301 ALERT
302 // FATAL priority
303 FATAL
304 )
305
306 var logLevelMap = map[string]LogLevel{
307 "TRACE": TRACE,
308 "DEBUG": DEBUG,
309 "INFO": INFO,
310 "NOTICE": NOTICE,
311 "WARN": WARN,
312 "ERROR": ERROR,
313 "CRIT": CRIT,
314 "ALERT": ALERT,
315 "FATAL": FATAL,
316 }
317
318 func (l LogLevel) String() string {
319 switch l {
320 case TRACE:
321 return "TRACE"
322 case DEBUG:
323 return "DEBUG"
324 case INFO:
325 return "INFO"
326 case NOTICE:
327 return "NOTICE"
328 case WARN:
329 return "WARN"
330 case ERROR:
331 return "ERROR"
332 case CRIT:
333 return "CRIT"
334 case ALERT:
335 return "ALERT"
336 case FATAL:
337 return "FATAL"
338 }
339 return "NOTSET"
340 }
341
342 // Personality allows to set the architecture for the container.
343 type Personality int64
344
345 const (
346 // X86 - Intel 32bit
347 X86 Personality = 0x0008
348
349 // X86_64 - Intel 64bit
350 X86_64 = 0x0000
351 )
352
353 const (
354 // MIGRATE_PRE_DUMP - pre-dump live migration phase
355 MIGRATE_PRE_DUMP = 0
356
357 // MIGRATE_DUMP - main live migration phase
358 MIGRATE_DUMP = 1
359
360 // MIGRATE_RESTORE - post migration phase
361 MIGRATE_RESTORE = 2
362
363 // MIGRATE_FEATURE_CHECK - migration feature check
364 MIGRATE_FEATURE_CHECK = 3
365 )
366
367 // CriuFeatures represents a set of CRIU features
368 type CriuFeatures uint64
369
370 const (
371 // FEATURE_MEM_TRACK - memory tracking support
372 FEATURE_MEM_TRACK CriuFeatures = 1 << iota
373
374 // FEATURE_LAZY_PAGES - lazy pages support
375 FEATURE_LAZY_PAGES
376 )