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 604
Expand file tree
/
Copy pathinit.go
More file actions
201 lines (158 loc) · 3.97 KB
/
init.go
File metadata and controls
201 lines (158 loc) · 3.97 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
kstack "koding/kites/kloud/stack"
"koding/kites/kloud/utils/object"
"koding/klientctl/config"
"koding/klientctl/endpoint/credential"
"koding/klientctl/endpoint/kloud"
"koding/klientctl/endpoint/stack"
"koding/klientctl/endpoint/team"
"koding/klientctl/helper"
"github.com/codegangsta/cli"
"github.com/koding/logging"
yaml "gopkg.in/yaml.v2"
)
func Init(c *cli.Context, log logging.Logger, _ string) (int, error) {
if err := isLocked(); err != nil {
return 1, err
}
if _, err := os.Stat(config.Konfig.Template.File); os.IsNotExist(err) {
fmt.Printf("Initializing with new %s template file...\n\n", config.Konfig.Template.File)
if err := templateInit(config.Konfig.Template.File, false, ""); err != nil {
return 1, err
}
}
p, err := readTemplate(config.Konfig.Template.File)
if err != nil {
return 1, err
}
provider, err := kstack.ReadProvider(p)
if err != nil {
return 1, errors.New("failed to read cloud provider: " + err.Error())
}
ident := credential.Used()[provider]
switch {
case ident == "":
opts := &credential.ListOptions{
Provider: provider,
Team: team.Used().Name,
}
c, err := credential.List(opts)
if err != nil {
log.Debug("credential.List failure: %s", err)
break
}
creds, ok := c[provider]
if !ok || len(creds) == 0 {
fmt.Printf("Creating new credential for %q provider...\n\n", strings.Title(provider))
opts := &credential.CreateOptions{
Provider: provider,
Team: team.Used().Name,
}
if err := credentialCreate("", opts, false); err != nil {
return 1, err
}
break
}
for i, cred := range creds {
fmt.Printf("[%d] %s\n", i+1, cred.Title)
}
s, err := helper.Ask("\nChoose credential to use [1]: ")
if err != nil {
return 1, err
}
if s == "" {
s = "1"
}
n, err := strconv.Atoi(s)
if err != nil {
return 1, fmt.Errorf("unrecognized credential chosen: %s", s)
}
if n--; n < 0 || n >= len(creds) {
return 1, fmt.Errorf("invalid credential chosen: %d", n)
}
ident = creds[n].Identifier
credential.Use(ident)
}
if ident == "" {
ident = credential.Used()[provider]
}
fmt.Printf("Creating new stack...\n\n")
defTitle := strings.Title(fmt.Sprintf("%s %s Stack", kstack.Pokemon(), provider))
title, err := helper.Ask("Stack name [%s]: ", defTitle)
if err != nil {
return 1, err
}
if title == "" {
title = defTitle
}
opts := &stack.CreateOptions{
Team: team.Used().Name,
Title: title,
Credentials: []string{ident},
Template: p,
}
resp, err := stack.Create(opts)
if err != nil {
return 1, err
}
fmt.Fprintf(os.Stderr, "\nCreatad %q stack with %s ID.\nWaiting for the stack to finish building...\n\n", resp.Title, resp.StackID)
for e := range kloud.Wait(resp.EventID) {
if e.Error != nil {
return 1, fmt.Errorf("\nBuilding %q stack failed:\n%s\n", resp.Title, e.Error)
}
fmt.Printf("[%d%%] %s\n", e.Event.Percentage, e.Event.Message)
}
if err := writelock(resp.StackID, resp.Title); err != nil {
return 1, err
}
return 0, nil
}
type lock struct {
Stack struct {
ID string `yaml:"id"`
Title string `yaml:"title"`
} `yaml:"stack"`
}
func isLocked() error {
p, err := ioutil.ReadFile(".kd.lock")
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
var l lock
if err := yaml.Unmarshal(p, &l); err != nil {
return err
}
return fmt.Errorf("Project already initialized with %q stack (%s)", l.Stack.Title, l.Stack.ID)
}
func writelock(id, title string) error {
var l lock
l.Stack.ID = id
l.Stack.Title = title
p, err := yaml.Marshal(&l)
if err != nil {
return err
}
return ioutil.WriteFile(".kd.lock", p, 0644)
}
func readTemplate(file string) ([]byte, error) {
p, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
var m map[string]interface{}
if err := yaml.Unmarshal(p, &m); err != nil {
return nil, err
}
return json.Marshal(object.FixYAML(m))
}