geploy

package module
v0.0.0-...-5def0cd Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 17 Imported by: 0

README

Geploy

Deploy your go application with one-tap.

Installation

$ go install github.com/callmemhz/geploy/cmd/gg@latest
$ echo unalias gg >> ~/.bash_profile && source ~/.bash_profile

Usage

$ gg deploy
$ gg redeploy

Documentation

Index

Constants

View Source
const (
	SequenceMode = 0
	ParallelMode = 1
	LooseMode    = 1 << 1
)
View Source
const (
	EnsureDocker  = `which docker || (apt-get update -y && apt-get install docker.io -y)`
	EnsureCurl    = `which curl || (apt-get update -y && apt-get install curl)`
	EnsureTraefik = `` // fixme 为了方便调试,这里加了 insecure
	/* 258-byte string literal not displayed */
)

Variables

View Source
var (
	DockerLoginRegistry  = func(usr, pwd string) string { return fmt.Sprintf(`docker login -u %s -p %s`, usr, pwd) }
	DockerRemoveImage    = func(image, tag string) string { return "docker image rm --force " + image + ":" + tag }
	DockerPullImage      = func(image, tag string) string { return "docker pull " + image + ":" + tag }
	DockerStartTraefik   = `docker start traefik`
	DockerRunHealthcheck = func(service, port, image, tag string, env map[string]string) string {
		cmd := `docker run --detach`
		cmd += fmt.Sprintf(` --name healthcheck-%s-%s`, service, tag)
		cmd += fmt.Sprintf(` --publish 3999:%s`, port)
		cmd += fmt.Sprintf(` --label service=healthcheck-%s`, service)
		for k, v := range env {
			cmd += " -e " + k + "=" + v
		}
		cmd += " " + image + ":" + tag
		return cmd
	}
	HealthCheck = func(entrypoint string) string {
		return `curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 ` + entrypoint
	}
	DockerStopContainer = func(name string) string {
		return fmt.Sprintf(`docker container ls --all --filter name=%s --quiet | xargs docker stop`, name)
	}
	DockerRemoveContainer = func(name string) string {
		return fmt.Sprintf(`docker container ls --all --filter name=%s --quiet | xargs docker container rm`, name)
	}
	DockerStartApplicationContainer = func(service, port, image, tag, healthcheck string, env map[string]string) string {
		return strings.Join([]string{`docker run --detach --restart unless-stopped`,
			`--log-opt max-size=10m`,
			`--name ` + service + "-" + tag,
			func() string {
				entries := make([]string, 0, len(env))
				for k, v := range env {
					entries = append(entries, "-e "+k+"="+v)
				}
				return strings.Join(entries, " ")
			}(),
			`--label service=` + service,
			`--label role="web"`,
			fmt.Sprintf(`--label traefik.http.routers.%s.rule="PathPrefix(\"/\")"`, service),
			fmt.Sprintf(`--label traefik.http.services.%s.loadbalancer.healthcheck.path="%s"`, service, healthcheck),
			fmt.Sprintf(`--label traefik.http.services.%s.loadbalancer.healthcheck.interval="1s"`, service),
			fmt.Sprintf(`--label traefik.http.middlewares.%s.retry.attempts="5"`, service),
			fmt.Sprintf(`--label traefik.http.middlewares.%s.retry.initialinterval="500ms"`, service),
			image + ":" + tag,
		}, " ")
	}
	DockerPruneOldContainers = func(service string) string {
		return fmt.Sprintf(`docker container prune --force --filter label=service=%s --filter until=72h`, service)
	}
	DockerPruneOldImages = func(service string) string {
		return fmt.Sprintf(`docker image prune --all --force --filter label=service=%s --filter until=168h`, service)
	}
)
View Source
var (
	HostKeyCallback ssh.HostKeyCallback
	Signer          ssh.Signer
)
View Source
var CommandCounter uint32

Functions

func Deploy

func Deploy(g *Group, cfg *DeployConfig, reDeploy bool)

func Setup

func Setup(g *Group, cfg *DeployConfig)

Types

type DeployConfig

type DeployConfig struct {
	Service     string   `yaml:"service"`
	Port        string   `yaml:"port"`
	Image       string   `yaml:"image"`
	Servers     []string `yaml:"servers"`
	HealthCheck string   `yaml:"health_check,omitempty"`

	SshConfig struct {
		Username string `yaml:"usr"`
	} `yaml:"ssh,omitempty"`

	Registry struct {
		// todo customize registry center
		Username any `yaml:"usr"`
		Password any `yaml:"pwd"`
	} `yaml:"registry"`

	Env []string `yaml:"env"`
	// contains filtered or unexported fields
}

func LoadDeployConfig

func LoadDeployConfig() (*DeployConfig, error)

type Group

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

func GroupServers

func GroupServers(servers ...*Server) *Group

func (*Group) Error

func (g *Group) Error(i int) error

func (*Group) Errors

func (g *Group) Errors() []error

func (*Group) Ignore

func (g *Group) Ignore() *Group

func (*Group) Loose

func (g *Group) Loose(on bool) *Group

func (*Group) Parallel

func (g *Group) Parallel() *Group

func (*Group) Printf

func (g *Group) Printf(format string, args ...any) *Group

func (*Group) Println

func (g *Group) Println(args ...any) *Group

func (*Group) Run

func (g *Group) Run(cmds ...string) *Group

func (*Group) Sequence

func (g *Group) Sequence() *Group

type Server

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

func NewServer

func NewServer(host, usr string) (*Server, error)

func (*Server) Close

func (s *Server) Close() error

func (*Server) Run

func (s *Server) Run(cmds ...string) (string, string, error)

func (*Server) Session

func (s *Server) Session() (*ssh.Session, error)

Directories

Path Synopsis
cmd
gg

Jump to

Keyboard shortcuts

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