Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 289e82c

Browse files
committed
support multiple regions for hyper.sh
1 parent ec19526 commit 289e82c

File tree

5 files changed

+72
-25
lines changed

5 files changed

+72
-25
lines changed

api/client/cli.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"net/url"
89
"os"
910
"runtime"
1011

@@ -46,8 +47,9 @@ type DockerCli struct {
4647
// client is the http client that performs all API operations
4748
client client.APIClient
4849
// state holds the terminal state
49-
state *term.State
50-
host string
50+
state *term.State
51+
region string
52+
host string
5153
}
5254

5355
// Initialize calls the init function that will setup the configuration for the client
@@ -134,7 +136,7 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
134136
}
135137
cli.configFile = configFile
136138

137-
host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
139+
host, err := cli.getServerHost(clientFlags.Common.Region, clientFlags.Common.TLSOptions)
138140
if err != nil {
139141
return err
140142
}
@@ -160,8 +162,14 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
160162
cloudConfig.AccessKey = cc.AccessKey
161163
cloudConfig.SecretKey = cc.SecretKey
162164
} else {
163-
cloudConfig.AccessKey = os.Getenv("HYPER_ACCESS")
164-
cloudConfig.SecretKey = os.Getenv("HYPER_SECRET")
165+
cc, ok := configFile.CloudConfig[cliconfig.DefaultHyperFormat]
166+
if ok {
167+
cloudConfig.AccessKey = cc.AccessKey
168+
cloudConfig.SecretKey = cc.SecretKey
169+
} else {
170+
cloudConfig.AccessKey = os.Getenv("HYPER_ACCESS")
171+
cloudConfig.SecretKey = os.Getenv("HYPER_SECRET")
172+
}
165173
}
166174
if cloudConfig.AccessKey == "" || cloudConfig.SecretKey == "" {
167175
fmt.Fprintf(cli.err, "WARNING: null cloud config\n")
@@ -173,6 +181,7 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
173181
}
174182
cli.client = client
175183
cli.host = host
184+
cli.region = clientFlags.Common.Region
176185

177186
if cli.in != nil {
178187
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
@@ -187,14 +196,14 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
187196
return cli
188197
}
189198

