This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathunmountfolder.go
More file actions
161 lines (134 loc) · 4.49 KB
/
Copy pathunmountfolder.go
File metadata and controls
161 lines (134 loc) · 4.49 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
153
154
155
156
157
158
159
160
161
package remote
import (
"errors"
"fmt"
"github.com/koding/kite"
"koding/klient/remote/machine"
"koding/klient/remote/req"
)
const (
// systemUnmountFailed is the Kite Error Type used when either the unmounter
// fails, or generic (non-instanced) unmount fails.
systemUnmountFailed = "system-unmount-failed"
// mountNotFoundNoPath is returned when we were unable to find the given mount
// name, *and* the localPath is empty.
mountNotFoundNoPath = "mount-not-found-no-path"
)
// UnmountFolderHandler implements a kite handler for Remote.UnmountFolder
func (r *Remote) UnmountFolderHandler(kreq *kite.Request) (interface{}, error) {
var params req.UnmountFolder
if kreq.Args == nil {
return nil, errors.New("Required argument `name` was not passed.")
}
if err := kreq.Args.One().Unmarshal(¶ms); err != nil {
err = fmt.Errorf(
"Error '%s' while unmarshalling request '%s'.\n", err, kreq.Args.One(),
)
r.log.Error(err.Error())
return nil, err
}
return nil, r.UnmountFolder(params)
}
// UnmountFolder implements klient's remote.unmountFolder method. Unmounting
// the given local folder.
func (r *Remote) UnmountFolder(params req.UnmountFolder) (err error) {
if params.Name == "" && params.LocalPath == "" {
return errors.New("Missing required argument `name` or `localPath`.")
}
if params.Name != "" {
// Get the machine, if it exists. We only need this currently to clear
// the machine status on success, so if we find an error here, we can ignore it.
if remoteMachine, machErr := r.GetMachine(params.Name); machErr == nil {
defer func() {
// If the returned error value is nil, clear the machine status.
if err == nil {
remoteMachine.SetStatus(machine.MachineStatusUnknown, "")
}
}()
}
}
m, ok := r.mounts.FindByName(params.Name)
if !ok {
r.log.Warning(
"remote.unmountFolder: Unable to find mount instance. name:%s, localPath:%s",
params.Name, params.LocalPath,
)
// If we are unable to find the mount instance. If the localPath is also empty,
// there's nothing we can do - return an error.
//
// We're returning a custom kite-error, so that klientctl can check for this
// specific error type and return a custom UX for this specific error.
if params.LocalPath == "" {
return &kite.Error{
Type: mountNotFoundNoPath,
Message: fmt.Sprintf(
"Unable to locate the mount name %s, and localPath is empty.",
params.Name,
)}
}
// If we can't find the mount, but a localPath was supplied, unmount that.
if err := r.unmountPath(params.LocalPath); err != nil {
// We're using a custom error here, to help KD identify a possibly common
// error circumstance and report it to the user.
return newKiteErr(systemUnmountFailed, err)
}
// Mount wasn't found, but we succeeded in mounting the localPath. End of func.
return nil
}
if m.Intervaler != nil {
r.log.Info(
"remote.unmountFolder: Unsubscribing from Sync Intervaler. Ip:%s, localPath:%s",
m.IP, m.LocalPath,
)
m.Intervaler.Stop()
} else {
r.log.Warning(
"remote.unmountFolder: Unable to locate Sync Intervaler. remotePath:%s, name:%s",
m.RemotePath, m.MountName,
)
}
if m.KiteTracker != nil {
r.log.Info(
"remote.unmountFolder: Unsubscribing from kitePinger. Ip:%s, localPath:%s",
m.IP, m.LocalPath,
)
m.KiteTracker.Unsubscribe(m.PingerSub)
} else {
r.log.Warning(
"remote.unmountFolder: Unable to locate kitePinger. remotePath:%s, name:%s",
m.RemotePath, m.MountName,
)
}
if m.Log == nil {
m.Log = r.log
}
// If removeMount encounters an error, we don't want to bail immediately.
// Rather, we want to still try to unmount the folder.
removeMountErr := r.RemoveMount(m)
if removeMountErr != nil {
r.log.Error(
"remote.unmountFolder: removeMount failed. name:%s, localPath:%s, err:%s",
params.Name, params.LocalPath, removeMountErr,
)
}
if err := m.Unmount(); err != nil {
r.log.Error(
"remote.unmountFolder: Unmount failed. name:%s, localPath:%s, err:%s",
params.Name, m.LocalPath, err,
)
// Note that we're choosing to return the unmounter error here, rather than
// a possible error from removeMount.
//
// We're using a custom error here, to help KD identify a possibly common
// error circumstance and report it to the user.
return newKiteErr(systemUnmountFailed, err)
}
return removeMountErr
}
// newKiteErr creates a new kite error from the given type-string and error.
func newKiteErr(t string, err error) *kite.Error {
return &kite.Error{
Type: t,
Message: err.Error(),
}
}