Skip to content

Commit

Permalink
Merge pull request #16 from Doer-org/feature/tar.gz
Browse files Browse the repository at this point in the history
Feature/tar.gz
  • Loading branch information
clcl777 authored Feb 10, 2024
2 parents e0c444d + e7b1676 commit efeaa78
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
go.work

dist/
tmp-tar/
tmp-tar/
.DS_Store
6 changes: 3 additions & 3 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ var pullCmd = &cobra.Command{
return err
}

err = api.ReceiveTarFromServer(id)
err = api.ReceiveTarGzFromServer(id)
if err != nil {
return err
}
err = docker.DecompressTarToImage()
err = docker.DecompressTarGzToImage()
if err != nil {
return err
}
respID, err := docker.CreateContainer()
if err != nil {
return err
}
err = docker.RunConrainer(respID)
err = docker.RunContainer(respID)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var createCmd = &cobra.Command{
if err != nil {
return err
}
docker.CompressImageToTar()
docker.CompressImageToTarGz()
err = api.SendTarToServer()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/api/receive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// TODO: 取り敢えず書いてみただけなので、動作確認しつつ適宜変更してください。
func ReceiveTarFromServer(id string) error {
func ReceiveTarGzFromServer(id string) error {
response, err := http.Get(BackendURL)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/api/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package api

var (
BackendURL = "http://localhost:8080"
filePath = "./tmp-tar/ketos-tmp-image.tar"
filePath = "./tmp-tar/ketos-tmp-image.tar.gz"
)
18 changes: 12 additions & 6 deletions internal/docker/compress_docker_image.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package docker

import (
"compress/gzip"
"context"
"io"
"os"

"github.com/docker/docker/client"
)

func CompressImageToTar() error {
// imageをtarに圧縮
func CompressImageToTarGz() error {
// imageをtar.gzに圧縮
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
Expand All @@ -17,20 +19,24 @@ func CompressImageToTar() error {
if _, err := os.Stat(TarTmpDir); os.IsNotExist(err) {
os.Mkdir(TarTmpDir, 0777)
}
tarFileName := TarTmpDir + "/" + ImageName + ".tar"
tarGzFileName := TarTmpDir + "/" + ImageName + ".tar.gz"
imageSaveResponse, err := cli.ImageSave(ctx, []string{ImageName})
if err != nil {
return err
}
defer imageSaveResponse.Close()
file, err := os.Create(tarFileName)
file, err := os.Create(tarGzFileName)
if err != nil {
return err
}
defer file.Close()
_, err = file.ReadFrom(imageSaveResponse)
if err != nil {

gw := gzip.NewWriter(file)
defer gw.Close()

if _, err = io.Copy(gw, imageSaveResponse); err != nil {
return err
}

return nil
}
13 changes: 13 additions & 0 deletions internal/docker/receive/create_docker_container_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
package docker

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateContainer(t *testing.T){
resp_id, err := CreateContainer()
assert.NotEmpty(t, resp_id)
t.Logf("resp_id: %s", resp_id)
assert.NoError(t, err)
}
35 changes: 0 additions & 35 deletions internal/docker/receive/decompress_tar.go

This file was deleted.

46 changes: 46 additions & 0 deletions internal/docker/receive/decompress_tar_gz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package docker

import (
"compress/gzip"
"context"
"os"

"github.com/Doer-org/ketos/internal/docker"
"github.com/docker/docker/client"
)

func DecompressTarGzToImage() error {
// tar.gzをimageに展開
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
tarGzFileName := "../../../tmp-tar" + "/" + docker.ImageName + ".tar.gz"

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

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

loadResponse, err := cli.ImageLoad(ctx, gzReader, true)
if err != nil {
return err
}
defer loadResponse.Body.Close()

// tar.gzファイルを削除するコードはコメントアウトされていますが、必要に応じて有効化してください
// err = os.Remove(tarGzFileName)
// if err != nil {
// panic(err)
// }

return nil
}
12 changes: 12 additions & 0 deletions internal/docker/receive/decompress_tar_gz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package docker

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecompressTarGzToImage(t *testing.T) {
err := DecompressTarGzToImage()
assert.NoError(t, err)
}
1 change: 0 additions & 1 deletion internal/docker/receive/decompress_tar_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/docker/receive/run_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/docker/docker/client"
)

func RunConrainer(respID string) error {
func RunContainer(respID string) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/docker/receive/run_docker_container_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package docker

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRunContainer(t *testing.T){
respId, err := CreateContainer()
assert.NotEmpty(t, respId)
t.Logf("respId: %s", respId)
assert.NoError(t, err)
err = RunContainer(respId)
assert.NoError(t, err)
}

0 comments on commit efeaa78

Please sign in to comment.