github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/biz/impl/runtime.go (about) 1 // Ultimate Provisioner: UP cmd 2 // Copyright (c) 2019 Stephen Cheng and contributors 3 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 7 8 package impl 9 10 import ( 11 "github.com/mohae/deepcopy" 12 "github.com/upcmd/up/model/core" 13 "github.com/upcmd/up/model/stack" 14 "github.com/upcmd/up/utils" 15 u "github.com/upcmd/up/utils" 16 "strings" 17 ) 18 19 var ( 20 TaskerStack = stack.New("tasker") 21 UpRunTimeVars = core.NewCache() 22 BaseDir string 23 ) 24 25 const ( 26 UP_RUNTIME_TASK_LAYER_NUMBER = "up_runtime_task_layer_number" 27 UP_RUNTIME_TASKER_LAYER_NUMBER = "up_runtime_tasker_layer_number" 28 UP_RUNTIME_TASK_PIPE_IN_CONTENT = "up_runtime_task_pipe_in_content" 29 //accessible only during the deferred finally processing 30 UP_RUNTIME_SHELL_EXEC_RESULT = "up_runtime_shell_exec_result" 31 ) 32 33 type TaskRuntimeContext struct { 34 Taskname string 35 TasknameLayered string 36 ExecbaseVars *core.Cache 37 TaskVars *core.Cache 38 ReturnVars *core.Cache 39 IsCalledExternally bool 40 } 41 42 func TaskerRuntime() *TaskerRuntimeContext { 43 return TaskerStack.GetTop().(*TaskerRuntimeContext) 44 } 45 46 func TaskFinallyStack() *stack.ExecStack { 47 return TaskerRuntime().Tasker.FinallyStack 48 } 49 50 func TaskRuntime() *TaskRuntimeContext { 51 if TaskerRuntime().Tasker.InFinalExec { 52 if TaskFinallyStack() != nil { 53 top := TaskFinallyStack().GetTop() 54 if top != nil { 55 return top.(*TaskRuntimeContext) 56 } 57 } 58 } else { 59 if taskStack := TaskerRuntime().Tasker.TaskStack; taskStack != nil { 60 top := taskStack.GetTop() 61 if top != nil { 62 return top.(*TaskRuntimeContext) 63 } 64 } 65 } 66 return nil 67 } 68 69 func SetDryrun() { 70 TaskerRuntime().Tasker.Dryrun = true 71 } 72 73 func SetBaseDir(dir string) { 74 BaseDir = dir 75 } 76 77 func GetBaseModuleName() string { 78 return u.MainConfig.ModuleName 79 } 80 81 type StepRuntimeContext struct { 82 Stepname string 83 Result *u.ExecResult 84 ContextVars *core.Cache 85 DataSyncInDvarExpand TransientSyncFunc 86 Timeout int 87 Flags *[]string 88 } 89 90 func StepRuntime() *StepRuntimeContext { 91 stack := StepStack() 92 if stack.GetLen() > 0 { 93 return stack.GetTop().(*StepRuntimeContext) 94 } else { 95 return nil 96 } 97 } 98 99 func StepStack() *stack.ExecStack { 100 return TaskerRuntime().Tasker.StepStack 101 } 102 103 func GetVault() *core.Cache { 104 return TaskerRuntime().Tasker.SecretVars 105 } 106 107 type BlockRuntimeContext struct { 108 BlockBaseVars *core.Cache 109 } 110 111 func BlockStack() *stack.ExecStack { 112 return TaskerRuntime().Tasker.BlockStack 113 } 114 115 func ConfigRuntime() *utils.UpConfig { 116 return TaskerRuntime().Tasker.Config 117 } 118 119 func debugVault() { 120 u.Ppmsg("Vault", GetVault()) 121 } 122 123 func secureCache(cache *core.Cache) *core.Cache { 124 tmpCache := deepcopy.Copy(*cache).(core.Cache) 125 for k, _ := range tmpCache { 126 if strings.HasPrefix(k, "secure_") { 127 tmpCache.Delete(k) 128 } 129 } 130 return &tmpCache 131 } 132 133 func debugVars() { 134 u.PlnBlue("-debug vars-") 135 136 u.Ppmsg("UpRunTimeVars", UpRunTimeVars) 137 u.Ppmsg("RuntimeVarsAndDvarsMerged", TaskerRuntime().Tasker.RuntimeVarsAndDvarsMerged) 138 139 if taskRuntime := TaskRuntime(); taskRuntime != nil { 140 u.Ppmsg("ExecbaseVars", taskRuntime.ExecbaseVars) 141 u.Ppmsg("TaskVars", taskRuntime.TaskVars) 142 } 143 144 if stepRuntime := StepRuntime(); stepRuntime != nil { 145 u.Ppmsg("ExecContextVars", stepRuntime.ContextVars) 146 } else { 147 u.PlnInfo("ExecContextVars is nil") 148 } 149 u.PlnBlue("--") 150 }