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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 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 (
"errors"
"fmt"
"strings"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/strutil"
)
type cmdValidate struct {
Monitor bool `long:"monitor"`
Enforce bool `long:"enforce"`
Forget bool `long:"forget"`
Refresh bool `long:"refresh"`
Positional struct {
ValidationSet string `positional-arg-name:"<validation-set>"`
} `positional-args:"yes"`
colorMixin
waitMixin
}
var shortValidateHelp = i18n.G("List or apply validation sets")
var longValidateHelp = i18n.G(`
The validate command lists or applies validation sets that state which snaps
are required or permitted to be installed together, optionally constrained to
fixed revisions.
A validation set can either be in monitoring mode, in which case its constraints
aren't enforced, or in enforcing mode, in which case snapd will not allow
operations which would result in snaps breaking the validation set's constraints.
`)
func init() {
addCommand("validate", shortValidateHelp, longValidateHelp, func() flags.Commander { return &cmdValidate{} }, waitDescs.also(colorDescs.also(map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"monitor": i18n.G("Monitor the given validations set"),
// TRANSLATORS: This should not start with a lowercase letter.
"enforce": i18n.G("Enforce the given validation set"),
// TRANSLATORS: This should not start with a lowercase letter.
"forget": i18n.G("Forget the given validation set"),
// TRANSLATORS: This should not start with a lowercase letter.
"refresh": i18n.G("Refresh or install snaps to satisfy enforced validation sets"),
})), []argDesc{{
// TRANSLATORS: This needs to begin with < and end with >
name: i18n.G("<validation-set>"),
// TRANSLATORS: This should not start with a lowercase letter.
desc: i18n.G("Validation set with an optional pinned sequence point, i.e. account-id/name[=seq]"),
}})
}
func fmtValid(res *client.ValidationSetResult) string {
if res.Valid {
return "valid"
}
return "invalid"
}
func fmtValidationSet(res *client.ValidationSetResult) string {
if res.PinnedAt == 0 {
return fmt.Sprintf("%s/%s", res.AccountID, res.Name)
}
return fmt.Sprintf("%s/%s=%d", res.AccountID, res.Name, res.PinnedAt)
}
func (cmd *cmdValidate) Execute(args []string) error {
// check that only one action is used at a time
var action string
for _, a := range []struct {
name string
set bool
}{
{"monitor", cmd.Monitor},
{"enforce", cmd.Enforce},
{"forget", cmd.Forget},
} {
if a.set {
if action != "" {
return fmt.Errorf("cannot use --%s and --%s together", action, a.name)
}
action = a.name
}
}
if cmd.Refresh && !cmd.Enforce {
return fmt.Errorf("--refresh can only be used together with --enforce")
}
if cmd.Positional.ValidationSet == "" && action != "" {
return fmt.Errorf("missing validation set argument")
}
var accountID, name string
var seq int
var err error
if cmd.Positional.ValidationSet != "" {
accountID, name, seq, err = snapasserts.ParseValidationSet(cmd.Positional.ValidationSet)
if err != nil {
return err
}
}
if action != "" {
if cmd.Refresh {
changeID, err := cmd.client.RefreshMany(nil, nil, &client.SnapOptions{
ValidationSets: []string{cmd.Positional.ValidationSet},
})
if err != nil {
return err
}
chg, err := cmd.wait(changeID)
if err != nil {
if err == noWait {
return nil
}
return err
}
var names []string
if err := chg.Get("snap-names", &names); err != nil && !errors.Is(err, client.ErrNoData) {
return err
}
if len(names) != 0 {
fmt.Fprintf(Stdout, i18n.G("Refreshed/installed snaps %s to enforce validation set %q\n"), strutil.Quoted(names), cmd.Positional.ValidationSet)
} else {
fmt.Fprintf(Stdout, i18n.G("Enforced validation set %q\n"), cmd.Positional.ValidationSet)
}
return nil
}
// forget
if cmd.Forget {
return cmd.client.ForgetValidationSet(accountID, name, seq)
}
// apply
opts := &client.ValidateApplyOptions{
Mode: action,
Sequence: seq,
}
res, err := cmd.client.ApplyValidationSet(accountID, name, opts)
if err != nil {
return err
}
// only print valid/invalid status for monitor mode; enforce fails with an error if invalid
// and otherwise has no output.
if action == "monitor" {
fmt.Fprintln(Stdout, fmtValid(res))
}
return nil
}
// no validation set argument, print list with extended info
if cmd.Positional.ValidationSet == "" {
vsets, err := cmd.client.ListValidationsSets()
if err != nil {
return err
}
if len(vsets) == 0 {
fmt.Fprintln(Stderr, i18n.G("No validations are available"))
return nil
}
esc := cmd.getEscapes()
w := tabWriter()
// TRANSLATORS: the %s is to insert a filler escape sequence (please keep it flush to the column header, with no extra spaces)
fmt.Fprintf(w, i18n.G("Validation\tMode\tSeq\tCurrent\t%s\tNotes\n"), fillerPublisher(esc))
for _, res := range vsets {
// TODO: fill notes when've clarity about them
var notes string
// doing it this way because otherwise it's a sea of %s\t%s\t%s
line := []string{
fmtValidationSet(res),
res.Mode,
fmt.Sprintf("%d", res.Sequence),
fmtValid(res),
notes,
}
fmt.Fprintln(w, strings.Join(line, "\t"))
}
w.Flush()
} else {
vset, err := cmd.client.ValidationSet(accountID, name, seq)
if err != nil {
return err
}
fmt.Fprintln(Stdout, fmtValid(vset))
// XXX: exit status 1 if invalid?
}
return nil
}
|