-
Notifications
You must be signed in to change notification settings - Fork 510
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
history: add history import command #3039
Open
tonistiigi
wants to merge
4
commits into
docker:master
Choose a base branch
from
tonistiigi:history-import
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,135 @@ | ||
package history | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
remoteutil "github.com/docker/buildx/driver/remote/util" | ||
"github.com/docker/buildx/util/cobrautil/completion" | ||
"github.com/docker/buildx/util/desktop" | ||
"github.com/docker/cli/cli/command" | ||
"github.com/pkg/browser" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type importOptions struct { | ||
file []string | ||
} | ||
|
||
func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) error { | ||
sock, err := desktop.BuildServerAddr() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tr := http.DefaultTransport.(*http.Transport).Clone() | ||
tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
network, addr, ok := strings.Cut(sock, "://") | ||
if !ok { | ||
return nil, errors.Errorf("invalid endpoint address: %s", sock) | ||
} | ||
return remoteutil.DialContext(ctx, network, addr) | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: tr, | ||
} | ||
|
||
var urls []string | ||
|
||
if len(opts.file) == 0 { | ||
u, err := importFrom(ctx, client, os.Stdin) | ||
if err != nil { | ||
return err | ||
} | ||
urls = append(urls, u...) | ||
} else { | ||
for _, fn := range opts.file { | ||
var f *os.File | ||
var rdr io.Reader = os.Stdin | ||
if fn != "-" { | ||
f, err = os.Open(fn) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to open file %s", fn) | ||
} | ||
rdr = f | ||
} | ||
u, err := importFrom(ctx, client, rdr) | ||
if err != nil { | ||
return err | ||
} | ||
urls = append(urls, u...) | ||
if f != nil { | ||
f.Close() | ||
} | ||
} | ||
} | ||
|
||
if len(urls) == 0 { | ||
return errors.New("no build records found in the bundle") | ||
} | ||
|
||
for i, url := range urls { | ||
fmt.Fprintln(dockerCli.Err(), url) | ||
if i == 0 { | ||
err = browser.OpenURL(url) | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func importFrom(ctx context.Context, c *http.Client, rdr io.Reader) ([]string, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create request") | ||
} | ||
|
||
resp, err := c.Do(req) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to send request, check if Docker Desktop is running") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
body, _ := io.ReadAll(resp.Body) | ||
return nil, errors.Errorf("failed to import build: %s", string(body)) | ||
} | ||
|
||
var refs []string | ||
dec := json.NewDecoder(resp.Body) | ||
if err := dec.Decode(&refs); err != nil { | ||
return nil, errors.Wrap(err, "failed to decode response") | ||
} | ||
|
||
var urls []string | ||
for _, ref := range refs { | ||
urls = append(urls, desktop.BuildURL(fmt.Sprintf(".imported/_/%s", ref))) | ||
} | ||
return urls, err | ||
} | ||
|
||
func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command { | ||
var options importOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "import [OPTIONS] < bundle.dockerbuild", | ||
Short: "Import a build into Docker Desktop", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runImport(cmd.Context(), dockerCli, options) | ||
}, | ||
ValidArgsFunction: completion.Disable, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringArrayVarP(&options.file, "file", "f", nil, "Import from a file path") | ||
|
||
return cmd | ||
} |
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,16 @@ | ||
# docker buildx history import | ||
|
||
<!---MARKER_GEN_START--> | ||
Import a build into Docker Desktop | ||
|
||
### Options | ||
|
||
| Name | Type | Default | Description | | ||
|:----------------|:--------------|:--------|:-----------------------------------------| | ||
| `--builder` | `string` | | Override the configured builder instance | | ||
| `-D`, `--debug` | `bool` | | Enable debug logging | | ||
| `-f`, `--file` | `stringArray` | | Import from a file path | | ||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
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,21 @@ | ||
package desktop | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
socketName = "docker-desktop-build.sock" | ||
socketPath = "Library/Containers/com.docker.docker/Data" | ||
) | ||
|
||
func BuildServerAddr() (string, error) { | ||
dir, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to get user home directory") | ||
} | ||
return "unix://" + filepath.Join(dir, socketPath, socketName), nil | ||
} |
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,25 @@ | ||
package desktop | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
socketName = "docker-desktop-build.sock" | ||
socketPath = ".docker/desktop" | ||
wslSocketPath = "/mnt/wsl/docker-desktop/shared-sockets/host-services" | ||
) | ||
|
||
func BuildServerAddr() (string, error) { | ||
if os.Getenv("WSL_DISTRO_NAME") != "" { | ||
return "unix://" + filepath.Join(wslSocketPath, socketName), nil | ||
} | ||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed this change for testing WSL support but doesn't work. I'm taking a look if this can be solved but in the meantime we could disable support for |
||
dir, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to get user home directory") | ||
} | ||
return "unix://" + filepath.Join(dir, socketPath, socketName), nil | ||
} |
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,13 @@ | ||
//go:build !windows && !darwin && !linux | ||
|
||
package desktop | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func BuildServerAddr() (string, error) { | ||
return "", errors.Errorf("Docker Desktop unsupported on %s", runtime.GOOS) | ||
} |
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 @@ | ||
package desktop | ||
|
||
func BuildServerAddr() (string, error) { | ||
return "npipe:////./pipe/dockerDesktopBuildServer", nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use args as file input instead of a flag and require at least one arg. Multiple files should be supported like Docker Desktop does atm imo:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not usually the convention I think when the file is optional and defaults to stdin. If file is arg then for stdin, the user should write
import -
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing multiple files together, I'm ok with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah indeed I missed it was reading from stdin. Sounds good to support multiple
--file
then