Skip to content

Commit

Permalink
Add support for PEBBLE_INIT_FROM.
Browse files Browse the repository at this point in the history
`PEBBLE_INIT_FROM` allows `pebble run` to copy layers and state from
another directory when seeding a new `PEBBLE` directory.
  • Loading branch information
hpidcock committed Feb 9, 2024
1 parent 55b78b4 commit 426d554
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/canonical/x-go v0.0.0-20230522092633-7947a7587f5b
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.2
github.com/otiai10/copy v1.14.0
github.com/pkg/term v1.1.0
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b
golang.org/x/sys v0.5.0
golang.org/x/sys v0.16.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -19,6 +20,7 @@ require (
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b h1:Qwe1rC8PSniVfAFPFJeyUkB+zcysC3RgJBAGk7eqBEU=
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
4 changes: 4 additions & 0 deletions internals/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ func getEnvPaths() (pebbleDir string, socketPath string) {
return pebbleDir, socketPath
}

func getCopySource() string {
return os.Getenv("PEBBLE_INIT_FROM")
}

type cliState struct {
NoticesLastListed time.Time `json:"notices-last-listed"`
NoticesLastOkayed time.Time `json:"notices-last-okayed"`
Expand Down
38 changes: 38 additions & 0 deletions internals/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/canonical/go-flags"
"github.com/otiai10/copy"

"github.com/canonical/pebble/client"
"github.com/canonical/pebble/cmd"
Expand Down Expand Up @@ -157,6 +158,11 @@ func runDaemon(rcmd *cmdRun, ch chan os.Signal, ready chan<- func()) error {
return err
}
}
err := maybeCopyPebbleDir(getCopySource(), pebbleDir)
if err != nil {
return err
}

dopts := daemon.Options{
Dir: pebbleDir,
SocketPath: socketPath,
Expand Down Expand Up @@ -275,3 +281,35 @@ func convertArgs(args [][]string) (map[string][]string, error) {

return mappedArgs, nil
}

func maybeCopyPebbleDir(srcDir, destDir string) error {
if srcDir == "" {
return nil
}
dirEnts, err := os.ReadDir(destDir)
if err != nil {
return err
}
if len(dirEnts) != 0 {
return nil
}
err = copy.Copy(srcDir, destDir, copy.Options{
OnSymlink: func(src string) copy.SymlinkAction { return copy.Shallow },
OnDirExists: func(src, dest string) copy.DirExistsAction { return copy.Merge },
PermissionControl: copy.PerservePermission,
Skip: func(srcInfo os.FileInfo, src, dest string) (bool, error) {
if srcInfo.Mode()&os.ModeSetuid != 0 {
return true, fmt.Errorf("cannot copy file %q to %q with setuid bit", src, dest)
}
if srcInfo.Mode()&os.ModeSetgid != 0 {
return true, fmt.Errorf("cannot copy file %q to %q with setgid bit", src, dest)
}
return false, nil
},
PreserveOwner: false,
})
if err != nil {
return fmt.Errorf("cannot copy %q to %q: %w", srcDir, destDir, err)
}
return nil
}

0 comments on commit 426d554

Please sign in to comment.