Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish #2

Merged
merged 4 commits into from
Jun 1, 2022
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bin/
.DS_Store

bin/
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var deleteCmd = &cobra.Command{
Use: "delete NAME",
Short: "Delete an Alpine VM by NAME.",
Short: "Delete an Alpine VM.",
Run: delete,
}

Expand Down
48 changes: 48 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"log"
"os"
"path/filepath"
"strings"

"github.com/beringresearch/macpine/utils"
"github.com/spf13/cobra"
)

// importCmd iports an Alpine VM from file
var importCmd = &cobra.Command{
Use: "import NAME",
Short: "Imports Alpine VM instances.",
Run: importMachine,
}

func importMachine(cmd *cobra.Command, args []string) {

if len(args) == 0 {
log.Fatal("missing name - please provide VM name")
return
}

userHomeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

_, err = utils.CopyFile(args[0], filepath.Join(userHomeDir, ".macpine", args[0]))
if err != nil {
log.Fatal(err)
}

err = utils.Uncompress(filepath.Join(userHomeDir, ".macpine", args[0]),
filepath.Join(userHomeDir, ".macpine", strings.Split(args[0], ".tar.gz")[0]))

if err != nil {
log.Println(err)
}

err = os.Remove(filepath.Join(userHomeDir, ".macpine", args[0]))
if err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"gopkg.in/yaml.v2"
)

// listCmd starts an Alpine instance
// listCmd lists Alpine instances
var listCmd = &cobra.Command{
Use: "list",
Short: "List all available Alpine VM instances.",
Expand Down
74 changes: 74 additions & 0 deletions cmd/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"time"

"github.com/beringresearch/macpine/host"
qemu "github.com/beringresearch/macpine/qemu"
"github.com/beringresearch/macpine/utils"
"github.com/spf13/cobra"
)

// publishCmd stops an Alpine instance
var publishCmd = &cobra.Command{
Use: "publish NAME",
Short: "Publish an Alpine VM.",
Run: publish,
}

func publish(cmd *cobra.Command, args []string) {

userHomeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

if len(args) == 0 {
log.Fatal("missing name - please provide VM name")
return
}

machineConfig := qemu.MachineConfig{
Alias: args[0],
}
machineConfig.Location = filepath.Join(userHomeDir, ".macpine", machineConfig.Alias)

err = host.Stop(machineConfig)
if err != nil {
log.Fatal(err)
}

time.Sleep(time.Second)

fileInfo, err := ioutil.ReadDir(machineConfig.Location)
if err != nil {
log.Fatal(err)
}

files := []string{}
for _, f := range fileInfo {
files = append(files, filepath.Join(machineConfig.Location, f.Name()))
}

out, err := os.Create(machineConfig.Alias + ".tar.gz")
if err != nil {
log.Fatalln("Error writing archive:", err)
}
defer out.Close()

// Create the archive and write the output to the "out" Writer
err = utils.Compress(files, out)
if err != nil {
log.Fatalln("error creating archive:", err)
}

err = host.Start(machineConfig)
if err != nil {
log.Fatal(err)
}

}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ func init() {
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(publishCmd)
rootCmd.AddCommand(importCmd)
}
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// startCmd starts an Alpine instance
var startCmd = &cobra.Command{
Use: "start NAME",
Short: "Start an Alpine VM by NAME.",
Short: "Start an Alpine VM.",
Run: start,
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// stopCmd stops an Alpine instance
var stopCmd = &cobra.Command{
Use: "stop NAME",
Short: "Stop an Alpine VM by NAME.",
Short: "Stop an Alpine VM.",
Run: stop,
}

Expand Down
95 changes: 94 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,114 @@
package utils

import (
"archive/tar"
"compress/gzip"
"embed"
_ "embed"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)

//go:embed *.txt
var f embed.FS

//Uncompress uncompresses gzip
func Uncompress(source string, destination string) error {
file, err := os.Open(source)
if err != nil {
return err
}
defer file.Close()

gzRead, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzRead.Close()

tarRead := tar.NewReader(gzRead)
for {
cur, err := tarRead.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}

os.MkdirAll(destination, 0777)

switch cur.Typeflag {

case tar.TypeReg:
create, err := os.Create(filepath.Join(destination, cur.Name))
if err != nil {
return err
}
defer create.Close()
create.ReadFrom(tarRead)
case tar.TypeLink:
os.Link(cur.Linkname, cur.Name)
}
}
return nil
}

// Compress creates a tar.gz of a Direcotry
func Compress(files []string, buf io.Writer) error {
gw := gzip.NewWriter(buf)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()

for _, file := range files {
err := addToArchive(tw, file)
if err != nil {
return err
}
}

return nil
}

func addToArchive(tw *tar.Writer, filename string) error {

file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

info, err := file.Stat()
if err != nil {
return err
}

header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}

header.Name = filepath.Base(filename)

err = tw.WriteHeader(header)
if err != nil {
return err
}

_, err = io.Copy(tw, file)
if err != nil {
return err
}

return nil
}

//CopyFile copies file from src to dst
func CopyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
Expand Down