Skip to content

Commit

Permalink
feat: to judge uncontrolled in backend (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Mar 22, 2024
1 parent de08559 commit b81b8a6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api/app_management/openapi.yaml
Expand Up @@ -392,6 +392,8 @@ paths:
parameters:
- $ref: "#/components/parameters/DryRun"
- $ref: "#/components/parameters/CheckPortConflict"
# TODO remove the unused parameters later.
# the params is deprecated. because frontend is hard to know is the app is stable or not.
- $ref: "#/components/parameters/Uncontrolled"
requestBody:
$ref: "#/components/requestBodies/RequestComposeApp"
Expand Down
1 change: 1 addition & 0 deletions route/v2/appstore.go
Expand Up @@ -197,6 +197,7 @@ func (a *AppManagement) ComposeAppStableTag(ctx echo.Context, id codegen.StoreAp
})
}

// TODO refactor this with MainService
storeInfo, err := composeApp.StoreInfo(true)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{
Expand Down
50 changes: 48 additions & 2 deletions route/v2/compose_app.go
Expand Up @@ -99,6 +99,38 @@ func (a *AppManagement) MyComposeApp(ctx echo.Context, id codegen.ComposeAppID)
})
}

func (a *AppManagement) IsNewComposeUncontrolled(newComposeApp *service.ComposeApp) (bool, error) {
// to check if the new compose app is uncontrolled
newTag, err := newComposeApp.MainTag()
if err != nil {
return false, err
}

// TODO refactor this. because if user not update. the status will be uncontrolled.
if newTag == "latest" {
return true, nil
} else {
// compare store info
StoreApp, err := service.MyService.V2AppStore().ComposeApp(newComposeApp.Name)
if err != nil {
return false, err
}

if StoreApp == nil {
return false, errors.New("store app not found")
}
StableTag, err := StoreApp.MainTag()
if err != nil {
return false, err
}
if StableTag != newTag {
return true, nil
} else {
return false, nil
}
}
}

func (a *AppManagement) ApplyComposeAppSettings(ctx echo.Context, id codegen.ComposeAppID, params codegen.ApplyComposeAppSettingsParams) error {
if id == "" {
message := ErrComposeAppIDNotProvided.Error()
Expand Down Expand Up @@ -136,7 +168,14 @@ func (a *AppManagement) ApplyComposeAppSettings(ctx echo.Context, id codegen.Com
})
}

if params.Uncontrolled != nil && *params.Uncontrolled {
uncontrolled, err := a.IsNewComposeUncontrolled(newComposeApp)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{
Message: &message,
})
}
if uncontrolled {
// set to uncontrolled app
xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS]
xcasaosMap, ok := xcasaos.(map[string]interface{})
Expand Down Expand Up @@ -264,7 +303,14 @@ func (a *AppManagement) InstallComposeApp(ctx echo.Context, params codegen.Insta
})
}

if params.Uncontrolled != nil && *params.Uncontrolled {
uncontrolled, err := a.IsNewComposeUncontrolled(composeApp)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{
Message: &message,
})
}
if uncontrolled {
// set to uncontrolled app
xcasaos := composeApp.Extensions[common.ComposeExtensionNameXCasaOS]
xcasaosMap, ok := xcasaos.(map[string]interface{})
Expand Down
23 changes: 23 additions & 0 deletions service/compose_app.go
Expand Up @@ -374,6 +374,29 @@ func (a *ComposeApp) Apps() map[string]*App {
return apps
}

func (a *ComposeApp) MainService() (*App, error) {
storeInfo, err := a.StoreInfo(false)
if err != nil {
return nil, err
}

if storeInfo.Main == nil || *storeInfo.Main == "" {
return nil, ErrMainServiceNotFound
}

return a.App(*storeInfo.Main), nil
}

func (a *ComposeApp) MainTag() (string, error) {
mainService, err := a.MainService()
if err != nil {
return "", err
}
_, newTag := docker.ExtractImageAndTag(mainService.Image)

return newTag, nil
}

func (a *ComposeApp) Containers(ctx context.Context) (map[string][]api.ContainerSummary, error) {
service, dockerClient, err := apiService()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions service/errs.go
Expand Up @@ -16,4 +16,5 @@ var (
ErrNotFoundInAppStore = fmt.Errorf("not found in app store")
ErrSetStoreAppID = fmt.Errorf("failed to set store app ID")
ErrStoreInfoNotFound = fmt.Errorf("store info not found")
ErrMainServiceNotFound = fmt.Errorf("main service not found")
)

0 comments on commit b81b8a6

Please sign in to comment.