-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): Add tools installer to automatically install tools.
- Loading branch information
1 parent
b7a46b2
commit c397ed2
Showing
10 changed files
with
269 additions
and
3 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 |
---|---|---|
|
@@ -147,4 +147,9 @@ var err error | |
|
||
return nil | ||
}, | ||
}) | ||
}) | ||
|
||
func init(){ | ||
rootCmd.AddCommand(metaUpgradeCmd) | ||
} | ||
|
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,114 @@ | ||
// Copyright © 2018 NAME HERE <EMAIL ADDRESS> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cheynewallace/tabby" | ||
"github.com/kyokomi/emoji" | ||
"github.com/naveego/bosun/pkg/bosun" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var toolsCmd = addCommand(rootCmd, &cobra.Command{ | ||
Use: "tools", | ||
Short: "Commands for listing and installing tools.", | ||
|
||
}) | ||
|
||
var toolsListCmd = addCommand(toolsCmd, &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists known tools", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
b := mustGetBosun() | ||
tools := b.GetTools() | ||
|
||
byFromPath := map[string][]bosun.ToolDef{} | ||
for _, tool := range tools { | ||
byFromPath[tool.FromPath] = append(byFromPath[tool.FromPath], tool) | ||
} | ||
|
||
for fromPath, tools := range byFromPath { | ||
fmt.Printf("Defined in %s:\n", fromPath) | ||
t := tabby.New() | ||
t.AddHeader("Name", "Installed", "Location", "Description") | ||
|
||
for _, tool := range tools { | ||
|
||
var installInfo string | ||
var location string | ||
executable, installErr := tool.GetExecutable() | ||
if installErr != nil { | ||
if tool.Installer != nil { | ||
if _, ok := tool.GetInstaller(); ok { | ||
installInfo = emoji.Sprint(":cloud:") | ||
location = "(installable)" | ||
} else { | ||
installInfo = emoji.Sprintf(":x:") | ||
} | ||
} | ||
} else { | ||
installInfo = emoji.Sprintf(":heavy_check_mark:") | ||
location = executable | ||
} | ||
|
||
t.AddLine(tool.Name, installInfo, location, tool.Description) | ||
} | ||
t.Print() | ||
fmt.Println() | ||
} | ||
}, | ||
}) | ||
|
||
var toolsInstallCmd = addCommand(toolsCmd, &cobra.Command{ | ||
Use: "install {tool}", | ||
Short: "Installs a tool.", | ||
SilenceUsage:true, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
b := mustGetBosun() | ||
tools := b.GetTools() | ||
var tool bosun.ToolDef | ||
var ok bool | ||
name := args[0] | ||
for _, tool = range tools { | ||
if tool.Name == name { | ||
ok = true | ||
break | ||
} | ||
} | ||
if !ok { | ||
return errors.Errorf("no tool found with name %q", name) | ||
} | ||
|
||
ctx := b.NewContext() | ||
|
||
installer, ok := tool.GetInstaller() | ||
if !ok { | ||
return errors.Errorf("could not get installer for %q", name) | ||
} | ||
|
||
err := installer.Execute(ctx) | ||
|
||
return err | ||
}, | ||
}) | ||
|
||
|
||
func init(){ | ||
rootCmd.AddCommand(metaUpgradeCmd) | ||
} | ||
|
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
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
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,132 @@ | ||
package bosun | ||
|
||
import ( | ||
"github.com/hashicorp/go-getter" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type ToolDef struct { | ||
FromPath string `yaml:"-"` | ||
Name string `yaml:"name"` | ||
Description string `yaml:"description"` | ||
URL string `yaml:"url,omitempty"` | ||
Cmd map[string]string `yaml:"cmd,omitempty"` | ||
Installer map[string]Installer `yaml:"installer,omitempty"` | ||
} | ||
|
||
type Installer struct { | ||
Script string `yaml:"script,omitempty"` | ||
Getter *GetterConfig `yaml:"getter,omitempty"` | ||
} | ||
|
||
type GetterConfig struct { | ||
URL string `yaml:"url"` | ||
Mappings map[string]string `yaml:"mappings"` | ||
} | ||
|
||
func (t ToolDef) GetExecutable() (string, error) { | ||
|
||
var ok bool | ||
var cmd string | ||
for key, val := range t.Cmd { | ||
if strings.Contains(key, runtime.GOOS) { | ||
cmd = val | ||
ok = true | ||
} | ||
} | ||
if !ok { | ||
return "", errors.Errorf("no cmd registered for os %q", runtime.GOOS) | ||
} | ||
|
||
ex, err := exec.LookPath(cmd) | ||
return ex, err | ||
} | ||
|
||
func (t ToolDef) GetInstaller() (*Installer, bool) { | ||
|
||
if t.Installer == nil { | ||
return nil, false | ||
} | ||
|
||
var ok bool | ||
var installer Installer | ||
for key, val := range t.Installer { | ||
if strings.Contains(key, runtime.GOOS) { | ||
installer = val | ||
ok = true | ||
} | ||
} | ||
|
||
if !ok { | ||
return nil, false | ||
} | ||
|
||
return &installer, true | ||
} | ||
|
||
func (t ToolDef) RunInstall(ctx BosunContext) error { | ||
installer, ok := t.GetInstaller() | ||
if !ok { | ||
return errors.New("no installer script available") | ||
} | ||
|
||
err := installer.Execute(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = t.GetExecutable() | ||
if err != nil { | ||
return errors.Wrap(err, "install completed, but executable still not found") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i Installer) Execute(ctx BosunContext) error { | ||
if i.Script != "" { | ||
cmd := &Command{Script:i.Script} | ||
_, err := cmd.Execute(ctx, CommandOpts{StreamOutput:true}) | ||
return err | ||
} | ||
|
||
if i.Getter != nil { | ||
tmp, err := ioutil.TempDir(os.TempDir(), "bosun-install") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx.Log.Debugf("Downloading from %s to %s", i.Getter.URL, tmp) | ||
|
||
defer func(){ | ||
ctx.Log.Debugf("Deleting %s", tmp) | ||
os.RemoveAll(tmp) | ||
} () | ||
|
||
err = getter.Get(tmp, i.Getter.URL) | ||
if err != nil { | ||
return errors.Errorf("error getting content from %q: %s", i.Getter.URL, err) | ||
} | ||
ctx.Log.Debugf("Download complete.") | ||
|
||
for from, to := range i.Getter.Mappings { | ||
|
||
from = filepath.Join(tmp, from) | ||
to = os.ExpandEnv(to) | ||
|
||
ctx.Log.Debugf("Moving %s to %s.") | ||
err = os.Rename(from, to) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return errors.New("no install strategy defined") | ||
} |
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