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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
//go:build !no_workceptor
// +build !no_workceptor
package workceptor
import (
"context"
"fmt"
"os"
"path"
"strconv"
"strings"
"github.com/ansible/receptor/pkg/controlsvc"
)
type workceptorCommandType struct {
w *Workceptor
}
type workceptorCommand struct {
w *Workceptor
subcommand string
params map[string]interface{}
}
func (t *workceptorCommandType) InitFromString(params string) (controlsvc.ControlCommand, error) {
tokens := strings.Split(params, " ")
if len(tokens) == 0 {
return nil, fmt.Errorf("no work subcommand")
}
c := &workceptorCommand{
w: t.w,
subcommand: strings.ToLower(tokens[0]),
params: make(map[string]interface{}),
}
switch c.subcommand {
case "submit":
if len(tokens) < 3 {
return nil, fmt.Errorf("work submit requires a target node and work type")
}
c.params["node"] = tokens[1]
c.params["worktype"] = tokens[2]
if len(tokens) > 3 {
c.params["params"] = strings.Join(tokens[3:], " ")
}
case "list":
if len(tokens) > 1 {
c.params["unitid"] = tokens[1]
}
case "status", "cancel", "release", "force-release":
if len(tokens) < 2 {
return nil, fmt.Errorf("work %s requires a unit ID", c.subcommand)
}
if len(tokens) > 2 {
return nil, fmt.Errorf("work %s does not take parameters after the unit ID", c.subcommand)
}
c.params["unitid"] = tokens[1]
case "results":
if len(tokens) < 2 {
return nil, fmt.Errorf("work results requires a unit ID")
}
if len(tokens) > 3 {
return nil, fmt.Errorf("work results only takes a unit ID and optional start position")
}
c.params["unitid"] = tokens[1]
if len(tokens) > 2 {
var err error
c.params["startpos"], err = strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
return nil, fmt.Errorf("error converting start position to integer: %s", err)
}
} else {
c.params["startpos"] = int64(0)
}
}
return c, nil
}
// strFromMap extracts a string from a map[string]interface{}, handling errors.
func strFromMap(config map[string]interface{}, name string) (string, error) {
value, ok := config[name]
if !ok {
return "", fmt.Errorf("field %s missing", name)
}
valueStr, ok := value.(string)
if !ok {
return "", fmt.Errorf("field %s must be a string", name)
}
return valueStr, nil
}
// intFromMap extracts an int64 from a map[string]interface{}, handling errors.
func intFromMap(config map[string]interface{}, name string) (int64, error) {
value, ok := config[name]
if !ok {
return 0, fmt.Errorf("field %s missing", name)
}
valueInt, ok := value.(int64)
if ok {
return valueInt, nil
}
valueFloat, ok := value.(float64)
if ok {
return int64(valueFloat), nil
}
valueStr, ok := value.(string)
if ok {
valueInt, err := strconv.ParseInt(valueStr, 10, 64)
if err != nil {
return valueInt, err
}
}
return 0, fmt.Errorf("field %s value %s is not convertible to an int", name, value)
}
func boolFromMap(config map[string]interface{}, name string) (bool, error) {
value, ok := config[name]
if !ok {
return false, fmt.Errorf("field %s missing", name)
}
valueBoolStr, ok := value.(string)
if !ok {
return false, fmt.Errorf("field %s must be a string", name)
}
if valueBoolStr == "true" {
return true, nil
}
if valueBoolStr == "false" {
return false, nil
}
return false, fmt.Errorf("field %s value %s is not convertible to a bool", name, value)
}
func (t *workceptorCommandType) InitFromJSON(config map[string]interface{}) (controlsvc.ControlCommand, error) {
subCmd, err := strFromMap(config, "subcommand")
if err != nil {
return nil, err
}
c := &workceptorCommand{
w: t.w,
subcommand: strings.ToLower(subCmd),
params: make(map[string]interface{}),
}
switch c.subcommand {
case "submit":
for k, v := range config {
_, ok := v.(string)
if !ok {
return nil, fmt.Errorf("submit parameters must all be strings and %s is not", k)
}
c.params[k] = v
}
_, err := strFromMap(c.params, "node")
if err != nil {
return nil, err
}
_, err = strFromMap(c.params, "worktype")
if err != nil {
return nil, err
}
case "status", "cancel", "release", "force-release":
c.params["unitid"], err = strFromMap(config, "unitid")
if err != nil {
return nil, err
}
signature, err := strFromMap(config, "signature")
if err == nil {
c.params["signature"] = signature
}
case "list":
unitID, err := strFromMap(config, "unitid")
if err == nil {
c.params["unitid"] = unitID
}
case "results":
c.params["unitid"], err = strFromMap(config, "unitid")
if err != nil {
return nil, err
}
c.params["startpos"], err = intFromMap(config, "startpos")
if err != nil {
return nil, err
}
signature, err := strFromMap(config, "signature")
if err == nil {
c.params["signature"] = signature
}
}
return c, nil
}
func (c *workceptorCommand) processSignature(workType, signature string, connIsUnix, signWork bool) error {
shouldVerifySignature := c.w.ShouldVerifySignature(workType, signWork)
if !shouldVerifySignature && signature != "" {
return fmt.Errorf("work type did not expect a signature")
}
if shouldVerifySignature && !connIsUnix {
err := c.w.VerifySignature(signature)
if err != nil {
return err
}
}
return nil
}
func getSignWorkFromStatus(status *StatusFileData) bool {
red, ok := status.ExtraData.(*RemoteExtraData)
if ok {
return red.SignWork
}
return false
}
// Worker function called by the control service to process a "work" command.
func (c *workceptorCommand) ControlFunc(ctx context.Context, nc controlsvc.NetceptorForControlCommand, cfo controlsvc.ControlFuncOperations) (map[string]interface{}, error) {
addr := cfo.RemoteAddr()
connIsUnix := false
if addr.Network() == "unix" {
connIsUnix = true
}
switch c.subcommand {
case "submit":
workNode, err := strFromMap(c.params, "node")
if err != nil {
return nil, err
}
workType, err := strFromMap(c.params, "worktype")
if err != nil {
return nil, err
}
tlsClient, err := strFromMap(c.params, "tlsclient")
if err != nil {
tlsClient = "" // optional so don't return
}
ttl, err := strFromMap(c.params, "ttl")
if err != nil {
ttl = ""
}
signWork, err := boolFromMap(c.params, "signwork")
if err != nil {
signWork = false
}
signature, err := strFromMap(c.params, "signature")
if err != nil {
signature = ""
}
workUnitID, err := strFromMap(c.params, "workUnitID")
if err != nil {
workUnitID = ""
}
workParams := make(map[string]string)
nonParams := []string{"command", "subcommand", "node", "worktype", "tlsclient", "ttl", "signwork", "signature"}
inNonParams := func(p string) bool {
for _, nonparam := range nonParams {
if p == nonparam {
return true
}
}
return false
}
for k, v := range c.params {
if ok := inNonParams(k); ok {
continue
}
vStr, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%s must be a string", k)
}
workParams[k] = vStr
}
err = c.processSignature(workType, signature, connIsUnix, signWork)
if err != nil {
return nil, err
}
isLocalHost := strings.EqualFold(workNode, "localhost")
var worker WorkUnit
if workNode == nc.NodeID() || isLocalHost {
if ttl != "" {
return nil, fmt.Errorf("ttl option is intended for remote work only")
}
worker, err = c.w.AllocateUnit(workType, workUnitID, workParams)
} else {
worker, err = c.w.AllocateRemoteUnit(workNode, workType, workUnitID, tlsClient, ttl, signWork, workParams)
}
if err != nil {
return nil, err
}
cfr := make(map[string]interface{})
cfr["unitid"] = worker.ID()
stdin, err := os.OpenFile(path.Join(worker.UnitDir(), "stdin"), os.O_CREATE+os.O_WRONLY, 0o600)
if err != nil {
return nil, err
}
worker.UpdateBasicStatus(WorkStatePending, "Waiting for Input Data", 0)
err = cfo.ReadFromConn(fmt.Sprintf("Work unit created with ID %s. Send stdin data and EOF.\n", worker.ID()), stdin, &controlsvc.SocketConnIO{})
if err != nil {
worker.UpdateBasicStatus(WorkStateFailed, fmt.Sprintf("Error reading input data: %s", err), 0)
return nil, err
}
err = stdin.Close()
if err != nil {
worker.UpdateBasicStatus(WorkStateFailed, fmt.Sprintf("Error reading input data: %s", err), 0)
return nil, err
}
worker.UpdateBasicStatus(WorkStatePending, "Starting Worker", 0)
err = worker.Start()
if err != nil && !IsPending(err) {
worker.UpdateBasicStatus(WorkStateFailed, fmt.Sprintf("Error starting worker: %s", err), 0)
return cfr, err
}
if IsPending(err) {
cfr["result"] = "Job Submitted"
} else {
cfr["result"] = "Job Started"
}
return cfr, nil
case "list":
var unitList []string
targetUnitID, ok := c.params["unitid"].(string)
if ok {
unitList = append(unitList, targetUnitID)
} else {
unitList = c.w.ListKnownUnitIDs()
}
cfr := make(map[string]interface{})
for i := range unitList {
unitID := unitList[i]
status, err := c.w.unitStatusForCFR(unitID)
if err != nil {
return nil, err
}
cfr[unitID] = status
}
return cfr, nil
case "status":
unitid, err := strFromMap(c.params, "unitid")
if err != nil {
return nil, err
}
cfr, err := c.w.unitStatusForCFR(unitid)
if err != nil {
return nil, err
}
return cfr, nil
case "cancel", "release", "force-release":
unitid, err := strFromMap(c.params, "unitid")
if err != nil {
return nil, err
}
signature, err := strFromMap(c.params, "signature")
if err != nil {
signature = ""
}
cfr := make(map[string]interface{})
var pendingMsg string
var completeMsg string
if c.subcommand == "cancel" {
pendingMsg = "cancel pending"
completeMsg = "cancelled"
} else {
pendingMsg = "release pending"
completeMsg = "released"
}
unit, err := c.w.findUnit(unitid)
if err != nil {
cfr["unit not found"] = unitid
return cfr, err
}
status := unit.Status()
signWork := getSignWorkFromStatus(status)
err = c.processSignature(status.WorkType, signature, connIsUnix, signWork)
if err != nil {
return nil, err
}
if c.subcommand == "cancel" {
err = unit.Cancel()
} else {
err = unit.Release(c.subcommand == "force-release")
}
if err != nil && !IsPending(err) {
return nil, err
}
if IsPending(err) {
cfr[pendingMsg] = unitid
} else {
cfr[completeMsg] = unitid
}
return cfr, nil
case "results":
unitid, err := strFromMap(c.params, "unitid")
if err != nil {
return nil, err
}
startPos, err := intFromMap(c.params, "startpos")
if err != nil {
return nil, err
}
signature, err := strFromMap(c.params, "signature")
if err != nil {
signature = ""
}
unit, err := c.w.findUnit(unitid)
if err != nil {
return nil, err
}
status := unit.Status()
signWork := getSignWorkFromStatus(status)
err = c.processSignature(status.WorkType, signature, connIsUnix, signWork)
if err != nil {
return nil, err
}
resultChan, err := c.w.GetResults(ctx, unitid, startPos)
if err != nil {
return nil, err
}
err = cfo.WriteToConn(fmt.Sprintf("Streaming results for work unit %s\n", unitid), resultChan)
if err != nil {
return nil, err
}
err = cfo.Close()
if err != nil {
return nil, err
}
return nil, nil
}
return nil, fmt.Errorf("bad command")
}
|