Browse Source

Reformated: buidl command create command

Added: import-tar command to import a tarball as anew image
layerset
Till Wegmüller 6 years ago
parent
commit
ee02657f50
  1. 2
      .gitignore
  2. 16
      cmd/imageadm/build.go
  3. 38
      cmd/imageadm/create.go
  4. 178
      cmd/imageadm/import-tar.go

2
.gitignore vendored

@ -118,3 +118,5 @@ ENV/
# Rope project settings
.ropeproject
sample_data

16
cmd/imageadm/build.go

@ -16,7 +16,7 @@ var (
build_output string
)
const build_default_baseDir string = "/tmp"
const build_default_baseDir = "/tmp"
// buildCmd represents the create command
var buildCmd = &cobra.Command{
@ -33,16 +33,6 @@ func init() {
buildCmd.Flags().StringVarP(&build_type, "type", "t", "", "use to overwrite the type of image to build")
buildCmd.Flags().BoolVarP(&build_notresolve, "notresolve", "n", false, "Use files in profile and not resolve from Filesystem")
buildCmd.Flags().StringVarP(&build_output, "output", "o", build_default_baseDir, "The Directory where a build will be saved to. defaults to /tmp/imgname")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func runBuild(cmd *cobra.Command, args []string) {
@ -56,7 +46,7 @@ func runBuild(cmd *cobra.Command, args []string) {
fmt.Printf("%s is not a Profile: %s", profilePath, err)
os.Exit(1)
}
config,err := image.LoadConfiguration(cfgFile)
config, err := image.LoadConfiguration(cfgFile)
if err != nil {
fmt.Printf("Cannot Load Configuration: %s", err)
os.Exit(1)
@ -86,4 +76,4 @@ func runBuild(cmd *cobra.Command, args []string) {
fmt.Printf("Image Creation Failed: %s\n", err)
os.Exit(1)
}
}
}

38
cmd/imageadm/create.go

@ -10,9 +10,9 @@ import (
)
var (
imgtype string
imgdir string
sets []string
imgtype string
imgdir string
sets []string
notresolve bool
)
@ -21,52 +21,42 @@ var createCmd = &cobra.Command{
Use: "create",
Short: "Create an image with the following options",
Long: `Instructs imageadm to create an image as specified.
Use -t to specify a type of image to create Supported Types are Chroot|ZFS|UFS
Use -t to specify a type of image to create Supported Types are Chroot|ZFS|UFS|ACI
`,
Run: createCmdrun,
Run: createCmdrun,
Args: cobra.MinimumNArgs(1),
}
func init() {
RootCmd.AddCommand(createCmd)
createCmd.Flags().StringVarP(&imgtype,"type", "t", "chroot", "Set this to Chroot, ZFS or UFS to specify which image to be created.")
createCmd.Flags().StringVarP(&imgdir,"dir", "D", ".", "Which directory to create the image in defaults to ")
createCmd.Flags().StringVarP(&imgtype, "type", "t", "chroot", "Set this to Chroot, ZFS or UFS to specify which image to be created.")
createCmd.Flags().StringVarP(&imgdir, "dir", "D", ".", "Which directory to create the image in defaults to ")
createCmd.Flags().StringArrayVarP(&sets, "set", "s", []string{}, "Which Filesets to include in the final Image. use config to show the sets")
createCmd.Flags().BoolVarP(&notresolve, "resolve", "n", false, "If the Files should be resolved from the sets. Defaults to true")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func createCmdrun(cmd *cobra.Command, args []string) {
imgname := args[0]
imgdir, err := filepath.Abs(imgdir)
if err != nil{
if err != nil {
panic(err)
}
imgp := filepath.Join(imgdir, imgname)
err = os.Mkdir(imgp, 0755)
if !os.IsExist(err){
if err != nil{
if !os.IsExist(err) {
if err != nil {
panic(err)
}
}
profile, err := image.NewProfile(imgname)
if err != nil{
if err != nil {
panic(err)
}
config, err := image.LoadConfiguration(cfgFile)
for _, set := range sets{
for _, set := range sets {
section, ok := config.Sections[set]
if ok{
if ok {
profile.FileSets = append(profile.FileSets, set)
profile.Users = append(profile.Users, config.GetAllFromSection(&section, "users")...)
profile.Groups = append(profile.Groups, config.GetAllFromSection(&section, "groups")...)
@ -77,7 +67,7 @@ func createCmdrun(cmd *cobra.Command, args []string) {
profile.ResolveFiles(&config)
}
err = profile.Save(imgp)
if err != nil{
if err != nil {
panic(err)
}
}

178
cmd/imageadm/import-tar.go

@ -0,0 +1,178 @@
package cmd
import (
"fmt"
"strings"
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"os"
"time"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/spf13/cobra"
)
var (
importTarFilePath string
importTarManifest string
importTarImage string
importTarName string
)
// createCmd represents the create command
var importTarCmd = &cobra.Command{
Use: "import-tar",
Short: "Import a OS Tarball",
Long: `Imports an OS Tarball as an Image. The only rule for the tarball is,
that it contains a folders rootfs folder and the root of the OS under that.
This command will then create an empty imageManifest or add the one specified by the -m command.
It can also grab a manifest from an existing image with -i.
This command is mostly a workaround for the golang Tar writer failing.
`,
Run: importTarCmdrun,
Args: cobra.MinimumNArgs(1),
}
func init() {
RootCmd.AddCommand(importTarCmd)
importTarCmd.Flags().StringVarP(&importTarFilePath, "file", "f", "", "the file to import")
importTarCmd.Flags().StringVarP(&importTarManifest, "manifest", "m", "", "the manifest to use for the new image")
importTarCmd.Flags().StringVarP(&importTarImage, "image", "i", "", "The Image to use as the source")
importTarCmd.Flags().StringVarP(&importTarName, "name", "n", "", "name for the new image")
}
func importTarCmdrun(cmd *cobra.Command, args []string) {
var manifest schema.ImageManifest
if importTarFilePath != "" {
f, err := os.Open(importTarFilePath)
defer f.Close()
if err != nil {
fmt.Printf("can not open manifest: %s\n", err)
os.Exit(1)
}
if err = json.NewDecoder(f).Decode(&manifest); err != nil {
fmt.Printf("not proper manifest: %s\n", err)
os.Exit(1)
}
} else if importTarName != "" {
manifest = *schema.BlankImageManifest()
manifest.Name = types.ACIdentifier(importTarName)
} else if importTarImage != "" {
f, err := os.Open(importTarImage)
if err != nil {
fmt.Printf("can not open image: %s\n", err)
os.Exit(1)
}
gr, err := gzip.NewReader(f)
if err != nil {
gr = nil
}
defer gr.Close()
var tr *tar.Reader
if gr != nil {
tr = tar.NewReader(gr)
} else {
tr = tar.NewReader(f)
}
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("could not read entry %s: %s\n", hdr.Name, err)
break
}
if hdr.Name == aci.ManifestFile {
err = json.NewDecoder(tr).Decode(&manifest)
if err != nil {
fmt.Printf("could not decode manifest from image: %s\n", err)
break
}
}
}
} else {
fmt.Printf("Either an original Image a manifest or a Name is required\n")
os.Exit(1)
}
if manifest.Name == "" {
os.Exit(1)
}
tarName := args[0]
aciName := tarName[:strings.Index(tarName, ".")] + ".aci"
var tw *tar.Writer
var tr *tar.Reader
aciFH, err := os.OpenFile(aciName, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
fmt.Printf("can not create new image file: %s\n", err)
os.Exit(1)
}
tarFH, err := os.OpenFile(tarName, os.O_RDONLY, os.ModePerm)
if err != nil {
fmt.Printf("can not open tar to import: %s\n", err)
os.Exit(1)
}
if strings.Contains(tarName, ".gz") {
gr, err := gzip.NewReader(tarFH)
if err != nil {
fmt.Printf("can not open tgz for reading: %s\n", err)
os.Exit(1)
}
defer gr.Close()
tr = tar.NewReader(gr)
} else {
tr = tar.NewReader(tarFH)
}
tw = tar.NewWriter(gzip.NewWriter(aciFH))
defer tw.Close()
out, err := manifest.MarshalJSON()
buff := bytes.NewBuffer(out)
if err != nil {
fmt.Printf("could not create manifest JSON: %s\n", err)
os.Exit(1)
}
now := time.Now()
hdr := tar.Header{
Name: aci.ManifestFile,
Mode: 0644,
Uid: 0,
Gid: 0,
Size: int64(buff.Len()),
ModTime: now,
Typeflag: tar.TypeReg,
Uname: "root",
Gname: "root",
ChangeTime: now,
}
err = tw.WriteHeader(&hdr)
if err != nil {
fmt.Printf("can not write header: %s\n", err)
os.Exit(1)
}
_, err = tw.Write(buff.Bytes())
if err != nil {
fmt.Printf("could not write content od manifest: %s\n", err)
os.Exit(1)
}
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("could not read entry %s: %s\n", hdr.Name, err)
break
}
tw.WriteHeader(hdr)
if _, err := io.Copy(tw, tr); err != nil {
fmt.Printf("could not copy %s\n", err)
}
}
}
Loading…
Cancel
Save