play

package
v0.0.0-...-9212266 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2018 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VERSION = "1.0.0"
)

VERSION defines current package version

Variables

View Source
var (
	ErrEmptyClient  = errors.New("Client is empty.")
	ErrEmptyCommand = errors.New("Command is empty.")
	ErrRunning      = errors.New("Command alreay running.")
	ErrNotRunning   = errors.New("Command not running or has finished.")
	ErrConnected    = errors.New("Client has connected.")
	ErrNotConnected = errors.New("Client not connected.")
	ErrOpened       = errors.New("Session has opened.")
	ErrNotOpened    = errors.New("Session not opened.")
	ErrNotSupported = errors.New("Not supported.")
)

Functions

func Errorf

func Errorf(format string, v ...interface{})

func Infof

func Infof(format string, v ...interface{})

func LocalTarCmdArgs

func LocalTarCmdArgs(path, exclude string) []string

LocalTarCmdArgs returns command to be run on local host.

func MaskUserHostWithPasswd

func MaskUserHostWithPasswd(host string) string

MaskUserHostWithPasswd masks passwd for sensitive

func NewTarStreamReader

func NewTarStreamReader(cwd, path, exclude string) (io.Reader, error)

NewTarStreamReader creates a tar stream reader from a local path. TODO: Refactor. Use "archive/tar" instead.

func PadStringWithTimestamp

func PadStringWithTimestamp(s string, n int) string

PadStringWithTimestamp returns new string leads with time

func RemoteTarCommand

func RemoteTarCommand(dir string) string

RemoteTarCommand returns command to be run on remote SSH host to properly receive the created TAR stream. TODO: Check for relative directory.

func ResolveLocalPath

func ResolveLocalPath(cwd, path, env string) (string, error)

ResolveLocalPath determines local file path related to cwd

func Warnf

func Warnf(format string, v ...interface{})

Types

type Book

type Book struct {
	// contains filtered or unexported fields
}

Book represents a set of commands to be run.

type Books

type Books struct {
	Names []string
	// contains filtered or unexported fields
}

Books is a list of user-defined books

func (*Books) Get

func (b *Books) Get(name string) ([]string, bool)

func (*Books) UnmarshalYAML

func (b *Books) UnmarshalYAML(unmarshal func(interface{}) error) error

type Client

type Client interface {
	io.WriteCloser

	Connect(host string) error
	Run(book *Book) error
	Wait() error
	Stdin() io.WriteCloser
	Stderr() io.Reader
	Stdout() io.Reader
	Signal(os.Signal) error
	Prompt() string
	LastError() error
}

Client is an interface wraps the connection/sessions.

type Command

type Command struct {
	Name    string            `yaml:"-"`       // Command name.
	Desc    string            `yaml:"desc"`    // Command description.
	Run     string            `yaml:"run"`     // Command(s) to be run remotelly.
	Script  string            `yaml:"script"`  // Load command(s) from script and run it remotelly.
	Uploads map[string]Upload `yaml:"uploads"` // See Upload struct.
	Serial  int               `yaml:"serial"`  // Max number of clients processing a book in parallel.
	Locally bool              `yaml:"locally"` // Command(s) to be run locally.
	Stdin   bool              `yaml:"stdin"`   // Attach localhost STDOUT to remote commands' STDIN?
	Once    bool              `yaml:"once"`    // The command should be run "once" (randomly on one host only).
}

Command represents command(s) to be run remotely.

type Commands

type Commands struct {
	Names []string
	// contains filtered or unexported fields
}

Commands is a list of user-defined commands

func (*Commands) Get

func (c *Commands) Get(name string) (Command, bool)

func (*Commands) UnmarshalYAML

func (c *Commands) UnmarshalYAML(unmarshal func(interface{}) error) error

type EnvVar

type EnvVar struct {
	Key   string
	Value string
}

EnvVar represents an environment variable

func (EnvVar) AsExport

func (e EnvVar) AsExport() string

AsExport returns the environment variable as a bash export statement

func (EnvVar) String

func (e EnvVar) String() string

type EnvVars

type EnvVars []*EnvVar

EnvVars is a list of environment variables that maps to a YAML map, but maintains order, enabling late variables to reference early variables.

func (*EnvVars) AsExport

func (e *EnvVars) AsExport() string

func (*EnvVars) ResolveValues

func (e *EnvVars) ResolveValues() error

func (*EnvVars) Set

func (e *EnvVars) Set(key, value string)

Set key to be equal value in this list.

func (EnvVars) Slice

func (e EnvVars) Slice() []string

func (*EnvVars) UnmarshalYAML

func (e *EnvVars) UnmarshalYAML(unmarshal func(interface{}) error) error

type ErrBook

type ErrBook struct {
	Book   *Book
	Reason string
}

ErrBook defines book error

func (ErrBook) Error

func (e ErrBook) Error() string

type ErrConnect

type ErrConnect struct {
	Host   string
	User   string
	Reason string
}

ErrConnect defines connection error with reason

func (ErrConnect) Error

func (e ErrConnect) Error() string

Error implements error

type ErrMustUpgrade

type ErrMustUpgrade struct {
	Msg string
}

ErrMustUpgrade defines upgrade error for old client

func (ErrMustUpgrade) Error

func (e ErrMustUpgrade) Error() string

type ErrPlayfileVersion

type ErrPlayfileVersion struct {
	Msg string
}

ErrPlayfileVersion defines error for unsupported playfile version

func (ErrPlayfileVersion) Error

func (e ErrPlayfileVersion) Error() string

