Skip to content

Commit 98cf8ab

Browse files
committed
Merge branch 'master' into support-git-tag
2 parents 484bd87 + ece7cbb commit 98cf8ab

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

cmd/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import (
2020
func Start(c *cli.Context) error {
2121
log.Debug("Step: config.LoadFromFile")
2222

23-
serverid, err := util.GetServerID()
23+
serverid, err := util.GetServerID(c.GlobalString("provider"))
24+
if err != nil {
25+
return err
26+
}
2427

2528
conf, err := config.LoadFromFile(c.String("config"))
2629
if err != nil {

cmd/update.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import (
1717
)
1818

1919
func Update(c *cli.Context) error {
20-
serverid, err := util.GetServerID()
20+
serverid, err := util.GetServerID(c.GlobalString("provider"))
21+
if err != nil {
22+
return err
23+
}
24+
2125
conf, err := config.LoadFromFile(c.String("config"))
2226
if err != nil {
2327
return err

container/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (d *Docker) containerCreate(name string, dir string, isApp bool) (*Containe
302302

303303
// to keep compatibility with older modaemon
304304
func (d *Docker) prepareLogsDir() error {
305-
if util.FileExists(containerLogsLocation) {
305+
if util.FileExists(containerLogsLocation + "log") {
306306
return nil
307307
}
308308

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"os"
87
"sort"
98
"strings"
109

@@ -33,6 +32,7 @@ func beforeActions(c *cli.Context) error {
3332
if c.GlobalBool("autoupdate") {
3433
versions.AutoUpdate(golatest())
3534
}
35+
log.Debugf("Set provider to %#v", c.GlobalString("provider"))
3636
return nil
3737
}
3838

@@ -70,6 +70,11 @@ func main() {
7070
Name: "autoupdate, U",
7171
Usage: "auto update before run",
7272
},
73+
cli.StringFlag{
74+
Name: "provider, P",
75+
Value: "aws",
76+
Usage: "set `Provider`",
77+
},
7378
}
7479

7580
// Common Flags for commands
@@ -118,7 +123,7 @@ func main() {
118123

119124
sort.Sort(cli.FlagsByName(app.Flags))
120125

121-
app.Run(os.Args)
126+
app.RunAndExitOnError()
122127
}
123128

124129
// FatalWriter just initiaizes cliErrWriter

util/util.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
var (
1515
ec2METAENDPOINT = "http://169.254.169.254/"
16+
ecsMETAENDPOINT = "http://100.100.100.200/"
1617
containerLogsLocation = "/var/modaemon/containerlogs"
1718
)
1819

@@ -36,42 +37,44 @@ func FetchContainerState() string {
3637
return strings.TrimSpace(string(dat))
3738
}
3839

39-
func GetServerID(s ...string) (string, error) {
40-
var sid string
41-
42-
if len(s) == 0 {
43-
s = append(s, "aws")
44-
}
45-
46-
switch s[0] {
47-
case "aws":
48-
sid = getServerIDforEC2()
49-
default:
50-
return sid, errors.New("Provider `" + s[0] + "` is not supported.")
40+
func GetServerID(s string) (string, error) {
41+
sid, err := getServerID(s)
42+
if err != nil {
43+
return "", err
5144
}
5245

5346
return sid, nil
5447
}
5548

56-
func getServerIDforEC2() string {
49+
func getServerID(provider string) (string, error) {
5750
timeout := time.Duration(5 * time.Second)
5851
client := http.Client{
5952
Timeout: timeout,
6053
}
6154

62-
resp, err := client.Get(ec2METAENDPOINT + "/latest/meta-data/instance-id")
55+
var endpoint string
56+
switch provider {
57+
case "aws":
58+
endpoint = ec2METAENDPOINT + "/latest/meta-data/instance-id"
59+
case "alicloud":
60+
endpoint = ecsMETAENDPOINT + "/latest/meta-data/instance-id"
61+
default:
62+
return "", errors.New("Provider `" + provider + "` is not supported.")
63+
}
64+
65+
resp, err := client.Get(endpoint)
6366
if err != nil {
6467
log.Warnf("%#v", err)
65-
return ""
68+
return "", errors.New("Failed to get ServerID")
6669
}
6770

6871
defer resp.Body.Close()
6972

7073
body, err := ioutil.ReadAll(resp.Body)
7174
if err != nil {
7275
log.Warnf("%#v", err)
73-
return ""
76+
return "", errors.New("Failed to get ServerID")
7477
}
7578

76-
return string(body)
79+
return string(body), nil
7780
}

0 commit comments

Comments
 (0)