-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
78 lines (70 loc) · 1.35 KB
/
server.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
package resource
import (
"html/template"
"net"
"os"
"path/filepath"
)
const (
aws_default_image_id = "ami-01748a72bed07727c"
aws_default_size = "t2.micro"
)
type Server struct {
Name string
Image *Image
Size string
Subnet *Subnet
Ports []int
KeyPair *KeyPair
PrivateIp *net.IP
ImageBuilder ImageBuilder
}
func newServer(name, size string, subnet *Subnet, key *KeyPair, ports []int) (*Server, error) {
s := &Server{
Name: name,
Size: size,
Subnet: subnet,
Ports: ports,
KeyPair: key,
PrivateIp: nil,
}
if size == "" {
s.Size = aws_default_size
} else {
s.Size = size
}
return s, nil
}
func (s *Server) SetImage(image *Image) {
s.Image = image
}
func (s *Server) SetImageBuilder(imageBuilder *ImageBuilder) {
s.ImageBuilder = *imageBuilder
}
func (s *Server) BuildTemplate(workDir string) error {
fileName := "kakoi-" + s.Name + ".tf"
file, err := os.Create(filepath.Join(workDir, fileName))
if err != nil {
return err
}
defer file.Close()
t, err := template.New("server.tf.tmpl").ParseFiles("/etc/kakoi/templates/aws/server.tf.tmpl")
if err != nil {
return err
}
return t.Execute(file, s)
}
type Image struct {
Id string
Path string
}
func NewImage(id, path string) *Image {
image := &Image{}
if id == "" {
image.Id = aws_default_image_id
} else {
image.Id = id
}
image.Path = path
return image
}