type LocalClient

type LocalClient struct {
	// contains filtered or unexported fields
}

LocalClient is a wrapper over the local host.

func NewLocalClient

func NewLocalClient(env string) *LocalClient

NewLocalClient creates a local client with given env, and export PLAY_HOST=localhost

func (*LocalClient) Close

func (c *LocalClient) Close() error

func (*LocalClient) Connect

func (c *LocalClient) Connect(_ string) error

Connect returns current user for local

func (*LocalClient) LastError

func (c *LocalClient) LastError() error

func (*LocalClient) Prompt

func (c *LocalClient) Prompt() string

func (*LocalClient) Run

func (c *LocalClient) Run(book *Book) error

Run execs book on local

func (*LocalClient) Signal

func (c *LocalClient) Signal(sig os.Signal) error

func (*LocalClient) Stderr

func (c *LocalClient) Stderr() io.Reader

func (*LocalClient) Stdin

func (c *LocalClient) Stdin() io.WriteCloser

func (*LocalClient) Stdout

func (c *LocalClient) Stdout() io.Reader

func (*LocalClient) Wait

func (c *LocalClient) Wait() error

Wait waits book to finish or return from error

func (*LocalClient) Write

func (c *LocalClient) Write(p []byte) (n int, err error)

type Network

type Network struct {
	Envs      EnvVars  `yaml:"env"`
	Hosts     []string `yaml:"hosts"`
	Inventory string   `yaml:"inventory"`
	Bastion   string   `yaml:"bastion"` // Jump host for the environment

	// Should these live on Hosts too? We'd have to change []string to struct, even in Playfile.
	User         string `yaml:"user"`
	Passwd       string `yaml:"passwd"`
	Port         int    `yaml:"port"`
	IdentityFile string `yaml:"identity_file"`
}

Network is group of hosts with extra custom env vars.

func (Network) ParseInventory

func (n Network) ParseInventory() ([]string, error)

ParseInventory runs the inventory command, if provided, and appends the command's output lines to the manually defined list of hosts.

type Networks

type Networks struct {
	Names []string
	// contains filtered or unexported fields
}

Networks is a list of user-defined networks

func (*Networks) Get

func (n *Networks) Get(name string) (Network, bool)

func (*Networks) UnmarshalYAML

func (n *Networks) UnmarshalYAML(unmarshal func(interface{}) error) error

type Play

type Play struct {
	// contains filtered or unexported fields
}

Play holds all books for running

func New

func New(config *Playfile) (*Play, error)

New returns *Play with config

func (*Play) Debug

func (play *Play) Debug(value bool)

func (*Play) Prompt

func (play *Play) Prompt(value bool)

func (*Play) Run

func (play *Play) Run(network *Network, envs EnvVars, commands ...*Command) error

Run runs set of commands on multiple hosts defined by network sequentially. TODO: This megamoth method needs a big refactor and should be split

to multiple smaller methods.

type Playfile

type Playfile struct {
	Version  string   `yaml:"version"`
	Envs     EnvVars  `yaml:"envs"`
	Networks Networks `yaml:"networks"`
	Commands Commands `yaml:"commands"`
	Books    Books    `yaml:"books"`
}

Playfile represents the play configuration YAML file.

func NewPlayfile

func NewPlayfile(data []byte) (*Playfile, error)

NewPlayfile parses configuration file and returns Playfile or error.

func NewPlayfileFromFile

func NewPlayfileFromFile(filename string) (*Playfile, error)

NewPlayfileFromFile returns *Playfile by parsing filename given or error

type SSHClient

type SSHClient struct {
	// contains filtered or unexported fields
}

SSHClient is a wrapper over the SSH connection/sessions.

func (*SSHClient) Close

func (c *SSHClient) Close() error

Close closes the underlying SSH connection and session.

func (*SSHClient) Connect

func (c *SSHClient) Connect(host string) error

Connect creates SSH connection to a specified host. It expects the host of the form "[ssh://][user:passwd@]host[:port]".

func (*SSHClient) ConnectWith

func (c *SSHClient) ConnectWith(host string, dialer SSHDialFunc) error

ConnectWith creates a SSH connection to a specified host. It will use dialer to establish the connection. TODO: Split Signers to its own method.

func (*SSHClient) LastError

func (c *SSHClient) LastError() error

func (*SSHClient) Prompt

func (c *SSHClient) Prompt() string

func (*SSHClient) Run

func (c *SSHClient) Run(book *Book) error

Run runs the book.Run command on remote host.

func (*SSHClient) Signal

func (c *SSHClient) Signal(sig os.Signal) error

func (*SSHClient) Stderr

func (c *SSHClient) Stderr() io.Reader

func (*SSHClient) Stdin

func (c *SSHClient) Stdin() io.WriteCloser

func (*SSHClient) Stdout

func (c *SSHClient) Stdout() io.Reader

func (*SSHClient) Wait

func (c *SSHClient) Wait() error

Wait waits until the remote command finishes and exits. NOTE: It closes the SSH session.

func (*SSHClient) Write

func (c *SSHClient) Write(p []byte) (n int, err error)

type SSHDialFunc

type SSHDialFunc func(net, addr string, config *ssh.ClientConfig) (*ssh.Client, error)

SSHDialFunc can dial a ssh server and return a client

type Upload

type Upload struct {
	Src    string `yaml:"src"`
	Dst    string `yaml:"dst"`
	Filter string `yaml:"filter"`
}

Upload represents file copy operation from localhost Src path to remote Dst path of every host in a given Network.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL