github.com/janma/nomad@v0.11.3/drivers/java/driver.go (about) 1 package java 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "runtime" 10 "time" 11 12 "github.com/hashicorp/consul-template/signals" 13 hclog "github.com/hashicorp/go-hclog" 14 "github.com/hashicorp/nomad/client/fingerprint" 15 "github.com/hashicorp/nomad/drivers/shared/eventer" 16 "github.com/hashicorp/nomad/drivers/shared/executor" 17 "github.com/hashicorp/nomad/helper/pluginutils/loader" 18 "github.com/hashicorp/nomad/plugins/base" 19 "github.com/hashicorp/nomad/plugins/drivers" 20 "github.com/hashicorp/nomad/plugins/drivers/utils" 21 "github.com/hashicorp/nomad/plugins/shared/hclspec" 22 pstructs "github.com/hashicorp/nomad/plugins/shared/structs" 23 ) 24 25 const ( 26 // pluginName is the name of the plugin 27 pluginName = "java" 28 29 // fingerprintPeriod is the interval at which the driver will send fingerprint responses 30 fingerprintPeriod = 30 * time.Second 31 32 // The key populated in Node Attributes to indicate presence of the Java driver 33 driverAttr = "driver.java" 34 driverVersionAttr = "driver.java.version" 35 36 // taskHandleVersion is the version of task handle which this driver sets 37 // and understands how to decode driver state 38 taskHandleVersion = 1 39 ) 40 41 var ( 42 // PluginID is the java plugin metadata registered in the plugin 43 // catalog. 44 PluginID = loader.PluginID{ 45 Name: pluginName, 46 PluginType: base.PluginTypeDriver, 47 } 48 49 // PluginConfig is the java driver factory function registered in the 50 // plugin catalog. 51 PluginConfig = &loader.InternalPluginConfig{ 52 Config: map[string]interface{}{}, 53 Factory: func(ctx context.Context, l hclog.Logger) interface{} { return NewDriver(ctx, l) }, 54 } 55 56 // pluginInfo is the response returned for the PluginInfo RPC 57 pluginInfo = &base.PluginInfoResponse{ 58 Type: base.PluginTypeDriver, 59 PluginApiVersions: []string{drivers.ApiVersion010}, 60 PluginVersion: "0.1.0", 61 Name: pluginName, 62 } 63 64 // configSpec is the hcl specification returned by the ConfigSchema RPC 65 configSpec = hclspec.NewObject(map[string]*hclspec.Spec{}) 66 67 // taskConfigSpec is the hcl specification for the driver config section of 68 // a taskConfig within a job. It is returned in the TaskConfigSchema RPC 69 taskConfigSpec = hclspec.NewObject(map[string]*hclspec.Spec{ 70 // It's required for either `class` or `jar_path` to be set, 71 // but that's not expressable in hclspec. Marking both as optional 72 // and setting checking explicitly later 73 "class": hclspec.NewAttr("class", "string", false), 74 "class_path": hclspec.NewAttr("class_path", "string", false), 75 "jar_path": hclspec.NewAttr("jar_path", "string", false), 76 "jvm_options": hclspec.NewAttr("jvm_options", "list(string)", false), 77 "args": hclspec.NewAttr("args", "list(string)", false), 78 }) 79 80 // capabilities is returned by the Capabilities RPC and indicates what 81 // optional features this driver supports 82 capabilities = &drivers.Capabilities{ 83 SendSignals: false, 84 Exec: false, 85 FSIsolation: drivers.FSIsolationNone, 86 NetIsolationModes: []drivers.NetIsolationMode{ 87 drivers.NetIsolationModeHost, 88 drivers.NetIsolationModeGroup, 89 }, 90 MountConfigs: drivers.MountConfigSupportNone, 91 } 92 93 _ drivers.DriverPlugin = (*Driver)(nil) 94 ) 95 96 func init() { 97 if runtime.GOOS == "linux" { 98 capabilities.FSIsolation = drivers.FSIsolationChroot 99 capabilities.MountConfigs = drivers.MountConfigSupportAll 100 } 101 } 102 103 // TaskConfig is the driver configuration of a taskConfig within a job 104 type TaskConfig struct { 105 Class string `codec:"class"` 106 ClassPath string `codec:"class_path"` 107 JarPath string `codec:"jar_path"` 108 JvmOpts []string `codec:"jvm_options"` 109 Args []string `codec:"args"` // extra arguments to java executable 110 } 111 112 // TaskState is the state which is encoded in the handle returned in 113 // StartTask. This information is needed to rebuild the taskConfig state and handler 114 // during recovery. 115 type TaskState struct { 116 ReattachConfig *pstructs.ReattachConfig 117 TaskConfig *drivers.TaskConfig 118 Pid int 119 StartedAt time.Time 120 } 121 122 // Driver is a driver for running images via Java 123 type Driver struct { 124 // eventer is used to handle multiplexing of TaskEvents calls such that an 125 // event can be broadcast to all callers 126 eventer *eventer.Eventer 127 128 // tasks is the in memory datastore mapping taskIDs to taskHandle 129 tasks *taskStore 130 131 // ctx is the context for the driver. It is passed to other subsystems to 132 // coordinate shutdown 133 ctx context.Context 134 135 // nomadConf is the client agent's configuration 136 nomadConfig *base.ClientDriverConfig 137 138 // logger will log to the Nomad agent 139 logger hclog.Logger 140 } 141 142 func NewDriver(ctx context.Context, logger hclog.Logger) drivers.DriverPlugin { 143 logger = logger.Named(pluginName) 144 return &Driver{ 145 eventer: eventer.NewEventer(ctx, logger), 146 tasks: newTaskStore(), 147 ctx: ctx, 148 logger: logger, 149 } 150 } 151 152 func (d *Driver) PluginInfo() (*base.PluginInfoResponse, error) { 153 return pluginInfo, nil 154 } 155 156 func (d *Driver) ConfigSchema() (*hclspec.Spec, error) { 157 return configSpec, nil 158 } 159 160 func (d *Driver) SetConfig(cfg *base.Config) error { 161 if cfg != nil && cfg.AgentConfig != nil { 162 d.nomadConfig = cfg.AgentConfig.Driver 163 } 164 return nil 165 } 166 167 func (d *Driver) TaskConfigSchema() (*hclspec.Spec, error) { 168 return taskConfigSpec, nil 169 } 170 171 func (d *Driver) Capabilities() (*drivers.Capabilities, error) { 172 return capabilities, nil 173 } 174 175 func (d *Driver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint, error) { 176 ch := make(chan *drivers.Fingerprint) 177 go d.handleFingerprint(ctx, ch) 178 return ch, nil 179 } 180 181 func (d *Driver) handleFingerprint(ctx context.Context, ch chan *drivers.Fingerprint) { 182 ticker := time.NewTimer(0) 183 for { 184 select { 185 case <-ctx.Done(): 186 return 187 case <-d.ctx.Done(): 188 return 189 case <-ticker.C: 190 ticker.Reset(fingerprintPeriod) 191 ch <- d.buildFingerprint() 192 } 193 } 194 } 195 196 func (d *Driver) buildFingerprint() *drivers.Fingerprint { 197 fp := &drivers.Fingerprint{ 198 Attributes: map[string]*pstructs.Attribute{}, 199 Health: drivers.HealthStateHealthy, 200 HealthDescription: drivers.DriverHealthy, 201 } 202 203 if runtime.GOOS == "linux" { 204 // Only enable if w are root and cgroups are mounted when running on linux system 205 if !utils.IsUnixRoot() { 206 fp.Health = drivers.HealthStateUndetected 207 fp.HealthDescription = drivers.DriverRequiresRootMessage 208 return fp 209 } 210 211 mount, err := fingerprint.FindCgroupMountpointDir() 212 if err != nil { 213 fp.Health = drivers.HealthStateUnhealthy 214 fp.HealthDescription = drivers.NoCgroupMountMessage 215 d.logger.Warn(fp.HealthDescription, "error", err) 216 return fp 217 } 218 219 if mount == "" { 220 fp.Health = drivers.HealthStateUnhealthy 221 fp.HealthDescription = drivers.CgroupMountEmpty 222 return fp 223 } 224 } 225 226 version, runtime, vm, err := javaVersionInfo() 227 if err != nil { 228 // return no error, as it isn't an error to not find java, it just means we 229 // can't use it. 230 fp.Health = drivers.HealthStateUndetected 231 fp.HealthDescription = "" 232 return fp 233 } 234 235 fp.Attributes[driverAttr] = pstructs.NewBoolAttribute(true) 236 fp.Attributes[driverVersionAttr] = pstructs.NewStringAttribute(version) 237 fp.Attributes["driver.java.runtime"] = pstructs.NewStringAttribute(runtime) 238 fp.Attributes["driver.java.vm"] = pstructs.NewStringAttribute(vm) 239 240 return fp 241 } 242 243 func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error { 244 if handle == nil { 245 return fmt.Errorf("handle cannot be nil") 246 } 247 248 // COMPAT(0.10): pre 0.9 upgrade path check 249 if handle.Version == 0 { 250 return d.recoverPre09Task(handle) 251 } 252 253 // If already attached to handle there's nothing to recover. 254 if _, ok := d.tasks.Get(handle.Config.ID); ok { 255 d.logger.Debug("nothing to recover; task already exists", 256 "task_id", handle.Config.ID, 257 "task_name", handle.Config.Name, 258 ) 259 return nil 260 } 261 262 var taskState TaskState 263 if err := handle.GetDriverState(&taskState); err != nil { 264 d.logger.Error("failed to decode taskConfig state from handle", "error", err, "task_id", handle.Config.ID) 265 return fmt.Errorf("failed to decode taskConfig state from handle: %v", err) 266 } 267 268 plugRC, err := pstructs.ReattachConfigToGoPlugin(taskState.ReattachConfig) 269 if err != nil { 270 d.logger.Error("failed to build ReattachConfig from taskConfig state", "error", err, "task_id", handle.Config.ID) 271 return fmt.Errorf("failed to build ReattachConfig from taskConfig state: %v", err) 272 } 273 274 execImpl, pluginClient, err := executor.ReattachToExecutor(plugRC, 275 d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID)) 276 if err != nil { 277 d.logger.Error("failed to reattach to executor", "error", err, "task_id", handle.Config.ID) 278 return fmt.Errorf("failed to reattach to executor: %v", err) 279 } 280 281 h := &taskHandle{ 282 exec: execImpl, 283 pid: taskState.Pid, 284 pluginClient: pluginClient, 285 taskConfig: taskState.TaskConfig, 286 procState: drivers.TaskStateRunning, 287 startedAt: taskState.StartedAt, 288 exitResult: &drivers.ExitResult{}, 289 logger: d.logger, 290 } 291 292 d.tasks.Set(taskState.TaskConfig.ID, h) 293 294 go h.run() 295 return nil 296 } 297 298 func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) { 299 if _, ok := d.tasks.Get(cfg.ID); ok { 300 return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) 301 } 302 303 var driverConfig TaskConfig 304 if err := cfg.DecodeDriverConfig(&driverConfig); err != nil { 305 return nil, nil, fmt.Errorf("failed to decode driver config: %v", err) 306 } 307 308 if driverConfig.Class == "" && driverConfig.JarPath == "" { 309 return nil, nil, fmt.Errorf("jar_path or class must be specified") 310 } 311 312 absPath, err := GetAbsolutePath("java") 313 if err != nil { 314 return nil, nil, fmt.Errorf("failed to find java binary: %s", err) 315 } 316 317 args := javaCmdArgs(driverConfig) 318 319 d.logger.Info("starting java task", "driver_cfg", hclog.Fmt("%+v", driverConfig), "args", args) 320 321 handle := drivers.NewTaskHandle(taskHandleVersion) 322 handle.Config = cfg 323 324 pluginLogFile := filepath.Join(cfg.TaskDir().Dir, "executor.out") 325 executorConfig := &executor.ExecutorConfig{ 326 LogFile: pluginLogFile, 327 LogLevel: "debug", 328 FSIsolation: capabilities.FSIsolation == drivers.FSIsolationChroot, 329 } 330 331 exec, pluginClient, err := executor.CreateExecutor( 332 d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID), 333 d.nomadConfig, executorConfig) 334 if err != nil { 335 return nil, nil, fmt.Errorf("failed to create executor: %v", err) 336 } 337 338 user := cfg.User 339 if user == "" { 340 user = "nobody" 341 } 342 343 execCmd := &executor.ExecCommand{ 344 Cmd: absPath, 345 Args: args, 346 Env: cfg.EnvList(), 347 User: user, 348 ResourceLimits: true, 349 Resources: cfg.Resources, 350 TaskDir: cfg.TaskDir().Dir, 351 StdoutPath: cfg.StdoutPath, 352 StderrPath: cfg.StderrPath, 353 Mounts: cfg.Mounts, 354 Devices: cfg.Devices, 355 NetworkIsolation: cfg.NetworkIsolation, 356 } 357 358 ps, err := exec.Launch(execCmd) 359 if err != nil { 360 pluginClient.Kill() 361 return nil, nil, fmt.Errorf("failed to launch command with executor: %v", err) 362 } 363 364 h := &taskHandle{ 365 exec: exec, 366 pid: ps.Pid, 367 pluginClient: pluginClient, 368 taskConfig: cfg, 369 procState: drivers.TaskStateRunning, 370 startedAt: time.Now().Round(time.Millisecond), 371 logger: d.logger, 372 } 373 374 driverState := TaskState{ 375 ReattachConfig: pstructs.ReattachConfigFromGoPlugin(pluginClient.ReattachConfig()), 376 Pid: ps.Pid, 377 TaskConfig: cfg, 378 StartedAt: h.startedAt, 379 } 380 381 if err := handle.SetDriverState(&driverState); err != nil { 382 d.logger.Error("failed to start task, error setting driver state", "error", err) 383 exec.Shutdown("", 0) 384 pluginClient.Kill() 385 return nil, nil, fmt.Errorf("failed to set driver state: %v", err) 386 } 387 388 d.tasks.Set(cfg.ID, h) 389 go h.run() 390 return handle, nil, nil 391 } 392 393 func javaCmdArgs(driverConfig TaskConfig) []string { 394 args := []string{} 395 // Look for jvm options 396 if len(driverConfig.JvmOpts) != 0 { 397 args = append(args, driverConfig.JvmOpts...) 398 } 399 400 // Add the classpath 401 if driverConfig.ClassPath != "" { 402 args = append(args, "-cp", driverConfig.ClassPath) 403 } 404 405 // Add the jar 406 if driverConfig.JarPath != "" { 407 args = append(args, "-jar", driverConfig.JarPath) 408 } 409 410 // Add the class 411 if driverConfig.Class != "" { 412 args = append(args, driverConfig.Class) 413 } 414 415 // Add any args 416 if len(driverConfig.Args) != 0 { 417 args = append(args, driverConfig.Args...) 418 } 419 420 return args 421 } 422 423 func (d *Driver) WaitTask(ctx context.Context, taskID string) (<-chan *drivers.ExitResult, error) { 424 handle, ok := d.tasks.Get(taskID) 425 if !ok { 426 return nil, drivers.ErrTaskNotFound 427 } 428 429 ch := make(chan *drivers.ExitResult) 430 go d.handleWait(ctx, handle, ch) 431 432 return ch, nil 433 } 434 435 func (d *Driver) handleWait(ctx context.Context, handle *taskHandle, ch chan *drivers.ExitResult) { 436 defer close(ch) 437 var result *drivers.ExitResult 438 ps, err := handle.exec.Wait(ctx) 439 if err != nil { 440 result = &drivers.ExitResult{ 441 Err: fmt.Errorf("executor: error waiting on process: %v", err), 442 } 443 } else { 444 result = &drivers.ExitResult{ 445 ExitCode: ps.ExitCode, 446 Signal: ps.Signal, 447 } 448 } 449 450 select { 451 case <-ctx.Done(): 452 return 453 case <-d.ctx.Done(): 454 return 455 case ch <- result: 456 } 457 } 458 459 func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) error { 460 handle, ok := d.tasks.Get(taskID) 461 if !ok { 462 return drivers.ErrTaskNotFound 463 } 464 465 if err := handle.exec.Shutdown(signal, timeout); err != nil { 466 if handle.pluginClient.Exited() { 467 return nil 468 } 469 return fmt.Errorf("executor Shutdown failed: %v", err) 470 } 471 472 return nil 473 } 474 475 func (d *Driver) DestroyTask(taskID string, force bool) error { 476 handle, ok := d.tasks.Get(taskID) 477 if !ok { 478 return drivers.ErrTaskNotFound 479 } 480 481 if handle.IsRunning() && !force { 482 return fmt.Errorf("cannot destroy running task") 483 } 484 485 if !handle.pluginClient.Exited() { 486 if err := handle.exec.Shutdown("", 0); err != nil { 487 handle.logger.Error("destroying executor failed", "err", err) 488 } 489 490 handle.pluginClient.Kill() 491 } 492 493 d.tasks.Delete(taskID) 494 return nil 495 } 496 497 func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { 498 handle, ok := d.tasks.Get(taskID) 499 if !ok { 500 return nil, drivers.ErrTaskNotFound 501 } 502 503 return handle.TaskStatus(), nil 504 } 505 506 func (d *Driver) TaskStats(ctx context.Context, taskID string, interval time.Duration) (<-chan *drivers.TaskResourceUsage, error) { 507 handle, ok := d.tasks.Get(taskID) 508 if !ok { 509 return nil, drivers.ErrTaskNotFound 510 } 511 512 return handle.exec.Stats(ctx, interval) 513 } 514 515 func (d *Driver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) { 516 return d.eventer.TaskEvents(ctx) 517 } 518 519 func (d *Driver) SignalTask(taskID string, signal string) error { 520 handle, ok := d.tasks.Get(taskID) 521 if !ok { 522 return drivers.ErrTaskNotFound 523 } 524 525 sig := os.Interrupt 526 if s, ok := signals.SignalLookup[signal]; ok { 527 sig = s 528 } else { 529 d.logger.Warn("unknown signal to send to task, using SIGINT instead", "signal", signal, "task_id", handle.taskConfig.ID) 530 531 } 532 return handle.exec.Signal(sig) 533 } 534 535 func (d *Driver) ExecTask(taskID string, cmd []string, timeout time.Duration) (*drivers.ExecTaskResult, error) { 536 if len(cmd) == 0 { 537 return nil, fmt.Errorf("error cmd must have at least one value") 538 } 539 handle, ok := d.tasks.Get(taskID) 540 if !ok { 541 return nil, drivers.ErrTaskNotFound 542 } 543 544 out, exitCode, err := handle.exec.Exec(time.Now().Add(timeout), cmd[0], cmd[1:]) 545 if err != nil { 546 return nil, err 547 } 548 549 return &drivers.ExecTaskResult{ 550 Stdout: out, 551 ExitResult: &drivers.ExitResult{ 552 ExitCode: exitCode, 553 }, 554 }, nil 555 } 556 557 var _ drivers.ExecTaskStreamingRawDriver = (*Driver)(nil) 558 559 func (d *Driver) ExecTaskStreamingRaw(ctx context.Context, 560 taskID string, 561 command []string, 562 tty bool, 563 stream drivers.ExecTaskStream) error { 564 565 if len(command) == 0 { 566 return fmt.Errorf("error cmd must have at least one value") 567 } 568 handle, ok := d.tasks.Get(taskID) 569 if !ok { 570 return drivers.ErrTaskNotFound 571 } 572 573 return handle.exec.ExecStreaming(ctx, command, tty, stream) 574 } 575 576 // GetAbsolutePath returns the absolute path of the passed binary by resolving 577 // it in the path and following symlinks. 578 func GetAbsolutePath(bin string) (string, error) { 579 lp, err := exec.LookPath(bin) 580 if err != nil { 581 return "", fmt.Errorf("failed to resolve path to %q executable: %v", bin, err) 582 } 583 584 return filepath.EvalSymlinks(lp) 585 }