Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Support minifying Docker Archives #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ smith
rpm
.idea/
.wercker
*.tar.gz
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
FROM oraclelinux:7-slim

RUN yum --enablerepo=ol7_optional_latest install -y git golang make
RUN yum install -y oracle-golang-release-el7 && yum install -y git golang make

WORKDIR /tmp
WORKDIR /src

ADD . .

RUN make install

FROM oraclelinux:7-slim

RUN yum install -y --enablerepo ol7_developer_EPEL pigz mock && yum clean all
RUN yum install -y oracle-epel-release-el7 && yum install -y pigz mock && yum clean all

ADD etc /etc

Expand Down
57 changes: 0 additions & 57 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

42 changes: 37 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ id = $(shell head -c20 /dev/urandom|od -An -tx1|tr -d ' \n')
# if it is cloned outside of the gopath. Go's vendor support only works if
# the project is inside the gopath
$(NAME): $(call gofiles,$(DIRS))
rm -rf build
mkdir -p build/src/$(REMOTE)
rm -f build/src/$(REMOTE)/$(NAME) && ln -s ../../../../ build/src/$(REMOTE)/$(NAME)
cd build/src/$(REMOTE)/$(NAME) && CGO_ENABLED=0 GOPATH=$(CURDIR)/build \
GO15VENDOREXPERIMENT=1 go build -a -x -v \
CGO_ENABLED=0 go build -a -x -v \
-ldflags '-X "main.ver=$(VERSION)" -X "main.sha=$(sha)" -B 0x$(id)' \
-o $(NAME) .

Expand Down Expand Up @@ -81,3 +77,39 @@ rpms: rpm/smith-$(VERSION)-3.x86_64.rpm
version:
@echo $(VERSION)

build-docker-clean:
rm -rf from-docker-image.tar.gz from-smith-image.tar.gz checksum.txt

ORG := ${USER}
REPO := ${ORG}/${NAME}
TAG := ${REPO}:${VERSION}

build-docker-build:
docker build -t ${TAG} .

build-docker-archive: build-docker-clean build-docker-build
docker save ${TAG} | gzip -c > from-docker-image.tar.gz
docker run \
-it --rm \
-v ${PWD}:/write \
-v tmp:/tmp \
${TAG} \
--docker \
-i from-smith-image.tar.gz \
--tag ${TAG}
docker load -i from-smith-image.tar.gz
sha1sum from-smith-image.tar.gz > checksum.txt
rm from-smith-image.tar.gz
docker run \
-it --rm \
-v ${PWD}:/write \
-v tmp:/tmp \
${TAG} \
--docker \
-i from-smith-image.tar.gz \
--tag ${TAG}
sha1sum -c checksum.txt
rm -rf from-docker-image.tar.gz from-smith-image.tar.gz checksum.txt
docker tag ${TAG} ${REPO}:latest

build-docker: build-docker-archive build-docker-clean
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ Both methods are described below, but __the Docker route is recommended__ as the

2. Build `smith` Docker image using the Dockerfile provided, optionally adding your own docker-repo-id to the tag:

`sudo docker build -t [<docker-repo-id>/]smith .`
`make build-docker`

or

`sudo docker build -t ${USER}/smith .`

3. Set up an alias (or script) to run `smith` from the command line:
```
Expand All @@ -52,7 +56,7 @@ smith(){
--privileged -v $PWD:/write \
-v cache:/var/cache \
-v /tmp:/tmp \
-v mock:/var/lib/mock [<docker-repo-id>/]smith $@
-v mock:/var/lib/mock ${USER}/smith $@
}
```
You should now be able to start building microcontainers (see below).
Expand Down Expand Up @@ -137,6 +141,8 @@ To use smith, simply create a smith.yaml defining your container and run
`smith`. If you want to overlay additional files or symlinks, simply place them
into a directory called `rootfs` beside smith.yaml.

Please consult the documentation for a list of all relevant fields for [smith.yaml](docs/smith-config-file.md)

