Skip to content

Commit

Permalink
Move snap proxy setting to cloud init runcmd rather than bootcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed Aug 19, 2021
1 parent 53902ad commit 096f7d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
36 changes: 32 additions & 4 deletions cloudconfig/cloudinit/cloudinit_ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ func (cfg *ubuntuCloudConfig) addRequiredPackages() {
}
}

var waitSnapSeeded = `
n=1
while true; do
printf "Attempt $n to wait for snapd to be seeded...\n"
snap wait core seed.loaded && break
if [ $n -eq 5 ]; then
echo "snapd not initialised"
break
fi
echo "Wait for snapd failed, retrying in 5s"
sleep 5
n=$((n+1))
done
`[1:]

// Updates proxy settings used when rendering the conf as a script
func (cfg *ubuntuCloudConfig) updateProxySettings(proxyCfg PackageManagerProxyConfig) error {
// Write out the apt proxy settings
Expand All @@ -291,11 +308,20 @@ func (cfg *ubuntuCloudConfig) updateProxySettings(proxyCfg PackageManagerProxyCo
filename))
}

once := false
addWaitSnapSeeded := func() {
if once {
return
}
cfg.AddRunCmd(waitSnapSeeded)
once = true
}
// Write out the snap http/https proxy settings
if snapProxy := proxyCfg.SnapProxy(); (snapProxy != proxy.Settings{}) {
addWaitSnapSeeded()
pkgCmder := cfg.paccmder[jujupackaging.SnapPackageManager]
for _, cmd := range pkgCmder.SetProxyCmds(snapProxy) {
cfg.AddBootCmd(cmd)
cfg.AddRunCmd(cmd)
}
}

Expand All @@ -307,19 +333,21 @@ func (cfg *ubuntuCloudConfig) updateProxySettings(proxyCfg PackageManagerProxyCo
}
logger.Infof("auto-detected snap store assertions from proxy")
logger.Infof("auto-detected snap store ID as %q", storeID)
addWaitSnapSeeded()
cfg.genSnapStoreProxyCmds(assertions, storeID)
} else if proxyCfg.SnapStoreAssertions() != "" && proxyCfg.SnapStoreProxyID() != "" {
addWaitSnapSeeded()
cfg.genSnapStoreProxyCmds(proxyCfg.SnapStoreAssertions(), proxyCfg.SnapStoreProxyID())
}

return nil
}

func (cfg *ubuntuCloudConfig) genSnapStoreProxyCmds(assertions, storeID string) {
cfg.AddBootCmd(fmt.Sprintf(
cfg.AddRunCmd(fmt.Sprintf(
`printf '%%s\n' %s > %s`,
utils.ShQuote(assertions),
"/etc/snap.assertions"))
cfg.AddBootCmd("snap ack /etc/snap.assertions")
cfg.AddBootCmd("snap set core proxy.store=" + storeID)
cfg.AddRunCmd("snap ack /etc/snap.assertions")
cfg.AddRunCmd("snap set core proxy.store=" + storeID)
}
9 changes: 9 additions & 0 deletions cloudconfig/cloudinit/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ = gc.Suite(HelperSuite{})
type fakeCfg struct {
CloudConfig
packageProxySettings proxy.Settings
snapProxySettings proxy.Settings
packageMirror string
addUpdateScripts bool
addUpgradeScripts bool
Expand All @@ -38,6 +39,7 @@ func (f *fakeCfg) addRequiredPackages() {
}
func (f *fakeCfg) updateProxySettings(s PackageManagerProxyConfig) error {
f.packageProxySettings = s.AptProxy()
f.snapProxySettings = s.SnapProxy()
return nil
}

Expand All @@ -50,16 +52,21 @@ func (HelperSuite) TestAddPkgCmdsCommon(c *gc.C) {
Ftp: "ftp",
NoProxy: "noproxy",
}
sps := proxy.Settings{
Http: "snap-http",
}
proxyCfg := packageManagerProxySettings{
aptProxy: pps,
aptMirror: "mirror",
snapProxy: sps,
}

upd, upg := true, true

err := addPackageCommandsCommon(f, proxyCfg, upd, upg, "trusty")
c.Assert(err, gc.IsNil)
c.Assert(f.packageProxySettings, gc.Equals, pps)
c.Assert(f.snapProxySettings, gc.Equals, sps)
c.Assert(f.packageMirror, gc.Equals, proxyCfg.aptMirror)
c.Assert(f.addUpdateScripts, gc.Equals, upd)
c.Assert(f.addUpgradeScripts, gc.Equals, upg)
Expand All @@ -70,6 +77,7 @@ func (HelperSuite) TestAddPkgCmdsCommon(c *gc.C) {
err = addPackageCommandsCommon(f, proxyCfg, upd, upg, "trusty")
c.Assert(err, gc.IsNil)
c.Assert(f.packageProxySettings, gc.Equals, pps)
c.Assert(f.snapProxySettings, gc.Equals, sps)
c.Assert(f.packageMirror, gc.Equals, proxyCfg.aptMirror)
c.Assert(f.addUpdateScripts, gc.Equals, upd)
c.Assert(f.addUpgradeScripts, gc.Equals, upg)
Expand All @@ -80,6 +88,7 @@ func (HelperSuite) TestAddPkgCmdsCommon(c *gc.C) {
err = addPackageCommandsCommon(f, proxyCfg, upd, upg, "precise")
c.Assert(err, gc.IsNil)
c.Assert(f.packageProxySettings, gc.Equals, pps)
c.Assert(f.snapProxySettings, gc.Equals, sps)
c.Assert(f.packageMirror, gc.Equals, proxyCfg.aptMirror)
// for precise we need to override addUpdateScripts to always be true
c.Assert(f.addUpdateScripts, gc.Equals, true)
Expand Down

0 comments on commit 096f7d9

Please sign in to comment.