host

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MIT Imports: 24 Imported by: 0

README

host Package

The host package provides an abstraction for file and process operations on local and remote SSH hosts.

Documentation

Overview

Provides remote file and command functions

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidArgs  = errors.New("invalid arguments")
	ErrNotSupported = errors.New("not supported")
	ErrNotAvailable = errors.New("not available")
	ErrExists       = errors.New("exists")
	ErrNotExists    = errors.New("not exists")
)
View Source
var Localhost = NewLocal()

Functions

func CopyAll

func CopyAll(srcHost Host, srcDir string, dstHost Host, dstDir string) (err error)

CopyAll copies a directory between any combination of local or remote locations

func CopyFile

func CopyFile(srcHost Host, srcPath string, dstHost Host, dstPath string) (err error)

CopyFile copies a file between two locations. Destination can be a directory or a file. Parent directories will be created as required. Any existing files will be overwritten.

Types

type Host

type Host interface {
	// informational
	String() string
	GetFs() afero.Fs
	Path(path string) string
	ServerVersion() string
	IsAvailable() bool
	IsLocal() bool
	LastError() error
	Username() string

	// file operations
	Chown(name string, uid, gid int) (err error)
	Glob(pattern string) (paths []string, err error)
	Lchown(name string, uid, gid int) (err error)
	Lstat(name string) (f fs.FileInfo, err error)
	MkdirAll(path string, perm os.FileMode) (err error)
	ReadDir(name string) (dirs []os.DirEntry, err error)
	ReadFile(name string) (b []byte, err error)
	Readlink(file string) (link string, err error)
	Remove(name string) (err error)
	RemoveAll(name string) (err error)
	Rename(oldpath, newpath string) (err error)
	Stat(name string) (f fs.FileInfo, err error)
	Symlink(oldname, newname string) (err error)
	WriteFile(name string, data []byte, perm os.FileMode) (err error)
	// these two do not conform to the afero / os interface
	Open(name string) (f io.ReadSeekCloser, err error)
	Create(path string, perms fs.FileMode) (out io.WriteCloser, err error)

	// process control
	Signal(pid int, signal syscall.Signal) (err error)
	Run(name string, args ...string) (output []byte, err error)
	Start(cmd *exec.Cmd, env []string, home, errfile string) (err error)
}

Host encapsulates all the methods required by callers to manage Geneos installs on a host.

This should have been based on (and extending) something like Afero, but this was quicker for the moment. This interface also provides process handling etc.

func NewLocal

func NewLocal() Host

func NewSSHRemote

func NewSSHRemote(name string, options ...any) Host

type Local

type Local struct {
}

Localhost operations

func (*Local) Chown

func (h *Local) Chown(name string, uid, gid int) (err error)

func (*Local) Create

func (h *Local) Create(path string, perms fs.FileMode) (out io.WriteCloser, err error)

func (*Local) GetFs

func (h *Local) GetFs() afero.Fs

func (*Local) Glob

func (h *Local) Glob(pattern string) (paths []string, err error)

func (*Local) IsAvailable

func (h *Local) IsAvailable() bool

IsAvailable returns true for Local

func (*Local) IsLocal

func (h *Local) IsLocal() bool

IsLocal returns true if h is local, which for Local it is

func (*Local) LastError

func (h *Local) LastError() error

func (*Local) Lchown

func (h *Local) Lchown(name string, uid, gid int) (err error)

change the symlink ownership on local system, issue chown for remotes

func (*Local) Lstat

func (h *Local) Lstat(name string) (f fs.FileInfo, err error)

Lstat wraps the os.Lstat and sftp.Lstat functions

func (*Local) MkdirAll

func (h *Local) MkdirAll(path string, perm os.FileMode) (err error)

func (*Local) Open

func (h *Local) Open(name string) (f io.ReadSeekCloser, err error)

func (*Local) Path

func (h *Local) Path(path string) string

func (*Local) ReadDir

func (h *Local) ReadDir(name string) (dirs []os.DirEntry, err error)

ReadDir reads the named directory and returns all its directory entries sorted by name.

func (*Local) ReadFile

func (h *Local) ReadFile(name string) (b []byte, err error)
func (h *Local) Readlink(file string) (link string, err error)

func (*Local) Remove

func (h *Local) Remove(name string) (err error)

func (*Local) RemoveAll

func (h *Local) RemoveAll(name string) (err error)

func (*Local) Rename

func (h *Local) Rename(oldpath, newpath string) (err error)

func (*Local) Run

func (h *Local) Run(name string, args ...string) (output []byte, err error)

func (*Local) ServerVersion

func (h *Local) ServerVersion() string

func (*Local) Signal

func (h *Local) Signal(pid int, signal syscall.Signal) (err error)

