generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic tools installation support (#252)
* Add generic installer support
- Loading branch information
1 parent
c3ef37d
commit 70e6a51
Showing
6 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package apt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/linuxsuren/http-downloader/pkg/exec" | ||
"runtime" | ||
) | ||
|
||
// CommonInstaller is the installer of Conntrack in CentOS | ||
type CommonInstaller struct { | ||
Name string | ||
} | ||
|
||
// Available check if support current platform | ||
func (d *CommonInstaller) Available() (ok bool) { | ||
if runtime.GOOS == "linux" { | ||
_, err := exec.LookPath("apt-get") | ||
ok = err == nil | ||
} | ||
return | ||
} | ||
|
||
// Install installs the Conntrack | ||
func (d *CommonInstaller) Install() (err error) { | ||
if err = exec.RunCommand("apt-get", "update", "-y"); err != nil { | ||
return | ||
} | ||
if err = exec.RunCommand("apt-get", "install", "-y", d.Name); err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
// Uninstall uninstalls the Conntrack | ||
func (d *CommonInstaller) Uninstall() (err error) { | ||
err = exec.RunCommand("apt-get", "remove", "-y", d.Name) | ||
return | ||
} | ||
|
||
// WaitForStart waits for the service be started | ||
func (d *CommonInstaller) WaitForStart() (ok bool, err error) { | ||
ok = true | ||
return | ||
} | ||
|
||
// Start starts the Conntrack service | ||
func (d *CommonInstaller) Start() error { | ||
fmt.Println("not supported yet") | ||
return nil | ||
} | ||
|
||
// Stop stops the Conntrack service | ||
func (d *CommonInstaller) Stop() error { | ||
fmt.Println("not supported yet") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package os | ||
|
||
import ( | ||
"fmt" | ||
"github.com/linuxsuren/http-downloader/pkg/exec" | ||
"github.com/linuxsuren/http-downloader/pkg/os/apt" | ||
"github.com/linuxsuren/http-downloader/pkg/os/core" | ||
"github.com/linuxsuren/http-downloader/pkg/os/yum" | ||
"gopkg.in/yaml.v3" | ||
"io/ioutil" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type genericPackages struct { | ||
Version string `yaml:"version"` | ||
Packages []genericPackage `yaml:"packages"` | ||
} | ||
|
||
type preInstall struct { | ||
IssuePrefix string `yaml:"issuePrefix"` | ||
Cmd CmdWithArgs `yaml:"cmd"` | ||
} | ||
|
||
type genericPackage struct { | ||
Alias string `yaml:"alias"` | ||
Name string `yaml:"name"` | ||
OS string `yaml:"os"` | ||
PackageManager string `yaml:"packageManager"` | ||
PreInstall []preInstall `yaml:"preInstall"` | ||
Dependents []string `yaml:"dependents"` | ||
InstallCmd CmdWithArgs `yaml:"install"` | ||
UninstallCmd CmdWithArgs `yaml:"uninstall"` | ||
Service bool `yaml:"isService"` | ||
StartCmd CmdWithArgs `yaml:"start"` | ||
StopCmd CmdWithArgs `yaml:"stop"` | ||
|
||
CommonInstaller core.Installer | ||
} | ||
|
||
// CmdWithArgs is a command with arguments | ||
type CmdWithArgs struct { | ||
Cmd string `yaml:"cmd"` | ||
Args []string `yaml:"args"` | ||
} | ||
|
||
func parseGenericPackages(configFile string, genericPackages *genericPackages) (err error) { | ||
var data []byte | ||
if data, err = ioutil.ReadFile(configFile); err != nil { | ||
err = fmt.Errorf("cannot read config file [%s], error: %v", configFile, err) | ||
return | ||
} | ||
|
||
if err = yaml.Unmarshal(data, genericPackages); err != nil { | ||
err = fmt.Errorf("failed to parse config file [%s], error: %v", configFile, err) | ||
return | ||
} | ||
return | ||
} | ||
|
||
// GenericInstallerRegistry registries a generic installer | ||
func GenericInstallerRegistry(configFile string, registry core.InstallerRegistry) (err error) { | ||
genericPackages := &genericPackages{} | ||
if err = parseGenericPackages(configFile, genericPackages); err != nil { | ||
return | ||
} | ||
|
||
// registry all the packages | ||
for i := range genericPackages.Packages { | ||
genericPackage := genericPackages.Packages[i] | ||
|
||
switch genericPackage.PackageManager { | ||
case "apt-get": | ||
genericPackage.CommonInstaller = &apt.CommonInstaller{Name: genericPackage.Name} | ||
case "yum": | ||
genericPackage.CommonInstaller = &yum.CommonInstaller{Name: genericPackage.Name} | ||
} | ||
|
||
registry.Registry(genericPackage.Name, &genericPackage) | ||
} | ||
return | ||
} | ||
|
||
func (i *genericPackage) Available() (ok bool) { | ||
if i.CommonInstaller != nil { | ||
ok = i.CommonInstaller.Available() | ||
} | ||
return | ||
} | ||
func (i *genericPackage) Install() (err error) { | ||
for index := range i.PreInstall { | ||
preInstall := i.PreInstall[index] | ||
|
||
if preInstall.IssuePrefix != "" && runtime.GOOS == "linux" { | ||
var data []byte | ||
if data, err = ioutil.ReadFile("/etc/issue"); err != nil { | ||
return | ||
} | ||
|
||
if strings.HasPrefix(string(data), preInstall.IssuePrefix) { | ||
if err = exec.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
if i.CommonInstaller != nil { | ||
err = i.CommonInstaller.Install() | ||
} else { | ||
err = fmt.Errorf("not support yet") | ||
} | ||
return | ||
} | ||
func (i *genericPackage) Uninstall() (err error) { | ||
if i.CommonInstaller != nil { | ||
err = i.CommonInstaller.Uninstall() | ||
} else { | ||
err = fmt.Errorf("not support yet") | ||
} | ||
return | ||
} | ||
func (i *genericPackage) IsService() bool { | ||
return i.Service | ||
} | ||
func (i *genericPackage) WaitForStart() (bool, error) { | ||
return true, nil | ||
} | ||
func (i *genericPackage) Start() error { | ||
return nil | ||
} | ||
func (i *genericPackage) Stop() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package yum | ||
|
||
import ( | ||
"fmt" | ||
"github.com/linuxsuren/http-downloader/pkg/exec" | ||
"runtime" | ||
) | ||
|
||
// CommonInstaller is the installer of Conntrack in CentOS | ||
type CommonInstaller struct { | ||
Name string | ||
} | ||
|
||
// Available check if support current platform | ||
func (d *CommonInstaller) Available() (ok bool) { | ||
if runtime.GOOS == "linux" { | ||
_, err := exec.LookPath("yum") | ||
ok = err == nil | ||
} | ||
return | ||
} | ||
|
||
// Install installs the Conntrack | ||
func (d *CommonInstaller) Install() (err error) { | ||
if err = exec.RunCommand("yum", "update", "-y"); err != nil { | ||
return | ||
} | ||
if err = exec.RunCommand("yum", "install", "-y", d.Name); err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
// Uninstall uninstalls the Conntrack | ||
func (d *CommonInstaller) Uninstall() (err error) { | ||
err = exec.RunCommand("yum", "remove", "-y", d.Name) | ||
return | ||
} | ||
|
||
// WaitForStart waits for the service be started | ||
func (d *CommonInstaller) WaitForStart() (ok bool, err error) { | ||
ok = true | ||
return | ||
} | ||
|
||
// Start starts the Conntrack service | ||
func (d *CommonInstaller) Start() error { | ||
fmt.Println("not supported yet") | ||
return nil | ||
} | ||
|
||
// Stop stops the Conntrack service | ||
func (d *CommonInstaller) Stop() error { | ||
fmt.Println("not supported yet") | ||
return nil | ||
} |