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

Commit df50ac3

Browse files
authored
Merge pull request #230 from carmark/region
support multiple regions for hyper.sh
2 parents ec19526 + 28ce86f commit df50ac3

33 files changed

+329
-134
lines changed

Dockerfile.dev

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ ENV DOCKER_TLS_VERIFY=
2929
ENV DOCKER_HOST=
3030
ENV ACCESS_KEY=
3131
ENV SECRET_KEY=
32-
32+
ENV REGION=
3333

3434
## Ensure /usr/bin/hyper
3535
RUN ln -s /go/src/github.com/hyperhq/hypercli/hyper/hyper /usr/bin/hyper
36-
RUN echo alias hypercli=\"hyper -H \${DOCKER_HOST}\" >> /root/.bashrc
36+
RUN echo alias hypercli=\"hyper --region \${DOCKER_HOST}\" >> /root/.bashrc
3737

3838

3939
## Ensure /go/src/github.com/docker/docker
@@ -51,7 +51,7 @@ ENTRYPOINT ["hack/generate-hyper-conf-dev.sh"]
5151
RUN yum install -y zsh
5252
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
5353
RUN sed -i "s/^ZSH_THEME=.*/ZSH_THEME=\"gianu\"/g" /root/.zshrc
54-
RUN echo alias hypercli=\"hyper -H \${DOCKER_HOST}\" >> /root/.zshrc
54+
RUN echo alias hypercli=\"hyper --region \${DOCKER_HOST}\" >> /root/.zshrc
5555

5656
# config git
5757
RUN git config --global color.ui true; \

Dockerfile.qa

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ENV BRANCH="master"
3333

3434
ENV ACCESS_KEY=
3535
ENV SECRET_KEY=
36+
ENV REGION=
3637

3738
RUN mkdir -p /go/src/github.com/hyperhq
3839
WORKDIR /go/src/github.com/hyperhq

api/client/cli.go

Lines changed: 31 additions & 15 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, dft, err := cli.getServerHost(clientFlags.Common.Region, clientFlags.Common.TLSOptions)
138140
if err != nil {
139141
return err
140142
}
@@ -155,13 +157,19 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
155157
return err
156158
}
157159
var cloudConfig cliconfig.CloudConfig
158-
cc, ok := configFile.CloudConfig[host]
159-
if ok {
160+
cc, ok := configFile.CloudConfig[cliconfig.DefaultHyperFormat]
161+
if ok && dft {
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[host]
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,12 @@ 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
185+
if cli.region == "" {
186+
if cli.region = cc.Region; cli.region == "" {
187+
cli.region = cli.getDefaultRegion()
188+
}
189+
}
176190

177191
if cli.in != nil {
178192
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
@@ -187,14 +201,16 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
187201
return cli
188202
}
189203

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")
204+
func (cli *DockerCli) getServerHost(region string, tlsOptions *tlsconfig.Options) (host string, dft bool, err error) {
205+
dft = false
206+
host = region
207+
if host == "" {
208+
host = os.Getenv("HYPER_DEFAULT_REGION")
209+
region = cli.getDefaultRegion()
210+
}
211+
if _, err := url.ParseRequestURI(host); err != nil {
212+
host = "tcp://" + region + "." + cliconfig.DefaultHyperEndpoint
213+
dft = true
198214
}
199215

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

api/client/config.go

Lines changed: 52 additions & 9 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,33 +49,75 @@ 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

74106
func (cli *DockerCli) checkCloudConfig() error {
75107
_, ok := cli.configFile.CloudConfig[cli.host]
76108
if !ok {
77-
return fmt.Errorf("Config info for the host is not found, please run 'hyper config %s' first.", cli.host)
109+
_, ok = cli.configFile.CloudConfig[cliconfig.DefaultHyperFormat]
110+
if !ok {
111+
return fmt.Errorf("Config info for the host is not found, please run 'hyper config %s' first.", cli.host)
112+
}
78113
}
79114
return nil
80115
}
116+
117+
func (cli *DockerCli) getDefaultRegion() string {
118+
cc, ok := cli.configFile.CloudConfig[cliconfig.DefaultHyperFormat]
119+
if ok && cc.Region != "" {
120+
return cc.Region
121+
}
122+
return cliconfig.DefaultHyperRegion
123+
}

api/client/func.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (cli *DockerCli) CmdFuncCreate(args ...string) error {
218218
if err != nil {
219219
return err
220220
}
221-
fmt.Fprintf(cli.out, "%s is created with the address of https://us-west-1.hyperfunc.io/call/%s/%s\n", fn.Name, fn.Name, fn.UUID)
221+
fmt.Fprintf(cli.out, "%s is created with the address of https://%s.hyperfunc.io/call/%s/%s\n", fn.Name, cli.region, fn.Name, fn.UUID)
222222
return nil
223223
}
224224

@@ -400,7 +400,7 @@ func (cli *DockerCli) CmdFuncCall(args ...string) error {
400400
}
401401
}
402402

403-
body, err := cli.client.FuncCall(context.Background(), name, stdin, *sync)
403+
body, err := cli.client.FuncCall(context.Background(), cli.region, name, stdin, *sync)
404404
if err != nil {
405405
return err
406406
}
@@ -435,7 +435,7 @@ func (cli *DockerCli) CmdFuncGet(args ...string) error {
435435

436436
callId := cmd.Arg(0)
437437

438-
body, err := cli.client.FuncGet(context.Background(), callId, *wait)
438+
body, err := cli.client.FuncGet(context.Background(), cli.region, callId, *wait)
439439
if err != nil {
440440
return err
441441
}
@@ -463,7 +463,7 @@ func (cli *DockerCli) CmdFuncLogs(args ...string) error {
463463
name := cmd.Arg(0)
464464
name = strings.Replace(name, "/", "", -1)
465465

466-
reader, err := cli.client.FuncLogs(context.Background(), name, *callId, *follow, *tail)
466+
reader, err := cli.client.FuncLogs(context.Background(), cli.region, name, *callId, *follow, *tail)
467467
if err != nil {
468468
return err
469469
}
@@ -503,7 +503,6 @@ func (cli *DockerCli) CmdFuncLogs(args ...string) error {
503503
}
504504
}
505505
}
506-
return nil
507506
}
508507

509508
// CmdFuncStatus Status the return of a func call
@@ -520,7 +519,7 @@ func (cli *DockerCli) CmdFuncStatus(args ...string) error {
520519
name := cmd.Arg(0)
521520
name = strings.Replace(name, "/", "", -1)
522521

523-
status, err := cli.client.FuncStatus(context.Background(), name)
522+
status, err := cli.client.FuncStatus(context.Background(), cli.region, name)
524523
if err != nil {
525524
return err
526525
}

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

hack/generate-hyper-conf-dev.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
if [ "${REGION}" == "" ];then
4+
REGION="us-west-1"
5+
fi
6+
37
if [ "$@" != "./build.sh" ];then
48
#ensure config for hyper cli
59
mkdir -p ~/.hyper
@@ -9,6 +13,11 @@ if [ "$@" != "./build.sh" ];then
913
"${DOCKER_HOST}": {
1014
"accesskey": "${ACCESS_KEY}",
1115
"secretkey": "${SECRET_KEY}"
16+
},
17+
"tcp://*.hyper.sh:443": {
18+
"accesskey": "${ACCESS_KEY}",
19+
"secretkey": "${SECRET_KEY}",
20+
"region": "${REGION}"
1221
}
1322
}
1423
}
@@ -20,7 +29,7 @@ EOF
2029
echo "Current hyper config: ~/.hyper/config.json"
2130
echo "----------------------------------------------------------------------------------------------"
2231
cat ~/.hyper/config.json \
23-
| sed 's/"secretkey":.*/"secretkey": "******************************"/g' \
32+
| sed 's/"secretkey":.*/"secretkey": "******************************",/g' \
2433
| sed 's/"auth":.*/"auth": "******************************"/g'
2534
echo "----------------------------------------------------------------------------------------------"
2635

