Skip to content

Commit

Permalink
Fix Fedora images (Fixes snapcore#42, Fixes snapcore#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAOF committed Jan 4, 2018
1 parent 5c0ebb5 commit dbcafcc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/spread/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/niemeyer/pretty"
"github.com/snapcore/spread/spread"
"github.com/RAOF/spread/spread"
)

var (
Expand Down
95 changes: 91 additions & 4 deletions spread/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import (
"golang.org/x/net/context"
)

type Distro int

const (
Unknown Distro = iota
Ubuntu
Debian
Fedora
)

func LXD(p *Project, b *Backend, o *Options) Provider {
return &lxdProvider{p, b, o}
}
Expand Down Expand Up @@ -59,6 +68,25 @@ func (s *lxdServer) ReuseData() interface{} {
return &s.d
}

func (s *lxdServer) Distro() Distro {
// TODO: This doesn't handle explicit LXD image specifiers
// (like images:fedora/26)
//
// Fixing that in general might require the user to specify the distro
// in the spread.yaml metadata
parts := strings.Split(s.System().Image, "-")
if parts[0] == "ubuntu" {
return Ubuntu
}
if parts[0] == "debian" {
return Debian
}
if parts[0] == "fedora" {
return Fedora
}
return Unknown
}

func (s *lxdServer) Discard(ctx context.Context) error {
output, err := exec.Command("lxc", "delete", "--force", s.d.Name).CombinedOutput()
if err != nil {
Expand All @@ -67,6 +95,44 @@ func (s *lxdServer) Discard(ctx context.Context) error {
return nil
}

func (s *lxdServer) Prepare(ctx context.Context) error {
if s.Distro() == Unknown {
s.Discard(ctx)
return fmt.Errorf("Unknown distro type for image %s, bailing", s.System().Image)
}

args := []string{"exec", s.d.Name, "--"}
args = append(args, sshInstallCommand(s.Distro())...)


output, err := exec.Command("lxc", args...).CombinedOutput()
if err != nil {
printf("Command output: %s", output)
s.Discard(ctx)
return err
}

err = s.p.tuneSSH(s.d.Name, s.Distro())
if err != nil {
s.Discard(ctx)
return err
}

if s.Distro() == Fedora {
// Fedora LXD images do *not* contain tar by default, so we must install it manually
args = []string{"exec", s.d.Name, "--", "dnf", "install", "--assumeyes", "tar"}
output, err = exec.Command("lxc", args...).CombinedOutput()

if err != nil {
printf("Command output: %s", output)
s.Discard(ctx)
return err
}
}

return nil
}

func (p *lxdProvider) Backend() *Backend {
return p.backend
}
Expand Down Expand Up @@ -138,9 +204,8 @@ func (p *lxdProvider) Allocate(ctx context.Context, system *System) (Server, err
}
}

err = p.tuneSSH(name)
err = s.Prepare(ctx)
if err != nil {
s.Discard(ctx)
return nil, err
}

Expand All @@ -156,6 +221,17 @@ func isDebArch(s string) bool {
return false
}

func sshInstallCommand(distro Distro) []string {
if distro == Ubuntu || distro == Debian {
return []string{"apt", "install", "openssh-server"}
}
if distro == Fedora {
return []string{"dnf", "--assumeyes", "install", "openssh-server"}
}
// Precondition failure - unknown distro!
return []string{}
}

func (p *lxdProvider) lxdImage(system *System) (string, error) {
// LXD loves the network. Force it to use a local image if available.
fingerprint, err := p.lxdLocalImage(system)
Expand Down Expand Up @@ -444,11 +520,22 @@ func (p *lxdProvider) serverJSON(name string) (*lxdServerJSON, error) {
return sjsons[0], nil
}

func (p *lxdProvider) tuneSSH(name string) error {
func sshReloadCommand(distro Distro) []string {
if distro == Fedora {
return []string{"systemctl", "restart", "sshd"}
}
if distro == Ubuntu || distro == Debian {
return []string{"systemctl", "restart", "ssh"}
}
// Precondition failure: unknown distro!
return []string{}
}

func (p *lxdProvider) tuneSSH(name string, distro Distro) error {
cmds := [][]string{
{"sed", "-i", `s/\(PermitRootLogin\|PasswordAuthentication\)\>.*/\1 yes/`, "/etc/ssh/sshd_config"},
{"/bin/bash", "-c", fmt.Sprintf("echo root:'%s' | chpasswd", p.options.Password)},
{"killall", "-HUP", "sshd"},
sshReloadCommand(distro),
}
for _, args := range cmds {
output, err := exec.Command("lxc", append([]string{"exec", name, "--"}, args...)...).CombinedOutput()
Expand Down
1 change: 1 addition & 0 deletions tests/lxd/spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ backends:
lxd:
systems:
- ubuntu-16.04
- fedora-26

path: /home/test

Expand Down

0 comments on commit dbcafcc

Please sign in to comment.