Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr265 committed Jun 16, 2017
0 parents commit aed6f4a
Show file tree
Hide file tree
Showing 15 changed files with 594 additions and 0 deletions.
15 changes: 15 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bullet

import (
"os"
"os/exec"

"github.com/FurqanSoftware/bullet/spec"
)

func Build(spec *spec.Spec) error {
cmd := exec.Command("/bin/bash", "-c", spec.Build.Script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
45 changes: 45 additions & 0 deletions cmd/bullet/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"log"

"github.com/FurqanSoftware/bullet"
"github.com/FurqanSoftware/bullet/spec"
"github.com/spf13/cobra"
)

var DeployHosts string
var DeploySkipBuild bool

var DeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy app to server",
Long: `This command packages and deploys the app to specific servers.`,
Run: func(cmd *cobra.Command, args []string) {
spec, err := spec.ParseFile("Bulletspec")
if err != nil {
log.Fatal(err)
return
}

nodes, err := bullet.ParseNodeSet(DeployHosts)
if err != nil {
log.Fatal(err)
return
}

err = bullet.Deploy(nodes, spec, bullet.DeployOptions{
SkipBuild: DeploySkipBuild,
})
if err != nil {
log.Fatal(err)
return
}
},
}

func init() {
DeployCmd.Flags().StringVarP(&DeployHosts, "hosts", "H", "", "Hosts to deploy to")
DeployCmd.Flags().BoolVarP(&DeploySkipBuild, "skip-build", "", false, "Skip build")
RootCmd.AddCommand(DeployCmd)
}
12 changes: 12 additions & 0 deletions cmd/bullet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"log"
)

func main() {
err := RootCmd.Execute()
if err != nil {
log.Fatal(err)
}
}
12 changes: 12 additions & 0 deletions cmd/bullet/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "github.com/spf13/cobra"

var RootCmd = &cobra.Command{
Use: "bullet",
Short: "Bullet is a fast deploy tool",
Long: `Bullet is a fast and flexible deploy tool built by Furqan Software and friends. Complete documentation is available at https://bullettool.com/.`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}
41 changes: 41 additions & 0 deletions cmd/bullet/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"log"

"github.com/FurqanSoftware/bullet"
"github.com/FurqanSoftware/bullet/spec"
"github.com/spf13/cobra"
)

var SetupHosts string

var SetupCmd = &cobra.Command{
Use: "setup",
Short: "Setup server for app",
Long: `This command prepares the server for the app.`,
Run: func(cmd *cobra.Command, args []string) {
spec, err := spec.ParseFile("Bulletspec")
if err != nil {
log.Fatal(err)
return
}

nodes, err := bullet.ParseNodeSet(SetupHosts)
if err != nil {
log.Fatal(err)
return
}

err = bullet.Setup(nodes, spec)
if err != nil {
log.Fatal(err)
return
}
},
}

func init() {
SetupCmd.Flags().StringVarP(&SetupHosts, "hosts", "H", "", "Hosts to configure")
RootCmd.AddCommand(SetupCmd)
}
73 changes: 73 additions & 0 deletions deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package bullet

import (
"fmt"
"log"
"os"

"github.com/FurqanSoftware/bullet/distro"
_ "github.com/FurqanSoftware/bullet/distro/ubuntu"
"github.com/FurqanSoftware/bullet/spec"
"github.com/FurqanSoftware/bullet/ssh"
)

type DeployOptions struct {
SkipBuild bool
}

func Deploy(nodes []Node, spec *spec.Spec, options DeployOptions) error {
if options.SkipBuild {
log.Print("Skipping build")

} else {
log.Print("Building")
err := Build(spec)
if err != nil {
return err
}
}

log.Print("Packaging")
rel, err := Package(spec)
if err != nil {
return err
}

for _, n := range nodes {
log.Printf("Connecting to %s", n.Addr())
c, err := ssh.Dial(n.Addr())
if err != nil {
return err
}

log.Printf("Uploading tarball %s", n.Addr())
tarPath := fmt.Sprintf("/tmp/%s.tar", rel.Name)
err = uploadTarball(c, tarPath, rel.Tarball)
if err != nil {
return err
}

d, err := distro.New(c)
if err != nil {
return err
}
err = d.ExtractTar(tarPath, fmt.Sprintf("/opt/bullet/%s/releases/%s", spec.Application.Identifier, rel.Name))
if err != nil {
return err
}
}
return nil
}

func uploadTarball(c *ssh.Client, dst string, tar Tarball) error {
f, err := os.Open(tar.Path)
if err != nil {
return err
}
defer f.Close()
s, err := f.Stat()
if err != nil {
return err
}
return c.Scp(dst, s.Mode(), s.Size(), f)
}
38 changes: 38 additions & 0 deletions distro/distro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package distro

import (
"errors"

"github.com/FurqanSoftware/bullet/ssh"
)

var ErrBadDistribution = errors.New("distro: unsupported distribution")

type Distro interface {
InstallDocker() error

MkdirAll(name string) error

ExtractTar(name, dir string) error

Detect() (bool, error)
}

func New(c *ssh.Client) (Distro, error) {
for _, fn := range DistroFuncs {
d := fn(c)
ok, err := d.Detect()
if err != nil {
return nil, err
}

if ok {
return d, nil
}
}
return nil, ErrBadDistribution
}

type DistroFunc func(c *ssh.Client) Distro

var DistroFuncs = []DistroFunc{}
63 changes: 63 additions & 0 deletions distro/ubuntu/ubuntu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ubuntu

import (
"fmt"

"github.com/FurqanSoftware/bullet/distro"
"github.com/FurqanSoftware/bullet/ssh"
)

type Ubuntu struct {
Client *ssh.Client
}

func New(c *ssh.Client) distro.Distro {
return &Ubuntu{
Client: c,
}
}

func (u *Ubuntu) InstallDocker() error {
cmds := []string{
"echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6",
"apt-get update",
"apt-get install -y apt-transport-https ca-certificates curl software-properties-common",
"curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -",
`add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"`,
"apt-get update",
"apt-get install -y docker-ce",
}
for _, cmd := range cmds {
err := u.Client.Run(cmd)
if err != nil {
return err
}
}
return nil
}

func (u *Ubuntu) MkdirAll(name string) error {
return u.Client.Run(fmt.Sprintf("mkdir -p %s", name))
}

func (u *Ubuntu) ExtractTar(name, dir string) error {
cmds := []string{
fmt.Sprintf("mkdir %s", dir),
fmt.Sprintf("tar -xf %s -C %s", name, dir),
}
for _, cmd := range cmds {
err := u.Client.Run(cmd)
if err != nil {
return err
}
}
return nil
}

func (u *Ubuntu) Detect() (bool, error) {
return true, nil
}

func init() {
distro.DistroFuncs = append(distro.DistroFuncs, New)
}
26 changes: 26 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bullet

import (
"strconv"
"strings"
)

type Node struct {
Host string
Port int
}

func ParseNodeSet(hosts string) ([]Node, error) {
nodes := []Node{}
for _, h := range strings.Split(hosts, ",") {
nodes = append(nodes, Node{
Host: h,
Port: 22,
})
}
return nodes, nil
}

func (n Node) Addr() string {
return n.Host + ":" + strconv.Itoa(n.Port)
}
Loading

0 comments on commit aed6f4a

Please sign in to comment.