hap

package module
v0.0.0-...-3cbeb6e Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2019 License: BSD-3-Clause Imports: 19 Imported by: 0

README

Hap - A simple and effective provisioner.

Hap helps manage build scripts with git and run them concurrently on multiple remote hosts using composable blocks.

First, hap create to setup a new local repo. Then add hosts to the generated Hapfile. Once hosts are in place, run hap build to execute the build blocks and commands specified in the Hapfile for each host. After hap build a .happened file is saved with the current sha of remote repo. To run hap build again a new commit is required or use the --force param.

To run arbitrary commands use hap c, and to execute individual scripts with hap exec. hap c will not push the latest or run from the current directory or use the environment files, those must be added as part of the command. hap exec will push the latest, use the current directory and the environment.

If you only have one host, just use the default section. Then the -h,--host flag while running hap is not necessary.

Make sure every build script is executable before committing to the local repo.

Installation

via Go
go get -u github.com/gwoo/hap/cmd/hap
Binaries

darwin/amd64

curl -L -C - -o /usr/local/bin/hap https://github.com/gwoo/hap/releases/download/v2.6/hap-darwin-amd64; chmod a+x /usr/local/bin/hap

linux/amd64

curl -L -C - -o /usr/local/bin/hap https://github.com/gwoo/hap/releases/download/v2.6/hap-linux-amd64; chmod a+x /usr/local/bin/hap

Basic Workflow

  • Run hap create <name>
  • Modify Hapfile
  • Run hap -h <host> build

Environment Variables

Hap exports HAP_HOSTNAME, HAP_USER, HAP_ADDR, HAP_IP, HAP_PORT for use in scripts. You can add your own by using the env section or the env statement in the host section.

Hapfile

The Hapfile uses git-config syntax. There are 5 sections, default, host, build, include, and env. The default section holds host config that will be applied to all hosts. The host section holds a named host config. A host config includes addr, username, password, identity, build, and cmd, env. Only addr is required. The identity should point to a local ssh private key that has access to the host via the authorized_keys. The build section holds mulitple cmds that could be applied to a host. Multiple build, cmd, or env are permitted for each host. In addition, an include section accepts multiple path statements and an env section accepts multiple file statements.

sections
  • host: Holds the configuration for a machine
    • addr: the host:port of the remote machine
    • dir: base directory on the remote machine
    • username: the name of the user to login and run commands
    • password: password for ssh password based authentication
    • identity: path to ssh private key for key based authentication
    • build: one or more groups of commands to run
    • cmd: one or more commands to run on a specific host
    • env: one or more environment files to apply to this host (can override env sections)
  • deploy: Holds the configuration for a deploy
    • host: one or more hosts
    • build: one or more groups of commands to run
    • cmd: one or more commands to run on a specific host
    • env: one or more environment files to apply to this host (can override env sections)
  • build: sets of commands to run
    • cmd: one or more commands to run
  • default : Holds the standard configurations that can be applied to all hosts
  • include: Allows other files to be included in the current configuration
    • path: a path to the Hapfile the hap
  • env: make variables available to the all commands
    • file: path to a file that can be sourced

Example Hapfile

A default build is specified, so init.sh and update.sh are executed for each host. Host one specifies two commands, notify.sh and cleanup.sh, to be run after the default build commands. For host one, the HAP_HOSTNAME will be one, the HAP_USER will be root, and the HAP_ADDR will be 10.0.20.10:22. Host two specifies no commands, so only the default build will be applied. For host two, the HAP_HOSTNAME will be two, the HAP_USER will be admin, and the HAP_ADDR will be 10.0.20.11:22. There is also a deploy that will return the hostname for one and two.

[default]
  username = "root"
  identity = "~/.ssh/id_rsa"
  build = "initialize" ; applied to all hosts

[host "one"]
  addr = "10.0.20.10:22"
  cmd = "./notify.sh"
  cmd = "./cleanup.sh"

[host "two"]
  username = "admin"
  identity = "~/.ssh/admin_rsa"
  addr = "10.0.20.11:22"

[deploy "get-hostname"]
  host = one
  host = two
  cmd = hostname

[build "initialize"]
  cmd = ./init.sh
  cmd = ./update.sh
  cmd = echo "initialized"

Usage

Usage of ./bin/hap:
--dry=false: Show commands without running them.
-f, --file="Hapfile": Location of a Hapfile.
--force=false: Force build even if it happened before.
--help=false: Show help
-h, --host="": Host to use for commands. Use glob patterns to match multiple hosts. Use --host=* for all hosts.
-v, --verbose=false: [deprecated] Verbose mode is always on

Available Commands:
hap build	        Run the builds and commands from the Hapfile.
hap c <command>		Run an arbitrary command on the remote host.
hap create <name>	Create a new Hapfile at <name>.
hap deploy <name>	Run the named deploy defined in the Hapfile.
hap exec <script>	Execute a script on the remote host.
hap push		Push current repo to the remote.

Advanced Usage

Sometimes you want to build more than one host. If the hosts follow a similar pattern you can reference all the hosts with a *. For example, app-01 and app-02 are configured. Then you can build both with hap -h app-* build or hap -h a* build.

Sometimes you have a lot of hosts that you want to manage in clusters. If you create multiple files you can use the --file flag to specify the location of the config. The file can be named anything. For example, hap -f Appfile -h app* push, will push all the app hosts in the Appfile.

License

The BSD License http://opensource.org/licenses/bsd-license.php.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClientConfig

func NewClientConfig(config SSHConfig) (*ssh.ClientConfig, error)

NewClientConfig constructs a new *ssh.ClientConfig

func NewKey

func NewKey(key string) (ssh.Signer, error)

NewKey parses and returns the interface for the key type (rsa, dss, etc)

func NewKeyFile

