diff --git a/pkg/api/server/handlers_cmd.go b/pkg/api/server/handlers_cmd.go index 2f36476..21d7da4 100644 --- a/pkg/api/server/handlers_cmd.go +++ b/pkg/api/server/handlers_cmd.go @@ -202,7 +202,7 @@ func (h *handlers) updateDevAppRepo(ctx *fiber.Ctx) error { }) } - err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, false) + _, err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, false) if err != nil { klog.Error("command upgraderepo error, ", err, ", ", name) return ctx.JSON(fiber.Map{ @@ -303,10 +303,10 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error { "message": fmt.Sprintf("Check namespace failed: %v", err), }) } - + version := "0.0.1" if source != "cli" { klog.Info("auto update repo") - err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, releaseNotExist) + version, err = command.UpdateRepo().WithDir(BaseDir).Run(ctx.Context(), name, releaseNotExist) if err != nil { klog.Error("command upgraderepo error, ", err, ", ", name) return ctx.JSON(fiber.Map{ @@ -316,7 +316,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error { } } - _, err = command.Install().Run(ctx.Context(), devName, token) + _, err = command.Install().Run(ctx.Context(), devName, token, version) if err != nil { klog.Error("command install error, ", err, ", ", name) @@ -648,12 +648,9 @@ func (h *handlers) uninstall(ctx *fiber.Ctx) error { klog.Errorf("update dev app state to undeploy err %v", err) } - klog.Infof("res: %#v", res.Data) return ctx.JSON(fiber.Map{ "code": http.StatusOK, - "data": map[string]string{ - "uid": res.Data.Data.UID, - }, + "data": res, }) } @@ -859,25 +856,19 @@ type SystemServerWrap struct { Data InstallationResponse `json:"data"` } -func uninstall(name, token string) (data *SystemServerWrap, err error) { - url := fmt.Sprintf("http://%s/system-server/v1alpha1/app/service.appstore/v1/UninstallDevApp", constants.SystemServer) - accessToken, err := command.GetAccessToken() - if err != nil { - return data, err - } +func uninstall(name, token string) (data map[string]interface{}, err error) { + url := fmt.Sprintf("http://appstore-service.os-framework:81/app-store/api/v2/apps/%s", name) client := resty.New().SetTimeout(5 * time.Second) resp, err := client.R(). SetHeader(restful.HEADER_ContentType, restful.MIME_JSON). SetHeader("X-Authorization", token). - SetHeader("X-Access-Token", accessToken). - SetBody(map[string]interface{}{ - "name": name, - }).Post(url) + Delete(url) if err != nil { + klog.Errorf("failed to send request to uninstall app %s, err=%v", name, err) return data, err } - klog.Info("resp.StatusCode: ", resp.StatusCode()) + klog.Info("request uninstall resp.StatusCode: ", resp.StatusCode()) if resp.StatusCode() != http.StatusOK { dump, e := httputil.DumpRequest(resp.Request.RawRequest, true) if e == nil { @@ -891,11 +882,6 @@ func uninstall(name, token string) (data *SystemServerWrap, err error) { return nil, err } - code := data.Code - if code != 0 { - return nil, errors.New(data.Message) - } - return data, nil } diff --git a/pkg/development/command/command_test.go b/pkg/development/command/command_test.go index c07713d..26d67b6 100644 --- a/pkg/development/command/command_test.go +++ b/pkg/development/command/command_test.go @@ -20,7 +20,7 @@ func TestCreateApp(t *testing.T) { } func TestInstall(t *testing.T) { - _, err := Install().Run(context.Background(), "newapp", "test") + _, err := Install().Run(context.Background(), "newapp", "test", "0.0.1") if err != nil { klog.Error(err) t.Fail() @@ -30,7 +30,7 @@ func TestInstall(t *testing.T) { } func TestUpdateRepo(t *testing.T) { - err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", false) + _, err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", false) if err != nil { klog.Error(err) t.Fail() diff --git a/pkg/development/command/install.go b/pkg/development/command/install.go index da3ea2e..c69fd5c 100644 --- a/pkg/development/command/install.go +++ b/pkg/development/command/install.go @@ -2,15 +2,12 @@ package command import ( "context" - "encoding/json" "errors" "fmt" "net/http" "net/http/httputil" "time" - "github.com/beclab/devbox/pkg/constants" - "github.com/emicklei/go-restful/v3" "github.com/go-resty/resty/v2" "k8s.io/klog/v2" @@ -23,54 +20,73 @@ func Install() *install { return &install{} } -func (c *install) Run(ctx context.Context, app string, token string) (string, error) { - accessToken, err := GetAccessToken() +func (c *install) Run(ctx context.Context, app string, token string, version string) (string, error) { + klog.Infof("run appname: %s", app) + + err := c.UploadChartToMarket(ctx, app, token, version) if err != nil { return "", err } - klog.Infof("run appname: %s", app) + for i := 0; i < 45; i++ { + klog.Infof("wait for chart %d", i) + time.Sleep(time.Second) + } + + // get chart tgz file from storage and push to market + // if more than one user upload same name tgz file to market what would happen - url := fmt.Sprintf("http://%s/system-server/v1alpha1/app/service.appstore/v1/InstallDevApp", constants.SystemServer) + url := fmt.Sprintf("http://appstore-service.os-framework:81/app-store/api/v2/apps/%s/install", app) client := resty.New().SetTimeout(5 * time.Second) + body := map[string]interface{}{ + "source": "local", + "app_name": app, + "version": version, + } + klog.Infof("install request body: %v", body) resp, err := client.R().SetHeader(restful.HEADER_ContentType, restful.MIME_JSON). SetHeader("X-Authorization", token). - SetHeader("X-Access-Token", accessToken). - SetBody( - map[string]interface{}{ - "appName": app, - "repoUrl": constants.RepoURL, - "source": "devbox", - }).Post(url) + SetBody(body).Post(url) if err != nil { + klog.Errorf("send install request failed : %v", err) return "", err } + klog.Infof("install: statusCode: %d", resp.StatusCode()) if resp.StatusCode() != http.StatusOK { dump, e := httputil.DumpRequest(resp.Request.RawRequest, true) if e == nil { - klog.Error("reauest bfl.InstallDevApp", string(dump)) + klog.Error("request bfl.InstallDevApp", string(dump)) } return "", errors.New(string(resp.Body())) } klog.Infof("body: %s\n", string(resp.Body())) - ret := make(map[string]interface{}) - err = json.Unmarshal(resp.Body(), &ret) - if err != nil { - return "", err - } - code, ok := ret["code"] - if int(code.(float64)) != 0 { - return "", fmt.Errorf("%s", ret["message"]) - } - if ok && int(code.(float64)) == 0 { - data := ret["data"].(map[string]interface{}) - code, ok := data["code"] - if ok && int(code.(float64)) != http.StatusOK { - return "", fmt.Errorf("message: %s", data["message"]) - } + return "", nil - } +} - return "", nil +func (c *install) UploadChartToMarket(ctx context.Context, app string, token string, version string) error { + client := resty.New().SetTimeout(30 * time.Second) + chartFilePath := fmt.Sprintf("/storage/%s-%s.tgz", app, version) + klog.Infof("chartFilePath: %s", chartFilePath) + resp, err := client.R(). + SetHeader("X-Authorization", token). + SetFile("chart", chartFilePath). + SetFormData(map[string]string{ + "source": "local", + }).Post("http://appstore-service.os-framework:81/app-store/api/v2/apps/upload") + if err != nil { + klog.Errorf("upload app %s chart to market failed %w", app, err) + return fmt.Errorf("upload app %s chart to market failed %w", app, err) + } + if resp.StatusCode() != http.StatusOK { + dump, e := httputil.DumpRequest(resp.Request.RawRequest, true) + if e != nil { + klog.Error("request /app-store/api/v2/apps/upload", string(dump)) + } + klog.Errorf("status code not = 200, err=%v", string(resp.Body())) + return errors.New(string(resp.Body())) + } + klog.Infof("update app %s chart to market success", app) + return nil } diff --git a/pkg/development/command/updaterepo.go b/pkg/development/command/updaterepo.go index 0b81265..64721f9 100644 --- a/pkg/development/command/updaterepo.go +++ b/pkg/development/command/updaterepo.go @@ -26,29 +26,32 @@ func (c *updateRepo) WithDir(dir string) *updateRepo { return c } -func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error { +func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) (string, error) { if app == "" { - return errors.New("repo path must be specified") + return "", errors.New("repo path must be specified") } realPath := filepath.Join(c.baseCommand.dir, app) chart, err := helm.LoadChart(realPath) if err != nil { klog.Error("load chart to upgrade repo error, ", err, ", ", realPath) - return err + return "", err } klog.Info("upgrade chart version, ", app) version, err := helm.GetChartVersion(chart) if err != nil { - return err + return "", err } newVersion := version.IncPatch() + uploadChartVersion := version.String() if !notExist { + uploadChartVersion = newVersion.String() + klog.Infof("uploadChartVersion: %s", uploadChartVersion) err = helm.UpgradeChartVersion(chart, app, realPath, &newVersion) if err != nil { klog.Error("upgrade chart version error, ", err) - return err + return "", err } } @@ -86,21 +89,21 @@ func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error { chartYamlBak := filepath.Join(realPath, "Chart.bak") chartDeferFunc, err := backupAndRestoreFile(chartYaml, chartYamlBak) if err != nil { - return err + return "", err } defer chartDeferFunc() err = helm.UpdateChartName(chart, app, realPath) if err != nil { klog.Error("update chart name error, ", err) - return err + return "", err } if !notExist { err = helm.UpdateAppCfgVersion(realPath, &newVersion) if err != nil { klog.Error("update OlaresManifest.yaml metadata.version error, ", err) - return err + return "", err } } @@ -108,21 +111,21 @@ func (c *updateRepo) Run(ctx context.Context, app string, notExist bool) error { appcfgBak := filepath.Join(realPath, "OlaresManifest.yaml.bak") appcfgDeferFunc, err := backupAndRestoreFile(appcfg, appcfgBak) if err != nil { - return err + return "", err } defer appcfgDeferFunc() err = helm.UpdateAppCfgName(app, realPath) if err != nil { - return err + return "", err } output, err := c.baseCommand.run(ctx, "helm", "cm-push", "-f", app, "http://localhost:8888", "--debug") if err != nil { if len(output) > 0 { - return errors.New(output) + return "", errors.New(output) } - return err + return "", err } result := strings.Split(output, "\n") 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 { } } - return nil + klog.Infof("update repo app %s, newVersion: %s", app, uploadChartVersion) + return uploadChartVersion, nil } diff --git a/pkg/development/helm/chart.go b/pkg/development/helm/chart.go index e84bd28..0f4b479 100644 --- a/pkg/development/helm/chart.go +++ b/pkg/development/helm/chart.go @@ -163,6 +163,7 @@ func UpdateAppCfgName(name, path string) error { } appCfg.Metadata.Name = appDevName + appCfg.Metadata.AppID = appDevName appCfg.Metadata.Title = appDevName //if version != nil {