many: move network initialization to a separate service. #1765

Closed
wants to merge 3 commits into
from
@@ -0,0 +1,45 @@
+// -*- Mode: Go; indent-tabs-mode: t -*-
+
+/*
+ * Copyright (C) 2016 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 (
+ "github.com/jessevdk/go-flags"
+
+ "github.com/snapcore/snapd/overlord/boot"
+)
+
+type cmdInternalInitNetwork struct{}
+
+func init() {
+ cmd := addCommand("init-network",
+ "internal",
+ "internal", func() flags.Commander {
+ return &cmdInternalInitNetwork{}
+ })
+ cmd.hidden = true
+}
+
+func (x *cmdInternalInitNetwork) Execute(args []string) error {
+ if len(args) > 0 {
+ return ErrExtraArgs
+ }
+
+ return boot.InitialNetworkConfig()
+}
View
@@ -42,6 +42,10 @@ override_dh_systemd_enable:
dh_systemd_enable \
-pubuntu-core-snapd-units \
snapd.firstboot.service
+ # enable the init-network service
+ dh_systemd_enable \
+ -pubuntu-core-snapd-units \
+ snapd.initnetwork.service
# we want the auto-update timer enabled by default
dh_systemd_enable \
-psnapd \
@@ -0,0 +1,14 @@
+[Unit]
+Description=Apply default network configuration if none exists
+After=local-fs.target
+Before=network-pre.target snapd.service
+DefaultDependencies=false
+ConditionPathExistsGlob=!/etc/netplan/*.yaml
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/snap init-network
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
@@ -1,2 +1,3 @@
debian/snapd.boot-ok.service /lib/systemd/system/
debian/snapd.firstboot.service /lib/systemd/system/
+debian/snapd.initnetwork.service /lib/systemd/system/
View
@@ -21,7 +21,6 @@ package firstboot
import (
"os"
- "os/exec"
"path/filepath"
"github.com/snapcore/snapd/dirs"
@@ -44,35 +43,3 @@ func StampFirstBoot() error {
return osutil.AtomicWriteFile(dirs.SnapFirstBootStamp, []byte{}, 0644, 0)
}
-
-var netplanConfigFile = "/etc/netplan/00-initial-config.yaml"
-var enableConfig = []string{"netplan", "apply"}
-
-var netplanConfigData = `
-network:
- version: 2
- ethernets:
- all:
- match:
- name: "*"
- dhcp4: true
-`
-
-// InitialNetworkConfig writes and applies a netplan config that
-// enables dhcp on all wired interfaces. In the long run this should
-// be run as part of the config-changed hook and read the snap's
-// config to determine the netplan config to write.
-func InitialNetworkConfig() error {
- if err := osutil.AtomicWriteFile(netplanConfigFile, []byte(netplanConfigData), 0644, 0); err != nil {
- return err
- }
-
- enable := exec.Command(enableConfig[0], enableConfig[1:]...)
- enable.Stdout = os.Stdout
- enable.Stderr = os.Stderr
- if err := enable.Run(); err != nil {
- return err
- }
-
- return nil
-}
@@ -28,7 +28,6 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/firstboot"
- "github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/assertstate"
@@ -214,9 +213,6 @@ func FirstBoot() error {
if firstboot.HasRun() {
return ErrNotFirstBoot
}
- if err := firstboot.InitialNetworkConfig(); err != nil {
- logger.Noticef("Failed during inital network configuration: %s", err)
- }
// snappy will be in a very unhappy state if this happens,
// because populateStateFromSeed will error if there
@@ -0,0 +1,49 @@
+package boot
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+
+ "github.com/snapcore/snapd/osutil"
+)
+
+var netplanConfigFile = "/run/netplan/00-initial-config.yaml"
+var enableConfig = []string{"netplan", "apply"}
+
+var netplanConfigData = `
+network:
+ version: 2
+ ethernets:
+ all:
+ match:
+ name: "*"
+ dhcp4: true
+`
+
+var osRemove = os.Remove
+
+// InitialNetworkConfig writes and applies a netplan config that
+// enables dhcp on all wired interfaces. In the long run this should
+// be run as part of the config-changed hook and read the snap's
+// config to determine the netplan config to write.
+func InitialNetworkConfig() error {
+ if err := os.MkdirAll(filepath.Dir(netplanConfigFile), 0755); err != nil {
+ return err
+ }
+ if err := osutil.AtomicWriteFile(netplanConfigFile, []byte(netplanConfigData), 0644, 0); err != nil {
+ return err
+ }
+
+ enable := exec.Command(enableConfig[0], enableConfig[1:]...)
+ enable.Stdout = os.Stdout
+ enable.Stderr = os.Stderr
+ if err := enable.Run(); err != nil {
+ return err
+ }
+ if err := osRemove(netplanConfigFile); err != nil {
+ return err
+ }
+
+ return nil
+}
@@ -17,59 +17,60 @@
*
*/
-package firstboot
+package boot
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
- "testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
)
-func TestStore(t *testing.T) { TestingT(t) }
-
-type FirstBootTestSuite struct {
+type InitialNetworkConfigTestSuite struct {
netplanConfigFile string
enableConfig []string
+ removedFiles []string
}
-var _ = Suite(&FirstBootTestSuite{})
+var _ = Suite(&InitialNetworkConfigTestSuite{})
-func (s *FirstBootTestSuite) SetUpTest(c *C) {
+func (s *InitialNetworkConfigTestSuite) SetUpTest(c *C) {
tempdir := c.MkDir()
dirs.SetRootDir(tempdir)
s.netplanConfigFile = netplanConfigFile
netplanConfigFile = filepath.Join(c.MkDir(), "config.yaml")
s.enableConfig = enableConfig
enableConfig = []string{"/bin/true"}
+ osRemove = func(name string) error { s.removedFiles = append(s.removedFiles, name); return nil }
}
-func (s *FirstBootTestSuite) TearDownTest(c *C) {
+func (s *InitialNetworkConfigTestSuite) TearDownTest(c *C) {
netplanConfigFile = s.netplanConfigFile
enableConfig = s.enableConfig
+ osRemove = os.Remove
}
-func (s *FirstBootTestSuite) TestInitialNetworkConfig(c *C) {
+func (s *InitialNetworkConfigTestSuite) TestInitialNetworkConfig(c *C) {
c.Check(InitialNetworkConfig(), IsNil)
bs, err := ioutil.ReadFile(netplanConfigFile)
c.Assert(err, IsNil)
c.Check(string(bs), Equals, netplanConfigData)
+ c.Assert(s.removedFiles, DeepEquals, []string{netplanConfigFile})
}
-func (s *FirstBootTestSuite) TestInitialNetworkConfigBadPath(c *C) {
+func (s *InitialNetworkConfigTestSuite) TestInitialNetworkConfigBadPath(c *C) {
netplanConfigFile = "/no/such/thing"
err := InitialNetworkConfig()
c.Check(err, NotNil)
- c.Check(os.IsNotExist(err), Equals, true)
+ c.Check(os.IsPermission(err), Equals, true)
}
-func (s *FirstBootTestSuite) TestInitialNetworkConfigEnableFails(c *C) {
+func (s *InitialNetworkConfigTestSuite) TestInitialNetworkConfigEnableFails(c *C) {
enableConfig = []string{"/bin/false"}
err := InitialNetworkConfig()
c.Check(err, NotNil)