github.com/qiniu/go-sdk/v7@v7.27.0/examples/sandbox_connect/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/qiniu/go-sdk/v7/sandbox"
    11  )
    12  
    13  func main() {
    14  	apiKey := os.Getenv("QINIU_API_KEY")
    15  	if apiKey == "" {
    16  		log.Fatal("请设置 QINIU_API_KEY 环境变量")
    17  	}
    18  
    19  	apiURL := os.Getenv("QINIU_SANDBOX_API_URL")
    20  
    21  	c, err := sandbox.NewClient(&sandbox.Config{
    22  		APIKey:   apiKey,
    23  		Endpoint: apiURL,
    24  	})
    25  	if err != nil {
    26  		log.Fatalf("创建客户端失败: %v", err)
    27  	}
    28  
    29  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    30  	defer cancel()
    31  
    32  	// 连接到已有沙箱,需替换为实际的沙箱 ID
    33  	sandboxID := os.Getenv("SANDBOX_ID")
    34  	if sandboxID == "" {
    35  		// 如果未指定,列出现有沙箱并选取第一个
    36  		sandboxes, err := c.List(ctx, nil)
    37  		if err != nil {
    38  			log.Fatalf("列出沙箱失败: %v", err)
    39  		}
    40  		if len(sandboxes) > 0 {
    41  			sandboxID = sandboxes[0].SandboxID
    42  			fmt.Printf("找到运行中的沙箱: %s\n", sandboxID)
    43  		}
    44  	}
    45  
    46  	// 如果没有可用沙箱,自行创建一个用于演示连接
    47  	if sandboxID == "" {
    48  		fmt.Println("没有运行中的沙箱,自动创建一个用于演示...")
    49  
    50  		templates, err := c.ListTemplates(ctx, nil)
    51  		if err != nil {
    52  			log.Fatalf("列出模板失败: %v", err)
    53  		}
    54  
    55  		var templateID string
    56  		for _, tmpl := range templates {
    57  			if tmpl.BuildStatus == sandbox.BuildStatusReady || tmpl.BuildStatus == sandbox.BuildStatusUploaded {
    58  				templateID = tmpl.TemplateID
    59  				break
    60  			}
    61  		}
    62  		if templateID == "" {
    63  			log.Fatal("没有构建成功的模板")
    64  		}
    65  
    66  		timeout := int32(60)
    67  		created, _, err := c.CreateAndWait(ctx, sandbox.CreateParams{
    68  			TemplateID: templateID,
    69  			Timeout:    &timeout,
    70  		}, sandbox.WithPollInterval(2*time.Second))
    71  		if err != nil {
    72  			log.Fatalf("创建沙箱失败: %v", err)
    73  		}
    74  		sandboxID = created.ID()
    75  		fmt.Printf("沙箱已创建: %s\n", sandboxID)
    76  
    77  		defer func() {
    78  			killCtx, killCancel := context.WithTimeout(context.Background(), 10*time.Second)
    79  			defer killCancel()
    80  			if err := created.Kill(killCtx); err != nil {
    81  				log.Printf("终止沙箱失败: %v", err)
    82  			} else {
    83  				fmt.Printf("沙箱 %s 已终止\n", sandboxID)
    84  			}
    85  		}()
    86  	}
    87  
    88  	timeout := int32(300)
    89  	sb, err := c.Connect(ctx, sandboxID, sandbox.ConnectParams{
    90  		Timeout: timeout,
    91  	})
    92  	if err != nil {
    93  		log.Fatalf("连接沙箱失败: %v", err)
    94  	}
    95  
    96  	fmt.Printf("已连接到沙箱: %s (模板: %s)\n", sb.ID(), sb.TemplateID())
    97  
    98  	// 获取沙箱详情
    99  	info, err := sb.GetInfo(ctx)
   100  	if err != nil {
   101  		log.Fatalf("获取详情失败: %v", err)
   102  	}
   103  	fmt.Printf("状态: %s, CPU: %d 核, 内存: %d MB\n", info.State, info.CPUCount, info.MemoryMB)
   104  }