Skip to content

Commit

Permalink
feat(update): support updates via shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed May 9, 2019
1 parent 905c731 commit c94edd8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion base/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func LocalShellScriptRunner(basepath string) cron.RunJobFunc {
return func(ctx context.Context, streams ioes.IOStreams, job *cron.Job) error {
path := job.Name
if qfs.PathKind(job.Name) == "local" {
path = filepath.Join(basepath, path)
// TODO (b5) - need to first check that path can't be found
// path = filepath.Join(basepath, path)
}

cmd := exec.Command(path)
Expand Down
1 change: 1 addition & 0 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewTestFactory(c *regclient.Client) (tf TestFactory, err error) {
rpc: nil,
config: cfg,
node: tnode.(*p2p.QriNode),
inst: lib.NewInstanceFromConfigAndNode(cfg, tnode.(*p2p.QriNode)),
}, nil
}

Expand Down
9 changes: 9 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,12 @@ func (inst *Instance) RPC() *rpc.Client {
func (inst *Instance) Teardown() {
inst.teardown()
}

// QriPath returns the path to where qri is operating from on the local filesystem
// there are situations where this will be a temporary directory
func (inst *Instance) QriPath() string {
if inst.cfg != nil && inst.cfg.Repo != nil && inst.cfg.Repo.Path != "" {
return inst.cfg.Repo.Path
}
return os.TempDir()
}
34 changes: 26 additions & 8 deletions lib/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@ import (
"context"
"fmt"
"io"
"os"
"path/filepath"

"github.com/qri-io/dataset"
"github.com/qri-io/ioes"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/actions"
"github.com/qri-io/qri/base"
"github.com/qri-io/qri/cron"
"github.com/qri-io/qri/repo"
)

// NewUpdateMethods creates a configuration handle from an instance
func NewUpdateMethods(inst *Instance) *UpdateMethods {
return &UpdateMethods{inst: inst}
m := &UpdateMethods{
inst: inst,
scriptsPath: filepath.Join(inst.QriPath(), "update_scripts"),
}

if err := os.MkdirAll(m.scriptsPath, os.ModePerm); err != nil {
log.Error("creating update scripts directory: %s", err.Error())
}

return m
}

// UpdateMethods enapsulates logic for scheduled updates
type UpdateMethods struct {
inst *Instance
ScriptsPath string
scriptsPath string
}

// CoreRequestsName specifies this is a Methods object
Expand All @@ -43,6 +55,12 @@ type ScheduleParams struct {

// Schedule creates a job and adds it to the scheduler
func (m *UpdateMethods) Schedule(in *ScheduleParams, out *cron.Job) (err error) {
if base.PossibleShellScript(in.Name) {
if err = qfs.AbsPath(&in.Name); err != nil {
return
}
}

if m.inst.rpc != nil {
return m.inst.rpc.Call("UpdateMethods.Schedule", in, out)
}
Expand All @@ -64,6 +82,7 @@ func (m *UpdateMethods) Schedule(in *ScheduleParams, out *cron.Job) (err error)

func (m *UpdateMethods) jobFromScheduleParams(p *ScheduleParams) (job *cron.Job, err error) {
if base.PossibleShellScript(p.Name) {
// TODO (b5) - confirm file exists & is executable
return base.ShellScriptToJob(qfs.NewMemfileBytes(p.Name, nil), p.Periodicity, nil)
}

Expand Down Expand Up @@ -170,7 +189,7 @@ func (m *UpdateMethods) Run(p *Job, res *repo.DatasetRef) (err error) {
err = m.runDatasetUpdate(updateParams, res)

case cron.JTShellScript:
runner := base.LocalShellScriptRunner(m.ScriptsPath)
runner := base.LocalShellScriptRunner(m.scriptsPath)
err = runner(m.inst.ctx, m.inst.streams, p)

default:
Expand Down Expand Up @@ -214,10 +233,10 @@ func (m *UpdateMethods) runDatasetUpdate(p *UpdateParams, res *repo.DatasetRef)
return err
}

// if !base.InLocalNamespace(m.inst.Repo(), &ref) {
// *res, err = actions.UpdateRemoteDataset(m.node, &ref, true)
// return err
// }
if !base.InLocalNamespace(m.inst.Repo(), &ref) {
*res, err = actions.UpdateRemoteDataset(m.inst.Node(), &ref, true)
return err
}

// default to recalling transfrom scripts for local updates
// TODO (b5): not sure if this should be here or in client libraries
Expand Down Expand Up @@ -261,7 +280,6 @@ func newUpdateRunner(newInst func(ctx context.Context, streams ioes.IOStreams) (
}

m := NewUpdateMethods(inst)
m.ScriptsPath = scriptsPath
res := &repo.DatasetRef{}
return m.Run(job, res)
}
Expand Down

0 comments on commit c94edd8

Please sign in to comment.