unsafe

package
v0.0.0-...-6843e3f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: Apache-2.0, BSD-2-Clause Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:    "unsafe",
	Aliases: []string{"u"},
	Usage:   "unsafe operations",
	Subcommands: []*cli.Command{
		Init,
		RestoreDB,
		RemoveImage,
		cubebox.Destroy,
		cubebox.DestroyAll,
		volumedb,
	},
}
View Source
var Init = &cli.Command{
	Name:  "init",
	Usage: "init cubelet",
	Flags: []cli.Flag{},
	Action: func(context *cli.Context) error {
		if !commands.AskForConfirm("init will destroy ALL of the resource, continue only if you confirm", 3) {
			return nil
		}

		conn, ctx, cancel, err := commands.NewGrpcConn(context)
		if err != nil {
			return err
		}
		defer conn.Close()
		defer cancel()
		client := nbi.NewCubeLetClient(conn)
		req := &nbi.InitRequest{
			RequestID: uuid.New().String(),
		}
		rsp, err := client.InitHost(ctx, req)
		if err != nil {
			return err
		}
		if !ret.IsSuccessCode(rsp.GetCode()) {
			log.Printf("InitHost failure:%v, %v", req.RequestID, rsp.GetCode())
			return fmt.Errorf("failed to init host, code: %v, msg: %v", rsp.GetCode().String(), rsp.GetMessage())
		}
		log.Printf("InitHost rsp:%+v", rsp)
		return nil
	},
}
View Source
var RemoveImage = &cli.Command{
	Name:  "rmi",
	Usage: "remove images",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "all",
			Aliases: []string{"a"},
			Usage:   "remove all images",
		},
	},
	Action: func(cliContext *cli.Context) error {
		var toRemoveNamespaceImages map[string][]string = make(map[string][]string)
		if cliContext.Bool("all") {
			client, ctx, cancel, err := ctrCommands.NewClient(cliContext)
			if err != nil {
				return err
			}
			defer cancel()
			var (
				imageStore = client.ImageService()
			)
			nslist, err := client.NamespaceService().List(ctx)
			if err != nil {
				return fmt.Errorf("failed to list namespaces: %w", err)
			}
			for _, ns := range nslist {
				var toRemoveImage []string
				imageList, err := imageStore.List(namespaces.WithNamespace(ctx, ns))
				if err != nil {
					return fmt.Errorf("failed to list images: %w", err)
				}
				for _, image := range imageList {
					toRemoveImage = append(toRemoveImage, image.Name)
				}
				toRemoveNamespaceImages[ns] = toRemoveImage
			}
		} else {
			toRemoveNamespaceImages[cliContext.String("namespace")] = cliContext.Args().Slice()
			if len(toRemoveNamespaceImages[cliContext.String("namespace")]) == 0 {
				return fmt.Errorf("image id must be provided: %w", errdefs.ErrInvalidArgument)
			}
		}

		if !commands.AskForConfirm("will destroy image, continue only if you confirm", 3) {
			return nil
		}

		conn, ctx, cancel, err := commands.NewGrpcConn(cliContext)
		if err != nil {
			return err
		}
		defer conn.Close()
		defer cancel()

		client := images.NewImagesClient(conn)
		for ns, toRemoveImages := range toRemoveNamespaceImages {
			for _, imageName := range toRemoveImages {
				resp, err := client.DestroyImage(ctx, &images.DestroyImageRequest{
					Spec: &images.ImageSpec{
						Annotations: map[string]string{
							constants.AnnotationCubeletNameSpace: ns,
						},
						Image: imageName,
					},
				})

				if err != nil {
					return err
				}
				if !ret.IsSuccessCode(resp.GetRet().GetRetCode()) {
					return fmt.Errorf("destroy image %q: %v", imageName, resp.GetRet().GetRetMsg())
				}
				log.Printf("destroy image %q %v", imageName, resp.GetRet().GetRetMsg())
			}
		}

		return nil
	},
}
View Source
var RestoreDB = &cli.Command{
	Name:  "restoredb",
	Usage: "restore metadata from backup db",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "config",
			Aliases: []string{"c"},
			Usage:   "path to the configuration file",
			Value:   "/usr/local/services/cubetoolbox/Cubelet/config/config.toml",
		},
	},
	Action: func(context *cli.Context) error {
		if !commands.AskForConfirm("restore will forcibly replace the containerd metadata db, continue only if you confirm", 3) {
			return nil
		}

		config := &srvconfig.Config{}
		if err := srvconfig.LoadConfig(gocontext.Background(), context.String("config"), config); err != nil {
			return err
		}

		backupPluginURI := fmt.Sprintf("%s.%s", constants.InternalPlugin, constants.BackupID.ID())
		boltPluginURI := fmt.Sprintf("%v.%v", plugins.MetadataPlugin, "bolt")

		backupFile := filepath.Join(config.Root, backupPluginURI, boltPluginURI, "meta.db")
		targetFile := filepath.Join(config.State, boltPluginURI, "meta.db")

		if exist, _ := utils.DenExist(backupFile); !exist {
			fmt.Printf("SKIP: backup file %s not exist\n", backupFile)
			return nil
		}

		return utils.SafeCopyFile(targetFile, backupFile)
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL