forked from snapcore/snapd
/
cmd_run.go
113 lines (95 loc) · 2.39 KB
/
cmd_run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)
func init() {
const (
short = "Fetch and run repair assertions as necessary for the device"
long = ""
)
if _, err := parser.AddCommand("run", short, long, &cmdRun{}); err != nil {
panic(err)
}
}
type cmdRun struct{}
var baseURL *url.URL
func init() {
var baseurl string
if snapdenv.UseStagingStore() {
baseurl = "https://api.staging.snapcraft.io/v2/"
} else {
baseurl = "https://api.snapcraft.io/v2/"
}
// allow redirecting assertion requests under a different base url
if forcedURL := os.Getenv("SNAPPY_FORCE_SAS_URL"); forcedURL != "" {
baseurl = forcedURL
}
var err error
baseURL, err = url.Parse(baseurl)
if err != nil {
panic(fmt.Sprintf("cannot setup base url: %v", err))
}
}
var rootBrandIDs = []string{"canonical"}
func (c *cmdRun) Execute(args []string) error {
if err := os.MkdirAll(dirs.SnapRunRepairDir, 0755); err != nil {
return err
}
flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
if err != nil {
return err
}
err = flock.TryLock()
if err == osutil.ErrAlreadyLocked {
return fmt.Errorf("cannot run, another snap-repair run already executing")
}
if err != nil {
return err
}
defer flock.Unlock()
run := NewRunner()
run.BaseURL = baseURL
err = run.LoadState()
if err != nil {
return err
}
for _, rootRepairBrandID := range rootBrandIDs {
for {
repair, err := run.Next(rootRepairBrandID)
if err == ErrRepairNotFound {
// no more repairs
break
}
if err != nil {
return err
}
if err := repair.Run(); err != nil {
return err
}
}
}
return nil
}