Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotknopf committed Oct 12, 2017
2 parents 23c0ab8 + d2d9f6d commit 1beff3e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 82 deletions.
49 changes: 40 additions & 9 deletions commands/build/build.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package build

import (
"fmt"
"log"
"strings"

"../../images"
"../../utils"
"github.com/urfave/cli"
)

// Command Returns the command to be passed to a cli context.
func Command() cli.Command {
return cli.Command{
Name: "build",
Usage: "Build an image.",
UsageText: "darch build [options] [image]",
Name: "build",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir",
Expand All @@ -23,25 +23,56 @@ func Command() cli.Command {
Name: "tags",
Value: "local",
},
cli.StringFlag{
Name: "imagePrefix",
Value: "",
},
},
Action: func(c *cli.Context) error {
return build(c.Args().First(), c.String("imagesDir"), strings.Split(c.String("tags"), ","))
err := build(c.Args().First(), c.String("imagesDir"), strings.Split(c.String("tags"), ","), c.String("imagePrefix"))
if err != nil {
return cli.NewExitError(err, 1)
}
return err
},
}
}

func build(imageName string, imagesDir string, tags []string) error {
func build(name string, imagesDir string, tags []string, imagePrefix string) error {

if len(name) == 0 {
return fmt.Errorf("Name is required")
}

if len(imagesDir) == 0 {
return fmt.Errorf("Images directory is required")
}

imagesDir = utils.ExpandPath(imagesDir)

log.Println("Name: " + name)
log.Println("Images directory: " + imagesDir)
log.Println("Image name: " + imageName)
log.Println("Image prefix: " + imagePrefix)

if len(tags) > 0 {
log.Println("Tags:")
for _, tag := range tags {
log.Println("\t" + tag)
}
} else {
log.Println("Tags: none")
}

imageDefinition, err := images.BuildDefinition(imageName, imagesDir)
imageDefinition, err := images.BuildDefinition(name, imagesDir)

if err != nil {
return cli.NewExitError(err, 1)
}

err = images.BuildImageLayer(imageDefinition,
tags)
err = images.BuildImageLayer(
imageDefinition,
tags,
imagePrefix)

if err != nil {
return cli.NewExitError(err, 1)
Expand Down
41 changes: 23 additions & 18 deletions commands/extract/extract.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package extract

import (
"fmt"
"log"
"path"

"../../images"
"../../utils"
"github.com/urfave/cli"
)

Expand All @@ -15,42 +17,45 @@ func Command() cli.Command {
Usage: "Extract an image.",
UsageText: "darch extract [options] [image]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir",
Value: ".",
},
cli.StringFlag{
Name: "tag",
Value: "local",
},
cli.StringFlag{
Name: "destination",
Name: "destination",
Value: "/var/darch",
},
},
Action: func(c *cli.Context) error {
return extract(c.Args().First(), c.String("imagesDir"), c.String("tag"), c.String("destination"))
err := extract(c.Args().First(), c.String("tag"), c.String("destination"))
if err != nil {
return cli.NewExitError(err, 1)
}
return err
},
}
}

