Skip to content

Commit 83eb302

Browse files
authored
feat: fit market api (#53)
1 parent c3d4e4c commit 83eb302

File tree

5 files changed

+78
-71
lines changed

5 files changed

+78
-71
lines changed

pkg/api/server/handlers_cmd.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (h *handlers) updateDevAppRepo(ctx *fiber.Ctx) error {
202202
})
203203
}
204204

205-
err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, false)
205+
_, err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, false)
206206
if err != nil {
207207
klog.Error("command upgraderepo error, ", err, ", ", name)
208208
return ctx.JSON(fiber.Map{
@@ -303,10 +303,10 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {
303303
"message": fmt.Sprintf("Check namespace failed: %v", err),
304304
})
305305
}
306-
306+
version := "0.0.1"
307307
if source != "cli" {
308308
klog.Info("auto update repo")
309-
err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, releaseNotExist)
309+
version, err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, releaseNotExist)
310310
if err != nil {
311311
klog.Error("command upgraderepo error, ", err, ", ", name)
312312
return ctx.JSON(fiber.Map{
@@ -316,7 +316,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {
316316
}
317317
}
318318

319-
_, err = command.Install().Run(ctx.Context(), devName, token)
319+
_, err = command.Install().Run(ctx.Context(), devName, token, version)
320320

321321
if err != nil {
322322
klog.Error("command install error, ", err, ", ", name)
@@ -648,12 +648,9 @@ func (h *handlers) uninstall(ctx *fiber.Ctx) error {
648648
klog.Errorf("update dev app state to undeploy err %v", err)
649649
}
650650

651-
klog.Infof("res: %#v", res.Data)
652651
return ctx.JSON(fiber.Map{
653652
"code": http.StatusOK,
654-
"data": map[string]string{
655-
"uid": res.Data.Data.UID,
656-
},
653+
"data": res,
657654
})
658655
}
659656

@@ -859,25 +856,19 @@ type SystemServerWrap struct {
859856
Data InstallationResponse `json:"data"`
860857
}
861858

862-
func uninstall(name, token string) (data *SystemServerWrap, err error) {
863-
url := fmt.Sprintf("http://%s/system-server/v1alpha1/app/service.appstore/v1/UninstallDevApp", constants.SystemServer)
864-
accessToken, err := command.GetAccessToken()
865-
if err != nil {
866-
return data, err
867-
}
859+
func uninstall(name, token string) (data map[string]interface{}, err error) {
860+
url := fmt.Sprintf("http://appstore-service.os-framework:81/app-store/api/v2/apps/%s", name)
868861

869862
client := resty.New().SetTimeout(5 * time.Second)
870863
resp, err := client.R().
871864
SetHeader(restful.HEADER_ContentType, restful.MIME_JSON).
872865
SetHeader("X-Authorization", token).
873-
SetHeader("X-Access-Token", accessToken).
874-
SetBody(map[string]interface{}{
875-
"name": name,
876-
}).Post(url)
866+
Delete(url)
877867
if err != nil {
868+
klog.Errorf("failed to send request to uninstall app %s, err=%v", name, err)
878869
return data, err
879870
}
880-
klog.Info("resp.StatusCode: ", resp.StatusCode())
871+
klog.Info("request uninstall resp.StatusCode: ", resp.StatusCode())
881872
if resp.StatusCode() != http.StatusOK {
882873
dump, e := httputil.DumpRequest(resp.Request.RawRequest, true)
883874
if e == nil {
@@ -891,11 +882,6 @@ func uninstall(name, token string) (data *SystemServerWrap, err error) {
891882
return nil, err
892883
}
893884

894-
code := data.Code
895-
if code != 0 {
896-
return nil, errors.New(data.Message)
897-
}
898-
899885
return data, nil
900886
}
901887

pkg/development/command/command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestCreateApp(t *testing.T) {
2020
}
2121

2222
func TestInstall(t *testing.T) {
23-
_, err := Install().Run(context.Background(), "newapp", "test")
23+
_, err := Install().Run(context.Background(), "newapp", "test", "0.0.1")
2424
if err != nil {
2525
klog.Error(err)
2626
t.Fail()
@@ -30,7 +30,7 @@ func TestInstall(t *testing.T) {
3030
}
3131

3232
func TestUpdateRepo(t *testing.T) {
33-
err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", false)
33+
_, err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", false)
3434
if err != nil {
3535
klog.Error(err)
3636
t.Fail()

pkg/development/command/install.go

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package command
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"net/http"
98
"net/http/httputil"
109
"time"
1110

12-
"github.com/beclab/devbox/pkg/constants"
13-
1411
"github.com/emicklei/go-restful/v3"
1512
"github.com/go-resty/resty/v2"
1613
"k8s.io/klog/v2"
@@ -23,54 +20,73 @@ func Install() *install {
2320
return &install{}
2421
}
2522

26-
func (c *install) Run(ctx context.Context, app string, token string) (string, error) {
27-
accessToken, err := GetAccessToken()
23+
func (c *install) Run(ctx context.Context, app string, token string, version string) (string, error) {
24+
klog.Infof("run appname: %s", app)
25+
26+
err := c.UploadChartToMarket(ctx, app, token, version)
2827
if err != nil {
2928
return "", err
3029
}
31-
klog.Infof("run appname: %s", app)
30+
for i := 0; i < 45; i++ {
31+
klog.Infof("wait for chart %d", i)
32+
time.Sleep(time.Second)
33+
}
34+
35+
// get chart tgz file from storage and push to market
36+
// if more than one user upload same name tgz file to market what would happen
3237

33-
url := fmt.Sprintf("http://%s/system-server/v1alpha1/app/service.appstore/v1/InstallDevApp", constants.SystemServer)
38+
url := fmt.Sprintf("http://appstore-service.os-framework:81/app-store/api/v2/apps/%s/install", app)
3439
client := resty.New().SetTimeout(5 * time.Second)
40+
body := map[string]interface{}{
41+
"source": "local",
42+
"app_name": app,
43+
"version": version,
44+
}
45+
klog.Infof("install request body: %v", body)
3546
resp, err := client.R().SetHeader(restful.HEADER_ContentType, restful.MIME_JSON).
3647
SetHeader("X-Authorization", token).
37-
SetHeader("X-Access-Token", accessToken).
38-
SetBody(
39-
map[string]interface{}{
40-
"appName": app,
41-
"repoUrl": constants.RepoURL,
42-
"source": "devbox",
43-
}).Post(url)
48+
SetBody(body).Post(url)
4449
if err != nil {
50+
klog.Errorf("send install request failed : %v", err)
4551
return "", err
4652
}
53+
klog.Infof("install: statusCode: %d", resp.StatusCode())
4754
if resp.StatusCode() != http.StatusOK {
4855
dump, e := httputil.DumpRequest(resp.Request.RawRequest, true)
4956
if e == nil {
50-
klog.Error("reauest bfl.InstallDevApp", string(dump))
57+
klog.Error("request bfl.InstallDevApp", string(dump))
5158
}
5259
return "", errors.New(string(resp.Body()))
5360
}
5461
klog.Infof("body: %s\n", string(resp.Body()))
55-
ret := make(map[string]interface{})
56-
err = json.Unmarshal(resp.Body(), &ret)
57-
if err != nil {
58-
return "", err
59-
}
6062

61-
code, ok := ret["code"]
62-
if int(code.(float64)) != 0 {
63-
return "", fmt.Errorf("%s", ret["message"])
64-
}
65-
if ok && int(code.(float64)) == 0 {
66-
data := ret["data"].(map[string]interface{})
67-
code, ok := data["code"]
68-
if ok && int(code.(float64)) != http.StatusOK {
69-
return "", fmt.Errorf("message: %s", data["message"])
70-
}
63+
return "", nil
7164

72-
}
65+
}
7366

74-
return "", nil
67+
func (c *install) UploadChartToMarket(ctx context.Context, app string, token string, version string) error {
68+
client := resty.New().SetTimeout(30 * time.Second)
7569

70+
chartFilePath := fmt.Sprintf("/storage/%s-%s.tgz", app, version)
71+
klog.Infof("chartFilePath: %s", chartFilePath)
72+
resp, err := client.R().
73+
SetHeader("X-Authorization", token).
74+
SetFile("chart", chartFilePath).
75+
SetFormData(map[string]string{
76+
"source": "local",
77+
}).Post("http://appstore-service.os-framework:81/app-store/api/v2/apps/upload")
78+
if err != nil {
79+
klog.Errorf("upload app %s chart to market failed %w", app, err)
80+
return fmt.Errorf("upload app %s chart to market failed %w", app, err)
81+
}
82+
if resp.StatusCode() != http.StatusOK {
83+
dump, e := httputil.DumpRequest(resp.Request.RawRequest, true)
84+
if e != nil {
85+
klog.Error("request /app-store/api/v2/apps/upload", string(dump))
86+
}
87+
klog.Errorf("status code not = 200, err=%v", string(resp.Body()))
88+
return errors.New(string(resp.Body()))
89+
}
90+
klog.Infof("update app %s chart to market success", app)
91+
return nil
7692
}

pkg/development/command/updaterepo.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,32 @@ func (c *updateRepo) WithDir(dir string) *updateRepo {
2626
return c
2727
}
2828

29-
func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error {
29+
func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) (string, error) {
3030
if app == "" {
31-
return errors.New("repo path must be specified")
31+
return "", errors.New("repo path must be specified")
3232
}
3333
realPath := filepath.Join(c.baseCommand.dir, app)
3434

3535
chart, err := helm.LoadChart(realPath)
3636
if err != nil {
3737
klog.Error("load chart to upgrade repo error, ", err, ", ", realPath)
38-
return err
38+
return "", err
3939
}
4040

4141
klog.Info("upgrade chart version, ", app)
4242
version, err := helm.GetChartVersion(chart)
4343
if err != nil {
44-
return err
44+
return "", err
4545
}
4646
newVersion := version.IncPatch()
47+
uploadChartVersion := version.String()
4748
if !notExist {
49+
uploadChartVersion = newVersion.String()
50+
klog.Infof("uploadChartVersion: %s", uploadChartVersion)
4851
err = helm.UpgradeChartVersion(chart, app, realPath, &newVersion)
4952
if err != nil {
5053
klog.Error("upgrade chart version error, ", err)
51-
return err
54+
return "", err
5255
}
5356
}
5457

@@ -86,43 +89,43 @@ func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error {
8689
chartYamlBak := filepath.Join(realPath, "Chart.bak")
8790
chartDeferFunc, err := backupAndRestoreFile(chartYaml, chartYamlBak)
8891
if err != nil {
89-
return err
92+
return "", err
9093
}
9194
defer chartDeferFunc()
9295

9396
err = helm.UpdateChartName(chart, app, realPath)
9497
if err != nil {
9598
klog.Error("update chart name error, ", err)
96-
return err
99+
return "", err
97100
}
98101

99102
if !notExist {
100103
err = helm.UpdateAppCfgVersion(realPath, &newVersion)
101104
if err != nil {
102105
klog.Error("update OlaresManifest.yaml metadata.version error, ", err)
103-
return err
106+
return "", err
104107
}
105108
}
106109

107110
appcfg := filepath.Join(realPath, constants.AppCfgFileName)
108111
appcfgBak := filepath.Join(realPath, "OlaresManifest.yaml.bak")
109112
appcfgDeferFunc, err := backupAndRestoreFile(appcfg, appcfgBak)
110113
if err != nil {
111-
return err
114+
return "", err
112115
}
113116
defer appcfgDeferFunc()
114117

115118
err = helm.UpdateAppCfgName(app, realPath)
116119
if err != nil {
117-
return err
120+
return "", err
118121
}
119122

120123
output, err := c.baseCommand.run(ctx, "helm", "cm-push", "-f", app, "http://localhost:8888", "--debug")
121124
if err != nil {
122125
if len(output) > 0 {
123-
return errors.New(output)
126+
return "", errors.New(output)
124127
}
125-
return err
128+
return "", err
126129
}
127130
result := strings.Split(output, "\n")
128131
if len(result) > 0 && result[len(result)-2] == "Done." {
@@ -134,7 +137,8 @@ func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error {
134137
}
135138

136139
}
137-
return nil
140+
klog.Infof("update repo app %s, newVersion: %s", app, uploadChartVersion)
141+
return uploadChartVersion, nil
138142

139143
}
140144

pkg/development/helm/chart.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func UpdateAppCfgName(name, path string) error {
163163
}
164164

165165
appCfg.Metadata.Name = appDevName
166+
appCfg.Metadata.AppID = appDevName
166167
appCfg.Metadata.Title = appDevName
167168

168169
//if version != nil {

0 commit comments

Comments
 (0)