aboutsummaryrefslogtreecommitdiff
path: root/cmd/appsluggr/main.go
blob: 891b40c60aa4271ee400ae23f10708ff689a908c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"archive/tar"
	"compress/gzip"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/otiai10/copy"
	"within.website/x/internal"
)

var (
	web         = flag.String("web", "", "path to binary for web process")
	webScale    = flag.Int("web-scale", 1, "default scale for web process if defined")
	worker      = flag.String("worker", "", "path to binary for worker process")
	workerScale = flag.Int("worker-scale", 1, "default scale for worker process if defined")
	fname       = flag.String("fname", "slug.tar.gz", "slug name")
)

func main() {
	internal.HandleStartup()

	fout, err := os.Create(*fname)
	if err != nil {
		log.Fatal(err)
	}
	defer fout.Close()

	gzw := gzip.NewWriter(fout)
	defer gzw.Close()

	tw := tar.NewWriter(gzw)
	defer tw.Close()

	dir, err := ioutil.TempDir("", "appsluggr")
	if err != nil {
		log.Fatal(err)
	}

	defer os.RemoveAll(dir) // clean up

	os.MkdirAll(filepath.Join(dir, "bin"), 0777)
	var procfile, scalefile string

	copy.Copy("translations", filepath.Join(dir, "translations"))
	if *web != "" {
		procfile += "web: /app/bin/web\n"
		scalefile += fmt.Sprintf("web=%d", *webScale)
		Copy(*web, filepath.Join(dir, "bin", "web"))

	}
	if *worker != "" {
		procfile += "worker: /app/bin/worker\n"
		scalefile += fmt.Sprintf("worker=%d", *workerScale)
		Copy(*worker, filepath.Join(dir, "bin", "worker"))
	}

	os.MkdirAll(filepath.Join(dir, ".config"), 0777)

	err = ioutil.WriteFile(filepath.Join(dir, ".buildpacks"), []byte("https://github.com/ryandotsmith/null-buildpack"), 0666)
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile(filepath.Join(dir, "DOKKU_SCALE"), []byte(scalefile), 0666)
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile(filepath.Join(dir, "Procfile"), []byte(procfile), 0666)
	if err != nil {
		log.Fatal(err)
	}

	filepath.Walk(dir, func(file string, fi os.FileInfo, err error) error {
		// return on any error
		if err != nil {
			log.Printf("got error on %s: %v", file, err)
			return err
		}

		if fi.IsDir() {
			return nil // not a file.  ignore.
		}

		// create a new dir/file header
		header, err := tar.FileInfoHeader(fi, fi.Name())
		if err != nil {
			return err
		}

		// update the name to correctly reflect the desired destination when untaring
		header.Name = strings.TrimPrefix(strings.Replace(file, dir, "", -1), string(filepath.Separator))

		// write the header
		if err := tw.WriteHeader(header); err != nil {
			return err
		}

		// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
		if !fi.Mode().IsRegular() {
			return nil
		}

		// open files for taring
		f, err := os.Open(file)
		if err != nil {
			return err
		}

		// copy file data into tar writer
		if _, err := io.Copy(tw, f); err != nil {
			return err
		}

		// manually close here after each file operation; defering would cause each file close
		// to wait until all operations have completed.
		f.Close()

		return nil
	})
}

// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
func Copy(src, dst string) error {
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()
	st, err := in.Stat()
	if err != nil {
		return err
	}

	out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, st.Mode())
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}
	return out.Close()
}