Skip to content

Commit

Permalink
Merge branch 'release/0.28.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotknopf committed Apr 21, 2019
2 parents 0f247df + e741d2f commit 1853ddb
Show file tree
Hide file tree
Showing 23 changed files with 884 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ install:
@install -D -m 755 scripts/hooks/hostname $(DESTDIR)/etc/darch/hooks/hostname/hook
@echo "installing /etc/darch/hooks/ssh/hook"
@install -D -m 755 scripts/hooks/ssh $(DESTDIR)/etc/darch/hooks/ssh/hook
@echo "installing /etc/darch/hooks/machine-id/hook"
@install -D -m 755 scripts/hooks/machine-id $(DESTDIR)/etc/darch/hooks/machine-id/hook
@echo "installing /etc/grub.d/60_darch"
@install -D -m 755 scripts/grub-mkconfig-script $(DESTDIR)/etc/grub.d/60_darch
clean_bundle:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Darch
[![Build Status](https://travis-ci.org/godarch/darch.svg?branch=develop)](https://travis-ci.org/godarch/darch)
[![Build Status](https://travis-ci.org/godarch/darch.svg?branch=develop)](https://travis-ci.org/godarch/darch) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](http://paypal.me/pauldotknopf)

Think "Dockerfiles", but for immutable, stateless, graphical (or not) environments, booted bare-metal.

Expand Down
32 changes: 30 additions & 2 deletions pkg/cmd/darch/commands/images/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ package images
import (
"context"
"fmt"
"os"
"text/tabwriter"

"github.com/containerd/containerd/pkg/progress"
"github.com/godarch/darch/pkg/repository"
"github.com/urfave/cli"
)

var listCommand = cli.Command{
Name: "list",
Usage: "list images",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "print only the image refs",
},
},
Action: func(clicontext *cli.Context) error {
quiet := clicontext.Bool("quiet")

repo, err := repository.NewSession(repository.DefaultContainerdSocketLocation)
if err != nil {
Expand All @@ -24,10 +34,28 @@ var listCommand = cli.Command{
return err
}

if quiet {
for _, img := range imgs {
fmt.Println(img.Name + ":" + img.Tag)
}
return nil
}

tw := tabwriter.NewWriter(os.Stdout, 1, 8, 2, ' ', 0)
fmt.Fprintln(tw, "REPOSITORY\tTAG\tCREATED\tSIZE\t")
for _, img := range imgs {
fmt.Println(img.Name + ":" + img.Tag)
size, err := repo.GetImageSize(context.Background(), fmt.Sprintf("%s:%s", img.Name, img.Tag))
if err != nil {
return err
}

fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t\n",
img.Name,
img.Tag,
img.CreatedAt.Format("2006-01-02"),
progress.Bytes(size))
}

return nil
return tw.Flush()
},
}
2 changes: 1 addition & 1 deletion pkg/cmd/darch/commands/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var pullCommand = cli.Command{

fmt.Printf("pulling %s\n", imageRef.FullName())

err = repo.Pull(ctx.Background(), imageRef, resolver)
_, err = repo.Pull(ctx.Background(), imageRef, resolver)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/darch/commands/recipes/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package recipes
import (
"context"
"fmt"
"github.com/godarch/darch/pkg/cmd/darch/commands"
"github.com/godarch/darch/pkg/recipes"
"github.com/godarch/darch/pkg/repository"
"github.com/urfave/cli"
Expand Down Expand Up @@ -62,10 +63,15 @@ var buildCommand = cli.Command{
return err
}

resolver, err := commands.GetResolver(clicontext)
if err != nil {
return err
}

// Now, let's go through each recipe and build it.
for _, recipeName := range recipeNames {
fmt.Printf("building %s...\n", recipeName)
image, err := session.BuildRecipe(context.Background(), allRecipes[recipeName], defaultTag, imagePrefix, env)
image, err := session.BuildRecipe(context.Background(), allRecipes[recipeName], defaultTag, imagePrefix, env, resolver)
if err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/repository/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/opencontainers/image-spec/identity"

"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/remotes"

"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/images"
Expand All @@ -23,7 +25,7 @@ import (
)

// BuildRecipe Builds a recipe.
func (session *Session) BuildRecipe(ctx context.Context, recipe recipes.Recipe, tag string, imagePrefix string, env []string) (reference.ImageRef, error) {
func (session *Session) BuildRecipe(ctx context.Context, recipe recipes.Recipe, tag string, imagePrefix string, env []string, resolver remotes.Resolver) (reference.ImageRef, error) {

ctx = namespaces.WithNamespace(ctx, "darch")

Expand Down Expand Up @@ -56,7 +58,16 @@ func (session *Session) BuildRecipe(ctx context.Context, recipe recipes.Recipe,

img, err := session.client.GetImage(ctx, inheritsRef.FullName())
if err != nil {
return newImage, err
if errdefs.IsNotFound(err) {
fmt.Printf("pulling %s\n", inheritsRef.FullName())
img, err = session.Pull(ctx, inheritsRef, resolver)

if err != nil {
return newImage, err
}
} else {
return newImage, err
}
}

ws, err := workspace.NewWorkspace("/tmp")
Expand Down
13 changes: 13 additions & 0 deletions pkg/repository/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"context"
"github.com/containerd/containerd/platforms"
"time"

"github.com/containerd/containerd/errdefs"
Expand Down Expand Up @@ -43,6 +44,18 @@ func (session *Session) GetImages(ctx context.Context) ([]Image, error) {
return result, nil
}

// GetImageSize Returns the size of an image.
func (session *Session) GetImageSize(ctx context.Context, name string) (int64, error) {
ctx = namespaces.WithNamespace(ctx, "darch")

img, err := session.client.ImageService().Get(ctx, name)
if err != nil {
return -1, err
}

return img.Size(ctx, session.client.ContentStore(), platforms.Default())
}

// TagImage Tag an image.
func (session *Session) TagImage(ctx context.Context, source, destination reference.ImageRef) error {
ctx = namespaces.WithNamespace(ctx, "darch")
Expand Down
8 changes: 4 additions & 4 deletions pkg/repository/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func (r *overrideNameResolve) Pusher(ctx context.Context, ref string) (remotes.P
}

// Pull Pulls an image locally.
func (session *Session) Pull(ctx context.Context, imageRef reference.ImageRef, resolver remotes.Resolver) error {
func (session *Session) Pull(ctx context.Context, imageRef reference.ImageRef, resolver remotes.Resolver) (containerd.Image, error) {
pullRef := imageRef
if len(pullRef.Domain()) == 0 {
parsedRef, err := pullRef.WithDomain(reference.DefaultDomain)
if err != nil {
return err
return nil, err
}
pullRef = parsedRef
}

_, err := session.client.Pull(namespaces.WithNamespace(ctx, "darch"),
img, err := session.client.Pull(namespaces.WithNamespace(ctx, "darch"),
pullRef.FullName(),
containerd.WithResolver(&overrideNameResolve{
RealResolver: resolver,
Expand All @@ -47,5 +47,5 @@ func (session *Session) Pull(ctx context.Context, imageRef reference.ImageRef, r
}),
containerd.WithPullUnpack)

return err
return img, err
}
9 changes: 9 additions & 0 deletions pkg/staging/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"time"

"github.com/godarch/darch/pkg/reference"
"github.com/godarch/darch/pkg/utils"
Expand All @@ -18,6 +20,7 @@ type StagedImage struct {
InitRAMFS string
RootFS string
NoDoubleMount bool
CreationTime time.Time
}

// StagedImageNamed A StagedImage with a name and tag
Expand Down Expand Up @@ -73,11 +76,17 @@ func parseImageDir(imageDir string) (StagedImage, error) {
return result, fmt.Errorf("rootfs was invalid")
}

stat, err := os.Stat(imageDir)
if err != nil {
return result, err
}

result.InitRAMFS = config.InitRAMFS
result.Kernel = config.Kernel
result.KernelParams = config.KernelParams
result.RootFS = config.RootFS
result.NoDoubleMount = config.NoDoubleMount
result.CreationTime = stat.ModTime()

return result, nil
}
Expand Down
44 changes: 38 additions & 6 deletions pkg/staging/sort.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
package staging

// ByAge implements sort.Interface for []Person based on
// the Age field.
type sortStageImageNamed []StagedImageNamed
// sortStageImageNamedByName implements sort.Interface for []StagedImageNamed
// based on the FullName field in an ascending order.
type sortStagedImageNamedByName []StagedImageNamed

func (a sortStageImageNamed) Len() int { return len(a) }
func (a sortStageImageNamed) Less(i, j int) bool { return a[i].Ref.FullName() < a[j].Ref.FullName() }
func (a sortStageImageNamed) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortStagedImageNamedByName) Len() int { return len(a) }
func (a sortStagedImageNamedByName) Less(i, j int) bool {
return a[i].Ref.FullName() < a[j].Ref.FullName()
}
func (a sortStagedImageNamedByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// sortStageImageNamedByNameDesc implements sort.Interface for []StagedImageNamed
// based on the FullName field in an descending order.
type sortStagedImageNamedByNameDesc []StagedImageNamed

func (a sortStagedImageNamedByNameDesc) Len() int { return len(a) }
func (a sortStagedImageNamedByNameDesc) Less(i, j int) bool {
return a[i].Ref.FullName() > a[j].Ref.FullName()
}
func (a sortStagedImageNamedByNameDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// sortStagedImageNamedByAge implements sort.Interface for []StagedImageNamed
// based on the CreationTime field in an ascending order.
type sortStagedImageNamedByAge []StagedImageNamed

func (a sortStagedImageNamedByAge) Len() int { return len(a) }
func (a sortStagedImageNamedByAge) Less(i, j int) bool {
return a[i].CreationTime.Before(a[j].CreationTime)
}
func (a sortStagedImageNamedByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// sortStagedImageNamedByAgeDesc implements sort.Interface for []StagedImageNamed
// based on the CreationTime field in an descending order.
type sortStagedImageNamedByAgeDesc []StagedImageNamed

func (a sortStagedImageNamedByAgeDesc) Len() int { return len(a) }
func (a sortStagedImageNamedByAgeDesc) Less(i, j int) bool {
return a[i].CreationTime.After(a[j].CreationTime)
}
func (a sortStagedImageNamedByAgeDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
2 changes: 1 addition & 1 deletion pkg/staging/staging.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (session *Session) GetAllStaged() ([]StagedImageNamed, error) {
}

// Sort the images.
sort.Sort(sortStageImageNamed(result))
sort.Sort(sortStagedImageNamedByName(result))

return result, nil
}
Expand Down
26 changes: 26 additions & 0 deletions scripts/hooks/machine-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env sh
set -e

help() {
echo "Help..."
}

install() {
# Let's copy our a machine-id if we
# haven't currently stored it.
if [ ! -e /etc/darch/hooks/current-machine-id ]; then
if [ -e /etc/machine-id ]; then
cp /etc/machine-id /etc/darch/hooks/current-machine-id
fi
fi

cp /etc/darch/hooks/current-machine-id . || true
}

run() {
if [ -e "$DARCH_HOOK_DIR/current-machine-id" ]; then
rm -f "$DARCH_ROOT_FS/etc/machine-id"
mkdir -p "$DARCH_ROOT_FS/etc" || true
cp "$DARCH_HOOK_DIR/current-machine-id" "$DARCH_ROOT_FS/etc/machine-id"
fi
}
1 change: 1 addition & 0 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ github.com/gogo/protobuf v1.0.0
github.com/gogo/googleapis 08a7655d27152912db7aaf4f983275eaf8d128ef
github.com/gobwas/glob 51eb1ee00b6d931c66d229ceeb7c31b985563420
github.com/docker/go-events 9461782956ad83b30282bf90e31fa6a70c255ba9
github.com/docker/go-units v0.3.1
github.com/docker/distribution b38e5838b7b2f2ad48e06ec4b500011976080621
github.com/docker/docker 86f080cff0914e9694068ed78d503701667c4c00
github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23
Expand Down
Loading

0 comments on commit 1853ddb

Please sign in to comment.