Skip to content

Commit

Permalink
Working BorgRun class.
Browse files Browse the repository at this point in the history
  • Loading branch information
m3nu committed Jul 16, 2019
1 parent 314db0d commit 2aa21fd
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.git
.idea
vendor
deploy

vendor
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ubuntu-19.04:
docker build -f docker/ubuntu_19_04.Dockerfile -t vorta/ubuntu19_04 .
docker cp $(docker ps -alq):/home/user/vorta/deploy/linux deploy/
58 changes: 49 additions & 9 deletions borg/common.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
package borg

import (
"bufio"
"encoding/json"
"io"

"errors"
"fmt"
"log"
"os"
"os/exec"
"golang.org/x/sync/semaphore"
"vorta-go/models"

"vorta-go/app"
)


var borgProcessSlot = semaphore.NewWeighted(1)

type BorgRun struct {
Bin *BorgBin
CommonBorgArgs []string
SubCommand string
SubCommandArgs []string
ExtraBorgArgs []string
Repo *models.Repo
Profile *models.Profile
}

type BorgLogMessage struct {
LogType string `json:"type"`
Message string `json:"message"`
Levelname string `json:"levelname"`
Name string `json:"name"`
Time float32 `json:"time"`
}

func (r *BorgRun) Prepare() error {

// checks: binary available,
Expand All @@ -36,24 +49,44 @@ func (r *BorgRun) Prepare() error {
return errors.New("Backup is already in progress.")
}

r.CommonBorgArgs = append(r.CommonBorgArgs, "--info", "--log-json")
return nil
}


func (r *BorgRun) Run() {
mergedArgs := append(r.CommonBorgArgs, r.SubCommand)
mergedArgs = append(mergedArgs, r.SubCommandArgs...)
cmd := exec.Command(
"/Users/manu/.pyenv/shims/borg",
"info", "--debug", "uy5cg8ky@uy5cg8ky.repo.borgbase.com:repo")
app.CurrentCommand = cmd
r.Bin.Path,
mergedArgs...
)
//app.CurrentCommand = cmd
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "BORG_PASSPHRASE=xxx")

stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(stderr)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}

scanner := json.NewDecoder(stderr)
go func() {
for scanner.Scan() {
app.StatusUpdateChannel <- scanner.Text()
l := BorgLogMessage{}
for {
err = scanner.Decode(&l)
if err == io.EOF {
return
}
if err != nil {
app.Log.Error(err)
}
app.StatusUpdateChannel <- l.Message
app.Log.Info(l.Message)
}
}()

Expand All @@ -62,9 +95,16 @@ func (r *BorgRun) Run() {
}
app.StatusUpdateChannel <- "Started Command"

var result map[string]interface{}
if err := json.NewDecoder(stdout).Decode(&result); err != nil {
log.Fatal(err)
}

if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
app.StatusUpdateChannel <- "Finished Command"
app.CurrentCommand = nil
borgProcessSlot.Release(1)

fmt.Println(result)
}
22 changes: 15 additions & 7 deletions borg/info.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package borg

type BorgInfoRun struct {
import (
"vorta-go/models"
)

type InfoRun struct {
BorgRun
}

func NewBorgInfoRun() (*BorgInfoRun, error) {
r := BorgRun{
SubCommand: "info",
}
func NewInfoRun(profile *models.Profile, repo *models.Repo) (*InfoRun, error) {
r := &InfoRun{}
r.SubCommand = "info"
r.SubCommandArgs = []string{"--json"}
r.Profile = profile
r.Repo = repo

err := r.Prepare()
if err != nil {
return nil, err
}


r.SubCommandArgs = append(r.SubCommandArgs, repo.Url)
return r, nil
}

func (r *BorgInfoRun) ProcessResult() {
func (r *InfoRun) ProcessResult() {

}
10 changes: 8 additions & 2 deletions docker/ubuntu_19_04.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ RUN $GOPATH/bin/qtsetup prep
RUN $GOPATH/bin/qtsetup check
RUN $GOPATH/bin/qtsetup generate

COPY . $HOME/vorta
RUN cd $HOME/vorta && go mod vendor
RUN mkdir $HOME/vorta
WORKDIR $HOME/vorta
COPY go.mod .
COPY go.sum .

RUN go mod download
COPY . .

RUN cd $HOME/vorta && $GOPATH/bin/qtdeploy -uic=false -quickcompiler -debug build
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ require (
github.com/mattn/go-sqlite3 v1.10.0
github.com/robfig/cron/v3 v3.0.0
github.com/sirupsen/logrus v1.4.2
github.com/stephenlyu/go-pkg-xmlx v0.0.0-20151201012946-76f54ee73233 // indirect
github.com/stephenlyu/goqtuic v0.0.0-20180825180006-53b29b85eb87 // indirect
github.com/therecipe/env_darwin_amd64_513 v0.0.0-20190626001412-d8e92e8db4d0 // indirect
github.com/therecipe/qt v0.0.0-20190628021130-a9acd1ab63c1
github.com/therecipe/qt/internal/binding/files/docs v0.0.0-20190628021130-a9acd1ab63c1 // indirect
Expand All @@ -25,7 +23,9 @@ require (
github.com/therecipe/qt/internal/binding/files/docs/5.9.0 v0.0.0-20190628021130-a9acd1ab63c1 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190712213246-8b927904ee0d // indirect
golang.org/x/tools v0.0.0-20190716021316-fefcef05abb1 // indirect

)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a h1:XCr/YX7O0uxRkLq2k1ApNQMims9eCioF9UpzIPBDmuo=
golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -86,3 +87,5 @@ golang.org/x/tools v0.0.0-20190420181800-aa740d480789 h1:FF0rjo15h51+N6642mf5S3Q
golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190712213246-8b927904ee0d h1:JZPFnINSinLUdC0BJDoKhrky8niIzXMPIY2oR07+I+E=
golang.org/x/tools v0.0.0-20190712213246-8b927904ee0d/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
golang.org/x/tools v0.0.0-20190716021316-fefcef05abb1 h1:zgTTk+1hZ1lfGM8FQX/Y4Fcv3HZXEfnrOJ8wZPQClOQ=
golang.org/x/tools v0.0.0-20190716021316-fefcef05abb1/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
13 changes: 11 additions & 2 deletions ui/mainwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ func (w *MainWindow) init() {

func (w *MainWindow) StartBackup(checked bool) {
app.StatusUpdateChannel <- "Running Borg"
b := borg.BorgRun{SubCommand: "info"}
b.Prepare()

p := models.Profile{}
models.DB.Get(&p, models.SqlProfileById, 1)

r := models.Repo{}
models.DB.Get(&r, models.SqlRepoById, 14)

b, err := borg.NewInfoRun(&p, &r)
if err != nil {
app.Log.Error(err)
}
go b.Run()
}

Expand Down

0 comments on commit 2aa21fd

Please sign in to comment.