If you are building the same container multiple times without editing the
package line, the `-f` parameter will rebuild the container without
reinstalling the package.
Expand Down Expand Up @@ -209,7 +215,7 @@ To build Smith directly from oci, the Docker command is slightly different:
smith(){
docker run -it --rm \
-v $PWD:/write \
-v tmp:/tmp vishvananda/smith $@
-v tmp:/tmp ${USER}/smith $@
}
```

Expand Down
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* create ArchiveWriter encapsulation
* create ArchiveReader encapsulation
* reuse layers from local machine cache if available?
38 changes: 29 additions & 9 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/oracle/smith/execute"

"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -27,9 +27,11 @@ type buildOptions struct {
conf string
dir string
buildNo string
format string
tag string
}

func isOci(uri string) bool {
func isContainer(uri string) bool {
// urls are oci images
if strings.HasPrefix(uri, "http://") ||
strings.HasPrefix(uri, "https://") {
Expand All @@ -54,12 +56,13 @@ func isOci(uri string) bool {
func installPackage(buildOpts *buildOptions, outputDir string, pkg *ConfigDef) ([]string, error) {
logrus.Infof("Installing package %v", pkg.Package)
if pkg.Type == "" {
if isOci(pkg.Package) {
pkg.Type = "oci"
if isContainer(pkg.Package) {
pkg.Type = "container"
} else {
pkg.Type = "mock"
}
}

switch pkg.Type {
case "mock":
if pkg.Mock.Config == "" {
Expand All @@ -76,7 +79,11 @@ func installPackage(buildOpts *buildOptions, outputDir string, pkg *ConfigDef) (
sort.Strings(packages)
return packages, nil
case "oci":
if err := buildOci(buildOpts, outputDir, pkg); err != nil {
logrus.Warnf("smith build type %s is deprecated, please use %s", pkg.Type, "container")
pkg.Type = "container"
fallthrough
case "container":
if err := buildContainerArchive(buildOpts, outputDir, pkg); err != nil {
return nil, err
}
return nil, nil
Expand Down Expand Up @@ -225,10 +232,23 @@ func buildContainer(tarfile string, buildOpts *buildOptions) bool {

// pack
logrus.Infof("Packing image into %v", outpath)
if err := WriteOciFromBuild(pkg, buildDir, outpath, metadata, extraBlobs); err != nil {
logrus.Errorf("Failed to pack dir into %v: %v", outpath, err)

switch buildOpts.format {
case "oci":
if err := WriteOciFromBuild(buildOpts, pkg, buildDir, outpath, metadata, extraBlobs); err != nil {
logrus.Errorf("Failed to pack dir into %v: %v", outpath, err)
return false
}
case "docker":
if err := WriteDockerFromBuild(buildOpts, pkg, buildDir, outpath, metadata, extraBlobs); err != nil {
logrus.Fatalf("Failed to pack dir into %v: %v", outpath, err)
return false
}
default:
logrus.Fatalf("unknown build output format: %v", buildOpts.format)
return false
}

return true
}

Expand Down Expand Up @@ -329,7 +349,7 @@ func buildMock(buildOpts *buildOptions, outputDir string, pkg *ConfigDef, pkgMfs
return nil
}

func buildOci(buildOpts *buildOptions, outputDir string, pkg *ConfigDef) error {
func buildContainerArchive(buildOpts *buildOptions, outputDir string, pkg *ConfigDef) error {
uid, gid := os.Getuid(), os.Getgid()
unpackDir := filepath.Join(os.TempDir(), "smith-unpack-"+strconv.Itoa(uid))

Expand Down Expand Up @@ -410,7 +430,7 @@ func buildOci(buildOpts *buildOptions, outputDir string, pkg *ConfigDef) error {
if err := os.MkdirAll(unpackDir, 0755); err != nil {
return err
}
if err := ExtractOci(image, unpackDir); err != nil {
if err := extractLayers(image, unpackDir); err != nil {
return err
}
if err := readablePathsFromExecutor(executor, pkg.Paths); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"io/ioutil"

"github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/sirupsen/logrus"
)

type MockDef struct {
Expand Down
2 changes: 1 addition & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)

func CopyTree(baseDir, outputDir string, globs []string, excludes []string, nss, follow, chroot bool) error {
Expand Down
2 changes: 1 addition & 1 deletion deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var (
Expand Down
Loading