This repository was archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality to copy to/from the droplet with a custom API, add …
…documentation
- Loading branch information
Showing
21 changed files
with
722 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## Type of Change | ||
<!-- What part of the source are you modifying? --> | ||
|
||
## What OS are you on? | ||
<!-- Please place your operating systen here --> | ||
|
||
## What issue does this relate to? | ||
<!-- Use a GitHub keyword ('resolves #xx', 'fixes #xx', 'closes #xx') to automatically close the relevant issue. --> | ||
|
||
### What should this PR do? | ||
<!-- Write a quick bullet point summary of the changes this PR should be making. --> | ||
|
||
### What are the acceptance criteria? | ||
<!-- Write a list of what should reviewers be checking before they approve this PR. --> | ||
<!-- If there are UI changes, include before and after screenshots in a table for comparison. --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ jobs: | |
uses: actions/upload-artifact@v1 | ||
with: | ||
name: builds | ||
path: dist | ||
path: cli-dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,7 @@ | |
|
||
# IDEA data. | ||
.idea/ | ||
|
||
# Dist's | ||
cli-dist | ||
droplet-tools-dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# copyback | ||
A simple tool for inside the Linux/FreeBSD droplet to copy data back to the host. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"github.com/jakemakesstuff/structuredhttp" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Used to gracefully error the application. | ||
func gracefulError(message string) { | ||
println(message) | ||
os.Exit(1) | ||
} | ||
|
||
// Used to handle the transfer to the host. | ||
func transferToHost(hostRelPath string, r io.Reader, len uint, perm os.FileMode) { | ||
// Initialise the transfer. | ||
type transferInit struct { | ||
LocalPath string | ||
TotalBytes uint | ||
Perm os.FileMode | ||
} | ||
buf := &bytes.Buffer{} | ||
encoder := gob.NewEncoder(buf) | ||
err := encoder.Encode(&transferInit{ | ||
LocalPath: hostRelPath, | ||
TotalBytes: len, | ||
Perm: perm, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
resp, err := structuredhttp.POST("http://127.0.0.1:8190/v1/StartTransferSession").Reader(buf).Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = resp.RaiseForStatus() | ||
if err != nil { | ||
gracefulError(func() (text string) { text, _ = resp.Text(); return }()) | ||
} | ||
transferId, err := resp.Text() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Chunk the transfer into 1MB blocks. | ||
block := make([]byte, 1000000) | ||
for { | ||
// Read 1MB maximum. | ||
n, err := r.Read(block) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
panic(err) | ||
} | ||
resizedBlock := block[:n] | ||
|
||
// Upload the chunk. | ||
resp, err := structuredhttp.POST("http://127.0.0.1:8190/v1/HandleFragment").Bytes(resizedBlock).Header("Transfer-ID", transferId).Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = resp.RaiseForStatus() | ||
if err != nil { | ||
gracefulError(func() (text string) { text, _ = resp.Text(); return }()) | ||
} | ||
} | ||
} | ||
|
||
// Shows the command usage. | ||
func usage() { | ||
println("copyback - copy a file/folder back from the droplet") | ||
println("usage: copyback <droplet file/folder path> [host save location]") | ||
os.Exit(0) | ||
} | ||
|
||
// Used to process a file. | ||
func processFile(s os.FileInfo, dropletAbsPath, hostRelPath string) { | ||
// Get the size for later. | ||
size := s.Size() | ||
|
||
// Create a reader for the file. | ||
r, err := os.Open(dropletAbsPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer r.Close() | ||
|
||
// Upload the file. | ||
transferToHost(hostRelPath, r, uint(size), s.Mode()) | ||
} | ||
|
||
// The main function. | ||
func main() { | ||
// Check the arg count. | ||
if len(os.Args) == 1 { | ||
usage() | ||
} | ||
|
||
// Get the droplet path. | ||
dropletPath := os.Args[1] | ||
if dropletPath == "-h" { | ||
usage() | ||
} | ||
|
||
// Stat the droplet file. | ||
dropletAbsPath, err := filepath.Abs(dropletPath) | ||
if err != nil { | ||
gracefulError(err.Error()) | ||
} | ||
|
||
// Check if the file exists. | ||
s, err := os.Stat(dropletAbsPath) | ||
if err != nil { | ||
gracefulError(err.Error()) | ||
return | ||
} | ||
|
||
// Get the host relative path. | ||
var hostRelPath string | ||
if len(os.Args) > 2 { | ||
hostRelPath = os.Args[2] | ||
} else { | ||
if filepath.Base(dropletAbsPath) == "." { | ||
hostRelPath = "." | ||
} else { | ||
hostRelPath = "./"+filepath.Base(dropletAbsPath) | ||
} | ||
} | ||
|
||
// Check if this is a folder. | ||
if s.IsDir() { | ||
err := filepath.Walk(dropletAbsPath, func(path string, s os.FileInfo, err error) error { | ||
if s.IsDir() { | ||
return nil | ||
} | ||
diff, err := filepath.Rel(dropletAbsPath, path) | ||
if err != nil { | ||
return err | ||
} | ||
processFile(s, path, filepath.Join(hostRelPath, diff)) | ||
return nil | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} else { | ||
processFile(s, dropletAbsPath, hostRelPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/JakeMakesStuff/do-disposable/copyback | ||
|
||
go 1.14 | ||
|
||
require github.com/jakemakesstuff/structuredhttp v0.0.0-20200614104234-f8e4b2aebe68 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/jakemakesstuff/structuredhttp v0.0.0-20200614104234-f8e4b2aebe68 h1:vkIG58xJJFlq0UlieHO1YEGISr922HaN61dPPEyAVts= | ||
github.com/jakemakesstuff/structuredhttp v0.0.0-20200614104234-f8e4b2aebe68/go.mod h1:yWWc7Ao4LkCeYfEgk4nGTd3oLszff89CFyKS7gA9HMc= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# copyfrom | ||
A simple tool for inside the Linux/FreeBSD droplet to copy data from the host. |
Oops, something went wrong.