-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09a2a7f
commit b4f74a2
Showing
6 changed files
with
243 additions
and
1 deletion.
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
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,52 @@ | ||
package docker | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/saucelabs/saucectl/internal/credentials" | ||
"github.com/saucelabs/saucectl/internal/http" | ||
"github.com/saucelabs/saucectl/internal/region" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
registryClient http.DockerRegistry | ||
dockerPushTimeout = 1 * time.Minute | ||
) | ||
|
||
func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command { | ||
var regio string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "docker", | ||
Short: "Interact with docker registry", | ||
SilenceUsage: true, | ||
TraverseChildren: true, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if preRun != nil { | ||
preRun(cmd, args) | ||
} | ||
|
||
reg := region.FromString(regio) | ||
if reg == region.None { | ||
return errors.New("empty region. Options: us-west-1, eu-central-1") | ||
} | ||
|
||
creds := credentials.Get() | ||
url := reg.APIBaseURL() | ||
registryClient = http.NewDockerRegistry(url, creds.Username, creds.AccessKey, dockerPushTimeout) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
flags := cmd.PersistentFlags() | ||
flags.StringVarP(®io, "region", "r", "us-west-1", "The Sauce Labs region. Options: us-west-1, eu-central-1.") | ||
|
||
cmd.AddCommand( | ||
PushCommand(), | ||
) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/registry" | ||
"github.com/docker/docker/client" | ||
cmds "github.com/saucelabs/saucectl/internal/cmd" | ||
"github.com/saucelabs/saucectl/internal/segment" | ||
"github.com/saucelabs/saucectl/internal/usage" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func PushCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "push", | ||
Short: "push docker image to Sauce Labs container registry", | ||
SilenceUsage: true, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 || args[0] == "" { | ||
return errors.New("no docker image specified") | ||
} | ||
|
||
return nil | ||
}, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
tracker := segment.DefaultTracker | ||
|
||
go func() { | ||
tracker.Collect( | ||
cases.Title(language.English).String(cmds.FullName(cmd)), | ||
usage.Properties{}.SetFlags(cmd.Flags()), | ||
) | ||
_ = tracker.Close() | ||
}() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
auth, err := registryClient.Login(context.Background()) | ||
fmt.Println("auth: ", auth) | ||
if err != nil { | ||
return fmt.Errorf("failed to fetch auth token: %v", err) | ||
} | ||
return pushDockerImage(args[0], auth.Username, auth.Password) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func pushDockerImage(imageName, username, password string) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), dockerPushTimeout) | ||
defer cancel() | ||
|
||
cli, err := client.NewClientWithOpts(client.FromEnv) | ||
if err != nil { | ||
return fmt.Errorf("failed to create Docker client: %v", err) | ||
} | ||
|
||
authConfig := registry.AuthConfig{ | ||
Username: username, | ||
Password: password, | ||
} | ||
|
||
authBytes, err := json.Marshal(authConfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal docker auth: %v", err) | ||
} | ||
authBase64 := base64.URLEncoding.EncodeToString(authBytes) | ||
|
||
// Push the image to the registry | ||
pushOptions := types.ImagePushOptions{RegistryAuth: authBase64} | ||
out, err := cli.ImagePush(ctx, imageName, pushOptions) | ||
if err != nil { | ||
return fmt.Errorf("failed to push image: %v", err) | ||
} | ||
defer out.Close() | ||
|
||
// Print the push output | ||
_, err = io.Copy(os.Stdout, out) | ||
if err != nil { | ||
return fmt.Errorf("failed to copy push output: %v", err) | ||
} | ||
|
||
return 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,63 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
type DockerRegistry struct { | ||
HTTPClient *retryablehttp.Client | ||
URL string | ||
Username string | ||
AccessKey string | ||
} | ||
|
||
type AuthToken struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func NewDockerRegistry(url, username, accessKey string, timeout time.Duration) DockerRegistry { | ||
return DockerRegistry{ | ||
HTTPClient: NewRetryableClient(timeout), | ||
URL: url, | ||
Username: username, | ||
AccessKey: accessKey, | ||
} | ||
} | ||
|
||
func (c *DockerRegistry) Login(ctx context.Context) (AuthToken, error) { | ||
url := fmt.Sprintf("%s/v1alpha1/hosted/container-registry/authorization-token", c.URL) | ||
|
||
var authToken AuthToken | ||
req, err := NewRequestWithContext(ctx, http.MethodPost, url, nil) | ||
if err != nil { | ||
return authToken, err | ||
} | ||
req.SetBasicAuth(c.Username, c.AccessKey) | ||
|
||
r, err := retryablehttp.FromRequest(req) | ||
if err != nil { | ||
return authToken, err | ||
} | ||
|
||
resp, err := c.HTTPClient.Do(r) | ||
if err != nil { | ||
return authToken, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return authToken, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&authToken); err != nil { | ||
return authToken, err | ||
} | ||
return authToken, nil | ||
} |