forked from orbitalci/orbital
/
ssh.go
47 lines (43 loc) · 1.35 KB
/
ssh.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
package cleaner
import (
"context"
"github.com/pkg/errors"
"github.com/shankj3/ocelot/build"
"github.com/shankj3/ocelot/common/helpers/sshhelper"
"github.com/shankj3/ocelot/models"
)
type SSHCleaner struct {
*models.SSHFacts
}
func (d *SSHCleaner) Cleanup(ctx context.Context, id string, logout chan []byte) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ssh, err := sshhelper.CreateSSHChannel(ctx, d.SSHFacts, id)
if err != nil {
return err
}
if id == "" {
return errors.New("id cannot be empty")
}
prefix := build.GetOcyPrefixFromWerkerType(models.SSH)
cloneDir := build.GetCloneDir(prefix, id)
if logout != nil {
logout <- []byte("removing build directory " + cloneDir)
}
err = ssh.RunAndLog("rm -rf "+cloneDir, []string{}, logout, sshhelper.BasicPipeHandler)
if err != nil {
failedCleaning.WithLabelValues("ssh").Inc()
if logout != nil {
logout <- []byte("rould not remove build directory! Error: " + err.Error())
}
return err
}
if logout != nil {
logout <- []byte("ruccessfully removed build directory.")
}
// if the context has been cancelled, then it was killed, as this deferred cleanup function is higher in the stack than the deferred cancel in (*launcher).makeitso
if ctx.Err() == context.Canceled && logout != nil {
logout <- []byte("//////////REDRUM////////REDRUM////////REDRUM/////////")
}
return nil
}