github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  	var err error
    58  	baseURL, err = url.Parse(baseurl)
    59  	if err != nil {
    60  		panic(fmt.Sprintf("cannot setup base url: %v", err))
    61  	}
    62  }
    63  
    64  func (c *cmdRun) Execute(args []string) error {
    65  	if err := os.MkdirAll(dirs.SnapRunRepairDir, 0755); err != nil {
    66  		return err
    67  	}
    68  	flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
    69  	if err != nil {
    70  		return err
    71  	}
    72  	err = flock.TryLock()
    73  	if err == osutil.ErrAlreadyLocked {
    74  		return fmt.Errorf("cannot run, another snap-repair run already executing")
    75  	}
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer flock.Unlock()
    80  
    81  	run := NewRunner()
    82  	run.BaseURL = baseURL
    83  	err = run.LoadState()
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	for {
    89  		repair, err := run.Next("canonical")
    90  		if err == ErrRepairNotFound {
    91  			// no more repairs
    92  			break
    93  		}
    94  		if err != nil {
    95  			return err
    96  		}
    97  
    98  		if err := repair.Run(); err != nil {
    99  			return err
   100  		}
   101  	}
   102  	return nil
   103  }