Skip to content

Commit

Permalink
Merge pull request #18 from beclab/feat/change_ip
Browse files Browse the repository at this point in the history
feat: add change ip command
  • Loading branch information
eball authored Nov 5, 2024
2 parents 379d484 + 03cc645 commit a99c9cd
Show file tree
Hide file tree
Showing 28 changed files with 980 additions and 125 deletions.
22 changes: 20 additions & 2 deletions cmd/ctl/options/cli_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewCliTerminusInstallOptions() *CliTerminusInstallOptions {
func (o *CliTerminusInstallOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Version, "version", "v", "", "Set install-wizard version, e.g., 1.7.0, 1.7.0-rc.1, 1.8.0-20240813")
cmd.Flags().StringVar(&o.KubeType, "kube", "k3s", "Set kube type, e.g., k3s or k8s")
cmd.Flags().StringVar(&o.MiniKubeProfile, "profile", common.MinikubeDefaultProfileName, "Set Minikube profile name, only in MacOS platform, defaults to terminus-0")
cmd.Flags().StringVarP(&o.MiniKubeProfile, "profile", "p", "", "Set Minikube profile name, only in MacOS platform, defaults to "+common.MinikubeDefaultProfile)
cmd.Flags().StringVarP(&o.BaseDir, "base-dir", "b", "", "Set pre-install package base dir , default value $HOME/.terminus")
cmd.Flags().StringVar(&o.Manifest, "manifest", "", "Set pre-install package manifest file , default value {base-dir}/versions/v{version}installation.manifest")
}
Expand All @@ -65,5 +65,23 @@ func (o *CliPrepareSystemOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.RegistryMirrors, "registry-mirrors", "r", "", "Docker Container registry mirrors, multiple mirrors are separated by commas")
cmd.Flags().StringVarP(&o.BaseDir, "base-dir", "b", "", "Set pre-install package base dir , default value $HOME/.terminus")
cmd.Flags().StringVar(&o.Manifest, "manifest", "", "Set pre-install package manifest file , default value {base-dir}/versions/v{version}installation.manifest")
cmd.Flags().StringVar(&o.MinikubeProfile, "profile", common.MinikubeDefaultProfileName, "Set Minikube profile name, only in MacOS platform, defaults to terminus-0")
cmd.Flags().StringVarP(&o.MinikubeProfile, "profile", "p", "", "Set Minikube profile name, only in MacOS platform, defaults to "+common.MinikubeDefaultProfile)
}

type ChangeIPOptions struct {
Version string
BaseDir string
WSLDistribution string
MinikubeProfile string
}

func NewChangeIPOptions() *ChangeIPOptions {
return &ChangeIPOptions{}
}

func (o *ChangeIPOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Version, "version", "v", "", "Set install-wizard version, e.g., 1.7.0, 1.7.0-rc.1, 1.8.0-20240813")
cmd.Flags().StringVar(&o.BaseDir, "base-dir", "", "Set uninstall package base dir , defaults to $HOME/.terminus")
cmd.Flags().StringVarP(&o.WSLDistribution, "distribution", "d", "", "Set WSL distribution name, only in Windows platform, defaults to "+common.WSLDefaultDistribution)
cmd.Flags().StringVarP(&o.MinikubeProfile, "profile", "p", "", "Set Minikube profile name, only in MacOS platform, defaults to "+common.MinikubeDefaultProfile)
}
23 changes: 23 additions & 0 deletions cmd/ctl/os/changeip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package os

import (
"bytetrade.io/web3os/installer/cmd/ctl/options"
"bytetrade.io/web3os/installer/pkg/pipelines"
"github.com/spf13/cobra"
"log"
)

