github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  }
    35  
    36  func SetupInstallHook(st *state.State, snapName string) *state.Task {
    37  	hooksup := &HookSetup{
    38  		Snap:     snapName,
    39  		Hook:     "install",
    40  		Optional: true,
    41  	}
    42  
    43  	summary := fmt.Sprintf(i18n.G("Run install hook of %q snap if present"), hooksup.Snap)
    44  	task := HookTask(st, summary, hooksup, nil)
    45  
    46  	return task
    47  }
    48  
    49  func SetupPostRefreshHook(st *state.State, snapName string) *state.Task {
    50  	hooksup := &HookSetup{
    51  		Snap:     snapName,
    52  		Hook:     "post-refresh",
    53  		Optional: true,
    54  	}
    55  
    56  	summary := fmt.Sprintf(i18n.G("Run post-refresh hook of %q snap if present"), hooksup.Snap)
    57  	return HookTask(st, summary, hooksup, nil)
    58  }
    59  
    60  func SetupPreRefreshHook(st *state.State, snapName string) *state.Task {
    61  	hooksup := &HookSetup{
    62  		Snap:     snapName,
    63  		Hook:     "pre-refresh",
    64  		Optional: true,
    65  	}
    66  
    67  	summary := fmt.Sprintf(i18n.G("Run pre-refresh hook of %q snap if present"), hooksup.Snap)
    68  	task := HookTask(st, summary, hooksup, nil)
    69  
    70  	return task
    71  }
    72  
    73  type snapHookHandler struct {
    74  }
    75  
    76  func (h *snapHookHandler) Before() error {
    77  	return nil
    78  }
    79  
    80  func (h *snapHookHandler) Done() error {
    81  	return nil
    82  }
    83  
    84  func (h *snapHookHandler) Error(err error) error {
    85  	return nil
    86  }
    87  
    88  func SetupRemoveHook(st *state.State, snapName string) *state.Task {
    89  	hooksup := &HookSetup{
    90  		Snap:        snapName,
    91  		Hook:        "remove",
    92  		Optional:    true,
    93  		IgnoreError: true,
    94  	}
    95  
    96  	summary := fmt.Sprintf(i18n.G("Run remove hook of %q snap if present"), hooksup.Snap)
    97  	task := HookTask(st, summary, hooksup, nil)
    98  
    99  	return task
   100  }
   101  
   102  func setupHooks(hookMgr *HookManager) {
   103  	handlerGenerator := func(context *Context) Handler {
   104  		return &snapHookHandler{}
   105  	}
   106  
   107  	hookMgr.Register(regexp.MustCompile("^install$"), handlerGenerator)
   108  	hookMgr.Register(regexp.MustCompile("^post-refresh$"), handlerGenerator)
   109  	hookMgr.Register(regexp.MustCompile("^pre-refresh$"), handlerGenerator)
   110  	hookMgr.Register(regexp.MustCompile("^remove$"), handlerGenerator)
   111  }