Skip to content

Commit

Permalink
Initialize repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrash committed Apr 4, 2021
0 parents commit 0ff517d
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh
set -x -e -u

ssh $1 mkdir -p /root/go/src/github.com/ngrash/ftl
scp *.go $1:/root/go/src/github.com/ngrash/ftl/


date=$(date +%s)
dst=/var/log/ftl/$date/plans
ssh $1 mkdir -p $dst
scp $2 $1:$dst/../$2
ssh $1 go run $dst/../$2
87 changes: 87 additions & 0 deletions ftl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ftl

import (
"bytes"
"net/http"
"os/exec"
)

func Exec(command string, args ...string) (*exec.Cmd, error) {
cmd := exec.Command(command, args...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return cmd, err
}
return cmd, nil
}

func Installed(name string) (bool, error) {
_, err := Exec("dpkg", "-l", name)
if exit, ok := err.(*exec.ExitError); ok {
if exit.ExitCode() == 1 {
return false, nil
}
return false, err
}
return true, nil
}

func UpdateRepos() error {
_, err := Exec("apt-get", "update")
return err
}

func Missing(names ...string) []string {
var missing []string
for _, name := range names {
installed, err := Installed(name)
if err != nil {
panic(err)
}
if !installed {
missing = append(missing, name)
}
}
return missing
}

func Install(names ...string) error {
_, err := Exec("apt-get", append([]string{"install", "--yes"}, names...)...)
if err != nil {
return err
}

return nil
}

func DistroCodename() (string, error) {
cmd, err := Exec("lsb_release", "--short", "--codename")
if err != nil {
return "", err
}
return cmd.Stdout.(*bytes.Buffer).String(), nil
}

func AddRepo(repo, pubKey string) error {
resp, err := http.Get(pubKey)
if err != nil {
return err
}
defer resp.Body.Close()

cmd := exec.Command("apt-key", "add", "-")
cmd.Stdin = resp.Body
if err := cmd.Run(); err != nil {
return err
}

_, err = Exec("add-apt-repository", repo)
if err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/ngrash/ftl

go 1.15
36 changes: 36 additions & 0 deletions plans/docker_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"

"github.com/ngrash/ftl"
)

func main() {
installDocker()
}

func must(err error) {
if err != nil {
log.Fatal(err)
}
}

func installDocker() {
if len(ftl.Missing("docker-ce")) > 0 {
prereqs := []string{"apt-transport-https", "ca-certificates", "curl", "gnupg", "lsb-release"}
if missing := ftl.Missing(prereqs...); len(missing) > 0 {
must(ftl.UpdateRepos())
must(ftl.Install(missing...))
}

codename, err := ftl.DistroCodename()
if err != nil {
log.Fatal(err)
}
must(ftl.AddRepo(fmt.Sprintf("deb [arch=amd64] https://download.docker.com/linux/ubuntu %s stable", codename), "https://download.docker.com/linux/ubuntu/gpg"))
must(ftl.UpdateRepos())
must(ftl.Install("docker-ce"))
}
}

0 comments on commit 0ff517d

Please sign in to comment.