forked from megamsys/vertice
/
iaas.go
244 lines (208 loc) · 5.38 KB
/
iaas.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
242
243
244
/*
** Copyright [2013-2015] [Megam Systems]
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package iaas
import (
log "code.google.com/p/log4go"
"fmt"
"github.com/megamsys/libgo/db"
"github.com/megamsys/megamd/global"
"github.com/tsuru/config"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"errors"
)
// Every Tsuru IaaS must implement this interface.
type IaaS interface {
// Called when tsuru is creating a Machine.
CreateMachine(*global.PredefClouds, *global.AssemblyWithComponents, string) (string, error)
// Called when tsuru is destroying a Machine.
DeleteMachine(*global.PredefClouds, *global.AssemblyWithComponents) (string, error)
}
const defaultYAMLPath = "conf/commands.yaml"
type Attributes struct {
RiakHost string `json:"riak_host"`
AccountID string `json:"accounts_id"`
AssemblyID string `json:"assembly_id"`
RabbitMQ string `json:"rabbitmq_host"`
MonitorHost string `json:"monitor_host"`
KibanaHost string `json:"kibana_host"`
EtcdHost string `json:"etcd_host"`
}
type Plugins struct {
Tool string
Command *Commands
}
type Commands struct {
Create string
Delete string
List string
Data string
}
//type SshObject struct{
// Data string
/// }
var iaasProviders = make(map[string]IaaS)
func RegisterIaasProvider(name string, iaas IaaS) {
iaasProviders[name] = iaas
}
func GetIaasProvider(name string) (IaaS, *global.PredefClouds, error) {
pdc := &global.PredefClouds{}
err := errors.New("")
pdc_type := ""
if name == "megam" {
pdc_type = name
} else {
pdc, err = getProviderName(name)
if err != nil {
return nil, nil, fmt.Errorf("Error: Riak didn't cooperate:\n%s.", err)
}
}
provider, ok := iaasProviders[pdc_type]
if !ok {
return nil, nil, fmt.Errorf("IaaS provider not registered")
}
return provider, pdc, nil
//return nil
}
func getProviderName(host string) (*global.PredefClouds, error) {
pdc := &global.PredefClouds{}
predefBucket, perr := config.GetString("riak:predefclouds")
if perr != nil {
return pdc, perr
}
conn, err := db.Conn(predefBucket)
if err != nil {
return pdc, err
}
ferr := conn.FetchStruct(host, pdc)
if ferr != nil {
return pdc, ferr
}
sshkeyerr := downloadSshFiles(pdc, "key", 0600)
if sshkeyerr != nil {
return pdc, sshkeyerr
}
sshpuberr := downloadSshFiles(pdc, "pub", 0644)
if sshpuberr != nil {
return pdc, sshpuberr
}
return pdc, nil
}
func GetPlugins(cloud string) *Plugins {
p, _ := filepath.Abs(defaultYAMLPath)
log.Info(fmt.Errorf("Conf: %s", p))
data, err := ioutil.ReadFile(p)
if err != nil {
log.Info("error: %v", err)
}
m := make(map[interface{}]Plugins)
err = yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Info("error: %v", err)
}
for key, value := range m {
if key == cloud {
return &value
}
}
return &Plugins{}
}
func GetIdentityFileLocation(file string) (string, error) {
s := make([]string, 2)
s = strings.Split(file, "_")
email, name := s[0], s[1]
cloudkeysBucket, err := config.GetString("riak:cloud_keys")
if err != nil {
return "", err
}
megam_home, err := config.GetString("megam_home")
if err != nil {
return "", err
}
return megam_home + cloudkeysBucket + "/" + email + "/" + name, nil
}
type SshFile struct {
data string
}
func downloadSshFiles(pdc *global.PredefClouds, keyvalue string, permission os.FileMode) error {
sa := make([]string, 2)
sa = strings.Split(pdc.Access.IdentityFile, "_")
email, name := sa[0], sa[1]
ssh := &db.SshObject{}
sshBucket, serr := config.GetString("riak:ssh_files")
if serr != nil {
return serr
}
conn, err := db.Conn(sshBucket)
if err != nil {
return err
}
ferr := conn.FetchObject(pdc.Access.IdentityFile+"_"+keyvalue, ssh)
if ferr != nil {
return ferr
}
cloudkeysBucket, ckberr := config.GetString("riak:cloud_keys")
if ckberr != nil {
return ckberr
}
megam_home, ckberr := config.GetString("megam_home")
if ckberr != nil {
return ckberr
}
basePath := megam_home + cloudkeysBucket
dir := path.Join(basePath, email)
filePath := path.Join(dir, name+"."+keyvalue)
if _, err := os.Stat(dir); os.IsNotExist(err) {
fmt.Printf("no such file or directory: %s", dir)
if errm := os.MkdirAll(dir, 0777); errm != nil {
return errm
}
// open output file
_, err := os.Create(filePath)
if err != nil {
return err
}
}
errf := ioutil.WriteFile(filePath, []byte(ssh.Data), permission)
if errf != nil {
return errf
}
return nil
}
type AccessKeys struct {
AccessKey string `json:"-A"`
SecretKey string `json:"-K"`
}
func GetAccessKeys(pdc *global.PredefClouds) (*AccessKeys, error) {
keys := &AccessKeys{}
cakbBucket, cakberr := config.GetString("riak:cloud_access_keys")
if cakberr != nil {
return keys, cakberr
}
conn, err := db.Conn(cakbBucket)
if err != nil {
return keys, err
}
ferr := conn.FetchStruct(pdc.Access.VaultLocation, keys)
if ferr != nil {
return keys, ferr
}
return keys, nil
}