github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap-repair/cmd_run.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 main
    21  
    22  import (
    23  	"fmt"
    24  	"net/url"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"github.com/snapcore/snapd/dirs"
    29  	"github.com/snapcore/snapd/osutil"
    30  )
    31  
    32  func init() {
    33  	const (
    34  		short = "Fetch and run repair assertions as necessary for the device"
    35  		long  = ""
    36  	)
    37  
    38  	if _, err := parser.AddCommand("run", short, long, &cmdRun{}); err != nil {
    39  		panic(err)
    40  	}
    41  
    42  }
    43  
    44  type cmdRun struct{}
    45  
    46  var baseURL *url.URL
    47  
    48  func init() {
    49  	var baseurl string
    50  	if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
    51  		baseurl = "https://api.staging.snapcraft.io/v2/"
    52  	} else {
    53  		baseurl = "https://api.snapcraft.io/v2/"
    54  	}
    55  
    56  	var err error
    57  	baseURL, err = url.Parse(baseurl)
    58  	if err != nil {
    59  		panic(fmt.Sprintf("cannot setup base url: %v", err))
    60  	}
    61  }
    62  
    63  func (c *cmdRun) Execute(args []string) error {
    64  	if err := os.MkdirAll(dirs.SnapRunRepairDir, 0755); err != nil {
    65  		return err
    66  	}
    67  	flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
    68  	if err != nil {
    69  		return err
    70  	}
    71  	err = flock.TryLock()
    72  	if err == osutil.ErrAlreadyLocked {
    73  		return fmt.Errorf("cannot run, another snap-repair run already executing")
    74  	}
    75  	if err != nil {
    76  		return err
    77  	}
    78  	defer flock.Unlock()
    79  
    80  	run := NewRunner()
    81  	run.BaseURL = baseURL
    82  	err = run.LoadState()
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	for {
    88  		repair, err := run.Next("canonical")
    89  		if err == ErrRepairNotFound {
    90  			// no more repairs
    91  			break
    92  		}
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		if err := repair.Run(); err != nil {
    98  			return err
    99  		}
   100  	}
   101  	return nil
   102  }