@@ -36,7 +45,7 @@ Run in container(example):
3645
-----------------------------------------------------------
3746
cd integration-cli && go test # start autotest
3847
39-
# 'hypercli' is the alias of 'hyper -H \${DOCKER_HOST}'
48+
# 'hypercli' is the alias of 'hyper --region \${DOCKER_HOST}'
4049
4150
EOF
4251
fi

hack/generate-hyper-conf-qa.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
env
44

5+
if [ "${REGION}" == "" ];then
6+
REGION="us-west-1"
7+
fi
8+
59
# set default value of DOCKER_HOST and BRANCH
610
if [[ "$DOCKER_HOST" == "" ]];then
711
DOCKER_HOST="tcp://us-west-1.hyper.sh:443"
@@ -33,6 +37,11 @@ if [[ "$@" != "./build.sh" ]];then
3337
"${DOCKER_HOST}": {
3438
"accesskey": "${ACCESS_KEY}",
3539
"secretkey": "${SECRET_KEY}"
40+
},
41+
"tcp://*.hyper.sh:443": {
42+
"accesskey": "${ACCESS_KEY}",
43+
"secretkey": "${SECRET_KEY}",
44+
"region": "${REGION}"
3645
}
3746
}
3847
}
@@ -73,7 +82,7 @@ fi
7382
./build.sh
7483
ln -s /go/src/github.com/hyperhq/hypercli /go/src/github.com/docker/docker
7584
ln -s /go/src/github.com/hyperhq/hypercli/hyper/hyper /usr/bin/hyper
76-
echo alias hypercli=\"hyper -H \${DOCKER_HOST}\" >> /root/.bashrc
85+
echo alias hypercli=\"hyper --region \${DOCKER_HOST}\" >> /root/.bashrc
7786
source /root/.bashrc
7887

7988
echo "##############################################################################################"
@@ -83,7 +92,7 @@ echo "##########################################################################
8392
echo "Current hyper config: ~/.hyper/config.json"
8493
echo "----------------------------------------------------------------------------------------------"
8594
cat ~/.hyper/config.json \
86-
| sed 's/"secretkey":.*/"secretkey": "******************************"/g' \
95+
| sed 's/"secretkey":.*/"secretkey": "******************************",/g' \
8796
| sed 's/"auth":.*/"auth": "******************************"/g'
8897
echo "----------------------------------------------------------------------------------------------"
8998

0 commit comments

Comments
 (0)