github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/debug/images.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package debug
    16  
    17  import (
    18  	"fmt"
    19  )
    20  
    21  type ImagesManagement interface {
    22  	ShowDefaultImages() error
    23  	GetDefaultImage() (string, error)
    24  }
    25  
    26  const DefaultSealerRegistryURL = "registry.cn-qingdao.aliyuncs.com/sealer-apps/"
    27  
    28  // ImagesManager holds the default images information.
    29  type ImagesManager struct {
    30  	RegistryURL string
    31  
    32  	DefaultImagesMap map[string]string // "RichToolsOnUbuntu": "debug:ubuntu"
    33  	DefaultImageKey  string            // "RichToolsOnUbuntu"
    34  
    35  	DefaultImage string // RegistryURL + DefaultImagesMap[DefaultImageName]
    36  }
    37  
    38  func NewDebugImagesManager() *ImagesManager {
    39  	return &ImagesManager{
    40  		DefaultImagesMap: map[string]string{
    41  			"RichToolsOnUbuntu": "debug:ubuntu",
    42  		},
    43  
    44  		DefaultImageKey: "RichToolsOnUbuntu",
    45  	}
    46  }
    47  
    48  // ShowDefaultImages shows default images provided by debug.
    49  func (manager *ImagesManager) ShowDefaultImages() error {
    50  	if len(manager.RegistryURL) == 0 {
    51  		manager.RegistryURL = DefaultSealerRegistryURL
    52  	}
    53  	fmt.Println("There are several default images you can use:")
    54  	for key, value := range manager.DefaultImagesMap {
    55  		fmt.Println(key + ":  " + manager.RegistryURL + value)
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // GetDefaultImage return the default image provide by debug.
    62  func (manager *ImagesManager) GetDefaultImage() (string, error) {
    63  	if len(manager.RegistryURL) == 0 {
    64  		manager.RegistryURL = DefaultSealerRegistryURL
    65  	}
    66  	return manager.RegistryURL + manager.DefaultImagesMap[manager.DefaultImageKey], nil
    67  }