func NewCmdChangeIP() *cobra.Command {
o := options.NewChangeIPOptions()
cmd := &cobra.Command{
Use: "change-ip",
Short: "change The IP address of Terminus OS",
Run: func(cmd *cobra.Command, args []string) {
if err := pipelines.ChangeIPPipeline(o); err != nil {
log.Fatalf("error: %v", err)
}
},
}
o.AddFlags(cmd)
return cmd
}
1 change: 1 addition & 0 deletions cmd/ctl/os/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewCmdOs() *cobra.Command {
rootOsCmd.AddCommand(NewCmdPrepare())
rootOsCmd.AddCommand(NewCmdInstallOs())
rootOsCmd.AddCommand(NewCmdUninstallOs())
rootOsCmd.AddCommand(NewCmdChangeIP())
rootOsCmd.AddCommand(NewCmdPrintInfo())

return rootOsCmd
Expand Down
4 changes: 2 additions & 2 deletions pkg/bootstrap/os/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ func (c *ClearOSEnvironmentModule) Init() {
}

removeFiles := &task.RemoteTask{
Name: "RemoveFiles",
Name: "RemoveClusterFiles",
Desc: "Remove cluster files",
Hosts: c.Runtime.GetHostsByRole(common.K8s),
Action: new(RemoveFiles),
Action: new(RemoveClusterFiles),
Parallel: true,
}

Expand Down
105 changes: 103 additions & 2 deletions pkg/bootstrap/os/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package os

import (
"fmt"
"io/fs"
"io/ioutil"
"path"
"path/filepath"
Expand Down Expand Up @@ -348,17 +349,117 @@ func (r *RemoveNodeFiles) Execute(runtime connector.Runtime) error {
return nil
}

type RemoveFiles struct {
type RemoveClusterFiles struct {
common.KubeAction
}

func (r *RemoveFiles) Execute(runtime connector.Runtime) error {
func (r *RemoveClusterFiles) Execute(runtime connector.Runtime) error {
for _, file := range clusterFiles {
_, _ = runtime.GetRunner().Host.SudoCmd(fmt.Sprintf("rm -rf %s", file), false, false)
}
return nil
}

type BackupDirBase struct {
BackupDir string
}

func (b *BackupDirBase) InitPath(runtime connector.Runtime) error {
b.BackupDir = path.Clean(b.BackupDir)
if b.BackupDir == "." {
return errors.New("backup dir is empty")
}
if !strings.HasSuffix(b.BackupDir, runtime.GetWorkDir()) {
logger.Warnf("backup dir does not in workdir %s, prepending the path prefix for safety", runtime.GetWorkDir())
b.BackupDir = path.Join(runtime.GetWorkDir(), b.BackupDir)
}
if err := util.CreateDir(b.BackupDir); err != nil {
return errors.Wrapf(err, "failed to create backup dir %s", b.BackupDir)
}
return nil
}

type BackupFilesToDir struct {
common.KubeAction
*BackupDirBase
Files []string
}

func (a *BackupFilesToDir) Execute(runtime connector.Runtime) error {
if err := a.InitPath(runtime); err != nil {
return err
}
for _, file := range a.Files {
if file == "" {
continue
}
if !util.IsExist(file) {
logger.Warnf("backup target file does not exist: %s", file)
continue
}
if !filepath.IsAbs(file) {
var err error
file, err = filepath.Abs(file)
if err != nil {
return errors.Wrapf(err, "failed to get absolute path of %s", file)
}
}
if err := util.CreateDir(path.Join(a.BackupDir, path.Dir(file))); err != nil {
return errors.Wrapf(err, "failed to create backup dir %s for file %s", path.Dir(file), path.Base(file))
}
logger.Debugf("copying file %s to backup dir %s", file, a.BackupDir)
if err := util.CopyFile(file, path.Join(a.BackupDir, path.Dir(file))); err != nil {
return errors.Wrapf(err, "failed to copy file %s to backup dir", file)
}
}
return nil
}

type ClearBackUpDir struct {
common.KubeAction
*BackupDirBase
}

func (a *ClearBackUpDir) Execute(runtime connector.Runtime) error {
if err := a.InitPath(runtime); err != nil {
return err
}
if err := util.RemoveDir(a.BackupDir); err != nil {
return errors.Wrapf(err, "failed to remove backup dir %s", a.BackupDir)
}
return nil
}

type RestoreBackedUpFiles struct {
common.KubeAction
*BackupDirBase
}

func (a *RestoreBackedUpFiles) Execute(runtime connector.Runtime) error {
if err := a.InitPath(runtime); err != nil {
return err
}
return filepath.WalkDir(a.BackupDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
originalPath, err := filepath.Rel(a.BackupDir, path)
if err != nil {
return errors.Wrapf(err, "failed to get original path of backed up file %s", path)
}
if err := util.CreateDir(filepath.Dir(originalPath)); err != nil {
return errors.Wrapf(err, "failed to create original dir of backed up file %s", originalPath)
}
if err := util.CopyFile(path, originalPath); err != nil {
return errors.Wrapf(err, "failed to restore backed up file %s", originalPath)
}
return nil
})
}

type DaemonReload struct {
common.KubeAction
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ const (
KubeScriptDir = "/usr/local/bin/kube-scripts"
KubeletFlexvolumesPluginsDir = "/usr/libexec/kubernetes/kubelet-plugins/volume/exec"
K3sImageDir = "/var/lib/images"
MinikubeDefaultProfileName = "terminus-0"
MinikubeDefaultProfile = "terminus-0"
MinikubeEtcdCertDir = "/var/lib/minikube/certs/etcd"
WSLDefaultDistribution = "Ubuntu"
RunLockDir = "/var/run/lock"

InstallerScriptsDir = "scripts"
Expand Down Expand Up @@ -174,7 +175,9 @@ const (
)

const (
ManifestImageList = "images.mf"
ManifestImageList = "images.mf"
TerminusStateFilePrepared = ".prepared"
TerminusStateFileInstalled = ".installed"
)

const (
Expand Down Expand Up @@ -220,6 +223,7 @@ const (

CacheHostRedisPassword = "hostredis_password"
CachePreparedState = "prepare_state"
CacheInstalledState = "install_state"

CacheJuiceFsPath = "juicefs_binary_path"
CacheJuiceFsFileName = "juicefs_binary_filename"
Expand Down
35 changes: 28 additions & 7 deletions pkg/common/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -101,12 +102,14 @@ type Argument struct {

Request any `json:"-"`

IsCloudInstance bool `json:"is_cloud_instance"`
MinikubeProfile string `json:"minikube_profile"`
Environment []string `json:"environment"`
BaseDir string `json:"base_dir"`
Manifest string `json:"manifest"`
HostIP string `json:"host_ip"`
IsCloudInstance bool `json:"is_cloud_instance"`
MinikubeProfile string `json:"minikube_profile"`
WSLDistribution string `json:"wsl_distribution"`
Environment []string `json:"environment"`
BaseDir string `json:"base_dir"`
Manifest string `json:"manifest"`
ConsoleLogFileName string `json:"console_log_file_name"`
HostIP string `json:"host_ip"`
}

type PublicNetworkInfo struct {
Expand Down Expand Up @@ -249,6 +252,20 @@ func (a *Argument) SetStorage(storage *Storage) {

func (a *Argument) SetMinikubeProfile(profile string) {
a.MinikubeProfile = profile
if profile == "" && a.SystemInfo.IsDarwin() {
fmt.Printf("\nNote: Minikube profile is not set, will try to use the default profile: \"%s\"\n", MinikubeDefaultProfile)
fmt.Println("if this is not expected, please specify it explicitly by setting the --profile/-p option\n")
a.MinikubeProfile = MinikubeDefaultProfile
}
}

func (a *Argument) SetWSLDistribution(distribution string) {
a.WSLDistribution = distribution
if distribution == "" && a.SystemInfo.IsWindows() {
fmt.Printf("\nNote: WSL distribution is not set, will try to use the default distribution: \"%s\"\n", WSLDefaultDistribution)
fmt.Println("if this is not expected, please specify it explicitly by setting the --distribution/-d option\n")
a.WSLDistribution = WSLDefaultDistribution
}
}

func (a *Argument) SetReverseProxy() {
Expand Down Expand Up @@ -303,6 +320,10 @@ func (a *Argument) SetManifest(manifest string) {
a.Manifest = manifest
}

func (a *Argument) SetConsoleLogFileName(consoleLogFileName string) {
a.ConsoleLogFileName = consoleLogFileName
}

func NewKubeRuntime(flag string, arg Argument) (*KubeRuntime, error) {
loader := NewLoader(flag, arg)
cluster, err := loader.Load()
Expand All @@ -315,7 +336,7 @@ func NewKubeRuntime(flag string, arg Argument) (*KubeRuntime, error) {
}

base := connector.NewBaseRuntime(cluster.Name, connector.NewDialer(),
arg.Debug, arg.IgnoreErr, arg.Provider, arg.BaseDir, arg.TerminusVersion, arg.SystemInfo)
arg.Debug, arg.IgnoreErr, arg.Provider, arg.BaseDir, arg.TerminusVersion, arg.ConsoleLogFileName, arg.SystemInfo)

clusterSpec := &cluster.Spec
defaultCluster, roleGroups := clusterSpec.SetDefaultClusterSpec(arg.InCluster, arg.SystemInfo.IsDarwin())
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/local_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewLocalRuntime(debug, ingoreErr bool) (LocalRuntime, error) {
if err != nil {
return localRuntime, err
}
base := connector.NewBaseRuntime(name, connector.NewDialer(), debug, ingoreErr, nil, "", "", nil)
base := connector.NewBaseRuntime(name, connector.NewDialer(), debug, ingoreErr, nil, "", "", "", nil)

host := connector.NewHost()
host.Name = name
Expand Down
11 changes: 7 additions & 4 deletions pkg/core/connector/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type BaseRuntime struct {
k8sClient *kubernetes.Clientset
}

func NewBaseRuntime(name string, connector Connector, verbose bool, ignoreErr bool, sqlProvider storage.Provider, baseDir string, terminusVersion string, systemInfo Systems) BaseRuntime {
func NewBaseRuntime(name string, connector Connector, verbose bool, ignoreErr bool, sqlProvider storage.Provider, baseDir string, terminusVersion string, consoleLogFileName string, systemInfo Systems) BaseRuntime {
base := BaseRuntime{
ObjName: name,
connector: connector,
Expand All @@ -75,7 +75,7 @@ func NewBaseRuntime(name string, connector Connector, verbose bool, ignoreErr bo
fmt.Printf("[ERRO]: Failed to create work dir: %s\n", err)
os.Exit(1)
}
if err := base.InitLogger(); err != nil {
if err := base.InitLogger(consoleLogFileName); err != nil {
fmt.Printf("[ERRO]: Failed to init log entry: %s\n", err)
os.Exit(1)
}
Expand Down Expand Up @@ -229,10 +229,13 @@ func (b *BaseRuntime) HostIsDeprecated(host Host) bool {
return false
}

func (b *BaseRuntime) InitLogger() error {
func (b *BaseRuntime) InitLogger(consoleLogFileName string) error {
if consoleLogFileName == "" {
consoleLogFileName = "install.log"
}
// the JSON-structured logs under .terminus/logs/yyyy-mm-dd_hh-mm-ss.log
// and the console formatted logs under .terminus/versions/v{version}/install.log (for backward compatibility)
logger.InitLog(path.Join(b.baseDir, "logs"), path.Join(b.installerDir, "logs"))
logger.InitLog(path.Join(b.baseDir, "logs"), path.Join(b.installerDir, "logs", consoleLogFileName))
return nil
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/core/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var logger *zap.SugaredLogger

var FatalMessagePrefix = "[FATAL] "

func InitLog(jsonLogDir, consoleLogDir string) {
for _, logDir := range []string{jsonLogDir, consoleLogDir} {
func InitLog(jsonLogDir, consoleLogFilePath string) {
for _, logDir := range []string{jsonLogDir, path.Dir(consoleLogFilePath)} {
found, err := isDirExist(logDir)
if err != nil {
fmt.Println("log dir found error", err)
Expand All @@ -37,8 +37,7 @@ func InitLog(jsonLogDir, consoleLogDir string) {
if err != nil {
panic(err)
}
consoleLogFileName := path.Join(consoleLogDir, "install.log")
consoleLogFile, err := os.OpenFile(consoleLogFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, common.FileMode0755)
consoleLogFile, err := os.OpenFile(consoleLogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, common.FileMode0755)
if err != nil {
panic(err)
}
Expand Down
16 changes: 2 additions & 14 deletions pkg/core/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,8 @@ func Mkdir(dirName string) error {
}

func CopyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()

dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()

_, err = io.Copy(dstFile, srcFile)
if err != nil {
cmd := exec.Command("cp", src, dst)
if err := cmd.Run(); err != nil {
return err
}
return nil
Expand Down
Loading

0 comments on commit a99c9cd

Please sign in to comment.