Skip to content

Commit

Permalink
Add generic tools installation support (#252)
Browse files Browse the repository at this point in the history
* Add generic installer support
  • Loading branch information
LinuxSuRen committed May 22, 2022
1 parent c3ef37d commit 70e6a51
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
56 changes: 56 additions & 0 deletions pkg/os/apt/common.go
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
}
8 changes: 8 additions & 0 deletions pkg/os/core/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

// Installer is the interface of a installer
// Deprecated use AdvanceInstaller instead
type Installer interface {
Available() bool
Install() error
Expand All @@ -11,6 +12,13 @@ type Installer interface {
Stop() error
}

// AdvanceInstaller is a generic installer
type AdvanceInstaller interface {
Installer

IsService() bool
}

// InstallerRegistry is the interface of install registry
type InstallerRegistry interface {
Registry(string, Installer)
Expand Down
134 changes: 134 additions & 0 deletions pkg/os/generic_installer.go
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
}
16 changes: 14 additions & 2 deletions pkg/os/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/linuxsuren/http-downloader/pkg/os/core"
"github.com/linuxsuren/http-downloader/pkg/os/docker"
"github.com/linuxsuren/http-downloader/pkg/os/yum"
"github.com/mitchellh/go-homedir"
"path"
"path/filepath"
)

// DefaultInstallerRegistry is the default installer registry
Expand All @@ -24,6 +27,15 @@ func init() {
apt.SetInstallerRegistry(defaultInstallerRegistry)
brew.SetInstallerRegistry(defaultInstallerRegistry)
docker.SetInstallerRegistry(defaultInstallerRegistry)

var userHome string
var err error
if userHome, err = homedir.Dir(); err == nil {
configDir := path.Join(userHome, "/.config/hd-home")
if err = GenericInstallerRegistry(filepath.Join(configDir, "config/generic.yaml"), defaultInstallerRegistry); err != nil {
fmt.Println(err)
}
}
}

// Registry registries a DockerInstaller
Expand All @@ -45,8 +57,8 @@ func GetInstallers(name string) (installers []core.Installer, ok bool) {
// HasPackage finds if the target package installer exist
func HasPackage(name string) bool {
if installers, ok := GetInstallers(name); ok {
for _, installer := range installers {
if installer.Available() {
for _, item := range installers {
if item.Available() {
return true
}
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/os/yum/common.go
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
}

0 comments on commit 70e6a51

Please sign in to comment.