190-
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
191-
switch len(hosts) {
192-
case 0:
193-
host = cliconfig.DefaultHyperServer
194-
case 1:
195-
host = hosts[0]
196-
default:
197-
return "", errors.New("Please specify only one -H")
199+
func (cli *DockerCli) getServerHost(region string, tlsOptions *tlsconfig.Options) (host string, err error) {
200+
host = region
201+
if host == "" {
202+
host = os.Getenv("HYPER_DEFAULT_REGION")
203+
region = cli.getDefaultRegion()
204+
}
205+
if _, err := url.ParseRequestURI(host); err != nil {
206+
host = "tcp://" + region + "." + cliconfig.DefaultHyperEndpoint
198207
}
199208

200209
host, err = opts.ParseHost(tlsOptions != nil, host)

api/client/config.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
//
1616
// Usage: hyper config
1717
func (cli *DockerCli) CmdConfig(args ...string) error {
18-
cmd := Cli.Subcmd("config", []string{"[SERVER]"}, Cli.DockerCommands["config"].Description+".\nIf no server is specified, the default is defined as "+cliconfig.DefaultHyperServer, true)
18+
cmd := Cli.Subcmd("config", []string{"[REGION]"}, Cli.DockerCommands["config"].Description+".\nIf no region is specified, the default is defined as "+cliconfig.DefaultHyperRegion, true)
1919
cmd.Require(flag.Max, 1)
2020

2121
flAccesskey := cmd.String([]string{"-accesskey"}, "", "Access Key")
2222
flSecretkey := cmd.String([]string{"-secretkey"}, "", "Secret Key")
23+
flDefaultRegion := cmd.String([]string{"-default-region"}, "", "Default Region Endpoint")
2324

2425
cmd.ParseFlags(args, true)
2526

@@ -32,10 +33,10 @@ func (cli *DockerCli) CmdConfig(args ...string) error {
3233
if len(cmd.Args()) > 0 {
3334
serverAddress = cmd.Arg(0)
3435
} else {
35-
serverAddress = cliconfig.DefaultHyperServer
36+
serverAddress = cliconfig.DefaultHyperFormat
3637
}
3738

38-
_, err := cli.configureCloud(serverAddress, *flAccesskey, *flSecretkey)
39+
_, err := cli.configureCloud(serverAddress, *flDefaultRegion, *flAccesskey, *flSecretkey)
3940
if err != nil {
4041
return err
4142
}
@@ -48,26 +49,57 @@ func (cli *DockerCli) CmdConfig(args ...string) error {
4849
return nil
4950
}
5051

51-
func (cli *DockerCli) configureCloud(serverAddress, flAccesskey, flSecretkey string) (cliconfig.CloudConfig, error) {
52-
cloudConfig, ok := cli.configFile.CloudConfig[serverAddress]
53-
if !ok {
54-
cloudConfig = cliconfig.CloudConfig{}
52+
func (cli *DockerCli) configureCloud(serverAddress, flRegion, flAccesskey, flSecretkey string) (cliconfig.CloudConfig, error) {
53+
cloudConfig := cliconfig.CloudConfig{}
54+
if serverAddress != "" {
55+
if cc, ok := cli.configFile.CloudConfig[serverAddress]; ok {
56+
cloudConfig = cc
57+
} else {
58+
// for legacy format
59+
defaultHost := "tcp://" + cliconfig.DefaultHyperRegion + "." + cliconfig.DefaultHyperEndpoint
60+
cloudConfig, ok = cli.configFile.CloudConfig[defaultHost]
61+
if ok {
62+
delete(cli.configFile.CloudConfig, defaultHost)
63+
}
64+
}
5565
}
5666

67+
defaultRegion := cli.getDefaultRegion()
68+
if cloudConfig.Region != "" {
69+
defaultRegion = cloudConfig.Region
70+
}
5771
if flAccesskey = strings.TrimSpace(flAccesskey); flAccesskey == "" {
5872
cli.promptWithDefault("Enter Access Key", cloudConfig.AccessKey)
5973
flAccesskey = readInput(cli.in, cli.out)
6074
flAccesskey = strings.TrimSpace(flAccesskey)
75+
if flAccesskey == "" {
76+
flAccesskey = cloudConfig.AccessKey
77+
}
6178
}
6279
if flSecretkey = strings.TrimSpace(flSecretkey); flSecretkey == "" {
6380
cli.promptWithDefault("Enter Secret Key", cloudConfig.SecretKey)
6481
flSecretkey = readInput(cli.in, cli.out)
6582
flSecretkey = strings.TrimSpace(flSecretkey)
83+
if flSecretkey == "" {
84+
flSecretkey = cloudConfig.SecretKey
85+
}
86+
}
87+
if flRegion = strings.TrimSpace(flRegion); flRegion == "" {
88+
cli.promptWithDefault("Enter Default Region", defaultRegion)
89+
flRegion = readInput(cli.in, cli.out)
90+
flRegion = strings.TrimSpace(flRegion)
91+
if flRegion == "" {
92+
flRegion = defaultRegion
93+
}
6694
}
6795

6896
cloudConfig.AccessKey = flAccesskey
6997
cloudConfig.SecretKey = flSecretkey
70-
cli.configFile.CloudConfig[serverAddress] = cloudConfig
98+
cloudConfig.Region = flRegion
99+
if serverAddress != "" {
100+
cli.configFile.CloudConfig[serverAddress] = cloudConfig
101+
}
102+
71103
return cloudConfig, nil
72104
}
73105

@@ -78,3 +110,7 @@ func (cli *DockerCli) checkCloudConfig() error {
78110
}
79111
return nil
80112
}
113+
114+
func (cli *DockerCli) getDefaultRegion() string {
115+
return cliconfig.DefaultHyperRegion
116+
}

cli/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type CommonFlags struct {
1111
PostParse func()
1212

1313
Debug bool
14-
Hosts []string
14+
Region string
1515
LogLevel string
1616
TLS bool
1717
TLSVerify bool

cliconfig/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ const (
2424
// assumed to be this value.
2525
defaultIndexserver = "https://index.docker.io/v1/"
2626

27-
DefaultHyperServer = "tcp://us-west-1.hyper.sh:443"
27+
DefaultHyperFormat = "tcp://*.hyper.sh:443"
28+
DefaultHyperRegion = "us-west-1"
29+
DefaultHyperEndpoint = "hyper.sh:443"
2830
)
2931

3032
var (
@@ -50,6 +52,7 @@ func SetConfigDir(dir string) {
5052
type CloudConfig struct {
5153
AccessKey string `json:"accesskey"`
5254
SecretKey string `json:"secretkey"`
55+
Region string `json:"region"`
5356
}
5457

5558
// ConfigFile ~/.docker/config.json file info

hyper/common.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/docker/go-connections/tlsconfig"
1010
"github.com/hyperhq/hypercli/cli"
1111
"github.com/hyperhq/hypercli/cliconfig"
12-
"github.com/hyperhq/hypercli/opts"
1312
flag "github.com/hyperhq/hypercli/pkg/mflag"
1413
)
1514

@@ -50,7 +49,7 @@ func init() {
5049
cmd.StringVar(&tlsOptions.CertFile, []string{}, filepath.Join(dockerCertPath, defaultCertFile), "Path to TLS certificate file")
5150
cmd.StringVar(&tlsOptions.KeyFile, []string{}, filepath.Join(dockerCertPath, defaultKeyFile), "Path to TLS key file")
5251

53-
cmd.Var(opts.NewNamedListOptsRef("hosts", &commonFlags.Hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to")
52+
cmd.StringVar(&commonFlags.Region, []string{"R", "-region"}, "", "Set the region of hyper.sh")
5453
}
5554

5655
func postParseCommon() {

0 commit comments

Comments
 (0)