github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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 now := time.Now() 77 delay := catalogRefreshDelayBase 78 if r.nextCatalogRefresh.IsZero() { 79 // try to use the timestamp on the sections file 80 if st, err := os.Stat(dirs.SnapNamesFile); err == nil && st.ModTime().Before(now) { 81 // add the delay with the delta so we spread the load a bit 82 r.nextCatalogRefresh = st.ModTime().Add(catalogRefreshDelayWithDelta) 83 } else { 84 // first time scheduling, add the delta 85 delay = catalogRefreshDelayWithDelta 86 } 87 } 88 89 theStore := Store(r.state, nil) 90 needsRefresh := r.nextCatalogRefresh.IsZero() || r.nextCatalogRefresh.Before(now) 91 92 if !needsRefresh { 93 return nil 94 } 95 96 next := now.Add(delay) 97 // catalog refresh does not carry on trying on error 98 r.nextCatalogRefresh = next 99 100 logger.Debugf("Catalog refresh starting now; next scheduled for %s.", next) 101 102 err = refreshCatalogs(r.state, theStore) 103 switch err { 104 case nil: 105 logger.Debugf("Catalog refresh succeeded.") 106 case store.ErrTooManyRequests: 107 logger.Debugf("Catalog refresh postponed.") 108 err = nil 109 default: 110 logger.Debugf("Catalog refresh failed: %v.", err) 111 } 112 return err 113 } 114 115 var newCmdDB = advisor.Create 116 117 func refreshCatalogs(st *state.State, theStore StoreService) error { 118 st.Unlock() 119 defer st.Lock() 120 121 perfTimings := timings.New(map[string]string{"ensure": "refresh-catalogs"}) 122 123 if err := os.MkdirAll(dirs.SnapCacheDir, 0755); err != nil { 124 return fmt.Errorf("cannot create directory %q: %v", dirs.SnapCacheDir, err) 125 } 126 127 var sections []string 128 var err error 129 timings.Run(perfTimings, "get-sections", "query store for sections", func(tm timings.Measurer) { 130 sections, err = theStore.Sections(auth.EnsureContextTODO(), nil) 131 }) 132 if err != nil { 133 return err 134 } 135 sort.Strings(sections) 136 if err := osutil.AtomicWriteFile(dirs.SnapSectionsFile, []byte(strings.Join(sections, "\n")), 0644, 0); err != nil { 137 return err 138 } 139 140 namesFile, err := osutil.NewAtomicFile(dirs.SnapNamesFile, 0644, 0, osutil.NoChown, osutil.NoChown) 141 if err != nil { 142 return err 143 } 144 defer namesFile.Cancel() 145 146 cmdDB, err := newCmdDB() 147 if err != nil { 148 return err 149 } 150 151 // if all goes well we'll Commit() making this a NOP: 152 defer cmdDB.Rollback() 153 154 timings.Run(perfTimings, "write-catalogs", "query store for catalogs", func(tm timings.Measurer) { 155 err = theStore.WriteCatalogs(auth.EnsureContextTODO(), namesFile, cmdDB) 156 }) 157 if err != nil { 158 return err 159 } 160 161 err1 := namesFile.Commit() 162 err2 := cmdDB.Commit() 163 164 if err2 != nil { 165 return err2 166 } 167 168 st.Lock() 169 perfTimings.Save(st) 170 st.Unlock() 171 172 return err1 173 }