2015-12-22 09:56:33 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-12-22 09:56:33 -05:00
|
|
|
"fmt"
|
2016-05-25 15:34:51 -04:00
|
|
|
"io"
|
2018-07-29 09:14:17 -04:00
|
|
|
"log"
|
2015-12-22 09:56:33 -05:00
|
|
|
"os"
|
2018-07-29 09:14:17 -04:00
|
|
|
"os/user"
|
2016-05-25 15:34:51 -04:00
|
|
|
"path/filepath"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2015-12-22 09:56:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepExport struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-12-22 09:56:33 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
name := config.ContainerName
|
|
|
|
|
2018-07-29 09:14:17 -04:00
|
|
|
lxc_dir := "/var/lib/lxc"
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Cannot find current user. Falling back to /var/lib/lxc...")
|
|
|
|
}
|
|
|
|
if user.Uid != "0" && user.HomeDir != "" {
|
|
|
|
lxc_dir = filepath.Join(user.HomeDir, ".local", "share", "lxc")
|
|
|
|
}
|
|
|
|
|
|
|
|
containerDir := filepath.Join(lxc_dir, name)
|
2015-12-22 09:56:33 -05:00
|
|
|
outputPath := filepath.Join(config.OutputDir, "rootfs.tar.gz")
|
|
|
|
configFilePath := filepath.Join(config.OutputDir, "lxc-config")
|
|
|
|
|
|
|
|
configFile, err := os.Create(configFilePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating config file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
originalConfigFile, err := os.Open(config.ConfigFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error opening config file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(configFile, originalConfigFile)
|
2019-11-14 16:40:40 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error copying file %s: %v", config.ConfigFile, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-05-16 17:40:22 -04:00
|
|
|
commands := make([][]string, 3)
|
2015-12-22 09:56:33 -05:00
|
|
|
commands[0] = []string{
|
|
|
|
"lxc-stop", "--name", name,
|
|
|
|
}
|
|
|
|
commands[1] = []string{
|
|
|
|
"tar", "-C", containerDir, "--numeric-owner", "--anchored", "--exclude=./rootfs/dev/log", "-czf", outputPath, "./rootfs",
|
|
|
|
}
|
|
|
|
commands[2] = []string{
|
|
|
|
"chmod", "+x", configFilePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting container...")
|
|
|
|
for _, command := range commands {
|
2018-05-16 17:40:22 -04:00
|
|
|
err := RunCommand(command...)
|
2015-12-22 09:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error exporting container: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepExport) Cleanup(state multistep.StateBag) {}
|