Skip to content

Commit

Permalink
feat: set distro location (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
aby913 authored Jan 15, 2025
1 parent 37f2a82 commit 754f7e3
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 51 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ replace (
)

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/containerd/containerd v1.7.18
github.com/containers/image/v5 v5.32.2
Expand Down Expand Up @@ -76,7 +77,6 @@ require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
Expand Down Expand Up @@ -221,10 +221,10 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.5
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.27.0
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/klog/v2 v2.120.1
sigs.k8s.io/controller-runtime v0.18.4
)
3 changes: 3 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ const (
CacheAppValues = "app_built_in_values"

CacheCountPodsUsingHostIP = "count_pods_using_host_ip"

CacheWindowsDistroStoreLocation = "windows_distro_store_location"
)

const (
Expand Down Expand Up @@ -289,6 +291,7 @@ const (
ENV_DISABLE_HOST_IP_PROMPT = "DISABLE_HOST_IP_PROMPT"
ENV_AUTO_ADD_FIREWALL_RULES = "AUTO_ADD_FIREWALL_RULES"
ENV_TERMINUS_OS_DOMAINNAME = "TERMINUS_OS_DOMAINNAME"
ENV_DEFAULT_WSL_DISTRO_LOCATION = "DEFAULT_WSL_DISTRO_LOCATION" // If set to 1, the default WSL distro storage will be used.
)

// TerminusGlobalEnvs holds a group of general environment variables
Expand Down
3 changes: 2 additions & 1 deletion pkg/core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ const (
)

const (
DownloadUrl = "https://dc3p1870nn3cj.cloudfront.net"
DownloadUrl = "https://dc3p1870nn3cj.cloudfront.net"
DefaultBashUrl = "olares.sh"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions pkg/phase/cluster/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (w *windowsInstallPhaseBuilder) build() []module.Module {
return []module.Module{
&windows.InstallWSLModule{},
&windows.InstallWSLUbuntuDistroModule{},
&windows.GetDiskPartitionModule{},
&windows.MoveDistroModule{},
&windows.ConfigWslModule{},
&windows.InstallTerminusModule{},
}
Expand Down
55 changes: 38 additions & 17 deletions pkg/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

"bytetrade.io/web3os/installer/pkg/core/logger"
"github.com/pkg/errors"
"golang.org/x/text/encoding/simplifiedchinese"
utilexec "k8s.io/utils/exec"
)

type Charset string

const (
UTF8 = Charset("UTF-8")
GB18030 = Charset("GB18030")
DEFAULT Charset = "DEFAULT"
GBK Charset = "GBK"
UTF16 Charset = "UTF16"
)

type CommandExecute interface {
Expand Down Expand Up @@ -70,6 +71,17 @@ func (d *DefaultCommandExecutor) Run() (string, error) {
return cmd.run()
}

func (d *DefaultCommandExecutor) RunCmd(name string, charset Charset) (string, error) {
var cmd = &CommandExecutor{
name: name,
cmd: d.Commands,
printOutput: d.PrintOutput,
printLine: d.PrintLine,
}

return cmd.runcmd(charset)
}

func (d *DefaultCommandExecutor) Exec() (string, error) {
var cmd = &CommandExecutor{
name: "cmd",
Expand All @@ -96,6 +108,29 @@ func (command *CommandExecutor) getCmd() string {
return strings.Join(command.cmd, " ")
}

func (command *CommandExecutor) runcmd(charset Charset) (string, error) {
var res string
var exec = utilexec.New()

output, err := exec.Command(command.name, command.cmd...).Output()
switch charset {
case UTF16:
res = Utf16ToUtf8(output)
case GBK:
tmp, _ := GbkToUtf8(output)
res = string(tmp)
default:
res = string(output)
}

if err != nil {
return res, err
}

logger.Debugf("[exec] CMD: %s, OUTPUT: %s", fmt.Sprintf("%s %v", command.name, command.cmd), res)
return res, nil
}

func (command *CommandExecutor) run() (string, error) {
args := append([]string{command.prefix}, command.cmd...)
c := exec.Command(command.name, args...)
Expand Down Expand Up @@ -233,17 +268,3 @@ func (command *CommandExecutor) exec() (string, error) {
logger.Debugf("[exec] CMD: %s, OUTPUT: %s", c.String(), res)
return res, errors.Wrapf(err, "Failed to exec command: %s \n%s", command.getCmd(), res)
}

func ConvertByte2String(byte []byte, charset Charset) string {
var str string
switch charset {
case GB18030:
decodeBytes, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case UTF8:
fallthrough
default:
str = string(byte)
}
return str
}
9 changes: 9 additions & 0 deletions pkg/utils/disk_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

func GetDrives() []string {
return []string{}
}

func GetDiskSpace(path string) (total uint64, free uint64, err error) {
return total, free, err
}
9 changes: 9 additions & 0 deletions pkg/utils/disk_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

func GetDrives() []string {
return []string{}
}

func GetDiskSpace(path string) (total uint64, free uint64, err error) {
return total, free, err
}
40 changes: 40 additions & 0 deletions pkg/utils/disk_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"fmt"
"os"

"golang.org/x/sys/windows"
)

func GetDrives() []string {
var drives []string
for i := 'A'; i <= 'Z'; i++ {
drive := fmt.Sprintf("%c:\\", i)
if _, err := os.Stat(drive); err == nil {
drives = append(drives, drive)
}
}
return drives
}

func GetDiskSpace(path string) (total uint64, free uint64, err error) {
lpDirectoryName, err := windows.UTF16PtrFromString(path)
if err != nil {
return 0, 0, err
}

var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64

err = windows.GetDiskFreeSpaceEx(
lpDirectoryName,
&freeBytesAvailable,
&totalNumberOfBytes,
&totalNumberOfFreeBytes,
)
if err != nil {
return 0, 0, err
}

return totalNumberOfBytes, totalNumberOfFreeBytes, nil
}
31 changes: 31 additions & 0 deletions pkg/utils/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"bytes"
"io/ioutil"
"unicode/utf16"
"unicode/utf8"

"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

func GbkToUtf8(data []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(data), simplifiedchinese.GBK.NewDecoder())
return ioutil.ReadAll(reader)
}

func Utf16ToUtf8(data []byte) string {
u16s := make([]uint16, len(data)/2)
for i := 0; i < len(u16s); i++ {
u16s[i] = uint16(data[2*i]) | uint16(data[2*i+1])<<8
}

runes := utf16.Decode(u16s)
buf := make([]byte, 0, len(runes)*3)
for _, r := range runes {
buf = append(buf, make([]byte, utf8.RuneLen(r))...)
utf8.EncodeRune(buf[len(buf)-utf8.RuneLen(r):], r)
}
return string(buf)
}
32 changes: 32 additions & 0 deletions pkg/windows/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func (i *InstallWSLUbuntuDistroModule) Init() {
}
}

// Move the distro to another drive to avoid excessive system disk space usage.
// If using the import method, you can directly specify the location, and it will later be switched to the import method
type MoveDistroModule struct {
common.KubeModule
}

func (m *MoveDistroModule) Init() {
m.Name = "MoveDistro"

m.Tasks = []task.Interface{
&task.LocalTask{
Name: "MoveDistro",
Action: &MoveDistro{},
},
}
}

type ConfigWslModule struct {
common.KubeModule
}
Expand Down Expand Up @@ -116,3 +133,18 @@ func (u *UninstallOlaresModule) Init() {
},
}
}

type GetDiskPartitionModule struct {
common.KubeModule
}

func (g *GetDiskPartitionModule) Init() {
g.Name = "GetDiskPartition"

g.Tasks = []task.Interface{
&task.LocalTask{
Name: "GetDiskPartition",
Action: &GetDiskPartition{},
},
}
}
Loading

0 comments on commit 754f7e3

Please sign in to comment.