github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/hookstate/hooks.go (about) 1 /* 2 * Copyright (C) 2017 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 package hookstate 19 20 import ( 21 "fmt" 22 "regexp" 23 24 "github.com/snapcore/snapd/i18n" 25 "github.com/snapcore/snapd/overlord/snapstate" 26 "github.com/snapcore/snapd/overlord/state" 27 ) 28 29 func init() { 30 snapstate.SetupInstallHook = SetupInstallHook 31 snapstate.SetupPreRefreshHook = SetupPreRefreshHook 32 snapstate.SetupPostRefreshHook = SetupPostRefreshHook 33 snapstate.SetupRemoveHook = SetupRemoveHook 34 snapstate.SetupGateAutoRefreshHook = SetupGateAutoRefreshHook 35 } 36 37 func SetupInstallHook(st *state.State, snapName string) *state.Task { 38 hooksup := &HookSetup{ 39 Snap: snapName, 40 Hook: "install", 41 Optional: true, 42 } 43 44 summary := fmt.Sprintf(i18n.G("Run install hook of %q snap if present"), hooksup.Snap) 45 task := HookTask(st, summary, hooksup, nil) 46 47 return task 48 } 49 50 func SetupPostRefreshHook(st *state.State, snapName string) *state.Task { 51 hooksup := &HookSetup{ 52 Snap: snapName, 53 Hook: "post-refresh", 54 Optional: true, 55 } 56 57 summary := fmt.Sprintf(i18n.G("Run post-refresh hook of %q snap if present"), hooksup.Snap) 58 return HookTask(st, summary, hooksup, nil) 59 } 60 61 func SetupPreRefreshHook(st *state.State, snapName string) *state.Task { 62 hooksup := &HookSetup{ 63 Snap: snapName, 64 Hook: "pre-refresh", 65 Optional: true, 66 } 67 68 summary := fmt.Sprintf(i18n.G("Run pre-refresh hook of %q snap if present"), hooksup.Snap) 69 task := HookTask(st, summary, hooksup, nil) 70 71 return task 72 } 73 74 func SetupGateAutoRefreshHook(st *state.State, snapName string, base, restart bool) *state.Task { 75 hookSup := &HookSetup{ 76 Snap: snapName, 77 Hook: "gate-auto-refresh", 78 Optional: true, 79 } 80 summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), hookSup.Hook, hookSup.Snap) 81 hookCtx := map[string]interface{}{ 82 "base": base, 83 "restart": restart, 84 } 85 task := HookTask(st, summary, hookSup, hookCtx) 86 return task 87 } 88 89 type snapHookHandler struct { 90 } 91 92 func (h *snapHookHandler) Before() error { 93 return nil 94 } 95 96 func (h *snapHookHandler) Done() error { 97 return nil 98 } 99 100 func (h *snapHookHandler) Error(err error) error { 101 return nil 102 } 103 104 func SetupRemoveHook(st *state.State, snapName string) *state.Task { 105 hooksup := &HookSetup{ 106 Snap: snapName, 107 Hook: "remove", 108 Optional: true, 109 IgnoreError: true, 110 } 111 112 summary := fmt.Sprintf(i18n.G("Run remove hook of %q snap if present"), hooksup.Snap) 113 task := HookTask(st, summary, hooksup, nil) 114 115 return task 116 } 117 118 func setupHooks(hookMgr *HookManager) { 119 handlerGenerator := func(context *Context) Handler { 120 return &snapHookHandler{} 121 } 122 123 hookMgr.Register(regexp.MustCompile("^install$"), handlerGenerator) 124 hookMgr.Register(regexp.MustCompile("^post-refresh$"), handlerGenerator) 125 hookMgr.Register(regexp.MustCompile("^pre-refresh$"), handlerGenerator) 126 hookMgr.Register(regexp.MustCompile("^remove$"), handlerGenerator) 127 hookMgr.Register(regexp.MustCompile("^gate-auto-refresh$"), handlerGenerator) 128 }