Skip to content

Commit 6374d5a

Browse files
feat: create internal package (#20)
1 parent cb650f6 commit 6374d5a

File tree

4 files changed

+41
-157
lines changed

4 files changed

+41
-157
lines changed
Lines changed: 9 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package csvapp
1+
package cvm
22

33
/*
44
#include <unistd.h>
@@ -7,11 +7,10 @@ package csvapp
77
*/
88
import "C"
99
import (
10+
"apploader/internal/secret"
1011
"apploader/pkg/conversion"
1112
"apploader/pkg/file"
12-
"apploader/secret_server"
1313
"fmt"
14-
"io/ioutil"
1514
"log"
1615
"os"
1716
"os/exec"
@@ -36,42 +35,13 @@ const (
3635
SUPERVISOR_PATH = "/workplace/supervisord/apploader"
3736
)
3837

39-
type CsvApp struct {
40-
Kind string `yaml:"kind"`
41-
AppInfo []*TaskInfo `yaml:"app"`
42-
CsvAssistants []*TaskInfo `yaml:"csvAssistants"`
43-
}
44-
45-
type TaskInfo struct {
46-
TLSInfo *TLSInfo `yaml:"tls"`
47-
Name string `yaml:"name"`
48-
Type string `yaml:"type"`
49-
Entrypoint string `yaml:"entrypoint"`
50-
Env interface{} `yaml:"env"`
51-
Priority int `yaml:"-"`
52-
Args []string `yaml:"args"`
53-
}
54-
55-
type SupervisorConf struct {
56-
Name string
57-
Command string
58-
Workplace string
59-
Environment string
60-
Priority int
61-
}
62-
63-
type TLSInfo struct {
64-
CertPath string `yaml:"certPath"`
65-
CommonName string `yaml:"commonName"`
66-
}
67-
6838
func DoJob(ca *TaskInfo) error {
6939
if ca.Type != JOB {
7040
return fmt.Errorf("this task is not a job")
7141
}
7242

7343
envs := make([]string, 0)
74-
for k, v := range secret_server.Secret {
44+
for k, v := range secret.Secret {
7545
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
7646
}
7747

@@ -86,43 +56,6 @@ func DoJob(ca *TaskInfo) error {
8656
}
8757
}
8858

89-
if ca.TLSInfo != nil {
90-
encryptedDir := "/workplace/encryptedData"
91-
log.Printf("[warning] use PKI, please make sure %s is an encrypted disk", encryptedDir)
92-
caPath := path.Join(encryptedDir, "pki")
93-
if !file.Exists(path.Join(caPath, "existed")) {
94-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "ca", "-a", caPath, "-c", "/workplace/csv-agent/csvassistants/pkitool/conf/cert-conf")
95-
if err != nil {
96-
return fmt.Errorf("init pki ca failed, error: %s", err.Error())
97-
}
98-
err = ioutil.WriteFile(path.Join(caPath, "existed"), []byte("existed"), os.ModePerm)
99-
if err != nil {
100-
os.RemoveAll(caPath)
101-
return fmt.Errorf("mark ca existed faield, error: %s", err.Error())
102-
}
103-
}
104-
if !file.Exists(path.Join(ca.TLSInfo.CertPath, "existed")) {
105-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "server", "-a", caPath, "-o", ca.TLSInfo.CertPath, "-n", ca.TLSInfo.CommonName)
106-
if err != nil {
107-
return fmt.Errorf("create tls cert failed, error: %s\n", err.Error())
108-
}
109-
110-
err = ioutil.WriteFile(path.Join(ca.TLSInfo.CertPath, "existed"), []byte("existed"), os.ModePerm)
111-
if err != nil {
112-
os.RemoveAll(ca.TLSInfo.CertPath)
113-
return fmt.Errorf("mark ca existed faield, error: %s", err.Error())
114-
}
115-
}
116-
117-
caString, err := ioutil.ReadFile(path.Join(caPath, "ca.crt"))
118-
if err != nil {
119-
return fmt.Errorf("read ca.crt failed, error: %s", err.Error())
120-
}
121-
log.Println("the ca.crt is as follow")
122-
fmt.Printf("%s", caString)
123-
log.Println("*********************************")
124-
}
125-
12659
log.Printf("entrypoint is %s", ca.Entrypoint)
12760
return RunCommand(ca.Entrypoint, envs, ca.Args...)
12861

@@ -189,36 +122,7 @@ func ExecvDockerApp(ca *TaskInfo) {
189122
log.Fatalf("task is not a docker app")
190123
}
191124

192-
if ca.TLSInfo != nil {
193-
encryptedDir := "/workplace/encryptedData"
194-
log.Printf("[warning] use PKI, please make sure %s is an encrypted disk", encryptedDir)
195-
caPath := path.Join(encryptedDir, "pki")
196-
if !file.Exists(path.Join(caPath, "existed")) {
197-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "ca", "-a", caPath, "-c", "/workplace/csv-agent/csvassistants/pkitool/conf/cert-conf")
198-
if err != nil {
199-
log.Fatalf("init pki ca failed, error: %s", err.Error())
200-
}
201-
err = ioutil.WriteFile(path.Join(caPath, "existed"), []byte("existed"), os.ModePerm)
202-
if err != nil {
203-
os.RemoveAll(caPath)
204-
log.Fatalf("mark ca existed faield, error: %s", err.Error())
205-
}
206-
}
207-
if !file.Exists(path.Join(ca.TLSInfo.CertPath, "existed")) {
208-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "server", "-a", caPath, "-o", ca.TLSInfo.CertPath, "-n", ca.TLSInfo.CommonName)
209-
if err != nil {
210-
log.Fatalf("create tls cert failed, error: %s\n", err.Error())
211-
}
212-
213-
err = ioutil.WriteFile(path.Join(ca.TLSInfo.CertPath, "existed"), []byte("existed"), os.ModePerm)
214-
if err != nil {
215-
os.RemoveAll(ca.TLSInfo.CertPath)
216-
log.Fatalf("mark ca existed faield, error: %s", err.Error())
217-
}
218-
}
219-
}
220-
221-
for k, v := range secret_server.Secret {
125+
for k, v := range secret.Secret {
222126
err := os.Setenv(k, v)
223127
if err != nil {
224128
log.Fatalf("set secret env failed, error: %s\n", err.Error())
@@ -246,42 +150,6 @@ func CreateSevers(ca *TaskInfo) error {
246150
if ca.Type != SERVER {
247151
return fmt.Errorf("task is not a server")
248152
}
249-
if ca.TLSInfo != nil {
250-
encryptedDir := "/workplace/encryptedData"
251-
log.Printf("[warning] use PKI, please make sure %s is an encrypted disk", encryptedDir)
252-
caPath := path.Join(encryptedDir, "pki")
253-
if !file.Exists(path.Join(caPath, "existed")) {
254-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "ca", "-a", caPath, "-c", "/workplace/csv-agent/csvassistants/pkitool/conf/cert-conf")
255-
if err != nil {
256-
return fmt.Errorf("init pki ca failed, error: %s", err.Error())
257-
}
258-
err = ioutil.WriteFile(path.Join(caPath, "existed"), []byte("existed"), os.ModePerm)
259-
if err != nil {
260-
os.RemoveAll(caPath)
261-
return fmt.Errorf("mark ca existed faield, error: %s", err.Error())
262-
}
263-
}
264-
if !file.Exists(path.Join(ca.TLSInfo.CertPath, "existed")) {
265-
err := RunCommand("/workplace/csv-agent/csvassistants/pkitool/pkitool", nil, "cert", "-m", "server", "-a", caPath, "-o", ca.TLSInfo.CertPath, "-n", ca.TLSInfo.CommonName)
266-
if err != nil {
267-
return fmt.Errorf("create tls cert failed, error: %s\n", err.Error())
268-
}
269-
270-
err = ioutil.WriteFile(path.Join(ca.TLSInfo.CertPath, "existed"), []byte("existed"), os.ModePerm)
271-
if err != nil {
272-
os.RemoveAll(ca.TLSInfo.CertPath)
273-
return fmt.Errorf("mark ca existed faield, error: %s", err.Error())
274-
}
275-
}
276-
277-
caString, err := ioutil.ReadFile(path.Join(caPath, "ca.crt"))
278-
if err != nil {
279-
return fmt.Errorf("read ca.crt failed, error: %s", err.Error())
280-
}
281-
log.Println("the ca.crt is as follow")
282-
fmt.Printf("%s", caString)
283-
log.Println("*********************************")
284-
}
285153

286154
envs := make([]string, 0)
287155

@@ -332,22 +200,22 @@ func CreateSevers(ca *TaskInfo) error {
332200
}
333201

334202
func Start() {
335-
appfile, err := ioutil.ReadFile("conf/app.yml")
203+
appfile, err := os.ReadFile("conf/app.yml")
336204
if err != nil {
337205
log.Fatalf("read app.yml failed, error: %s\n", err.Error())
338206
}
339-
csvApp := new(CsvApp)
340-
err = yaml.Unmarshal(appfile, &csvApp)
207+
cvmApp := new(CvmApp)
208+
err = yaml.Unmarshal(appfile, &cvmApp)
341209
if err != nil {
342210
log.Fatalf("unmarshal app.yml failed, error: %s\n", err.Error())
343211
}
344212
time.Sleep(5 * time.Second)
345213

346214
log.Println("do all the job over")
347215

348-
startTask(csvApp.CsvAssistants)
216+
startTask(cvmApp.CvmAssistants)
349217

350-
startTask(csvApp.AppInfo)
218+
startTask(cvmApp.AppInfo)
351219
}
352220

353221
func startTask(tasks []*TaskInfo) {

apploader/internal/cvm/types.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cvm
2+
3+
// CvmApp is the main application configuration
4+
type CvmApp struct {
5+
Kind string `yaml:"kind"`
6+
AppInfo []*TaskInfo `yaml:"app"`
7+
CvmAssistants []*TaskInfo `yaml:"csvAssistants"`
8+
}
9+
10+
// TaskInfo is the information of a task
11+
type TaskInfo struct {
12+
Name string `yaml:"name"`
13+
Type string `yaml:"type"`
14+
Entrypoint string `yaml:"entrypoint"`
15+
Env interface{} `yaml:"env"`
16+
Priority int `yaml:"-"`
17+
Args []string `yaml:"args"`
18+
}
19+
20+
// SupervisorConf is the configuration of a supervisor
21+
type SupervisorConf struct {
22+
Name string
23+
Command string
24+
Workplace string
25+
Environment string
26+
Priority int
27+
}
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package secret_server
1+
package secret
22

33
import (
44
"log"
@@ -29,17 +29,6 @@ func StartSecretServer() {
2929
})
3030
})
3131

32-
//r.GET("/secret", func(c *gin.Context) {
33-
// k := c.PostForm("key")
34-
//
35-
// v := Secret[k]
36-
// fmt.Println(v)
37-
// c.JSON(http.StatusOK, gin.H{
38-
// "code": 200,
39-
// "message": "update secret successful",
40-
// "value":v,
41-
// })
42-
//})
4332
log.Printf("secret server start successful")
4433
r.Run(":9090") // listen and serve on 0.0.0.0:8080
4534
}

apploader/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package main
22

33
import (
4-
csvapp "apploader/csv-app"
5-
"apploader/secret_server"
4+
"apploader/internal/cvm"
5+
"apploader/internal/secret"
66
"log"
77
"os"
88
)
99

1010
func main() {
1111
log.SetFlags(log.Lshortfile | log.LstdFlags)
12-
go secret_server.StartSecretServer()
13-
csvapp.Start()
12+
go secret.StartSecretServer()
13+
cvm.Start()
1414
os.Exit(0)
1515
}

0 commit comments

Comments
 (0)