github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/cmdstate/cmdmgr.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package cmdstate
    21  
    22  import (
    23  	"strings"
    24  	"time"
    25  
    26  	"gopkg.in/tomb.v2"
    27  
    28  	"github.com/snapcore/snapd/osutil"
    29  	"github.com/snapcore/snapd/overlord/state"
    30  )
    31  
    32  // CommandManager helps running arbitrary commands as tasks.
    33  type CommandManager struct{}
    34  
    35  // Manager returns a new CommandManager.
    36  func Manager(st *state.State, runner *state.TaskRunner) *CommandManager {
    37  	runner.AddHandler("exec-command", doExec, nil)
    38  	return &CommandManager{}
    39  }
    40  
    41  // Ensure is part of the overlord.StateManager interface.
    42  func (m *CommandManager) Ensure() error {
    43  	return nil
    44  }
    45  
    46  var defaultExecTimeout = 5 * time.Second
    47  
    48  func doExec(t *state.Task, tomb *tomb.Tomb) error {
    49  	st := t.State()
    50  	st.Lock()
    51  	defer st.Unlock()
    52  
    53  	var ignore bool
    54  	if err := t.Get("ignore", &ignore); err != nil && err != state.ErrNoState {
    55  		return err
    56  	}
    57  	if ignore {
    58  		t.Logf("task ignored")
    59  		return nil
    60  	}
    61  
    62  	var argv []string
    63  	var tout time.Duration
    64  	if err := t.Get("argv", &argv); err != nil {
    65  		return err
    66  	}
    67  
    68  	err := t.Get("timeout", &tout)
    69  	// timeout is optional and might not be set
    70  	if err != nil && err != state.ErrNoState {
    71  		return err
    72  	}
    73  	if err == state.ErrNoState {
    74  		tout = defaultExecTimeout
    75  	}
    76  
    77  	// the command needs to be run with unlocked state, but after that
    78  	// we need to restore the lock (for Errorf, and for deferred unlocking
    79  	// above).
    80  	st.Unlock()
    81  	buf, err := osutil.RunAndWait(argv, nil, tout, tomb)
    82  	st.Lock()
    83  
    84  	if err != nil {
    85  		t.Errorf("# %s\n%s", strings.Join(argv, " "), buf)
    86  		return err
    87  	}
    88  
    89  	return nil
    90  }