func extract(imageName string, imagesDir string, tag string, destination string) error {
log.Println("Images directory: " + imagesDir)
log.Println("Image name: " + imageName)
log.Println("Tag: " + tag)
func extract(name string, tag string, destinationDirectory string) error {

imageDefinition, err := images.BuildDefinition(imageName, imagesDir)
if len(name) == 0 {
return fmt.Errorf("Name is required")
}

if err != nil {
return cli.NewExitError(err, 1)
if len(tag) == 0 {
return fmt.Errorf("Tag is required")
}

if len(destination) == 0 {
destination = path.Join(imageDefinition.ImagesDir, ".extracted", imageDefinition.Name+"-"+tag)
if len(destinationDirectory) == 0 {
return fmt.Errorf("Destination is required")
}

log.Println("Destination: " + destination)
destinationDirectory = utils.ExpandPath(destinationDirectory)
destinationDirectory = path.Join(destinationDirectory, name+"/"+tag)

images.ExtractImage(imageDefinition, tag, destination)
log.Println("Name: " + name)
log.Println("Tag: " + tag)
log.Println("Destination: " + destinationDirectory)

return nil
return images.ExtractImage(name, tag, destinationDirectory)
}
49 changes: 26 additions & 23 deletions commands/stage/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ import (
// Command Returns the command to be passed to a cli context.
func Command() cli.Command {
return cli.Command{
Name: "stage",
Usage: "Stage an image for booting.",
UsageText: "darch stage [options] [image]",
Name: "stage",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir",
Value: ".",
},
cli.StringFlag{
Name: "tag",
Value: "local",
},
cli.StringFlag{
Name: "source",
Value: "/var/darch",
},
cli.StringFlag{
Name: "fstab",
Value: "defaultfstab",
},
},
Action: func(c *cli.Context) error {
err := stage(c.Args().First(), c.String("imagesDir"), c.String("tag"), c.String("fstab"))
err := stage(c.Args().First(), c.String("tag"), c.String("source"), c.String("fstab"))
if err != nil {
return cli.NewExitError(err, 1)
}
Expand All @@ -41,28 +39,33 @@ func Command() cli.Command {
}
}

func stage(imageName string, imagesDir string, tag string, fstab string) error {
func stage(name string, tag string, sourceDirectory string, fstab string) error {

if len(imageName) == 0 {
return fmt.Errorf("An image name is required")
if len(name) == 0 {
return fmt.Errorf("Name is required")
}

imagesDir = utils.ExpandPath(imagesDir)
if len(tag) == 0 {
return fmt.Errorf("Tag is required")
}

if len(imagesDir) == 0 {
return fmt.Errorf("An images directory must be provided")
if len(sourceDirectory) == 0 {
return fmt.Errorf("Source is required")
}

log.Println("Image name: " + imageName)
log.Println("Images directory: " + imagesDir)
sourceDirectory = utils.ExpandPath(sourceDirectory)

log.Println("Name: " + name)
log.Println("Tag: " + tag)
log.Println("Source: " + sourceDirectory)

sourceDirectory := path.Join(imagesDir, ".extracted", imageName+"-"+tag)
sourceImageDirectory := path.Join(sourceDirectory, name+"/"+tag)

if !utils.DirectoryExists(sourceDirectory) {
return fmt.Errorf("No image found at %s", sourceDirectory)
if !utils.DirectoryExists(sourceImageDirectory) {
return fmt.Errorf("No image found at %s", sourceImageDirectory)
}

destinationDirectory := path.Join("/boot", "darch", imageName+"-"+tag)
destinationDirectory := path.Join("/boot", "darch", name, tag)

if utils.DirectoryExists(destinationDirectory) {
log.Println("Cleaning already existing staging directory...")
Expand All @@ -75,14 +78,14 @@ func stage(imageName string, imagesDir string, tag string, fstab string) error {

log.Printf("Copying boot files to %s...\n", destinationDirectory)

err := utils.CopyDir(sourceDirectory, destinationDirectory)
err := utils.CopyDir(sourceImageDirectory, destinationDirectory)
if err != nil {
return err
}

if len(fstab) > 0 {
if !path.IsAbs(fstab) {
fstab = utils.ExpandPath(path.Join(imagesDir, fstab))
fstab = utils.ExpandPath(path.Join(sourceDirectory, fstab))
}

log.Printf("Staging fstab file %s\n", fstab)
Expand All @@ -94,7 +97,7 @@ func stage(imageName string, imagesDir string, tag string, fstab string) error {
}
}

log.Printf("Successfully staged %s at tag %s\n", imageName, tag)
log.Printf("Successfully staged %s at tag %s\n", name, tag)

if !utils.FileExists("/etc/grub.d/60_darch") {
return fmt.Errorf("Grub generator doesn't exist at %s", "/etc/grub.d/60_darch")
Expand Down
7 changes: 5 additions & 2 deletions grub-mkconfig-script
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ set -e
# This gives us the "linux_entry" command
source /etc/grub.d/10_linux > /dev/null 2>&1

for i in /boot/darch/* ; do
for i in $(find /boot/darch/ -type d); do
linux="$i/vmlinuz-linux"
if [ ! -f $linux ]; then
continue
fi
gettext_printf "Found linux image: %s\n" "$linux" >&2
basename=`basename $linux`
dirname=`dirname $linux`
Expand All @@ -19,5 +22,5 @@ for i in /boot/darch/* ; do
darch_source_uuid=`blkid $darch_source_block_device | sed -n 's/.* UUID=\"\([^\"]*\)\".*/\1/p'`
darch_source_absolute=`echo ${i#$(df $i --output=target | tail -1)}`
darch_cmdline="darchdir=UUID=$darch_source_uuid:$darch_source_absolute"
linux_entry "Darch - $(basename $i)" "${version}" "simple" "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} $darch_cmdline"
linux_entry "Darch - $image_name" "${version}" "simple" "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} $darch_cmdline"
done
44 changes: 14 additions & 30 deletions images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import (

// ImageDefinition A struct representing an image to be built.
type ImageDefinition struct {
Name string
ImageDir string
ImagesDir string
Inherits []string
BuildImageScript string
ExtractImageScript string
Name string
ImageDir string
ImagesDir string
Inherits []string
}

type imageConfiguration struct {
Expand All @@ -41,23 +39,13 @@ func BuildDefinition(imageName string, imagesDir string) (*ImageDefinition, erro
image := ImageDefinition{}

image.ImagesDir = utils.ExpandPath(imagesDir)
image.ImageDir = path.Join(imagesDir, imageName)
image.BuildImageScript = path.Join(imagesDir, "build-image")
image.ExtractImageScript = path.Join(imagesDir, "extract-image")
image.ImageDir = path.Join(image.ImagesDir, imageName)
image.Name = imageName

if !utils.DirectoryExists(image.ImageDir) {
return nil, fmt.Errorf("Image directory %s doesn't exist", image.ImageDir)
}

if !utils.FileExists(image.BuildImageScript) {
return nil, fmt.Errorf("No build-image script exists at %s", image.BuildImageScript)
}

if !utils.FileExists(image.ExtractImageScript) {
return nil, fmt.Errorf("No build-image script exists at %s", image.ExtractImageScript)
}

imageConfiguration, err := loadImageConfiguration(image)

if err != nil {
Expand All @@ -72,20 +60,16 @@ func BuildDefinition(imageName string, imagesDir string) (*ImageDefinition, erro
}

// BuildImageLayer Run installation scripts on top of another image.
func BuildImageLayer(imageDefinition *ImageDefinition, tags []string) error {
func BuildImageLayer(imageDefinition *ImageDefinition, tags []string, buildPrefix string) error {
inherits := imageDefinition.Inherits[0]
if strings.HasPrefix(inherits, "external:") {
inherits = inherits[len("external:"):len(inherits)]
} else {
inherits = buildPrefix + inherits
}

log.Println("Building image " + imageDefinition.Name + ".")
log.Println("Building image " + buildPrefix + imageDefinition.Name + ".")
log.Println("Using parent image " + inherits + ".")
if len(tags) > 0 {
log.Println("Using the following tags:")
for _, tag := range tags {
log.Println("\t" + tag)
}
}

tmpImageName := "darch-building-" + imageDefinition.Name

Expand All @@ -110,13 +94,13 @@ func BuildImageLayer(imageDefinition *ImageDefinition, tags []string) error {
return err
}

err = runCommand("docker", "commit", tmpImageName, imageDefinition.Name)
err = runCommand("docker", "commit", tmpImageName, buildPrefix+imageDefinition.Name)
if err != nil {
return err
}

for _, tag := range tags {
err = runCommand("docker", "tag", imageDefinition.Name, imageDefinition.Name+":"+tag)
err = runCommand("docker", "tag", imageDefinition.Name, buildPrefix+imageDefinition.Name+":"+tag)
if err != nil {
return err
}
Expand All @@ -136,10 +120,10 @@ func BuildImageLayer(imageDefinition *ImageDefinition, tags []string) error {
}

// ExtractImage Extracts an image (with tag) to a specified directory
func ExtractImage(imageDefinition *ImageDefinition, tag string, destination string) error {
tmpImageName := "darch-extracting-" + imageDefinition.Name
func ExtractImage(name string, tag string, destination string) error {
tmpImageName := "darch-extracting-" + strings.Replace(name, "/", "", -1)

imageName := imageDefinition.Name
imageName := name
if len(tag) > 0 {
imageName = imageName + ":" + tag
}
Expand Down

0 comments on commit 1beff3e

Please sign in to comment.