mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	边缘节点远程安装文件最小化(从16.xM减少到2.xM)
This commit is contained in:
		@@ -2,7 +2,7 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
 | 
				
			||||||
	"github.com/iwind/gosock/pkg/gosock"
 | 
						"github.com/iwind/gosock/pkg/gosock"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
@@ -51,7 +51,7 @@ func main() {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		unzip := utils.NewUnzip(zipPath, targetPath)
 | 
							unzip := helpers.NewUnzip(zipPath, targetPath)
 | 
				
			||||||
		err := unzip.Run()
 | 
							err := unzip.Run()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			stderr("ERROR: " + err.Error())
 | 
								stderr("ERROR: " + err.Error())
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,9 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 注意这里的依赖文件应该最小化,从而使编译后的文件最小化
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
 | 
				
			||||||
	"github.com/iwind/gosock/pkg/gosock"
 | 
						"github.com/iwind/gosock/pkg/gosock"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
@@ -51,7 +52,7 @@ func main() {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		unzip := utils.NewUnzip(zipPath, targetPath)
 | 
							unzip := helpers.NewUnzip(zipPath, targetPath)
 | 
				
			||||||
		err := unzip.Run()
 | 
							err := unzip.Run()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			stderr("ERROR: " + err.Error())
 | 
								stderr("ERROR: " + err.Error())
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								internal/installers/helpers/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/installers/helpers/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					远程安装依赖文件,单独放在一个目录防止安装包过大
 | 
				
			||||||
							
								
								
									
										91
									
								
								internal/installers/helpers/unzip.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								internal/installers/helpers/unzip.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					package helpers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"archive/zip"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Unzip struct {
 | 
				
			||||||
 | 
						zipFile   string
 | 
				
			||||||
 | 
						targetDir string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewUnzip(zipFile string, targetDir string) *Unzip {
 | 
				
			||||||
 | 
						return &Unzip{
 | 
				
			||||||
 | 
							zipFile:   zipFile,
 | 
				
			||||||
 | 
							targetDir: targetDir,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (this *Unzip) Run() error {
 | 
				
			||||||
 | 
						if len(this.zipFile) == 0 {
 | 
				
			||||||
 | 
							return errors.New("zip file should not be empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(this.targetDir) == 0 {
 | 
				
			||||||
 | 
							return errors.New("target dir should not be empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						reader, err := zip.OpenReader(this.zipFile)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						defer func() {
 | 
				
			||||||
 | 
							_ = reader.Close()
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, file := range reader.File {
 | 
				
			||||||
 | 
							info := file.FileInfo()
 | 
				
			||||||
 | 
							target := this.targetDir + "/" + file.Name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 目录
 | 
				
			||||||
 | 
							if info.IsDir() {
 | 
				
			||||||
 | 
								stat, err := os.Stat(target)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									if !os.IsNotExist(err) {
 | 
				
			||||||
 | 
										return err
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										err = os.MkdirAll(target, info.Mode())
 | 
				
			||||||
 | 
										if err != nil {
 | 
				
			||||||
 | 
											return err
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								} else if !stat.IsDir() {
 | 
				
			||||||
 | 
									err = os.MkdirAll(target, info.Mode())
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 文件
 | 
				
			||||||
 | 
							err := func(file *zip.File, target string) error {
 | 
				
			||||||
 | 
								fileReader, err := file.Open()
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer func() {
 | 
				
			||||||
 | 
									_ = fileReader.Close()
 | 
				
			||||||
 | 
								}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								fileWriter, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, file.FileInfo().Mode())
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer func() {
 | 
				
			||||||
 | 
									_ = fileWriter.Close()
 | 
				
			||||||
 | 
								}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								_, err = io.Copy(fileWriter, fileReader)
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}(file, target)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								internal/installers/helpers/unzip_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								internal/installers/helpers/unzip_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					package helpers_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
 | 
				
			||||||
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
 | 
						_ "github.com/iwind/TeaGo/bootstrap"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestUnzip_Run(t *testing.T) {
 | 
				
			||||||
 | 
						var unzip = helpers.NewUnzip(Tea.Root+"/deploy/edge-node-v0.0.1.zip", Tea.Root+"/deploy/")
 | 
				
			||||||
 | 
						err := unzip.Run()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						t.Log("OK")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user