aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-10-05 14:03:55 -0700
committerChristine Dodrill <me@christine.website>2018-10-05 14:31:22 -0700
commitc63e73391d634bdb25b3df1c582bb56b6d9a0963 (patch)
tree81ec642a36c4ca84b3d0d4dfd8d2a91d09f762cd /internal
parentdbeba1e5c5c0bc534a515eb298ee4f1d49df4d20 (diff)
downloadx-c63e73391d634bdb25b3df1c582bb56b6d9a0963.tar.xz
x-c63e73391d634bdb25b3df1c582bb56b6d9a0963.zip
mastodon/sona-pi-toki-pona: build and deploy in go, not shell
Diffstat (limited to 'internal')
-rw-r--r--internal/greedo/doc.go2
-rw-r--r--internal/greedo/greedo.go85
-rw-r--r--internal/greedo/greedo_test.go11
-rw-r--r--internal/internal.go40
-rw-r--r--internal/minipaas/doc.go2
-rw-r--r--internal/minipaas/minipaas.go40
6 files changed, 180 insertions, 0 deletions
diff --git a/internal/greedo/doc.go b/internal/greedo/doc.go
new file mode 100644
index 0000000..5684040
--- /dev/null
+++ b/internal/greedo/doc.go
@@ -0,0 +1,2 @@
+// Package greedo is a set of functions to copy files to https://xena.greedo.xeserv.us/files.
+package greedo
diff --git a/internal/greedo/greedo.go b/internal/greedo/greedo.go
new file mode 100644
index 0000000..5ec2c58
--- /dev/null
+++ b/internal/greedo/greedo.go
@@ -0,0 +1,85 @@
+package greedo
+
+import (
+ "bytes"
+ "io"
+ "io/ioutil"
+ "net"
+ "os"
+ "path/filepath"
+
+ "github.com/tmc/scp"
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+)
+
+func getAgent() (agent.Agent, error) {
+ agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
+ return agent.NewClient(agentConn), err
+}
+
+const (
+ greedoAddr = `greedo.xeserv.us:22`
+ greedoUser = `xena`
+)
+
+// Dial opens a SSH client to greedo.
+func Dial() (*ssh.Client, error) {
+ agent, err := getAgent()
+ if err != nil {
+ return nil, err
+ }
+
+ client, err := ssh.Dial("tcp", greedoAddr, &ssh.ClientConfig{
+ User: greedoUser,
+ Auth: []ssh.AuthMethod{
+ ssh.PublicKeysCallback(agent.Signers),
+ },
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return client, nil
+}
+
+// Copy copies a local file reader to the remote destination path. This copies the enitre contents of contents into ram. Don't use this function if doing so is a bad idea. Only works on files less than 2 GB.
+func Copy(mode os.FileMode, fileName string, contents io.Reader, destinationPath string) error {
+ data, err := ioutil.ReadAll(contents)
+ if err != nil {
+ return err
+ }
+
+ client, err := Dial()
+ if err != nil {
+ return err
+ }
+
+ session, err := client.NewSession()
+ if err != nil {
+ return err
+ }
+
+ err = scp.Copy(int64(len(data)), mode, fileName, bytes.NewBuffer(data), destinationPath, session)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// CopyFile copies a file to Greedo's public files folder and returns its public-facing URL.
+func CopyFile(fileName string, contents io.Reader) (string, error) {
+ err := Copy(0644, fileName, contents, filepath.Join("public_html", "files", fileName))
+ if err != nil {
+ return "", err
+ }
+
+ return WebURL(fileName), nil
+}
+
+// WebURL constructs a public-facing URL for a given resource by fragment.
+func WebURL(fragment string) string {
+ return "https://xena.greedo.xeserv.us/files/" + fragment
+}
diff --git a/internal/greedo/greedo_test.go b/internal/greedo/greedo_test.go
new file mode 100644
index 0000000..4e1f857
--- /dev/null
+++ b/internal/greedo/greedo_test.go
@@ -0,0 +1,11 @@
+package greedo
+
+import "testing"
+
+func TestDial(t *testing.T) {
+ cli, err := Dial()
+ if err != nil {
+ t.Fatal(err)
+ }
+ cli.Close()
+}
diff --git a/internal/internal.go b/internal/internal.go
index f21624e..9c36aa3 100644
--- a/internal/internal.go
+++ b/internal/internal.go
@@ -1,17 +1,36 @@
package internal
import (
+ "context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
+ "os/exec"
+ "time"
"github.com/Xe/x/tools/license/licenses"
"go4.org/legal"
)
+// current working directory and date:time tag of app boot (useful for tagging slugs)
+var (
+ WD string
+ DateTag string
+)
+
+func init() {
+ lwd, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ WD = lwd
+ DateTag = time.Now().Format("010220061504")
+}
+
var (
licenseShow = flag.Bool("license", false, "show software license?")
)
@@ -38,3 +57,24 @@ func HandleLicense() {
os.Exit(0)
}
}
+
+// ShouldWork explodes if the given command with the given env, working dir and context fails.
+func ShouldWork(ctx context.Context, env []string, dir string, cmdName string, args ...string) {
+ loc, err := exec.LookPath(cmdName)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ cmd := exec.CommandContext(ctx, loc, args...)
+ cmd.Dir = dir
+ cmd.Env = env
+
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+
+ log.Printf("starting process, env: %v, pwd: %s, cmd: %s, args: %v", env, dir, loc, args)
+ err = cmd.Run()
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/internal/minipaas/doc.go b/internal/minipaas/doc.go
new file mode 100644
index 0000000..659774a
--- /dev/null
+++ b/internal/minipaas/doc.go
@@ -0,0 +1,2 @@
+// Package minipaas is a set of functions for interfacing with minipaas.xeserv.us.
+package minipaas
diff --git a/internal/minipaas/minipaas.go b/internal/minipaas/minipaas.go
new file mode 100644
index 0000000..5632c3c
--- /dev/null
+++ b/internal/minipaas/minipaas.go
@@ -0,0 +1,40 @@
+package minipaas
+
+import (
+ "net"
+ "os"
+
+ "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/agent"
+)
+
+func getAgent() (agent.Agent, error) {
+ agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
+ return agent.NewClient(agentConn), err
+}
+
+const (
+ minipaasAddr = `minipaas.xeserv.us:22`
+ minipaasUser = `dokku`
+)
+
+// Dial opens a SSH client to minipaas as the dokku user.
+func Dial() (*ssh.Client, error) {
+ agent, err := getAgent()
+ if err != nil {
+ return nil, err
+ }
+
+ client, err := ssh.Dial("tcp", minipaasAddr, &ssh.ClientConfig{
+ User: minipaasUser,
+ Auth: []ssh.AuthMethod{
+ ssh.PublicKeysCallback(agent.Signers),
+ },
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return client, nil
+}