-
Notifications
You must be signed in to change notification settings - Fork 67
/
storage.go
102 lines (86 loc) · 3.04 KB
/
storage.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
/*
Copyright 2014 Huawei Technologies Co., Ltd. All rights reserved.
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 storage
import (
"errors"
"fmt"
"sync"
"github.com/containerops/configure"
"github.com/Huawei/dockyard/utils"
)
// UpdateServiceStorage represents the storage interface
type UpdateServiceStorage interface {
// `url` is the database address or local directory (/tmp/cache)
// `km` is the key manager address
New(url string, km string) (UpdateServiceStorage, error)
// get the 'url' set by 'New'
String() string
Supported(url string) bool
// `protocal` is the supported protocals like 'app/v1, docker/v1'
// key: namespace/repository/appname
Get(protocal, key string) ([]byte, error)
// key: namespace/repository
GetMeta(protocal, key string) ([]byte, error)
// key: namespace/repository
GetMetaSign(protocal, key string) ([]byte, error)
// key: namespace
// We don't provide GetPrivateKeyFile since we don't save it on the storage server
GetPublicKey(protocal, key string) ([]byte, error)
// key: namespace/repository/appname
Put(protocal, key string, data []byte, method utils.EncryptMethod) (string, error)
// key: namespace/repository/appname
Delete(protocal, key string) error
// key: namespace/repository
List(protocal, key string) ([]string, error)
}
var (
usStoragesLock sync.Mutex
usStorages = make(map[string]UpdateServiceStorage)
// ErrorsUSSNotSupported occurs if a type is not supported
ErrorsUSSNotSupported = errors.New("storage type is not supported")
)
// RegisterStorage provides a way to dynamically register an implementation of a
// storage type.
//
// If RegisterStorage is called twice with the same name if 'storage type' is nil,
// or if the name is blank, it panics.
func RegisterStorage(name string, f UpdateServiceStorage) error {
if name == "" {
return errors.New("Could not register a Storage with an empty name")
}
if f == nil {
return errors.New("Could not register a nil Storage")
}
usStoragesLock.Lock()
defer usStoragesLock.Unlock()
if _, alreadyExists := usStorages[name]; alreadyExists {
return fmt.Errorf("Storage type '%s' is already registered", name)
}
usStorages[name] = f
return nil
}
// NewUpdateServiceStorage creates a storage interface by a url
func NewUpdateServiceStorage(url string, km string) (UpdateServiceStorage, error) {
if url == "" {
url = configure.GetString("updateserver.storage")
}
if km == "" {
km = configure.GetString("updateserver.keymanager")
}
for _, f := range usStorages {
if f.Supported(url) {
return f.New(url, km)
}
}
return nil, ErrorsUSSNotSupported
}