func (*Local) Start

func (h *Local) Start(cmd *exec.Cmd, env []string, home, errfile string) (err error)

func (*Local) Stat

func (h *Local) Stat(name string) (f fs.FileInfo, err error)

Stat wraps the os.Stat and sftp.Stat functions

func (*Local) String

func (h *Local) String() string
func (h *Local) Symlink(oldname, newname string) (err error)

func (*Local) Username

func (h *Local) Username() string

func (*Local) WriteFile

func (h *Local) WriteFile(name string, data []byte, perm os.FileMode) (err error)

type SSHOptions

type SSHOptions func(*SSHRemote)

func Hostname

func Hostname(hostname string) SSHOptions

func Keys

func Keys(paths ...string) SSHOptions

func Password

func Password(password *memguard.Enclave) SSHOptions

func Port

func Port(port uint16) SSHOptions

func Username

func Username(username string) SSHOptions

type SSHRemote

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

func (*SSHRemote) Chown

func (h *SSHRemote) Chown(name string, uid, gid int) error

func (*SSHRemote) Close

func (h *SSHRemote) Close()

func (*SSHRemote) CloseSFTP

func (h *SSHRemote) CloseSFTP()

func (*SSHRemote) Create

func (h *SSHRemote) Create(path string, perms fs.FileMode) (out io.WriteCloser, err error)

func (*SSHRemote) Dial

func (s *SSHRemote) Dial() (sc *ssh.Client, err error)

Dial connects to a remote host using ssh and returns an *ssh.Client on success. Each connection is cached and returned if found without checking if it is still valid. To remove a session call Close()

func (*SSHRemote) DialSFTP

func (h *SSHRemote) DialSFTP() (f *sftp.Client, err error)

DialSFTP connects to the remote host using SSH and returns an *sftp.Client is successful

func (*SSHRemote) GetFs

func (h *SSHRemote) GetFs() afero.Fs

func (*SSHRemote) Glob

func (h *SSHRemote) Glob(pattern string) ([]string, error)

func (*SSHRemote) IsAvailable

func (s *SSHRemote) IsAvailable() bool

IsAvaialble returns true is the remote host can be contacted

func (*SSHRemote) IsLocal

func (h *SSHRemote) IsLocal() bool

IsLocal returns true if h is local, which for SSH is always false

func (*SSHRemote) LastError

func (h *SSHRemote) LastError() error

func (*SSHRemote) Lchown

func (h *SSHRemote) Lchown(name string, uid, gid int) error

Lchown just down a Chown() for remote files

func (*SSHRemote) Lstat

func (h *SSHRemote) Lstat(name string) (fs.FileInfo, error)

Lstat wraps the os.Lstat and sftp.Lstat functions

func (*SSHRemote) MkdirAll

func (h *SSHRemote) MkdirAll(path string, perm os.FileMode) error

func (*SSHRemote) Open

func (h *SSHRemote) Open(name string) (io.ReadSeekCloser, error)

func (*SSHRemote) Path

func (h *SSHRemote) Path(path string) string

func (*SSHRemote) ReadDir

func (h *SSHRemote) ReadDir(name string) (dirs []os.DirEntry, err error)

ReadDir reads the named directory and returns all its directory entries sorted by name.

func (*SSHRemote) ReadFile

func (h *SSHRemote) ReadFile(name string) (b []byte, err error)
func (h *SSHRemote) Readlink(file string) (string, error)

func (*SSHRemote) Remove

func (h *SSHRemote) Remove(name string) error

func (*SSHRemote) RemoveAll

func (h *SSHRemote) RemoveAll(name string) (err error)

func (*SSHRemote) Rename

func (h *SSHRemote) Rename(oldpath, newpath string) error

func (*SSHRemote) Run

func (h *SSHRemote) Run(name string, args ...string) (output []byte, err error)

func (*SSHRemote) ServerVersion

func (h *SSHRemote) ServerVersion() string

func (*SSHRemote) Signal

func (h *SSHRemote) Signal(pid int, signal syscall.Signal) (err error)

func (*SSHRemote) Start

func (h *SSHRemote) Start(cmd *exec.Cmd, env []string, home, errfile string) (err error)

func (*SSHRemote) Stat

func (h *SSHRemote) Stat(name string) (fs.FileInfo, error)

Stat wraps the os.Stat and sftp.Stat functions

func (*SSHRemote) String

func (h *SSHRemote) String() string
func (h *SSHRemote) Symlink(oldname, newname string) error

func (*SSHRemote) Username

func (s *SSHRemote) Username() string

func (*SSHRemote) WriteFile

func (h *SSHRemote) WriteFile(name string, data []byte, perm os.FileMode) (err error)

Jump to

Keyboard shortcuts

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