forked from docker-flow/docker-flow-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.go
More file actions
77 lines (70 loc) · 2.42 KB
/
Copy pathremove.go
File metadata and controls
77 lines (70 loc) · 2.42 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
package main
import (
"fmt"
"strings"
)
type Removable interface {
Executable
}
type Remove struct {
ConfigsPath string `short:"c" long:"configs-path" default:"/cfg" description:"The path to the configurations directory"`
ConsulAddresses []string
InstanceName string `long:"proxy-instance-name" env:"PROXY_INSTANCE_NAME" default:"docker-flow" required:"true" description:"The name of the proxy instance."`
ServiceName string `short:"s" long:"service-name" required:"true" description:"The name of the service that should be removed (e.g. my-service)."`
TemplatesPath string `short:"t" long:"templates-path" default:"/cfg/tmpl" description:"The path to the templates directory"`
Mode string
}
var remove Remove
// TODO: Change to addresses
var NewRemove = func(serviceName, configsPath, templatesPath string, consulAddresses []string, instanceName, mode string) Removable {
return &Remove{
ServiceName: serviceName,
TemplatesPath: templatesPath,
ConfigsPath: configsPath,
ConsulAddresses: consulAddresses,
InstanceName: instanceName,
Mode: mode,
}
}
func (m *Remove) Execute(args []string) error {
logPrintf("Removing %s configuration", m.ServiceName)
if err := m.removeFiles(m.TemplatesPath, m.ServiceName, m.ConsulAddresses, m.InstanceName, m.Mode); err != nil {
logPrintf(err.Error())
return err
}
if err := proxy.CreateConfigFromTemplates(m.TemplatesPath, m.ConfigsPath); err != nil {
logPrintf(err.Error())
return err
}
if err := proxy.Reload(); err != nil {
logPrintf(err.Error())
return err
}
return nil
}
func (m *Remove) removeFiles(templatesPath, serviceName string, registryAddresses []string, instanceName, mode string) error {
logPrintf("Removing the %s configuration files", serviceName)
paths := []string{
fmt.Sprintf("%s/%s-fe.cfg", templatesPath, serviceName),
fmt.Sprintf("%s/%s-be.cfg", templatesPath, serviceName),
}
mu.Lock()
defer mu.Unlock()
for _, path := range paths {
if err := osRemove(path); err != nil {
return err
}
}
if !strings.EqualFold(mode, "service") && !strings.EqualFold(mode, "swarm") {
var err error
if len(registryAddresses) > 0 {
for _, address := range registryAddresses {
if err = registryInstance.DeleteService([]string{address}, serviceName, instanceName); err == nil {
return nil
}
}
return fmt.Errorf("Could not remove the service from Consul\n%s", err.Error())
}
}
return nil
}