[lxd] fixup some publish stuff
This commit is contained in:
parent
c62f9a0301
commit
8326d7b6ac
|
@ -5,8 +5,7 @@ import (
|
|||
)
|
||||
|
||||
type Artifact struct {
|
||||
dir string
|
||||
f []string
|
||||
id string
|
||||
}
|
||||
|
||||
func (*Artifact) BuilderId() string {
|
||||
|
@ -14,15 +13,15 @@ func (*Artifact) BuilderId() string {
|
|||
}
|
||||
|
||||
func (a *Artifact) Files() []string {
|
||||
return a.f
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Artifact) Id() string {
|
||||
return "Container"
|
||||
func (a *Artifact) Id() string {
|
||||
return a.id
|
||||
}
|
||||
|
||||
func (a *Artifact) String() string {
|
||||
return fmt.Sprintf("Container: %s", a.dir)
|
||||
return fmt.Sprintf("image: %s", a.id)
|
||||
}
|
||||
|
||||
func (a *Artifact) State(name string) interface{} {
|
||||
|
@ -31,5 +30,6 @@ func (a *Artifact) State(name string) interface{} {
|
|||
|
||||
func (a *Artifact) Destroy() error {
|
||||
//return os.RemoveAll(a.dir)
|
||||
//TODO
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -83,8 +83,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
}
|
||||
|
||||
artifact := &Artifact{
|
||||
// dir: b.config.OutputDir,
|
||||
// f: files,
|
||||
id: state.Get("imageFingerprint").(string),
|
||||
}
|
||||
|
||||
return artifact, nil
|
||||
|
|
|
@ -7,47 +7,28 @@ import (
|
|||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type stepPublish struct{}
|
||||
|
||||
func (s *stepPublish) Run(state multistep.StateBag) multistep.StepAction {
|
||||
var stdout, stderr bytes.Buffer
|
||||
config := state.Get("config").(*Config)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
name := config.ContainerName
|
||||
|
||||
commands := [][]string{
|
||||
{"lxc", "stop", "--force", name},
|
||||
{"lxc", "publish", name, "--alias", config.OutputImage},
|
||||
args := []string{
|
||||
"lxc", "publish", "--force", name, "--alias", config.OutputImage,
|
||||
}
|
||||
|
||||
ui.Say("Publishing container...")
|
||||
for _, command := range commands {
|
||||
err := s.SudoCommand(command...)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error publishing container: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *stepPublish) Cleanup(state multistep.StateBag) {}
|
||||
|
||||
func (s *stepPublish) SudoCommand(args ...string) error {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
log.Printf("Executing sudo command: %#v", args)
|
||||
cmd := exec.Command("sudo", args...)
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
|
||||
stdoutString := strings.TrimSpace(stdout.String())
|
||||
stderrString := strings.TrimSpace(stderr.String())
|
||||
|
||||
|
@ -58,5 +39,20 @@ func (s *stepPublish) SudoCommand(args ...string) error {
|
|||
log.Printf("stdout: %s", stdoutString)
|
||||
log.Printf("stderr: %s", stderrString)
|
||||
|
||||
return err
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
r := regexp.MustCompile("([0-9a-fA-F]+)$")
|
||||
fingerprint := r.FindAllStringSubmatch(stdoutString, -1)[0][0]
|
||||
|
||||
ui.Say(fmt.Sprintf("Created image: %s", fingerprint))
|
||||
|
||||
state.Put("imageFingerprint", fingerprint)
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *stepPublish) Cleanup(state multistep.StateBag) {}
|
||||
|
|
Loading…
Reference in New Issue