github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap-repair/cmd_run.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017-2020 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  	"github.com/snapcore/snapd/snapdenv"
    31  )
    32  
    33  func init() {
    34  	const (
    35  		short = "Fetch and run repair assertions as necessary for the device"
    36  		long  = ""
    37  	)
    38  
    39  	if _, err := parser.AddCommand("run", short, long, &cmdRun{}); err != nil {
    40  		panic(err)
    41  	}
    42  
    43  }
    44  
    45  type cmdRun struct{}
    46  
    47  var baseURL *url.URL
    48  
    49  func init() {
    50  	var baseurl string
    51  	if snapdenv.UseStagingStore() {
    52  		baseurl = "https://api.staging.snapcraft.io/v2/"
    53  	} else {
    54  		baseurl = "https://api.snapcraft.io/v2/"
    55  	}
    56  
    57  	// allow redirecting assertion requests under a different base url
    58  	if forcedURL := os.Getenv("SNAPPY_FORCE_SAS_URL"); forcedURL != "" {
    59  		baseurl = forcedURL
    60  	}
    61  
    62  	var err error
    63  	baseURL, err = url.Parse(baseurl)
    64  	if err != nil {
    65  		panic(fmt.Sprintf("cannot setup base url: %v", err))
    66  	}
    67  }
    68  
    69  var rootBrandIDs = []string{"canonical"}
    70  
    71  func (c *cmdRun) Execute(args []string) error {
    72  	if err := os.MkdirAll(dirs.SnapRunRepairDir, 0755); err != nil {
    73  		return err
    74  	}
    75  	flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
    76  	if err != nil {
    77  		return err
    78  	}
    79  	err = flock.TryLock()
    80  	if err == osutil.ErrAlreadyLocked {
    81  		return fmt.Errorf("cannot run, another snap-repair run already executing")
    82  	}
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer flock.Unlock()
    87  
    88  	run := NewRunner()
    89  	run.BaseURL = baseURL
    90  	err = run.LoadState()
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	for _, rootRepairBrandID := range rootBrandIDs {
    96  		for {
    97  			repair, err := run.Next(rootRepairBrandID)
    98  			if err == ErrRepairNotFound {
    99  				// no more repairs
   100  				break
   101  			}
   102  			if err != nil {
   103  				return err
   104  			}
   105  
   106  			if err := repair.Run(); err != nil {
   107  				return err
   108  			}
   109  		}
   110  	}
   111  
   112  	return nil
   113  }