/
runtime.go
243 lines (221 loc) · 6.26 KB
/
runtime.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Package runtime is the micro runtime
package runtime
import (
"os"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/config/cmd"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/runtime"
pb "github.com/micro/go-micro/v2/runtime/service/proto"
"github.com/micro/micro/v2/service/runtime/handler"
"github.com/micro/micro/v2/service/runtime/manager"
"github.com/micro/micro/v2/service/runtime/profile"
)
var (
// Name of the runtime
Name = "go.micro.runtime"
// Address of the runtime
Address = ":8088"
)
// Run the runtime service
func Run(ctx *cli.Context, srvOpts ...micro.Option) {
log.Init(log.WithFields(map[string]interface{}{"service": "runtime"}))
// Get the profile
var prof []string
switch ctx.String("profile") {
case "local":
prof = profile.Local()
case "server":
prof = profile.Server()
case "kubernetes":
prof = profile.Kubernetes()
case "platform":
prof = profile.Platform()
}
// Init plugins
for _, p := range Plugins() {
p.Init(ctx)
}
if len(ctx.String("address")) > 0 {
Address = ctx.String("address")
}
if len(ctx.String("server_name")) > 0 {
Name = ctx.String("server_name")
}
if len(Address) > 0 {
srvOpts = append(srvOpts, micro.Address(Address))
}
// create runtime
muRuntime := *cmd.DefaultCmd.Options().Runtime
if ctx.IsSet("source") {
muRuntime.Init(runtime.WithSource(ctx.String("source")))
}
// append name
srvOpts = append(srvOpts, micro.Name(Name))
// new service
service := micro.NewService(srvOpts...)
// create a new runtime manager
manager := manager.New(muRuntime,
manager.Store(service.Options().Store),
manager.Profile(prof),
)
// start the manager
if err := manager.Start(); err != nil {
log.Errorf("failed to start: %s", err)
os.Exit(1)
}
// register the runtime handler
pb.RegisterRuntimeHandler(service.Server(), &handler.Runtime{
// Client to publish events
Client: micro.NewEvent("go.micro.runtime.events", service.Client()),
// using the micro runtime
Runtime: manager,
})
// start runtime service
if err := service.Run(); err != nil {
log.Errorf("error running service: %v", err)
}
// stop the manager
if err := manager.Stop(); err != nil {
log.Errorf("failed to stop: %s", err)
os.Exit(1)
}
}
// Flags is shared flags so we don't have to continually re-add
func Flags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "source",
Usage: "Set the source url of the service e.g github.com/micro/services",
},
&cli.StringFlag{
Name: "image",
Usage: "Set the image to use for the container",
},
&cli.StringFlag{
Name: "command",
Usage: "Command to exec",
},
&cli.StringFlag{
Name: "args",
Usage: "Command args",
},
&cli.StringFlag{
Name: "type",
Usage: "The type of service operate on",
},
&cli.StringSliceFlag{
Name: "env_vars",
Usage: "Set the environment variables e.g. foo=bar",
},
}
}
func Commands(options ...micro.Option) []*cli.Command {
command := []*cli.Command{
{
Name: "runtime",
Usage: "Run the micro runtime",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "Set the registry http address e.g 0.0.0.0:8088",
EnvVars: []string{"MICRO_SERVER_ADDRESS"},
},
&cli.StringFlag{
Name: "profile",
Usage: "Set the runtime profile to use for services e.g local, kubernetes, platform",
EnvVars: []string{"MICRO_RUNTIME_PROFILE"},
},
&cli.StringFlag{
Name: "source",
Usage: "Set the runtime source, e.g. micro/services",
EnvVars: []string{"MICRO_RUNTIME_SOURCE"},
},
&cli.IntFlag{
Name: "retries",
Usage: "Set the max retries per service",
EnvVars: []string{"MICRO_RUNTIME_RETRIES"},
},
},
Action: func(ctx *cli.Context) error {
Run(ctx, options...)
return nil
},
},
{
// In future we'll also have `micro run [x]` hence `micro run service` requiring "service"
Name: "run",
Usage: RunUsage,
Description: `Examples:
micro run github.com/micro/examples/helloworld
micro run . # deploy local folder to your local micro server
micro run ../path/to/folder # deploy local folder to your local micro server
micro run helloworld # deploy latest version, translates to micro run github.com/micro/services/helloworld
micro run helloworld@9342934e6180 # deploy certain version
micro run helloworld@branchname # deploy certain branch`,
Flags: Flags(),
Action: func(ctx *cli.Context) error {
runService(ctx, options...)
return nil
},
},
{
Name: "update",
Usage: UpdateUsage,
Description: `Examples:
micro update github.com/micro/examples/helloworld
micro update . # deploy local folder to your local micro server
micro update ../path/to/folder # deploy local folder to your local micro server
micro update helloworld # deploy master branch, translates to micro update github.com/micro/services/helloworld
micro update helloworld@branchname # deploy certain branch`,
Flags: Flags(),
Action: func(ctx *cli.Context) error {
updateService(ctx, options...)
return nil
},
},
{
Name: "kill",
Usage: KillUsage,
Flags: Flags(),
Description: `Examples:
micro kill github.com/micro/examples/helloworld
micro kill . # kill service deployed from local folder
micro kill ../path/to/folder # kill service deployed from local folder
micro kill helloworld # kill serviced deployed from master branch, translates to micro kill github.com/micro/services/helloworld
micro kill helloworld@branchname # kill service deployed from certain branch`,
Action: func(ctx *cli.Context) error {
killService(ctx, options...)
return nil
},
},
{
Name: "status",
Usage: GetUsage,
Flags: Flags(),
Action: func(ctx *cli.Context) error {
getService(ctx, options...)
return nil
},
},
{
Name: "logs",
Usage: "Get logs for a service",
Flags: logFlags(),
Action: func(ctx *cli.Context) error {
getLogs(ctx, options...)
return nil
},
},
}
for _, p := range Plugins() {
if cmds := p.Commands(); len(cmds) > 0 {
command[0].Subcommands = append(command[0].Subcommands, cmds...)
}
if flags := p.Flags(); len(flags) > 0 {
command[0].Flags = append(command[0].Flags, flags...)
}
}
return command
}