installer

package module
v0.0.0-...-8bfebc8 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: Apache-2.0 Imports: 22 Imported by: 0

README

go-compose-installer GoDoc

A toolkit to create installers based on docker compose.

go-compose-installer allows to deploy complex infrastructure applications using a single binary, with no more dependencies than docker.

Example

This is an example based on the docker compose tutorial, this example installs a basic environment based on a web server backed by a redis service.

package main

import (
	"github.com/src-d/go-compose-installer"
)

func main() {
	// New default Config, containing a suite of standard messages to be
	// printed at every operation.
	cfg := installer.NewDefaultConfig()

	// Name of the project, to be used in the messages.
	cfg.ProjectName = "example"

	// Compose YAML content, a standard docker compose version 2 file.
	cfg.Compose = [][]byte{yml}

	// The defined YAML, contains a template, some variables are not standard
	// variables, so we need to define it at the `Config.TemplateVars` field.
	cfg.TemplateVars = map[string]interface{}{
		"RedisTag": "4.0.6-alpine",
	}

	// Customized message for a success installation.
	cfg.Install.Messages.Success = "" +
		"The example was successfully installed!\n\n" +

		"To test the deployment please navigate to:\n" +
		"http://localhost:5000\n\n" +

		"To uninstall this example, just execute:\n" +
		"./example uninstall\n"

	// New instance of a Installer based on the given Config.
	p, err := installer.New("compose-installer-example", cfg)
	if err != nil {
		panic(err)
	}

	// Execution of the application.
	p.Run()
}

// Standard docker compose yaml file from:
// https://docs.docker.com/compose/gettingstarted/#where-to-go-next
var yml []byte = []byte(`
version: '2'
services:
  web:
    image: srcd/compose-example
    ports:
     - "5000:5000"
  redis:
    image: "redis:{{.RedisTag}}"
`)

The following code provides an CLI application allowing to anyone with just one command to install, start, stop and uninstall the environment, without any external dependency besides docker.

Usage:
  compose-installer-example [OPTIONS] <command>

Help Options:
  -h, --help  Show this help message

Available commands:
  install    Installs example into your system.
  start      Stars example.
  status     Show the status of example.
  stop       Stops example.
  uninstall  Remove example from your system.

Go Template support

Go templates are supported in the yaml file and also at all the messages.

The Template variables can be configured at the Config.TemplateVars, additionally to the custom defined variables some other variables are defined by default.

This is the list of default variables:

  • .Project - The project name, from the given Config.ProjectName.
  • .Home - Home folder of the user executing the installer.
  • .OS - Content of runtime.GOOS.
  • .Arch - Content ofruntime.GOARCH.
  • .Error - Only available at Failure messages, is the string of the error.

License

Apache License Version 2.0, see LICENSE

Documentation

Index

Constants

View Source
const (
	// EnvComposeFile overrides the built-in composer file.
	EnvComposeFile = "INSTALLER_COMPOSE"
)

Variables

View Source
var DefaultShell = []string{"/bin/sh", "-c"}

Functions

func ErrorToMap

func ErrorToMap(err error) map[string]interface{}

func NewListener

func NewListener(p *project.Project) chan<- events.Event

NewListener create a default listener for the specified project.

Types

type Action

type Action func(*Project, *Config) error

type Command

type Command struct {
	Debug  bool `long:"debug" description:"enables the debug mode."`
	NoExec bool `long:"no-exec" description:"disable any execution after the action."`
	// contains filtered or unexported fields
}

Command defines a the base CLI command. It implements the non-interface for a command, for go-flags, where a `Execute([]string) error` is expected. The flags defined here are common to any other command.

func (*Command) Execute

func (c *Command) Execute([]string) error

Execute execute the command, to be shadowed by specific implementations.

type Config

type Config struct {
	// ProjectName is the name given to the project being installed.
	ProjectName string
	// Compose is the content of one or more docker compose files in YAML format.
	Compose [][]byte
	// TemplateVars are custom defined variable to be replace on the values
	// supporting templates, such as docker compose files or text messages.
	TemplateVars map[string]interface{}
	// Install operation configuration.
	Install Operation
	// Uninstall operation configuration.
	Uninstall Operation
	// Status operation configuration.
	Status Operation
	// Start operation configuration.
	Start Operation
	// Stop operation configuration.
	Stop Operation
}

