forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_etc_hosts.go
158 lines (142 loc) · 3.45 KB
/
create_etc_hosts.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
package cmd
import (
"io"
"fmt"
"io/ioutil"
"net/url"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
)
const (
optionName = "name"
)
var (
create_etc_hosts_long = templates.LongDesc(`
Creates /etc/hosts entries for all current exposed services
`)
create_etc_hosts_example = templates.Examples(`
# Creates /etc/hosts entries for all current exposed services
sudo jx create etc-hosts
`)
)
// CreateEtcHostsOptions the options for the create spring command
type CreateEtcHostsOptions struct {
CreateOptions
Name string
IP string
}
// NewCmdCreateEtcHosts creates a command object for the "create" command
func NewCmdCreateEtcHosts(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &CreateEtcHostsOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "etc-hosts kind [url]",
Short: "Creates a new git server URL",
Aliases: []string{"etchosts", "etc_hosts"},
Long: create_etc_hosts_long,
Example: create_etc_hosts_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Name, optionName, "n", "/etc/hosts", "The etc hosts file to edit")
cmd.Flags().StringVarP(&options.IP, "ip", "i", "", "The IP address of the node to point the host entries to")
return cmd
}
// Run implements the command
func (o *CreateEtcHostsOptions) Run() error {
name := o.Name
if name == "" {
return util.MissingOption(name)
}
if o.IP == "" {
// lets find a node ip
ip, err := o.getCommandOutput("", "minikube", "ip")
if err != nil {
return err
}
o.IP = ip
}
if o.IP == "" {
return fmt.Errorf("Could not discover a node IP address")
}
client, ns, err := o.KubeClient()
if err != nil {
return err
}
urls, err := kube.FindServiceURLs(client, ns)
if err != nil {
return err
}
exists, err := util.FileExists(name)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("hosts file %s does not exist!", name)
}
data, err := ioutil.ReadFile(name)
if err != nil {
return err
}
text := string(data)
lines := strings.Split(text, "\n")
idx, ipLine := o.findIPLine(&lines)
for _, u := range urls {
ipLine = o.addUrl(u, ipLine)
}
lines[idx] = ipLine
newText := strings.Join(lines, "\n")
if newText != text {
err = ioutil.WriteFile(name, []byte(newText), util.DefaultWritePermissions)
if err != nil {
return err
}
log.Infof("Updated file %s\n", util.ColorInfo(name))
}
return nil
}
func (o *CreateEtcHostsOptions) addUrl(serviceUrl kube.ServiceURL, ipLine string) string {
text := serviceUrl.URL
u, err := url.Parse(text)
if err != nil {
log.Warnf("Ignored invalid URL %s %s", text, err)
return ipLine
}
host := u.Host
fields := strings.Fields(ipLine)
for i := 1; i < len(fields); i++ {
if fields[i] == host {
return ipLine
}
}
if !strings.HasSuffix(ipLine, " ") {
ipLine += " "
}
return ipLine + host
}
func (o *CreateEtcHostsOptions) findIPLine(lines *[]string) (int, string) {
prefix := o.IP + " "
for i, line := range *lines {
if strings.HasPrefix(line, prefix) {
return i, line
}
}
idx := len(*lines) + 2
*lines = append(*lines, "", "# jx added service entries", prefix)
return idx, prefix
}