-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eda64af
Showing
6 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM debian:buster | ||
|
||
RUN mkdir -p '/mnt/output' && \ | ||
mkdir -p '/mnt/pkgs' && \ | ||
mkdir -p '/build/src' && \ | ||
apt-get update && \ | ||
apt-get upgrade -y && \ | ||
apt-get -y install live-build cpio wget ca-certificates apt-utils | ||
|
||
COPY buildimage /usr/local/bin | ||
|
||
WORKDIR /build/src | ||
|
||
ENTRYPOINT /usr/local/bin/buildimage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 John Southworth | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
|
||
function import_source() { | ||
wget -O- -q http://repos.danosproject.org.s3-website-us-west-1.amazonaws.com/livebuild/danos-1908-amd64-vrouter.livebuild.tar | tar xv | ||
pushd danos-1908-amd64-vrouter | ||
mkdir -p config/archives/ | ||
echo 'deb http://s3-us-west-1.amazonaws.com/repos.danosproject.org/standard/ 1908 main' > config/archives/standard.list.chroot | ||
echo 'deb http://s3-us-west-1.amazonaws.com/repos.danosproject.org/bootstrap/ 1908 main' > config/archives/bootstrap.list.chroot | ||
wget https://s3-us-west-1.amazonaws.com/repos.danosproject.org/Release.key -O config/archives/standard.key.chroot | ||
wget https://s3-us-west-1.amazonaws.com/repos.danosproject.org/Release.key -O config/archives/bootstrap.key.chroot | ||
sed -i 's/--.*distribution .*\\/--distribution '"stretch"' \\/' auto/config | ||
popd | ||
} | ||
|
||
function import_preferred_packages() { | ||
mkdir -p danos-1908-amd64-vrouter/config/packages.chroot/ | ||
cp /mnt/pkgs/*.deb danos-1908-amd64-vrouter/config/packages.chroot/ | ||
} | ||
|
||
function build_image() { | ||
pushd danos-1908-amd64-vrouter | ||
auto/config | ||
auto/build | ||
popd | ||
} | ||
|
||
function export_image() { | ||
echo "exporting images... this may take a while" | ||
cp danos-1908-amd64-vrouter/danos-1908-amd64-vrouter* /mnt/output | ||
} | ||
|
||
function workaround_919659() { | ||
# Workaround https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=919659 | ||
sed -i '1161s%umount%#umount%' /usr/share/debootstrap/functions | ||
} | ||
|
||
workaround_919659 || exit 1 | ||
import_source || exit 1 | ||
import_preferred_packages | ||
build_image || exit 1 | ||
export_image || exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package buildimage | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/client" | ||
"github.com/docker/docker/pkg/stdcopy" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type Builder struct { | ||
cli *client.Client | ||
ctx context.Context | ||
version string | ||
destDir string | ||
pkgDir string | ||
|
||
containerID string | ||
} | ||
|
||
type MakeBuilderOption func(*Builder) | ||
|
||
func WithClient(cli *client.Client) MakeBuilderOption { | ||
return func(b *Builder) { | ||
b.cli = cli | ||
} | ||
} | ||
|
||
func WithContext(ctx context.Context) MakeBuilderOption { | ||
return func(b *Builder) { | ||
b.ctx = ctx | ||
} | ||
} | ||
|
||
func Version(version string) MakeBuilderOption { | ||
return func(b *Builder) { | ||
b.version = version | ||
} | ||
} | ||
|
||
func DestinationDirectory(destDir string) MakeBuilderOption { | ||
return func(b *Builder) { | ||
b.destDir = destDir | ||
} | ||
} | ||
|
||
func PreferredPackageDirectory(pkgDir string) MakeBuilderOption { | ||
return func(b *Builder) { | ||
b.pkgDir = pkgDir | ||
} | ||
} | ||
|
||
func MakeBuilder(opts ...MakeBuilderOption) (*Builder, error) { | ||
b := new(Builder) | ||
b.version = "latest" | ||
for _, opt := range opts { | ||
opt(b) | ||
} | ||
if b.cli == nil { | ||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.cli = cli | ||
} | ||
if b.ctx == nil { | ||
b.ctx = context.Background() | ||
} | ||
if b.destDir == "" { | ||
return nil, errors.New("must supply Destination Directory") | ||
} | ||
return b, nil | ||
} | ||
|
||
func (b *Builder) canonicalImageName() string { | ||
return "registry.hub.docker.com/" + b.imageName() | ||
} | ||
|
||
func (b *Builder) imageName() string { | ||
return "jsouthworth/danos-buildimage:" + b.version | ||
} | ||
|
||
func (b *Builder) pullEnvironment() error { | ||
log.Println("pulling environment", b.canonicalImageName()) | ||
r, err := b.cli.ImagePull( | ||
b.ctx, | ||
b.canonicalImageName(), | ||
types.ImagePullOptions{}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
io.Copy(os.Stdout, r) | ||
return nil | ||
} | ||
|
||
func (b *Builder) getBindMounts() []string { | ||
binds := []string{b.destDir + ":/mnt/output"} | ||
if b.pkgDir != "" { | ||
binds = append(binds, b.pkgDir+":/mnt/pkgs") | ||
} | ||
return binds | ||
} | ||
|
||
func (b *Builder) createEnvironment() error { | ||
log.Println("creating environment", b.canonicalImageName()) | ||
createResp, err := b.cli.ContainerCreate( | ||
b.ctx, | ||
&container.Config{ | ||
Image: b.canonicalImageName(), | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
}, | ||
&container.HostConfig{ | ||
Binds: b.getBindMounts(), | ||
Privileged: true, | ||
}, | ||
nil, | ||
"", | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
b.containerID = createResp.ID | ||
log.Println("containerID", b.containerID) | ||
return nil | ||
} | ||
|
||
func (b *Builder) buildImage() error { | ||
log.Println("building image", b.containerID) | ||
out, err := b.cli.ContainerAttach( | ||
b.ctx, | ||
b.containerID, | ||
types.ContainerAttachOptions{ | ||
Stream: true, | ||
Stdout: true, | ||
Stderr: true, | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
go stdcopy.StdCopy(os.Stdout, os.Stderr, out.Reader) | ||
|
||
err = b.cli.ContainerStart( | ||
b.ctx, | ||
b.containerID, | ||
types.ContainerStartOptions{}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = b.cli.ContainerWait(b.ctx, b.containerID) | ||
return err | ||
} | ||
|
||
func (b *Builder) deleteEnvironment() error { | ||
if b.containerID == "" { | ||
return nil | ||
} | ||
log.Println("deleting environment", b.containerID) | ||
return b.cli.ContainerRemove( | ||
b.ctx, | ||
b.containerID, | ||
types.ContainerRemoveOptions{}, | ||
) | ||
} | ||
|
||
func (b *Builder) Build() (err error) { | ||
type buildStep func() error | ||
|
||
defer func() { | ||
e := b.deleteEnvironment() | ||
if err == nil { | ||
err = e | ||
} | ||
}() | ||
|
||
steps := []buildStep{ | ||
b.pullEnvironment, | ||
b.createEnvironment, | ||
b.buildImage, | ||
} | ||
for _, step := range steps { | ||
err := step() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
bimg "jsouthworth.net/go/danos-buildimage" | ||
) | ||
|
||
var srcDir, destDir, pkgDir, version string | ||
|
||
func init() { | ||
flag.StringVar(&destDir, "dest", ".", "destination directory") | ||
flag.StringVar(&pkgDir, "pkg", "", "preferred package directory") | ||
flag.StringVar(&version, "version", "latest", "version of danos to build for") | ||
} | ||
|
||
func resolvePath(in string) string { | ||
out, err := filepath.Abs(in) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
return out | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
b, err := bimg.MakeBuilder( | ||
bimg.DestinationDirectory(resolvePath(destDir)), | ||
bimg.PreferredPackageDirectory(resolvePath(pkgDir)), | ||
bimg.Version(version), | ||
) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
err = b.Build() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module jsouthworth.net/go/danos-buildimage | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/docker/distribution v2.7.1+incompatible // indirect | ||
github.com/docker/docker v1.13.1 | ||
github.com/docker/go-connections v0.4.0 // indirect | ||
github.com/docker/go-units v0.4.0 // indirect | ||
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect | ||
golang.org/x/net v0.0.0-20191116160921-f9c825593386 | ||
) |