-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplications.go
143 lines (125 loc) · 5.01 KB
/
applications.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
package main
import (
"context"
"fmt"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type AppPost struct {
GithubRepoUrl string `json:"github-repo"`
GithubBranch string `json:"github-branch"`
Language string `json:"language"`
Port string `json:"port"`
Description string `json:"description,omitempty"`
Envs []Env `json:"envs,omitempty"`
}
type Application struct {
ID primitive.ObjectID `bson:"_id" json:"-"`
ContainerID string `bson:"containerID" json:"containerID,omitempty"`
Status string `bson:"status" json:"status,omitempty"`
StudentID int `bson:"studentID" json:"studentID,omitempty"`
Type string `bson:"type" json:"type,omitempty"`
Name string `bson:"name" json:"name,omitempty"`
Description string `bson:"description" json:"description,omitempty"`
GithubRepo string `bson:"githubRepo,omitemtpy" json:"githubRepo,omitempty"`
GithubBranch string `bson:"githubBranch,omitemtpy" json:"githubBranch,omitempty"`
LastCommitHash string `bson:"lastCommitHash,omitemtpy" json:"lastCommitHash,omitempty"`
Port string `bson:"port" json:"port,omitempty"`
ExternalPort string `bson:"externalPort" json:"externalPort,omitempty"`
Lang string `bson:"lang" json:"lang,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt,omitempty"`
IsPublic bool `bson:"isPublic" json:"isPublic"`
IsUpdatable bool `bson:"isUpdatable,omitempty" json:"isUpdatable"`
Img string `bson:"img,omitempty" json:"img,omitempty"`
Envs []Env `bson:"envs,omitempty" json:"envs,omitempty"`
Tags []string `bson:"tags,omitempty" json:"tags,omitempty"`
Stars []string `bson:"stars,omitempty" json:"stars,omitempty"`
}
type Env struct {
Key string `bson:"key" json:"key"`
Value string `bson:"value" json:"value"`
}
// CreateNewApplicationFromRepo creates a container from an image which is the one created from a student's repository
func (c ContainerController) CreateNewApplicationFromRepo(creatorID int, port, name, language, imageName string) (string, error) {
//generic configs for the container
containerConfig := &container.Config{
Image: imageName,
}
// externalPort, err := getFreePort()
// if err != nil {
// return "", err
// }
//host bindings config, hostPort is not set cause the engine will assign a dinamyc one
hostBinding := nat.PortBinding{
HostIP: "0.0.0.0",
//HostPort is the port that the host will listen to, since it's not set
//the docker engine will assign a random open port
// HostPort: strconv.Itoa(externalPort),
}
//set the port for the container (internal one)
containerPort, err := nat.NewPort("tcp", port)
if err != nil {
return "", err
}
//set a slice of possible port bindings
//since it's a db container we need just one
portBinding := nat.PortMap{containerPort: []nat.PortBinding{hostBinding}}
//set the configuration of the host
//set the port bindings and the restart policy
//!choose a restart policy
hostConfig := &container.HostConfig{
PortBindings: portBinding,
// RestartPolicy: container.RestartPolicy{
// Name: "on-failure",
// MaximumRetryCount: 3,
// },
}
//create the container
containerBody, err := c.cli.ContainerCreate(c.ctx, containerConfig,
hostConfig, nil, nil, fmt.Sprintf("%d-%s-%s", creatorID, name, language))
if err != nil {
return "", err
}
return containerBody.ID, nil
}
// GetAppInfoFromContainer returns the application metadata from the container id.
// The parameter checkCommit will make the function check if the last commit changed, if so the application
// returned will have isUpdatable set to true.
func (c ContainerController) GetAppInfoFromContainer(containerId string, checkCommit bool, util *Util) (Application, error) {
db, err := connectToDB()
if err != nil {
return Application{}, err
}
defer db.Client().Disconnect(context.TODO())
var app Application
err = db.Collection("applications").FindOne(context.TODO(), bson.M{"containerID": containerId}).Decode(&app)
if err != nil {
return Application{}, err
}
if checkCommit {
app.IsUpdatable, err = util.HasLastCommitChanged(app.LastCommitHash, app.GithubRepo, app.GithubBranch)
if err != nil {
return Application{}, err
}
}
// if getEnvs {
// envs := make(map[string]string)
// results, err := db.Query("SELECT * FROM envs WHERE applicationID=?", app.ID)
// if err != nil {
// return Application{}, nil, err
// }
// for results.Next() {
// var key, value string
// err = results.Scan(&key, &value)
// if err != nil {
// return Application{}, nil, err
// }
// envs[key] = value
// }
// return app, envs, nil
// }
return app, nil
}