about summary refs log tree commit diff
path: root/fun/quinistry/image.go
blob: 3daeac34d6bb1aab4a11d5d419a9f57622c4cb7f (plain) (blame)
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
// The code in this file creates a Docker image layer containing the binary of the
// application itself.

package main

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"
)

// This function creates a Docker-image digest (i.e. SHA256 hash with
// algorithm-specification prefix)
func Digest(b []byte) string {
	hash := sha256.New()
	hash.Write(b)

	return fmt.Sprintf("sha256:%x", hash.Sum(nil))
}

func GetImageOfCurrentExecutable() Image {
	binary := getCurrentBinary()
	tarArchive := createTarArchive(&map[string][]byte{
		"/main": binary,
	})

	configJson, configElem := createConfig([]string{Digest(tarArchive)})
	compressed := gzipArchive("Quinistry image", tarArchive)
	manifest := createManifest(&configElem, &compressed)
	manifestJson, _ := json.Marshal(manifest)

	return Image{
		Layer:          compressed,
		LayerDigest:    Digest(compressed),
		Manifest:       manifestJson,
		ManifestDigest: Digest(manifestJson),
		Config:         configJson,
		ConfigDigest:   Digest(configJson),
	}

}

func getCurrentBinary() []byte {
	path, _ := os.Executable()
	file, _ := ioutil.ReadFile(path)
	return file
}

func createTarArchive(files *map[string][]byte) []byte {
	buf := new(bytes.Buffer)
	w := tar.NewWriter(buf)

	for name, file := range *files {
		hdr := &tar.Header{
			Name: name,
			// Everything is executable \o/
			Mode: 0755,
			Size: int64(len(file)),
		}
		w.WriteHeader(hdr)
		w.Write(file)
	}

	if err := w.Close(); err != nil {
		log.Fatalln(err)
		os.Exit(1)
	}

	return buf.Bytes()
}

func gzipArchive(name string, archive []byte) []byte {
	buf := new(bytes.Buffer)
	w := gzip.NewWriter(buf)
	w.Name = name
	w.Write(archive)

	if err := w.Close(); err != nil {
		log.Fatalln(err)
		os.Exit(1)
	}

	return buf.Bytes()
}

func createConfig(layerDigests []string) (configJson []byte, elem Element) {
	now := time.Now()

	imageConfig := &ImageConfig{
		Cmd: []string{"/main"},
		Env: []string{"PATH=/"},
	}

	rootFs := RootFs{
		DiffIds: layerDigests,
		Type:    "layers",
	}

	history := []History{
		{
			Created:   now,
			CreatedBy: "Quinistry magic",
		},
	}

	config := Config{
		Created:      now,
		Author:       "tazjin",
		Architecture: "amd64",
		Os:           "linux",
		Config:       imageConfig,
		RootFs:       rootFs,
		History:      history,
	}

	configJson, _ = json.Marshal(config)

	elem = Element{
		MediaType: ImageConfigMediaType,
		Size:      len(configJson),
		Digest:    Digest(configJson),
	}

	return
}

func createManifest(config *Element, layer *[]byte) Manifest {
	layers := []Element{
		{
			MediaType: LayerMediaType,
			Size:      len(*layer),
			// Layers must contain the digest of the *gzipped* layer.
			Digest: Digest(*layer),
		},
	}

	return Manifest{
		SchemaVersion: 2,
		MediaType:     ManifestMediaType,
		Config:        *config,
		Layers:        layers,
	}
}