/
server.go
204 lines (176 loc) · 5.73 KB
/
server.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2018-2023, CS Systemes d'Information, http://csgroup.eu
*
* 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 nfs
import (
"context"
"github.com/CS-SI/SafeScale/v22/lib/backend/iaas"
sshapi "github.com/CS-SI/SafeScale/v22/lib/system/ssh/api"
"github.com/CS-SI/SafeScale/v22/lib/utils/fail"
"github.com/CS-SI/SafeScale/v22/lib/utils/retry"
"github.com/CS-SI/SafeScale/v22/lib/utils/temporal"
)
// Server getServer structure
type Server struct {
svc iaas.Service
SSHConfig sshapi.Connector
}
// NewServer instantiates a new nfs.getServer struct
func NewServer(svc iaas.Service, sshconfig sshapi.Connector) (srv *Server, err fail.Error) {
if sshconfig == nil {
return nil, fail.InvalidParameterError("sshconfig", "cannot be nil")
}
server := Server{
svc: svc,
SSHConfig: sshconfig,
}
return &server, nil
}
// Install installs and configure NFS service on the remote host
func (s *Server) Install(ctx context.Context) fail.Error {
if ctx == nil {
return fail.InvalidParameterCannotBeNilError("ctx")
}
timings, xerr := s.svc.Timings()
if xerr != nil {
return xerr
}
stdout, xerr := executeScript(ctx, timings, s.SSHConfig, "nfs_server_install.sh", map[string]interface{}{})
if xerr != nil {
xerr.Annotate("stdout", stdout)
return fail.Wrap(xerr, "error executing script to install nfs server")
}
return nil
}
// AddShare configures a local path to be exported by NFS
func (s *Server) AddShare(
ctx context.Context, path string,
options string, /*securityModes []string, readOnly, rootSquash, secure, async, noHide, crossMount, subtreeCheck bool*/
) fail.Error {
// FIXME: validate parameters
share, xerr := NewShare(s, path, options)
if xerr != nil {
return fail.Wrap(xerr, "failed to create the share")
}
// acl := ExportACL{
// IPAddress: "*",
// SecurityModes: []securityflavor.Enum{},
// Options: ExportOptions{
// ReadOnly: readOnly,
// NoRootSquash: !rootSquash,
// Secure: secure,
// Async: async,
// NoHide: noHide,
// CrossMount: crossMount,
// NoSubtreeCheck: !subtreeCheck,
// SetFSID: false,
// AnonUID: 0,
// AnonGID: 0,
// },
// }
// for _, securityMode := range securityModes {
// switch securityMode {
// case "sys":
// acl.SecurityModes = append(acl.SecurityModes, securityflavor.Sys)
// case "krb5":
// acl.SecurityModes = append(acl.SecurityModes, securityflavor.Krb5)
// case "krb5i":
// acl.SecurityModes = append(acl.SecurityModes, securityflavor.Krb5i)
// case "krb5p":
// acl.SecurityModes = append(acl.SecurityModes, securityflavor.Krb5p)
// default:
// return fmt.Errorf("cannot add the share, %s is not a valid security mode", securityMode)
// }
// }
// share.AddACL(acl)
return share.Add(ctx, s.svc)
}
// RemoveShare stops export of a local mount point by NFS on the remote server
func (s *Server) RemoveShare(ctx context.Context, path string) fail.Error {
timings, xerr := s.svc.Timings()
if xerr != nil {
return xerr
}
data := map[string]interface{}{
"Path": path,
}
stdout, xerr := executeScript(ctx, timings, s.SSHConfig, "nfs_server_path_unexport.sh", data)
if xerr != nil {
xerr.Annotate("stdout", stdout)
return fail.Wrap(xerr, "error executing script to unexport a shared directory")
}
return nil
}
// MountBlockDevice mounts a block device in the remote system
func (s *Server) MountBlockDevice(ctx context.Context, deviceName, mountPoint, format string, doNotFormat bool) (string, fail.Error) {
data := map[string]interface{}{
"Device": deviceName,
"MountPoint": mountPoint,
"FileSystem": format,
"DoNotFormat": doNotFormat,
}
timings, xerr := s.svc.Timings()
if xerr != nil {
return "", xerr
}
var stdout string
// FIXME: Add a retry here only if we catch an execution error or a connection error
rerr := retry.WhileUnsuccessfulWithLimitedRetries(func() error {
select {
case <-ctx.Done():
return retry.StopRetryError(ctx.Err())
default:
}
istdout, xerr := executeScript(ctx, timings, s.SSHConfig, "block_device_mount.sh", data)
if xerr != nil {
xerr.Annotate("stdout", istdout)
return xerr
}
stdout = istdout
return nil // we are done, break the retry
}, temporal.MinDelay(), 0, 4) // 4 retries and that's it
if rerr != nil {
return "", fail.Wrap(rerr, "error executing script to mount block device")
}
return stdout, nil
}
// UnmountBlockDevice unmounts a local block device on the remote system
func (s *Server) UnmountBlockDevice(ctx context.Context, volumeUUID string) fail.Error {
timings, xerr := s.svc.Timings()
if xerr != nil {
return xerr
}
data := map[string]interface{}{
"UUID": volumeUUID,
}
// FIXME: Add a retry here only if we catch an ExecutionError or a connection error
rerr := retry.WhileUnsuccessfulWithLimitedRetries(func() error {
select {
case <-ctx.Done():
return retry.StopRetryError(ctx.Err())
default:
}
stdout, xerr := executeScript(ctx, timings, s.SSHConfig, "block_device_unmount.sh", data)
if xerr != nil {
xerr.Annotate("stdout", stdout)
return xerr
}
return nil
}, temporal.MinDelay(), 0, 4) // 4 retries and that's it
if rerr != nil {
return fail.Wrap(rerr, "error executing script to unmount block device")
}
return nil
}