Config contains all the messages and exec operations for each command.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig returns a Config object will all Messages already configured with standard text suitable for any application.

type Exec

type Exec struct {
	Service string
	Cmd     string
}

type InstallCommand

type InstallCommand struct {
	Command
}

InstallCommand defines the "install" command from the installer.

func (*InstallCommand) Execute

func (c *InstallCommand) Execute([]string) error

type InstallOptions

type InstallOptions struct {
	// NoExec skip the execution of the exec command configured for this command.
	NoExec bool
}

type Installer

type Installer struct {
	Parser *flags.Parser
}

Installer represent the CLI application based on `go-flags` providing all the core commands: `install`, `start`, `stop` and `uninstall`.

func New

func New(name string, cfg *Config) (*Installer, error)

New returns a new Installer instance based on the given name and *Config.

func (*Installer) Run

func (p *Installer) Run()

Run executes the CLI application, in a standard usage this function should be called form a main.main function. It `os.Exit` after be executed.

type Listener

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

type LogFormatter

type LogFormatter struct {
	sync.Once
	// contains filtered or unexported fields
}

func (*LogFormatter) Format

func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error)

Format renders a single log entry

type Messages

type Messages struct {
	// Description to be shown in the help of the command.
	Description string
	// Announcement information shown before execute the command.
	Announcement string
	// Failure is the text to be shown just after an error happend.
	Failure string
	// Success in case of a successfully execution.
	Success string
}

Messages are the message to be printed after, before, etc, every command.

type Operation

type Operation struct {
	Messages Messages
	Execs    []*Exec
}

func (*Operation) Run

func (c *Operation) Run(p *Project, cfg *Config, a Action, noExec bool) error

type Project

type Project struct {
	Compose project.APIProject
	Docker  *client.Client
	// contains filtered or unexported fields
}

Project performs all the operations at docker compose level.

func NewProject

func NewProject(c *Config) (*Project, error)

NewProject returns a new Project based on the given Config.

func (*Project) Execute

func (p *Project) Execute(ctx context.Context, service string, cmd ...string) error

func (*Project) Install

func (p *Project) Install(ctx context.Context, opts *InstallOptions) error

func (*Project) MustRenderTemplate

func (p *Project) MustRenderTemplate(tmpl string, vars map[string]interface{}) string

func (*Project) RenderTemplate

func (p *Project) RenderTemplate(tmpl string, vars map[string]interface{}) (string, error)

func (*Project) Start

func (p *Project) Start(ctx context.Context, opts *StartOptions) error

func (*Project) Status

func (p *Project) Status(ctx context.Context) error

func (*Project) Stop

func (p *Project) Stop(ctx context.Context, opts *StopOptions) error

func (*Project) Uninstall

func (p *Project) Uninstall(ctx context.Context, opts *UninstallOptions) error

type StartCommand

type StartCommand struct {
	Command
}

StartCommand defines the "start" command from the installer.

func (*StartCommand) Execute

func (c *StartCommand) Execute([]string) error

type StartOptions

type StartOptions struct {
	// NoExec skip the execution of the exec command configured for this command.
	NoExec bool
}

type StatusCommand

type StatusCommand struct {
	Command
}

StatusCommand defines the "status" command from the installer.

func (*StatusCommand) Execute

func (c *StatusCommand) Execute([]string) error

type StopCommand

type StopCommand struct {
	Command
}

StopCommand defines the "stop" command from the installer.

func (*StopCommand) Execute

func (c *StopCommand) Execute([]string) error

type StopOptions

type StopOptions struct {
	// NoExec skip the execution of the exec command configured for this command.
	NoExec bool
}

type UninstallCommand

type UninstallCommand struct {
	Purge bool `long:"purge" description:"remove the docker images and volumes."`
	Force bool `long:"force" description:"force the uninstall process."`
	Command
}

UninstallCommand defines the "uninstall" command from the installer.

func (*UninstallCommand) Execute

func (c *UninstallCommand) Execute([]string) error

type UninstallOptions

type UninstallOptions struct {
	// NoExec skip the execution of the exec command configured for this command.
	NoExec bool
	// Purge remove all the installed images and volumes.
	Purge bool
	// Force force the un-installation even if an installation is not detected.
	Force bool
}

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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