@@ -2,15 +2,12 @@ package command
22
33import (
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}
0 commit comments