This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
init.go
241 lines (217 loc) · 7.44 KB
/
init.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bufio"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/helm/portforwarder"
"k8s.io/helm/pkg/tiller/environment"
"syscall"
"github.com/Azure/draft/cmd/draft/installer"
installerConfig "github.com/Azure/draft/cmd/draft/installer/config"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/draft/pack"
)
const (
initDesc = `
This command installs the server side component of Draft onto your
Kubernetes Cluster and sets up local configuration in $DRAFT_HOME (default ~/.draft/)
To set up just a local environment, use '--client-only'. That will configure
$DRAFT_HOME, but not attempt to connect to a remote cluster and install the Draft
deployment.
To dump information about the Draft chart, combine the '--dry-run' and '--debug' flags.
`
chartConfigTpl = `
basedomain: %s
registry:
url: %s
org: %s
authtoken: %s
`
)
type initCmd struct {
clientOnly bool
out io.Writer
in io.Reader
home draftpath.Home
yes bool
helmClient *helm.Client
}
func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
i := &initCmd{
out: out,
in: in,
}
cmd := &cobra.Command{
Use: "init",
Short: "initialize Draft on both client and server",
Long: initDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("This command does not accept arguments")
}
i.home = draftpath.Home(homePath())
return i.run()
},
}
f := cmd.Flags()
f.BoolVarP(&i.clientOnly, "client-only", "c", false, "install local configuration, but skip remote configuration")
f.BoolVar(&i.yes, "yes", false, "automatically accept configuration defaults (if detected). Exits non-zero if --yes is enabled and no cloud provider was found")
return cmd
}
// runInit initializes local config and installs Draft to Kubernetes Cluster
func (i *initCmd) run() error {
if err := ensureDirectories(i.home, i.out); err != nil {
return err
}
if err := ensurePacks(i.home, i.out); err != nil {
return err
}
fmt.Fprintf(i.out, "$DRAFT_HOME has been configured at %s.\n", draftHome)
if !i.clientOnly {
client, clientConfig, err := getKubeClient(kubeContext)
if err != nil {
return fmt.Errorf("Could not get a kube client: %s", err)
}
restClientConfig, err := clientConfig.ClientConfig()
if err != nil {
return fmt.Errorf("Could not retrieve client config from the kube client: %s", err)
}
tunnel, err := portforwarder.New(environment.DefaultTillerNamespace, client, restClientConfig)
if err != nil {
return fmt.Errorf("Could not get a connection to tiller: %s\nPlease ensure you have run `helm init`", err)
}
i.helmClient = helm.NewClient(helm.Host(fmt.Sprintf("localhost:%d", tunnel.Local)))
chartConfig, cloudProvider, err := installerConfig.FromClientConfig(clientConfig)
if err != nil {
return fmt.Errorf("Could not generate chart config from kube client config: %s", err)
}
if cloudProvider != "" {
fmt.Fprintf(i.out, "\nDraft detected that you are using %s as your cloud provider. AWESOME!\n", cloudProvider)
fmt.Fprintf(i.out, "Draft will be using the following configuration:\n\n'''\n%s'''\n\n", chartConfig.GetRaw())
fmt.Fprint(i.out, "Is this okay? [Y/n] ")
reader := bufio.NewReader(i.in)
text, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
text = strings.TrimSpace(text)
if text == "" || strings.ToLower(text) == "y" {
i.yes = true
}
}
if !i.yes || cloudProvider == "" {
// prompt for missing information
fmt.Fprintf(i.out, "\nIn order to install Draft, we need a bit more information...\n\n")
fmt.Fprint(i.out, "1. Enter your Docker registry URL (e.g. docker.io, quay.io, myregistry.azurecr.io): ")
reader := bufio.NewReader(i.in)
registryURL, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
registryURL = strings.TrimSpace(registryURL)
fmt.Fprint(i.out, "2. Enter your username: ")
dockerUser, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
dockerUser = strings.TrimSpace(dockerUser)
fmt.Fprint(i.out, "3. Enter your password: ")
// NOTE(bacongobbler): casting syscall.Stdin here to an int is intentional here as on
// Windows, syscall.Stdin is a Handler, which is of type uintptr.
dockerPass, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
fmt.Fprintf(i.out, "\n4. Enter your org where Draft will push images [%s]: ", dockerUser)
dockerOrg, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
dockerOrg = strings.TrimSpace(dockerOrg)
if dockerOrg == "" {
dockerOrg = dockerUser
}
fmt.Fprint(i.out, "5. Enter your top-level domain for ingress (e.g. draft.example.com): ")
basedomain, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Could not read input: %s", err)
}
basedomain = strings.TrimSpace(basedomain)
registryAuth := base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf(
`{"username":"%s","password":"%s"}`,
dockerUser,
dockerPass)))
chartConfig.Raw = fmt.Sprintf(chartConfigTpl, basedomain, registryURL, dockerOrg, registryAuth)
}
if err := installer.Install(i.helmClient, chartConfig); err != nil {
if IsReleaseAlreadyExists(err) {
fmt.Fprintln(i.out, "Warning: Draft is already installed in the cluster.\n"+
"Use --client-only to suppress this message.")
} else {
return fmt.Errorf("error installing Draft: %s", err)
}
} else {
fmt.Fprintln(i.out, "Draft has been installed into your Kubernetes Cluster.")
}
} else {
fmt.Fprintln(i.out, "Not installing Draft due to 'client-only' flag having been set")
}
fmt.Fprintln(i.out, "Happy Sailing!")
return nil
}
// ensureDirectories checks to see if $DRAFT_HOME exists
//
// If $DRAFT_HOME does not exist, this function will create it.
func ensureDirectories(home draftpath.Home, out io.Writer) error {
configDirectories := []string{
home.String(),
home.Plugins(),
home.Packs(),
}
for _, p := range configDirectories {
if fi, err := os.Stat(p); err != nil {
fmt.Fprintf(out, "Creating %s \n", p)
if err := os.MkdirAll(p, 0755); err != nil {
return fmt.Errorf("Could not create %s: %s", p, err)
}
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", p)
}
}
return nil
}
// ensurePacks checks to see if the default packs exist.
//
// If the pack does not exist, this function will create it.
func ensurePacks(home draftpath.Home, out io.Writer) error {
all, err := pack.Builtins()
if err != nil {
return err
}
for packName, files := range all {
fmt.Fprintf(out, "Creating pack %s...\n", packName)
if _, err := pack.Create(packName, home.Packs(), files); err != nil {
if err == pack.ErrPackExists {
fmt.Fprintf(out, "Pack %s already exists. Skipping!\n", packName)
} else {
return err
}
}
}
return nil
}
// IsReleaseAlreadyExists returns true if err matches the "release already exists"
// error from Helm; else returns false
func IsReleaseAlreadyExists(err error) bool {
alreadyExistsRegExp := regexp.MustCompile("a release named \"(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+\" already exists")
return alreadyExistsRegExp.MatchString(err.Error())
}