-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapplication_manager.go
182 lines (159 loc) · 4.63 KB
/
application_manager.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
package factory
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"strings"
"github.com/google/go-github/v41/github"
"github.com/sdslabs/gasper/configs"
pb "github.com/sdslabs/gasper/lib/factory/protos/application"
"github.com/sdslabs/gasper/types"
"golang.org/x/oauth2"
"google.golang.org/grpc"
)
// CreateApplication is a remote procedure call for creating an application in a worker node
func CreateApplication(language, owner, instanceURL string, data []byte) ([]byte, error) {
conn, err := grpc.Dial(
instanceURL,
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCredentials),
)
if err != nil {
return nil, err
}
defer conn.Close()
client := pb.NewApplicationFactoryClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := client.Create(ctx, &pb.RequestBody{
Language: language,
Owner: owner,
Data: data,
})
if err != nil {
return nil, err
}
return res.GetData(), nil
}
// RebuildApplication is a remote procedure call for rebuilding an application in a worker node
func RebuildApplication(name, instanceURL string) ([]byte, error) {
conn, err := grpc.Dial(
instanceURL,
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCredentials),
)
if err != nil {
return nil, err
}
defer conn.Close()
client := pb.NewApplicationFactoryClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := client.Rebuild(ctx, &pb.NameHolder{Name: name})
if err != nil {
return nil, err
}
return res.GetData(), nil
}
// DeleteApplication is a remote procedure call for deleting an application in a worker node
func DeleteApplication(name, instanceURL string) (*pb.DeletionResponse, error) {
conn, err := grpc.Dial(
instanceURL,
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCredentials),
)
if err != nil {
return nil, err
}
defer conn.Close()
client := pb.NewApplicationFactoryClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := client.Delete(ctx, &pb.NameHolder{Name: name})
if err != nil {
return nil, err
}
return res, nil
}
// FetchApplicationLogs is a remote procedure call for fetching logs of an application in a worker node
func FetchApplicationLogs(name, tail, instanceURL string) (*pb.LogResponse, error) {
conn, err := grpc.Dial(
instanceURL,
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCredentials),
)
if err != nil {
return nil, err
}
defer conn.Close()
client := pb.NewApplicationFactoryClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := client.FetchLogs(ctx, &pb.LogRequest{
Name: name,
Tail: tail,
})
if err != nil {
return nil, err
}
return res, nil
}
// NewApplicationFactory returns a new GRPC server for creating applications
func NewApplicationFactory(bindings pb.ApplicationFactoryServer) *grpc.Server {
srv := grpc.NewServer(
grpc.StreamInterceptor(streamInterceptor),
grpc.UnaryInterceptor(unaryInterceptor),
)
pb.RegisterApplicationFactoryServer(srv, bindings)
return srv
}
// CreateGithubRepository returns a git clone URL after creating a new repository
func CreateGithubRepository(repoName string) (*types.ApplicationRemote, error) {
tc := oauth2.NewClient(
context.Background(),
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: configs.GithubConfig.PAT, //PAT
},
),
)
client := github.NewClient(tc)
repo := &github.Repository{
Name: github.String(repoName),
Private: github.Bool(true),
}
repo, _, err := client.Repositories.Create(context.Background(), "", repo)
response := &types.ApplicationRemote{
GitURL: *repo.CloneURL,
}
return response, err
}
// DeleteGithubRepository deletes the specified repository
func DeleteGithubRepository(repoURL string) (bool, error) {
tc := oauth2.NewClient(
context.Background(),
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: configs.GithubConfig.PAT, //PAT
},
),
)
parts := strings.Split(repoURL, "/")
repoName := strings.TrimSuffix(parts[len(parts)-1], ".git")
client := github.NewClient(tc)
_, err := client.Repositories.Delete(context.Background(), configs.GithubConfig.Username, repoName)
if err != nil {
return false, err
}
return true, nil
}
// Encrypt encrypts the PAT using the public key
func Encrypt(key rsa.PublicKey) (string, error) {
label := []byte("OAEP Encrypted")
rng := rand.Reader
secretMessage := configs.GithubConfig.PAT
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, &key, []byte(secretMessage), label)
return base64.StdEncoding.EncodeToString(ciphertext), err
}