github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/overlord/snapstate/catalogrefresh.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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 snapstate 21 22 import ( 23 "fmt" 24 "os" 25 "sort" 26 "strings" 27 "time" 28 29 "github.com/snapcore/snapd/advisor" 30 "github.com/snapcore/snapd/dirs" 31 "github.com/snapcore/snapd/logger" 32 "github.com/snapcore/snapd/osutil" 33 "github.com/snapcore/snapd/overlord/auth" 34 "github.com/snapcore/snapd/overlord/state" 35 "github.com/snapcore/snapd/randutil" 36 "github.com/snapcore/snapd/store" 37 "github.com/snapcore/snapd/timings" 38 ) 39 40 var ( 41 catalogRefreshDelayBase = 24 * time.Hour 42 catalogRefreshDelayWithDelta = 24*time.Hour + 1 + randutil.RandomDuration(6*time.Hour) 43 ) 44 45 type catalogRefresh struct { 46 state *state.State 47 48 nextCatalogRefresh time.Time 49 } 50 51 func newCatalogRefresh(st *state.State) *catalogRefresh { 52 return &catalogRefresh{state: st} 53 } 54 55 // Ensure will ensure that the catalog refresh happens 56 func (r *catalogRefresh) Ensure() error { 57 r.state.Lock() 58 defer r.state.Unlock() 59 60 // sneakily don't do anything if in testing 61 if CanAutoRefresh == nil { 62 return nil 63 } 64 65 // if system is not seeded yet, it is first boot situation 66 // do not bother refreshing catalog, snap list is empty anyway 67 // beside there is high change device has no internet 68 var seeded bool 69 err := r.state.Get("seeded", &seeded) 70 if err == state.ErrNoState || !seeded { 71 logger.Debugf("CatalogRefresh:Ensure: skipping refresh, system is not seeded yet") 72 // not seeded yet 73 return nil 74 } 75 76 // similar to the not yet seeded case, on uc20 install mode it doesn't make 77 // sense to refresh the catalog for an ephemeral system 78 deviceCtx, err := DeviceCtx(r.state, nil, nil) 79 if err != nil { 80 // if we are seeded we should have a device context 81 return err 82 } 83 84 if deviceCtx.SystemMode() == "install" { 85 // skip the refresh 86 return nil 87 } 88 89 now := time.Now() 90 delay := catalogRefreshDelayBase 91 if r.nextCatalogRefresh.IsZero() { 92 // try to use the timestamp on the sections file 93 if st, err := os.Stat(dirs.SnapNamesFile); err == nil && st.ModTime().Before(now) { 94 // add the delay with the delta so we spread the load a bit 95 r.nextCatalogRefresh = st.ModTime().Add(catalogRefreshDelayWithDelta) 96 } else { 97 // first time scheduling, add the delta 98 delay = catalogRefreshDelayWithDelta 99 } 100 } 101 102 theStore := Store(r.state, nil) 103 needsRefresh := r.nextCatalogRefresh.IsZero() || r.nextCatalogRefresh.Before(now) 104 105 if !needsRefresh { 106 return nil 107 } 108 109 next := now.Add(delay) 110 // catalog refresh does not carry on trying on error 111 r.nextCatalogRefresh = next 112 113 logger.Debugf("Catalog refresh starting now; next scheduled for %s.", next) 114 115 err = refreshCatalogs(r.state, theStore) 116 switch err { 117 case nil: 118 logger.Debugf("Catalog refresh succeeded.") 119 case store.ErrTooManyRequests: 120 logger.Debugf("Catalog refresh postponed.") 121 err = nil 122 default: 123 logger.Debugf("Catalog refresh failed: %v.", err) 124 } 125 return err 126 } 127 128 var newCmdDB = advisor.Create 129 130 func refreshCatalogs(st *state.State, theStore StoreService) error { 131 st.Unlock() 132 defer st.Lock() 133 134 perfTimings := timings.New(map[string]string{"ensure": "refresh-catalogs"}) 135 136 if err := os.MkdirAll(dirs.SnapCacheDir, 0755); err != nil { 137 return fmt.Errorf("cannot create directory %q: %v", dirs.SnapCacheDir, err) 138 } 139 140 var sections []string 141 var err error 142 timings.Run(perfTimings, "get-sections", "query store for sections", func(tm timings.Measurer) { 143 sections, err = theStore.Sections(auth.EnsureContextTODO(), nil) 144 }) 145 if err != nil { 146 return err 147 } 148 sort.Strings(sections) 149 if err := osutil.AtomicWriteFile(dirs.SnapSectionsFile, []byte(strings.Join(sections, "\n")), 0644, 0); err != nil { 150 return err 151 } 152 153 namesFile, err := osutil.NewAtomicFile(dirs.SnapNamesFile, 0644, 0, osutil.NoChown, osutil.NoChown) 154 if err != nil { 155 return err 156 } 157 defer namesFile.Cancel() 158 159 cmdDB, err := newCmdDB() 160 if err != nil { 161 return err 162 } 163 164 // if all goes well we'll Commit() making this a NOP: 165 defer cmdDB.Rollback() 166 167 timings.Run(perfTimings, "write-catalogs", "query store for catalogs", func(tm timings.Measurer) { 168 err = theStore.WriteCatalogs(auth.EnsureContextTODO(), namesFile, cmdDB) 169 }) 170 if err != nil { 171 return err 172 } 173 174 err1 := namesFile.Commit() 175 err2 := cmdDB.Commit() 176 177 if err2 != nil { 178 return err2 179 } 180 181 st.Lock() 182 perfTimings.Save(st) 183 st.Unlock() 184 185 return err1 186 }