forked from sorintlab/stolon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
115 lines (97 loc) · 2.6 KB
/
common.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
// Copyright 2015 Sorint.lab
//
// 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 common
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"reflect"
"strings"
"github.com/satori/go.uuid"
)
const (
StorePrefix = "stolon/cluster"
SentinelLeaderKey = "sentinel-leader"
)
const PgUnixSocketDirectories = "/tmp"
type Role string
const (
RoleUndefined Role = "undefined"
RoleMaster Role = "master"
RoleStandby Role = "standby"
)
func UID() string {
u := uuid.NewV4()
return fmt.Sprintf("%x", u[:4])
}
func UUID() string {
return uuid.NewV4().String()
}
const (
stolonPrefix = "stolon_"
)
func StolonName(name string) string {
return stolonPrefix + name
}
func NameFromStolonName(stolonName string) string {
return strings.TrimPrefix(stolonName, stolonPrefix)
}
func IsStolonName(name string) bool {
return strings.HasPrefix(name, stolonPrefix)
}
type Parameters map[string]string
func (s Parameters) Equals(is Parameters) bool {
return reflect.DeepEqual(s, is)
}
// WriteFileAtomicFunc atomically writes a file, it achieves this by creating a
// temporary file and then moving it. writeFunc is the func that will write
// data to the file.
// This function is taken from
// https://github.com/youtube/vitess/blob/master/go/ioutil2/ioutil.go
// Copyright 2012, Google Inc. BSD-license, see licenses/LICENSE-BSD-3-Clause
func WriteFileAtomicFunc(filename string, perm os.FileMode, writeFunc func(f io.Writer) error) error {
dir, name := path.Split(filename)
f, err := ioutil.TempFile(dir, name)
if err != nil {
return err
}
err = writeFunc(f)
if err == nil {
err = f.Sync()
}
if closeErr := f.Close(); err == nil {
err = closeErr
}
if permErr := os.Chmod(f.Name(), perm); err == nil {
err = permErr
}
if err == nil {
err = os.Rename(f.Name(), filename)
}
// Any err should result in full cleanup.
if err != nil {
os.Remove(f.Name())
}
return err
}
// WriteFileAtomic atomically writes a file
func WriteFileAtomic(filename string, perm os.FileMode, data []byte) error {
return WriteFileAtomicFunc(filename, perm,
func(f io.Writer) error {
_, err := f.Write(data)
return err
})
}