Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nornir-automation/gornir into lic…
Browse files Browse the repository at this point in the history
…ense
  • Loading branch information
dbarrosop committed Jun 9, 2019
2 parents 4a63ad0 + 1b41200 commit c861d11
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 113 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: go
sudo: false
go:
- 1.11.x
- 1.12.x
- tip
os:
- linux
- osx
matrix:
allow_failures:
- go: tip
fast_finish: true
env:
- GO111MODULE=on GOPROXY=https://proxy.golang.org
script:
- go test -v ./... -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[![GoDoc](https://godoc.org/github.com/nornir-automation/gornir?status.svg)](http://godoc.org/github.com/nornir-automation/gornir)
[![Build Status](https://travis-ci.org/nornir-automation/gornir.svg?branch=master)](https://travis-ci.org/nornir-automation/gornir)
[![codecov](https://codecov.io/gh/nornir-automation/gornir/branch/master/graph/badge.svg)](https://codecov.io/gh/nornir-automation/gornir)
[![Go Report Card](https://goreportcard.com/badge/github.com/nornir-automation/gornir)](https://goreportcard.com/report/github.com/nornir-automation/gornir)

gornir
======
Expand All @@ -22,25 +25,24 @@ import (
)

func main() {
logger := logger.NewLogrus(false)
log := logger.NewLogrus(false)

inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
}
gr := gornir.New().WithInventory(inv).WithLogger(log)

results, err := gr.RunSync(
"What's my ip?",
runner.Parallel(),
&task.RemoteCommand{Command: "ip addr | grep \\/24 | awk '{ print $2 }'"},
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}
output.RenderResults(os.Stdout, results, true)
}
Expand Down
76 changes: 38 additions & 38 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
// Package provides a pluggable framework with inventory management to help operate collections of devices.
// It's similar to https://github.com/nornir-automation/nornir/ but in golang.
// Package gornir provides a pluggable framework with inventory management to help operate
// collections of devices.
// It's similar to https://github.com/nornir-automation/nornir/ but in Go.
//
// The goal is to be able to operate on many devices with little effort. For instance:
//
// package main
//
// import (
// "os"
//
// "github.com/nornir-automation/gornir/pkg/gornir"
// "github.com/nornir-automation/gornir/pkg/plugins/inventory"
// "github.com/nornir-automation/gornir/pkg/plugins/logger"
// "github.com/nornir-automation/gornir/pkg/plugins/output"
// "github.com/nornir-automation/gornir/pkg/plugins/runner"
// "github.com/nornir-automation/gornir/pkg/plugins/task"
// )
//
// func main() {
// logger := logger.NewLogrus(false)
//
// inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
// if err != nil {
// logger.Fatal(err)
// }
//
// gr := &gornir.Gornir{
// Inventory: inventory,
// Logger: logger,
// }
//
// results, err := gr.RunSync(
// "What's my ip?",
// runner.Parallel(),
// &task.RemoteCommand{Command: "ip addr | grep \\/24 | awk '{ print $2 }'"},
// )
// if err != nil {
// logger.Fatal(err)
// }
// output.RenderResults(os.Stdout, results, true)
// }
// package main
//
// import (
// "os"
//
// "github.com/nornir-automation/gornir/pkg/gornir"
// "github.com/nornir-automation/gornir/pkg/plugins/inventory"
// "github.com/nornir-automation/gornir/pkg/plugins/logger"
// "github.com/nornir-automation/gornir/pkg/plugins/output"
// "github.com/nornir-automation/gornir/pkg/plugins/runner"
// "github.com/nornir-automation/gornir/pkg/plugins/task"
// )
//
// func main() {
// log := logger.NewLogrus(false)
//
// file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
// plugin := inventory.FromYAML{HostsFile: file}
// inv, err := plugin.Create()
// if err != nil {
// log.Fatal(err)
// }
//
// gr := gornir.New().WithInventory(inv).WithLogger(log)
//
// results, err := gr.RunSync(
// "What's my ip?",
// runner.Parallel(),
// &task.RemoteCommand{Command: "ip addr | grep \\/24 | awk '{ print $2 }'"},
// )
// if err != nil {
// log.Fatal(err)
// }
// output.RenderResults(os.Stdout, results, true)
// }
//
// would render:
//
Expand Down
17 changes: 8 additions & 9 deletions examples/1_simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ import (
)

func main() {
logger := logger.NewLogrus(false) // instantiate a logger plugin
// Instantiate a logger plugin
log := logger.NewLogrus(false)

// Load the inventory using the FromYAMLFile plugin
inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

// Instantiate Gornir
gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
}
gr := gornir.New().WithInventory(inv).WithLogger(log)

// Following call is going to execute the task over all the hosts using the runner.Parallel runner.
// Said runner is going to handle the parallelization for us. Gornir.RunS is also going to block
Expand All @@ -36,7 +35,7 @@ func main() {
&task.RemoteCommand{Command: "ip addr | grep \\/24 | awk '{ print $2 }'"},
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

// next call is going to print the result on screen
Expand Down
24 changes: 12 additions & 12 deletions examples/2_simple_with_filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package main

import (
"context"
"os"

"github.com/nornir-automation/gornir/pkg/gornir"
Expand All @@ -14,32 +13,33 @@ import (
)

func main() {
logger := logger.NewLogrus(false)
// Instantiate a logger plugin
log := logger.NewLogrus(false)

inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
// Load the inventory using the FromYAMLFile plugin
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
}

gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
log.Fatal(err)
}

// define a function we will use to filter the hosts
filter := func(ctx context.Context, h *gornir.Host) bool {
filter := func(h *gornir.Host) bool {
return h.Hostname == "dev1.group_1" || h.Hostname == "dev4.group_2"
}

gr := gornir.New().WithInventory(inv).WithLogger(log)

// Before calling Gornir.RunS we call Gornir.Filter and pass the function defined
// above. This will narrow down the inventor to the hosts matching the filter
results, err := gr.Filter(context.Background(), filter).RunSync(
results, err := gr.Filter(filter).RunSync(
"What's my ip?",
runner.Parallel(),
&task.RemoteCommand{Command: "ip addr | grep \\/24 | awk '{ print $2 }'"},
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

output.RenderResults(os.Stdout, results, true)
Expand Down
18 changes: 9 additions & 9 deletions examples/3_grouped_simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ func (c *checkMemoryAndCPU) Run(ctx context.Context, wg *sync.WaitGroup, jp *gor
}

func main() {
// main is business as usual
logger := logger.NewLogrus(false)
// Instantiate a logger plugin
log := logger.NewLogrus(false)

inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
// Load the inventory using the FromYAMLFile plugin
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
}
gr := gornir.New().WithInventory(inv).WithLogger(log)

results, err := gr.RunSync(
"Let's run a couple of commands",
runner.Parallel(),
&checkMemoryAndCPU{},
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}
output.RenderResults(os.Stdout, results, true)
}
17 changes: 9 additions & 8 deletions examples/4_advanced_1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import (
)

func main() {
logger := logger.NewLogrus(false)
// Instantiate a logger plugin
log := logger.NewLogrus(false)

inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
// Load the inventory using the FromYAMLFile plugin
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
}
gr := gornir.New().WithInventory(inv).WithLogger(log)

results := make(chan *gornir.JobResult, len(gr.Inventory.Hosts))

Expand All @@ -41,7 +42,7 @@ func main() {
results,
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

// Next call will block until the runner is done
Expand Down
17 changes: 9 additions & 8 deletions examples/5_advanced_2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ import (
)

func main() {
logger := logger.NewLogrus(false)
// Instantiate a logger plugin
log := logger.NewLogrus(false)

inventory, err := inventory.FromYAMLFile("/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml")
// Load the inventory using the FromYAMLFile plugin
file := "/go/src/github.com/nornir-automation/gornir/examples/hosts.yaml"
plugin := inventory.FromYAML{HostsFile: file}
inv, err := plugin.Create()
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

gr := &gornir.Gornir{
Inventory: inventory,
Logger: logger,
}
gr := gornir.New().WithInventory(inv).WithLogger(log)

results := make(chan *gornir.JobResult, len(gr.Inventory.Hosts))

Expand All @@ -40,7 +41,7 @@ func main() {
results,
)
if err != nil {
logger.Fatal(err)
log.Fatal(err)
}

// This goroutine is going to wait for the runner
Expand Down
34 changes: 29 additions & 5 deletions pkg/gornir/gornir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package Gornir implements the core functionality and define the needed interfaces to integrate with the framework
// Package gornir implements the core functionality and define the needed
// interfaces to integrate with the framework
package gornir

import (
Expand All @@ -16,15 +17,38 @@ type Gornir struct {
Logger Logger // Logger for the object
}

// Filter filters the hosts in the inventory returning a copy of the current
// Gornir instance but with only the hosts that passed the filter
func (gr *Gornir) Filter(ctx context.Context, f FilterFunc) *Gornir {
// New is a Gornir constructor. It is currently no different that new,
// however is a placeholder for any future defaults.
func New() *Gornir {
return new(Gornir)
}

// WithInventory creates a new Gornir with an Inventory.
func (gr *Gornir) WithInventory(inv Inventory) *Gornir {
return &Gornir{
Inventory: gr.Inventory.Filter(ctx, f),
Inventory: &inv,
Logger: gr.Logger,
}
}

// Filter creates a new Gornir with a filtered Inventory.
// It filters the hosts in the inventory returning a copy of the current
// Gornir instance but with only the hosts that passed the filter.
func (gr *Gornir) Filter(f FilterFunc) *Gornir {
return &Gornir{
Inventory: gr.Inventory.Filter(f),
Logger: gr.Logger,
}
}

// WithLogger creates a new Gornir with a Logger.
func (gr *Gornir) WithLogger(l Logger) *Gornir {
return &Gornir{
Inventory: gr.Inventory,
Logger: l,
}
}

// RunSync will execute the task over the hosts in the inventory using the given runner.
// This function will block until all the tasks are completed.
func (gr *Gornir) RunSync(title string, runner Runner, task Task) (chan *JobResult, error) {
Expand Down
Loading

0 comments on commit c861d11

Please sign in to comment.