Skip to content

Commit

Permalink
Introduce packages 'debos' and 'actions'
Browse files Browse the repository at this point in the history
This change allows to import and use core modules from "debos" package and
actions from "actions" package. Add prefix 'debos.' to all symbols imported
from core "debos" package to satisfy syntax check.

Package "actions" shouldn't keep any common function or structure:

- function 'DownloadHttpUrl()' is moved from 'DownloadAction' to "debos"
  package. New file 'net.go' has been added to keep a common network functions
  in core part of debos.

- move common functions for extracting files from 'UnpackAction' to 'debos'
  package. File 'archiver.go' has been added for common operations on archives.

Signed-off-by: Denis Pynkin <[email protected]>
  • Loading branch information
d4s committed Sep 5, 2017
1 parent 8a89531 commit 5d73e46
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 212 deletions.
12 changes: 8 additions & 4 deletions actions/apt_action.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main
package actions

import (
"github.com/go-debos/debos"
)

type AptAction struct {
BaseAction `yaml:",inline"`
debos.BaseAction `yaml:",inline"`
Recommends bool
Packages []string
}

func (apt *AptAction) Run(context *DebosContext) error {
func (apt *AptAction) Run(context *debos.DebosContext) error {
apt.LogStart()
aptOptions := []string{"apt-get", "-y"}

Expand All @@ -17,7 +21,7 @@ func (apt *AptAction) Run(context *DebosContext) error {
aptOptions = append(aptOptions, "install")
aptOptions = append(aptOptions, apt.Packages...)

c := NewChrootCommand(context.rootdir, context.Architecture)
c := debos.NewChrootCommand(context.Rootdir, context.Architecture)
c.AddEnv("DEBIAN_FRONTEND=noninteractive")

err := c.Run("apt", "apt-get", "update")
Expand Down
22 changes: 12 additions & 10 deletions actions/debootstrap_action.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package main
package actions

import (
"fmt"
"io"
"os"
"path"
"strings"

"github.com/go-debos/debos"
)

type DebootstrapAction struct {
BaseAction `yaml:",inline"`
debos.BaseAction `yaml:",inline"`
Suite string
Mirror string
Variant string
KeyringPackage string `yaml: keyring-package`
Components []string
}

func (d *DebootstrapAction) RunSecondStage(context DebosContext) error {
func (d *DebootstrapAction) RunSecondStage(context debos.DebosContext) error {
cmdline := []string{
"/debootstrap/debootstrap",
"--no-check-gpg",
Expand All @@ -28,14 +30,14 @@ func (d *DebootstrapAction) RunSecondStage(context DebosContext) error {
cmdline = append(cmdline, fmt.Sprintf("--components=%s", s))
}

c := NewChrootCommand(context.rootdir, context.Architecture)
c := debos.NewChrootCommand(context.Rootdir, context.Architecture)
// Can't use nspawn for debootstrap as it wants to create device nodes
c.ChrootMethod = CHROOT_METHOD_CHROOT
c.ChrootMethod = debos.CHROOT_METHOD_CHROOT

return c.Run("Debootstrap (stage 2)", cmdline...)
}

func (d *DebootstrapAction) Run(context *DebosContext) error {
func (d *DebootstrapAction) Run(context *debos.DebosContext) error {
d.LogStart()
cmdline := []string{"debootstrap", "--no-check-gpg",
"--merged-usr"}
Expand Down Expand Up @@ -63,11 +65,11 @@ func (d *DebootstrapAction) Run(context *DebosContext) error {
}

cmdline = append(cmdline, d.Suite)
cmdline = append(cmdline, context.rootdir)
cmdline = append(cmdline, context.Rootdir)
cmdline = append(cmdline, d.Mirror)
cmdline = append(cmdline, "/usr/share/debootstrap/scripts/unstable")

err := Command{}.Run("Debootstrap", cmdline...)
err := debos.Command{}.Run("Debootstrap", cmdline...)

if err != nil {
return err
Expand All @@ -81,7 +83,7 @@ func (d *DebootstrapAction) Run(context *DebosContext) error {
}

/* HACK */
srclist, err := os.OpenFile(path.Join(context.rootdir, "etc/apt/sources.list"),
srclist, err := os.OpenFile(path.Join(context.Rootdir, "etc/apt/sources.list"),
os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
Expand All @@ -95,7 +97,7 @@ func (d *DebootstrapAction) Run(context *DebosContext) error {
}
srclist.Close()

c := NewChrootCommand(context.rootdir, context.Architecture)
c := debos.NewChrootCommand(context.Rootdir, context.Architecture)

return c.Run("apt clean", "/usr/bin/apt-get", "clean")
}
67 changes: 14 additions & 53 deletions actions/download_action.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,19 @@
package main
package actions

import (
"fmt"
"io"
"log"
"net/http"
"github.com/go-debos/debos"
"net/url"
"os"
"path"
)

type DownloadAction struct {
BaseAction `yaml:",inline"`
Url string // URL for downloading
Filename string // File name, overrides the name from URL.
Unpack bool // Unpack downloaded file to directory dedicated for download
Compression string // compression type
Name string // exporting path to file or directory(in case of unpack)
}

// Function for downloading single file object with http(s) protocol
func DownloadHttpUrl(url, filename string) error {
log.Printf("Download started: '%s' -> '%s'\n", url, filename)

// TODO: Proxy support?

// Check if file object already exists.
fi, err := os.Stat(filename)
if !os.IsNotExist(err) && !fi.Mode().IsRegular() {
return fmt.Errorf("Failed to download '%s': '%s' exists and it is not a regular file\n", url, filename)
}

resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Url '%s' returned status code %d (%s)\n", url, resp.StatusCode, http.StatusText(resp.StatusCode))
}

// Output file
output, err := os.Create(filename)
if err != nil {
return err
}
defer output.Close()

if _, err := io.Copy(output, resp.Body); err != nil {
return err
}

return nil
debos.BaseAction `yaml:",inline"`
Url string // URL for downloading
Filename string // File name, overrides the name from URL.
Unpack bool // Unpack downloaded file to directory dedicated for download
Compression string // compression type
Name string // exporting path to file or directory(in case of unpack)
}

// validateUrl checks if supported URL is passed from recipe
Expand All @@ -76,7 +37,7 @@ func (d *DownloadAction) validateUrl() (*url.URL, error) {
return url, nil
}

func (d *DownloadAction) Verify(context *DebosContext) error {
func (d *DownloadAction) Verify(context *debos.DebosContext) error {

if len(d.Name) == 0 {
return fmt.Errorf("Property 'name' is mandatory for download action\n")
Expand All @@ -85,7 +46,7 @@ func (d *DownloadAction) Verify(context *DebosContext) error {
return err
}

func (d *DownloadAction) Run(context *DebosContext) error {
func (d *DownloadAction) Run(context *debos.DebosContext) error {
var filename string
d.LogStart()

Expand All @@ -103,12 +64,12 @@ func (d *DownloadAction) Run(context *DebosContext) error {
if len(filename) == 0 {
return fmt.Errorf("Incorrect filename is provided for '%s'", d.Url)
}
filename = path.Join(context.scratchdir, filename)
filename = path.Join(context.Scratchdir, filename)
originPath := filename

switch url.Scheme {
case "http", "https":
err := DownloadHttpUrl(url.String(), filename)
err := debos.DownloadHttpUrl(url.String(), filename)
if err != nil {
return err
}
Expand All @@ -118,14 +79,14 @@ func (d *DownloadAction) Run(context *DebosContext) error {

if d.Unpack == true {
targetdir := filename + ".d"
err := UnpackTarArchive(filename, targetdir, d.Compression, "--no-same-owner", "--no-same-permissions")
err := debos.UnpackTarArchive(filename, targetdir, d.Compression, "--no-same-owner", "--no-same-permissions")
if err != nil {
return err
}
originPath = targetdir
}

context.origins[d.Name] = originPath
context.Origins[d.Name] = originPath

return nil
}
32 changes: 17 additions & 15 deletions actions/filesystem_deploy_action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package actions

import (
"errors"
Expand All @@ -9,41 +9,43 @@ import (
"os"
"path"
"strings"

"github.com/go-debos/debos"
)

type FilesystemDeployAction struct {
BaseAction `yaml:",inline"`
debos.BaseAction `yaml:",inline"`
SetupFSTab bool `yaml:setup-fstab`
SetupKernelCmdline bool `yaml:setup-kernel-cmdline`
}

func newFilesystemDeployAction() *FilesystemDeployAction {
func NewFilesystemDeployAction() *FilesystemDeployAction {
fd := &FilesystemDeployAction{SetupFSTab: true, SetupKernelCmdline: true}
fd.Description = "Deploying filesystem"

return fd
}

func (fd *FilesystemDeployAction) setupFSTab(context *DebosContext) error {
if context.imageFSTab.Len() == 0 {
func (fd *FilesystemDeployAction) setupFSTab(context *debos.DebosContext) error {
if context.ImageFSTab.Len() == 0 {
return errors.New("Fstab not generated, missing image-partition action?")
}

log.Print("Setting up fstab")

err := os.MkdirAll(path.Join(context.rootdir, "etc"), 0755)
err := os.MkdirAll(path.Join(context.Rootdir, "etc"), 0755)
if err != nil {
return fmt.Errorf("Couldn't create etc in image: %v", err)
}

fstab := path.Join(context.rootdir, "etc/fstab")
fstab := path.Join(context.Rootdir, "etc/fstab")
f, err := os.OpenFile(fstab, os.O_RDWR|os.O_CREATE, 0755)

if err != nil {
return fmt.Errorf("Couldn't open fstab: %v", err)
}

_, err = io.Copy(f, &context.imageFSTab)
_, err = io.Copy(f, &context.ImageFSTab)

if err != nil {
return fmt.Errorf("Couldn't write fstab: %v", err)
Expand All @@ -53,14 +55,14 @@ func (fd *FilesystemDeployAction) setupFSTab(context *DebosContext) error {
return nil
}

func (fd *FilesystemDeployAction) setupKernelCmdline(context *DebosContext) error {
func (fd *FilesystemDeployAction) setupKernelCmdline(context *debos.DebosContext) error {
log.Print("Setting up /etc/kernel/cmdline")

err := os.MkdirAll(path.Join(context.rootdir, "etc", "kernel"), 0755)
err := os.MkdirAll(path.Join(context.Rootdir, "etc", "kernel"), 0755)
if err != nil {
return fmt.Errorf("Couldn't create etc/kernel in image: %v", err)
}
path := path.Join(context.rootdir, "etc/kernel/cmdline")
path := path.Join(context.Rootdir, "etc/kernel/cmdline")
current, _ := ioutil.ReadFile(path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)

Expand All @@ -70,7 +72,7 @@ func (fd *FilesystemDeployAction) setupKernelCmdline(context *DebosContext) erro

cmdline := fmt.Sprintf("%s %s\n",
strings.TrimSpace(string(current)),
context.imageKernelRoot)
context.ImageKernelRoot)

_, err = f.WriteString(cmdline)
if err != nil {
Expand All @@ -81,16 +83,16 @@ func (fd *FilesystemDeployAction) setupKernelCmdline(context *DebosContext) erro
return nil
}

func (fd *FilesystemDeployAction) Run(context *DebosContext) error {
func (fd *FilesystemDeployAction) Run(context *debos.DebosContext) error {
fd.LogStart()
/* Copying files is actually silly hafd, one has to keep permissions, ACL's
* extended attribute, misc, other. Leave it to cp...
*/
err := Command{}.Run("Deploy to image", "cp", "-a", context.rootdir+"/.", context.imageMntDir)
err := debos.Command{}.Run("Deploy to image", "cp", "-a", context.Rootdir+"/.", context.ImageMntDir)
if err != nil {
return fmt.Errorf("rootfs deploy failed: %v", err)
}
context.rootdir = context.imageMntDir
context.Rootdir = context.ImageMntDir

if fd.SetupFSTab {
err = fd.setupFSTab(context)
Expand Down
Loading

0 comments on commit 5d73e46

Please sign in to comment.