本文整理汇总了Golang中github.com/snapcore/snapd/overlord/state.State类的典型用法代码示例。如果您正苦于以下问题:Golang State类的具体用法?Golang State怎么用?Golang State使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了State类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: checkChangeConflict
func checkChangeConflict(s *state.State, snapName string, snapst *SnapState) error {
for _, task := range s.Tasks() {
k := task.Kind()
chg := task.Change()
if (k == "link-snap" || k == "unlink-snap") && (chg == nil || !chg.Status().Ready()) {
ss, err := TaskSnapSetup(task)
if err != nil {
return fmt.Errorf("internal error: cannot obtain snap setup from task: %s", task.Summary())
}
if ss.Name() == snapName {
return fmt.Errorf("snap %q has changes in progress", snapName)
}
}
}
if snapst != nil {
// caller wants us to also make sure the SnapState in state
// matches the one they provided. Necessary because we need to
// unlock while talking to the store, during which a change can
// sneak in (if it's before the taskset is created) (e.g. for
// install, while getting the snap info; for refresh, when
// getting what needs refreshing).
var cursnapst SnapState
if err := Get(s, snapName, &cursnapst); err != nil && err != state.ErrNoState {
return err
}
// TODO: implement the rather-boring-but-more-performant SnapState.Equals
if !reflect.DeepEqual(snapst, &cursnapst) {
return fmt.Errorf("snap %q state changed during install preparations", snapName)
}
}
return nil
}
示例2: doFetch
func doFetch(s *state.State, userID int, fetching func(asserts.Fetcher) error) error {
// TODO: once we have a bulk assertion retrieval endpoint this approach will change
user, err := userFromUserID(s, userID)
if err != nil {
return err
}
sto := snapstate.Store(s)
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
// TODO: ignore errors if already in db?
return sto.Assertion(ref.Type, ref.PrimaryKey, user)
}
f := newFetcher(s, retrieve)
s.Unlock()
err = fetching(f)
s.Lock()
if err != nil {
return err
}
// TODO: trigger w. caller a global sanity check if a is revoked
// (but try to save as much possible still),
// or err is a check error
return f.commit()
}
示例3: checkAliasConflict
func checkAliasConflict(st *state.State, snapName, alias string) error {
// check against snaps
var snapNames map[string]*json.RawMessage
err := st.Get("snaps", &snapNames)
if err != nil && err != state.ErrNoState {
return err
}
for name := range snapNames {
if name == alias || strings.HasPrefix(alias, name+".") {
return &aliasConflictError{
Alias: alias,
Snap: snapName,
Reason: fmt.Sprintf("it conflicts with the command namespace of installed snap %q", name),
}
}
}
// check against aliases
return checkAgainstEnabledAliases(st, func(otherAlias, otherSnap string) error {
if otherAlias == alias {
return &aliasConflictError{
Alias: alias,
Snap: snapName,
Reason: fmt.Sprintf("already enabled for %q", otherSnap),
}
}
return nil
})
}
示例4: cachedStore
func cachedStore(s *state.State) StoreService {
ubuntuStore := s.Cached(cachedStoreKey{})
if ubuntuStore == nil {
return nil
}
return ubuntuStore.(StoreService)
}
示例5: Alias
// Alias enables the provided aliases for the snap with the given name.
func Alias(st *state.State, snapName string, aliases []string) (*state.TaskSet, error) {
var snapst SnapState
err := Get(st, snapName, &snapst)
if err == state.ErrNoState {
return nil, fmt.Errorf("cannot find snap %q", snapName)
}
if err != nil {
return nil, err
}
if !snapst.Active {
return nil, fmt.Errorf("enabling aliases for disabled snap %q not supported", snapName)
}
if err := checkChangeConflict(st, snapName, nil); err != nil {
return nil, err
}
snapsup := &SnapSetup{
SideInfo: &snap.SideInfo{RealName: snapName},
}
alias := st.NewTask("alias", fmt.Sprintf(i18n.G("Enable aliases for snap %q"), snapsup.Name()))
alias.Set("snap-setup", &snapsup)
toEnable := map[string]string{}
for _, alias := range aliases {
toEnable[alias] = "enabled"
}
alias.Set("aliases", toEnable)
return state.NewTaskSet(alias), nil
}
示例6: NewUser
// NewUser tracks a new authenticated user and saves its details in the state
func NewUser(st *state.State, username, macaroon string, discharges []string) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err == state.ErrNoState {
authStateData = AuthState{}
} else if err != nil {
return nil, err
}
sort.Strings(discharges)
authStateData.LastID++
authenticatedUser := UserState{
ID: authStateData.LastID,
Username: username,
Macaroon: macaroon,
Discharges: discharges,
StoreMacaroon: macaroon,
StoreDischarges: discharges,
}
authStateData.Users = append(authStateData.Users, authenticatedUser)
st.Set("auth", authStateData)
return &authenticatedUser, nil
}
示例7: Disable
// Disable sets a snap to the inactive state
func Disable(s *state.State, name string) (*state.TaskSet, error) {
var snapst SnapState
err := Get(s, name, &snapst)
if err == state.ErrNoState {
return nil, fmt.Errorf("cannot find snap %q", name)
}
if err != nil {
return nil, err
}
if !snapst.Active {
return nil, fmt.Errorf("snap %q already disabled", name)
}
if err := checkChangeConflict(s, name, nil); err != nil {
return nil, err
}
ss := &SnapSetup{
SideInfo: &snap.SideInfo{
RealName: name,
Revision: snapst.Current,
},
}
stopSnapServices := s.NewTask("stop-snap-services", fmt.Sprintf(i18n.G("Stop snap %q (%s) services"), ss.Name(), snapst.Current))
stopSnapServices.Set("snap-setup", &ss)
unlinkSnap := s.NewTask("unlink-snap", fmt.Sprintf(i18n.G("Make snap %q (%s) unavailable to the system"), ss.Name(), snapst.Current))
unlinkSnap.Set("snap-setup-task", stopSnapServices.ID())
unlinkSnap.WaitFor(stopSnapServices)
return state.NewTaskSet(stopSnapServices, unlinkSnap), nil
}
示例8: allLocalSnapInfos
// allLocalSnapInfos returns the information about the all current snaps and their SnapStates.
func allLocalSnapInfos(st *state.State) ([]aboutSnap, error) {
st.Lock()
defer st.Unlock()
snapStates, err := snapstate.All(st)
if err != nil {
return nil, err
}
about := make([]aboutSnap, 0, len(snapStates))
var firstErr error
for _, snapState := range snapStates {
info, err := snapState.CurrentInfo()
if err != nil {
// XXX: aggregate instead?
if firstErr == nil {
firstErr = err
}
continue
}
about = append(about, aboutSnap{info, snapState})
}
return about, firstErr
}
示例9: CheckMacaroon
// CheckMacaroon returns the UserState for the given macaroon/discharges credentials
func CheckMacaroon(st *state.State, macaroon string, discharges []string) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err != nil {
return nil, ErrInvalidAuth
}
NextUser:
for _, user := range authStateData.Users {
if user.Macaroon != macaroon {
continue
}
if len(user.Discharges) != len(discharges) {
continue
}
// sort discharges (stored users' discharges are already sorted)
sort.Strings(discharges)
for i, d := range user.Discharges {
if d != discharges[i] {
continue NextUser
}
}
return &user, nil
}
return nil, ErrInvalidAuth
}
示例10: patch1
// patch1 adds the snap type and the current revision to the snap state.
func patch1(s *state.State) error {
var stateMap map[string]*snapstate.SnapState
err := s.Get("snaps", &stateMap)
if err == state.ErrNoState {
return nil
}
if err != nil {
return err
}
for snapName, snapState := range stateMap {
seq := snapState.Sequence
if len(seq) == 0 {
continue
}
typ := snap.TypeApp
snapInfo, err := readInfo(snapName, seq[len(seq)-1])
if err != nil {
logger.Noticef("Recording type for snap %q: cannot retrieve info, assuming it's a app: %v", snapName, err)
} else {
logger.Noticef("Recording type for snap %q: setting to %q", snapName, snapInfo.Type)
typ = snapInfo.Type
}
snapState.SetType(typ)
snapState.Current = seq[len(seq)-1].Revision
}
s.Set("snaps", stateMap)
return nil
}
示例11: cachedDB
func cachedDB(s *state.State) *asserts.Database {
db := s.Cached(cachedDBKey{})
if db == nil {
panic("internal error: needing an assertion database before the assertion manager is initialized")
}
return db.(*asserts.Database)
}
示例12: updateInfo
func updateInfo(st *state.State, snapst *SnapState, channel string, userID int, flags Flags) (*snap.Info, error) {
user, err := userFromUserID(st, userID)
if err != nil {
return nil, err
}
curInfo, err := snapst.CurrentInfo()
if err != nil {
return nil, err
}
if curInfo.SnapID == "" { // covers also trymode
return nil, fmt.Errorf("cannot refresh local snap %q", curInfo.Name())
}
refreshCand := &store.RefreshCandidate{
// the desired channel
Channel: channel,
DevMode: flags.DevModeAllowed(),
Block: snapst.Block(),
SnapID: curInfo.SnapID,
Revision: curInfo.Revision,
Epoch: curInfo.Epoch,
}
theStore := Store(st)
st.Unlock() // calls to the store should be done without holding the state lock
res, err := theStore.ListRefresh([]*store.RefreshCandidate{refreshCand}, user)
st.Lock()
if len(res) == 0 {
return nil, fmt.Errorf("snap %q has no updates available", curInfo.Name())
}
return res[0], nil
}
示例13: ResetAliases
// ResetAliases resets the provided aliases for the snap with the given name to their default state, enabled for auto-aliases, disabled otherwise.
func ResetAliases(st *state.State, snapName string, aliases []string) (*state.TaskSet, error) {
var snapst SnapState
err := Get(st, snapName, &snapst)
if err == state.ErrNoState {
return nil, fmt.Errorf("cannot find snap %q", snapName)
}
if err != nil {
return nil, err
}
if !snapst.Active { // TODO: we might want to support this
return nil, fmt.Errorf("resetting aliases to their default state for disabled snap %q not supported", snapName)
}
if err := checkChangeConflict(st, snapName, nil); err != nil {
return nil, err
}
snapsup := &SnapSetup{
SideInfo: &snap.SideInfo{RealName: snapName},
}
alias := st.NewTask("alias", fmt.Sprintf(i18n.G("Reset aliases for snap %q"), snapsup.Name()))
alias.Set("snap-setup", &snapsup)
toReset := map[string]string{}
for _, alias := range aliases {
toReset[alias] = "auto"
}
alias.Set("aliases", toReset)
return state.NewTaskSet(alias), nil
}
示例14: checkSnap
// checkSnap ensures that the snap can be installed.
func checkSnap(st *state.State, snapFilePath string, si *snap.SideInfo, curInfo *snap.Info, flags Flags) error {
// This assumes that the snap was already verified or --dangerous was used.
s, _, err := openSnapFile(snapFilePath, si)
if err != nil {
return err
}
if s.NeedsDevMode() && !flags.DevModeAllowed() {
return fmt.Errorf("snap %q requires devmode or confinement override", s.Name())
}
// verify we have a valid architecture
if !arch.IsSupportedArchitecture(s.Architectures) {
return fmt.Errorf("snap %q supported architectures (%s) are incompatible with this system (%s)", s.Name(), strings.Join(s.Architectures, ", "), arch.UbuntuArchitecture())
}
// check assumes
err = checkAssumes(s)
if err != nil {
return err
}
st.Lock()
defer st.Unlock()
for _, check := range checkSnapCallbacks {
err := check(st, s, curInfo, flags)
if err != nil {
return err
}
}
return nil
}
示例15: Disconnect
// Disconnect returns a set of tasks for disconnecting an interface.
func Disconnect(s *state.State, plugSnap, plugName, slotSnap, slotName string) (*state.TaskSet, error) {
summary := fmt.Sprintf(i18n.G("Disconnect %s:%s from %s:%s"),
plugSnap, plugName, slotSnap, slotName)
task := s.NewTask("disconnect", summary)
task.Set("slot", interfaces.SlotRef{Snap: slotSnap, Name: slotName})
task.Set("plug", interfaces.PlugRef{Snap: plugSnap, Name: plugName})
return state.NewTaskSet(task), nil
}