New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
corecfg: add proxy configuration via snap set core proxy.{http,https,ftp}=...
#3594
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fb6d567
add first go version of the core configure code
mvo5 a7dad0b
add spread tests for snapctl internal configure-core
mvo5 6dde7b8
refactor to make code easier to test
mvo5 1de88c3
simplify snapctl/main.go
mvo5 af73181
add missing check for err
mvo5 467bf49
address review comments (thanks to Gustavo)
mvo5 415c9d5
Merge remote-tracking branch 'upstream/master' into configure-hook
mvo5 a5979de
simplify regexp as suggested by Gustavo
mvo5 16a6517
improve snapctl-configure-core test to ensure changes will be fully u…
mvo5 6cdecb1
Merge remote-tracking branch 'upstream/master' into configure-hook
mvo5 2cdbbc2
add support for `snap set core proxy.{http,https,ftp,all}`
mvo5 3dec293
rename piConfig() -> piConfigFile() (thanks zyga)
mvo5 f88d6fa
Merge branch 'configure-hook' into configure-hook-proxy
mvo5 95c164f
Merge remote-tracking branch 'upstream/master' into configure-hook
mvo5 e9c7366
Merge branch 'configure-hook' into configure-hook-proxy
mvo5 bbcaea5
address review feedback
mvo5 d28920e
simplify regexp as suggested by Gustavo and John (thanks!)
mvo5 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 corecfg | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/snapcore/snapd/osutil" | ||
"github.com/snapcore/snapd/release" | ||
"github.com/snapcore/snapd/systemd" | ||
) | ||
|
||
var ( | ||
Stdout = os.Stdout | ||
Stderr = os.Stderr | ||
) | ||
|
||
// ensureSupportInterface checks that the system has the core-support | ||
// interface. An error is returned if this is not the case | ||
func ensureSupportInterface() error { | ||
_, err := systemd.SystemctlCmd("--version") | ||
return err | ||
} | ||
|
||
func snapctlGet(key string) (string, error) { | ||
raw, err := exec.Command("snapctl", "get", key).CombinedOutput() | ||
if err != nil { | ||
return "", fmt.Errorf("cannot run snapctl: %s", osutil.OutputErr(raw, err)) | ||
} | ||
|
||
output := strings.TrimRight(string(raw), "\n") | ||
return output, nil | ||
} | ||
|
||
func Run() error { | ||
// see if it makes sense to run at all | ||
if release.OnClassic { | ||
return fmt.Errorf("cannot run core-configure on classic distribution") | ||
} | ||
if err := ensureSupportInterface(); err != nil { | ||
return fmt.Errorf("cannot run systemctl - core-support interface seems disconnected: %v", err) | ||
} | ||
|
||
// handle the various core config options: | ||
// service.*.disable | ||
if err := handleServiceDisableConfiguration(); err != nil { | ||
return err | ||
} | ||
// system.power-key-action | ||
if err := handlePowerButtonConfiguration(); err != nil { | ||
return err | ||
} | ||
// pi-config.* | ||
if err := handlePiConfiguration(); err != nil { | ||
return err | ||
} | ||
// proxy.{http,https,ftp} | ||
if err := handleProxyConfiguration(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 corecfg_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/corecfg" | ||
"github.com/snapcore/snapd/release" | ||
"github.com/snapcore/snapd/testutil" | ||
) | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type coreCfgSuite struct{} | ||
|
||
var _ = Suite(&coreCfgSuite{}) | ||
|
||
func (s *coreCfgSuite) TestConfigureErrorsOnClassic(c *C) { | ||
restore := release.MockOnClassic(true) | ||
defer restore() | ||
|
||
err := corecfg.Run() | ||
c.Check(err, ErrorMatches, "cannot run core-configure on classic distribution") | ||
} | ||
|
||
func (s *coreCfgSuite) TestConfigureErrorOnMissingCoreSupport(c *C) { | ||
restore := release.MockOnClassic(false) | ||
defer restore() | ||
|
||
mockSystemctl := testutil.MockCommand(c, "systemctl", ` | ||
echo "simulate missing core-support" | ||
exit 1 | ||
`) | ||
defer mockSystemctl.Restore() | ||
|
||
err := corecfg.Run() | ||
c.Check(err, ErrorMatches, `(?m)cannot run systemctl - core-support interface seems disconnected: \[--version\] failed with exit status 1: simulate missing core-support`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 corecfg | ||
|
||
var ( | ||
UpdatePiConfig = updatePiConfig | ||
SwitchHandlePowerKey = switchHandlePowerKey | ||
SwitchDisableService = switchDisableService | ||
UpdateKeyValueStream = updateKeyValueStream | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2017 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 corecfg | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/snapcore/snapd/dirs" | ||
"github.com/snapcore/snapd/osutil" | ||
) | ||
|
||
// valid pi config keys | ||
var piConfigKeys = map[string]bool{ | ||
"disable_overscan": true, | ||
"framebuffer_width": true, | ||
"framebuffer_height": true, | ||
"framebuffer_depth": true, | ||
"framebuffer_ignore_alpha": true, | ||
"overscan_left": true, | ||
"overscan_right": true, | ||
"overscan_top": true, | ||
"overscan_bottom": true, | ||
"overscan_scale": true, | ||
"display_rotate": true, | ||
"hdmi_group": true, | ||
"hdmi_mode": true, | ||
"hdmi_drive": true, | ||
"avoid_warnings": true, | ||
"gpu_mem_256": true, | ||
"gpu_mem_512": true, | ||
"gpu_mem": true, | ||
"sdtv_aspect": true, | ||
"config_hdmi_boost": true, | ||
"hdmi_force_hotplug": true, | ||
} | ||
|
||
func updatePiConfig(path string, config map[string]string) error { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
toWrite, err := updateKeyValueStream(f, piConfigKeys, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if toWrite != nil { | ||
s := strings.Join(toWrite, "\n") | ||
return osutil.AtomicWriteFile(path, []byte(s), 0644, 0) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func piConfigFile() string { | ||
return filepath.Join(dirs.GlobalRootDir, "/boot/uboot/config.txt") | ||
} | ||
|
||
func handlePiConfiguration() error { | ||
if osutil.FileExists(piConfigFile()) { | ||
// snapctl can actually give us the whole dict in | ||
// JSON, in a single call; use that instead of this. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason we're not doing this? |
||
config := map[string]string{} | ||
for key := range piConfigKeys { | ||
output, err := snapctlGet(fmt.Sprintf("pi-config.%s", strings.Replace(key, "_", "-", -1))) | ||
if err != nil { | ||
return err | ||
} | ||
config[key] = output | ||
} | ||
if err := updatePiConfig(piConfigFile(), config); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hold on, if there is nothing to write does that mean we should remove the original file or do nothing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do nothing, the original file may contain data that is opaque to us (like more entries in /etc/environment that we do not manage ourselfs).