Skip to content

Commit

Permalink
use provisioning to find out about developer mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Jun 10, 2015
1 parent 1dcb323 commit 89dfaa7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
18 changes: 18 additions & 0 deletions provisioning/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,21 @@ func IsSideLoaded(bootloaderDir string) bool {

return false
}

// InDeveloperMode returns true if the image was build with --developer-mode
func InDeveloperMode(bootloaderDir string) bool {
file := filepath.Join(bootloaderDir, InstallYamlFile)

if !helpers.FileExists(file) {
// no idea
return false
}

InstallYaml, err := parseInstallYaml(file)
if err != nil {
// no idea
return false
}

return InstallYaml.InstallOptions.DeveloperMode
}
18 changes: 18 additions & 0 deletions provisioning/provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ func (ts *ProvisioningTestSuite) TestParseInstallYamlData(c *C) {
func (ts *ProvisioningTestSuite) TestInDeveloperModeEmpty(c *C) {
c.Assert(InDeveloperMode(""), Equals, false)
}

func (ts *ProvisioningTestSuite) TestInDeveloperModeWithDevModeOn(c *C) {
err := ioutil.WriteFile(ts.mockYamlFile, []byte(`
options:
developer-mode: true
`), 0644)
c.Assert(err, IsNil)
c.Assert(InDeveloperMode(ts.mockBootDir), Equals, true)
}

func (ts *ProvisioningTestSuite) TestInDeveloperModeWithDevModeOff(c *C) {
err := ioutil.WriteFile(ts.mockYamlFile, []byte(`
options:
developer-mode: false
`), 0644)
c.Assert(err, IsNil)
c.Assert(InDeveloperMode(ts.mockBootDir), Equals, false)
}
28 changes: 3 additions & 25 deletions snappy/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
package snappy

import (
"io/ioutil"
"os"
"sort"
"strings"

"launchpad.net/snappy/progress"
"launchpad.net/snappy/provisioning"
)

// InstallFlags can be used to pass additional flags to the install of a
Expand All @@ -43,28 +42,6 @@ const (
AllowOEM
)

// check if the image is in developer mode
// FIXME: this is a bit crude right now, but it seems like there is not more
// meta-data to check right now
// TODO: add feature to ubuntu-device-flash to write better info file when
// the image is in developer mode
func inDeveloperMode() bool {
f, err := os.Open(cloudMetaDataFile)
if err != nil {
return false
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return false
}
needle := "public-keys:\n"
if strings.Contains(string(data), needle) {
return true
}
return false
}

// Install the givens snap names provided via args. This can be local
// files or snaps that are queried from the store
func Install(name string, flags InstallFlags, meter progress.Meter) (string, error) {
Expand All @@ -87,7 +64,8 @@ func doInstall(name string, flags InstallFlags, meter progress.Meter) (snapName
if fi, err := os.Stat(name); err == nil && fi.Mode().IsRegular() {
// we allow unauthenticated package when in developer
// mode
if inDeveloperMode() {
p := newPartition()
if provisioning.InDeveloperMode(p.BootloaderDir()) {
flags |= AllowUnauthenticated
}

Expand Down
15 changes: 0 additions & 15 deletions snappy/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,6 @@ func makeCloudInitMetaData(c *C, content string) string {
return w.Name()
}

func (s *SnapTestSuite) TestNotInDeveloperMode(c *C) {
cloudMetaDataFile = makeCloudInitMetaData(c, `instance-id: nocloud-static`)
defer os.Remove(cloudMetaDataFile)
c.Assert(inDeveloperMode(), Equals, false)
}

func (s *SnapTestSuite) TestInDeveloperMode(c *C) {
cloudMetaDataFile = makeCloudInitMetaData(c, `instance-id: nocloud-static
public-keys:
- ssh-rsa AAAAB3NzAndSoOn
`)
defer os.Remove(cloudMetaDataFile)
c.Assert(inDeveloperMode(), Equals, true)
}

func (s *SnapTestSuite) TestInstallInstall(c *C) {
snapFile := makeTestSnapPackage(c, "")
name, err := Install(snapFile, AllowUnauthenticated|DoInstallGC, &progress.NullProgress{})
Expand Down

0 comments on commit 89dfaa7

Please sign in to comment.