func NewKeyFile(key string) (string, error)

NewKeyFile takes a key and returns the key file

func NewPublicKeyMethod

func NewPublicKeyMethod(key string) (ssh.AuthMethod, error)

NewPublicKeyMethod creates a new auth method for public keys

func NewRemoteWriter

func NewRemoteWriter(host string, w io.Writer) io.Writer

NewRemoteWriter returns a Writer with [host] prepended to the output

Types

type Build

type Build struct {
	Cmd []string
}

Build holds the cmds

type Default

type Default Host

Default holds the default settings

type Deploy

type Deploy struct {
	Host  []string
	Build []string
	Cmd   []string
	Env   []string
}

Deploy describes a group of remote machine

type Env

type Env struct {
	File []string
}

Env holds the files to source when running commands

type Git

type Git struct {
	Repo string
	Work string
	Key  string
}

Git struct

func (Git) Branch

func (g Git) Branch(name string) ([]byte, error)

Branch creates a new branch

func (Git) Checkout

func (g Git) Checkout(name string) ([]byte, error)

Checkout switches the branch

func (Git) Commit

func (g Git) Commit(message string) ([]byte, error)

Commit adds all files, including untracked to the repo

func (Git) Exists

func (g Git) Exists() error

Exists checks whether the git executable exists

func (Git) Push

func (g Git) Push(branch string) ([]byte, error)

Push forces push to the branch to the remote repo

func (Git) RevParse

func (g Git) RevParse() ([]byte, error)

RevParse returns current revision of HEAD

func (Git) UpdateSubmodules

func (g Git) UpdateSubmodules() ([]byte, error)

UpdateSubmodules updates and initializes submodules

type Hapfile

type Hapfile struct {
	Default Default
	Deploys map[string]*Deploy `gcfg:"deploy"`

	Hosts   map[string]*Host  `gcfg:"host"`
	Builds  map[string]*Build `gcfg:"build"`
	Include Include           `gcfg:"include"`
	Env     Env               `gcfg:"env"`
	// contains filtered or unexported fields
}

Hapfile defines the hosts, builds, and default

func NewHapfile

func NewHapfile(file string) (Hapfile, error)

NewHapfile constructs a new hapfile config

func (Hapfile) DeployHost

func (hf Hapfile) DeployHost(deploy, host string) *Host

DeployHost takes a name and returns the host If the name is empty and default addr exists return default. If no default is set it returns a random host.

func (Hapfile) GetDeployHosts

func (hf Hapfile) GetDeployHosts(deploy, host string) (map[string]*Host, error)

GetDeployHosts finds a list of hosts matching name string

func (Hapfile) GetHosts

func (hf Hapfile) GetHosts(name string) map[string]*Host

GetHosts finds a list of hosts matching name string

func (Hapfile) Host

func (hf Hapfile) Host(name string) *Host

Host takes a name and returns the host If the name is empty and default addr exists return default. If no default is set it returns a random host.

func (Hapfile) String

func (hf Hapfile) String() string

String returns the hapfile config as json

type Host

type Host struct {
	Name     string
	Dir      string
	Addr     string
	Username string
	Identity string
	Password string
	Env      []string
	Build    []string
	Cmd      []string
	// contains filtered or unexported fields
}

Host describes a remote machine

func (*Host) AddEnv

func (h *Host) AddEnv(cmds []string) []string

AddEnv includes env files in cmds

func (*Host) BuildCmds

func (h *Host) BuildCmds(builds map[string]*Build)

BuildCmds combines the builds and cmds

func (*Host) Cmds

func (h *Host) Cmds() []string

Cmds returns the cmds to build

func (*Host) GetDir

func (h *Host) GetDir() string

GetDir returns the current working directory

func (*Host) SetDefaults

func (h *Host) SetDefaults(d Default)

SetDefaults fills in missing host specific configs with defaults

type Include

type Include struct {
	Path []string
}

Include holds the files to include

type Remote

type Remote struct {
	Git  Git
	Dir  string
	Host *Host
	// contains filtered or unexported fields
}

Remote defines the remote machine to provision

func NewRemote

func NewRemote(host *Host) (*Remote, error)

NewRemote constructs a new remote machine

func (*Remote) Build

func (r *Remote) Build(force bool) error

Build executes the builds and cmds First execute builds specified in Hapfile Then execute any cmds specified in Hapfile

func (*Remote) Close

func (r *Remote) Close() error

Close ends an ssh session with a remote machine

func (*Remote) Connect

func (r *Remote) Connect() error

Connect starts an ssh session to a remote machine

func (*Remote) Env

func (r *Remote) Env() string

Env returns the preset environment variables to pass to execute

func (*Remote) Execute

func (r *Remote) Execute(commands []string) error

Execute will shell out to run one or more commands

func (*Remote) Initialize

func (r *Remote) Initialize() error

Initialize sets up a git repo on the remote machine

func (*Remote) Push

func (r *Remote) Push() error

Push updates the repo on the remote machine

func (*Remote) PushSubmodules

func (r *Remote) PushSubmodules() error

PushSubmodules runs Push() to put submodules into the proper location on the remote machine

type RemoteWriter

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

RemoteWriter is a Writer with host and io.Writer

func (*RemoteWriter) Write

func (hw *RemoteWriter) Write(p []byte) (int, error)

Write implements the io.Writer interface

type SSHConfig

type SSHConfig struct {
	Addr         string
	Username     string
	Identity     string
	Password     string
	ClientConfig *ssh.ClientConfig
}

SSHConfig holds the config for ssh connections

func NewSSHConfig

func NewSSHConfig(host *Host) SSHConfig

NewSSHConfig converts a Host into the SSHConfig

Directories

Path Synopsis
cmd
hap

Jump to

Keyboard shortcuts

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