-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlambda.go
189 lines (172 loc) · 3.76 KB
/
lambda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"io/ioutil"
"os"
log "github.com/Sirupsen/logrus"
"github.com/projecteru2/lambda/rpc"
"github.com/projecteru2/lambda/types"
"github.com/projecteru2/lambda/utils"
"github.com/projecteru2/lambda/versioninfo"
"gopkg.in/urfave/cli.v2"
"gopkg.in/yaml.v2"
)
var (
debug bool
admin bool
raw bool
)
func setupLog(l string) error {
level, err := log.ParseLevel(l)
if err != nil {
return err
}
log.SetLevel(level)
formatter := &log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
}
log.SetFormatter(formatter)
return nil
}
func initConfig(configPath string) (types.Config, error) {
config := types.Config{}
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
return config, err
}
if err := yaml.Unmarshal(bytes, &config); err != nil {
return config, err
}
return config, nil
}
func runLambda(c *cli.Context) error {
if debug {
setupLog("DEBUG")
} else {
setupLog("INFO")
}
config, err := initConfig(c.String("config"))
if err != nil {
log.Fatal(err)
}
runParams := utils.GetParams(c)
if admin {
runParams.Pod = config.Default.AdminPod
for _, v := range config.Default.AdminVolumes {
runParams.Volumes = append(runParams.Volumes, v)
}
}
runParams.Raw = raw
if runParams.Count > config.Concurrency {
log.Fatalf("Max concurrency limit %d", config.Concurrency)
}
runParams = utils.RebuildParams(runParams, config.Default)
server := utils.PickServer(config.Servers)
code := rpc.RunAndWait(server, runParams)
if code == 0 {
return nil
}
return cli.Exit("", code)
}
func init() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Print(versioninfo.VersionString())
}
}
func main() {
app := cli.App{}
app.Name = versioninfo.NAME
app.Usage = "run code on eru"
app.Version = versioninfo.VERSION
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "/etc/eru/lambda.yaml",
Usage: "config file path for lambda, in yaml",
},
&cli.StringFlag{
Name: "name",
Usage: "name for this lambda",
},
&cli.StringFlag{
Name: "command",
Usage: "how to run it",
},
&cli.StringSliceFlag{
Name: "env",
Usage: "set env can use multiple times",
},
&cli.StringSliceFlag{
Name: "volume",
Usage: "set volume can use multiple times",
},
&cli.StringFlag{
Name: "pod",
Usage: "where to run",
DefaultText: "in config file",
},
&cli.StringFlag{
Name: "network",
Usage: "SDN name",
DefaultText: "in config file",
},
&cli.StringFlag{
Name: "working-dir",
Usage: "use as current working dir",
DefaultText: "in config file",
},
&cli.StringFlag{
Name: "image",
Usage: "base image for running",
DefaultText: "in config file",
},
&cli.Float64Flag{
Name: "cpu",
Usage: "how many cpu",
DefaultText: "in config file",
},
&cli.Int64Flag{
Name: "mem",
Usage: "how many memory in bytes",
DefaultText: "in config file",
},
&cli.IntFlag{
Name: "timeout",
Usage: "when to interrupt",
DefaultText: "in config file",
},
&cli.IntFlag{
Name: "count",
Usage: "how many containers",
Value: 1,
},
&cli.BoolFlag{
Name: "raw",
Usage: "use image without lambda",
Value: false,
Destination: &raw,
},
&cli.BoolFlag{
Name: "admin",
Usage: "admin or not",
Value: false,
Destination: &admin,
},
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug",
Aliases: []string{"d"},
Value: false,
Destination: &debug,
},
&cli.BoolFlag{
Name: "stdin",
Usage: "open stdin for container",
Aliases: []string{"s"},
Value: false,
},
}
app.Action = runLambda
app.Run(os.Args)
}