Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup build #77

Merged
merged 19 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1edd549
Simplify image build cleanup process with helper function. Added clea…
Szubie May 30, 2022
7c5a920
Use of defer statements to cleanup in a more consistent fashion
Szubie May 30, 2022
1bc008f
Add abortFlag and a cancellable context to `BuildImage`. Moved gorout…
Szubie May 31, 2022
08c33fb
Extend the use of context.Context to more functions used during build
Szubie May 31, 2022
8019f00
Extend the abortFlag/context pattern used in `BuildImage` to `InitUni…
Szubie May 31, 2022
74e1aa9
Get image fingerprint from remote LXD simplestreams server before lau…
Szubie Jun 6, 2022
9c3d49d
Alter error checking to gather errors and return first one. This prev…
Szubie Jun 7, 2022
91c5210
Return fingerprint from `importLocal` even if error encountered to al…
Szubie Jun 7, 2022
57b542a
Move Postdeploy call into the InitUnit function. Postdeploy is an imp…
Szubie May 31, 2022
dc45d38
Cleanup during deployment in `InitUnit` using a defer statement to re…
Szubie May 31, 2022
868df61
Add proper cleanup code in `InitUnit` to delete the unit if an error …
Szubie Jun 7, 2022
a5fe81b
use defer to cleanup in PublishUnit function as well for consistency.
Szubie Jun 7, 2022
abe016d
Add extra cancellation check to `Launch` function after newly introdu…
Szubie Jun 7, 2022
bbb35e7
Remove duplicated unit removal code during build cleanup. Removed unn…
Szubie Jun 7, 2022
9486383
Comments for cleanup code
Szubie Jun 7, 2022
603412b
Reorder the select statement and blocking op.Wait in Exec() function …
Szubie Jun 8, 2022
5168193
Remove duplicate image/unit cleanup code in `importLocal` - cleanup i…
Szubie Jun 8, 2022
310de63
Move Postdeploy call within `InitUnit` to before the unit DB insertio…
Szubie Jun 8, 2022
7f04260
Check status code of commands during postdeploy, raise error if statu…
Szubie Jun 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,4 @@ func deploy(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}

err = host.Postdeploy(bravefile)
if err != nil {
log.Fatal(err)
}

}
104 changes: 41 additions & 63 deletions platform/helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package platform

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"os/user"
"path"
"path/filepath"
"regexp"
"syscall"

"github.com/bravetools/bravetools/shared"
"github.com/lxc/lxd/shared/api"
Expand Down Expand Up @@ -127,16 +126,22 @@ func createSharedVolume(storagePoolName string,
return nil
}

func importLXD(bravefile *shared.Bravefile, remote Remote) (fingerprint string, err error) {
fingerprint, err = Launch(bravefile.PlatformService.Name, bravefile.Base.Image, remote)
func importLXD(ctx context.Context, bravefile *shared.Bravefile, remote Remote) (fingerprint string, err error) {
if err = ctx.Err(); err != nil {
return "", err
}
fingerprint, err = Launch(ctx, bravefile.PlatformService.Name, bravefile.Base.Image, remote)
if err != nil {
return "", errors.New("failed to launch base unit: " + err.Error())
return fingerprint, errors.New("failed to launch base unit: " + err.Error())
}

return fingerprint, nil
}

func importGitHub(bravefile *shared.Bravefile, bh *BraveHost) (fingerprint string, err error) {
func importGitHub(ctx context.Context, bravefile *shared.Bravefile, bh *BraveHost) (fingerprint string, err error) {
if err = ctx.Err(); err != nil {
return "", err
}
home, _ := os.UserHomeDir()
imageLocation := filepath.Join(home, shared.ImageStore)

Expand All @@ -160,11 +165,14 @@ func importGitHub(bravefile *shared.Bravefile, bh *BraveHost) (fingerprint strin
remoteBravefile.Base.Image = remoteServiceName
remoteBravefile.PlatformService.Name = bravefile.PlatformService.Name

fingerprint, err = importLocal(remoteBravefile, bh.Remote)
fingerprint, err = importLocal(ctx, remoteBravefile, bh.Remote)
return fingerprint, err
}

func importLocal(bravefile *shared.Bravefile, remote Remote) (fingerprint string, err error) {
func importLocal(ctx context.Context, bravefile *shared.Bravefile, remote Remote) (fingerprint string, err error) {
if err = ctx.Err(); err != nil {
return "", err
}
home, _ := os.UserHomeDir()
location := filepath.Join(home, shared.ImageStore)

Expand All @@ -174,19 +182,28 @@ func importLocal(bravefile *shared.Bravefile, remote Remote) (fingerprint string
return fingerprint, errors.New("failed to import image: " + err.Error())
}

if err = ctx.Err(); err != nil {
return fingerprint, err
}

err = LaunchFromImage(bravefile.Base.Image, bravefile.PlatformService.Name, remote)
if err != nil {
DeleteImageByFingerprint(fingerprint, remote)
return fingerprint, errors.New("failed to launch unit: " + err.Error())
}

if err = ctx.Err(); err != nil {
return fingerprint, err
}

err = Start(bravefile.PlatformService.Name, remote)
if err != nil {
DeleteUnit(bravefile.PlatformService.Name, remote)
DeleteImageByFingerprint(fingerprint, remote)
return fingerprint, errors.New("failed to start a unit: " + err.Error())
}

if err = ctx.Err(); err != nil {
return fingerprint, err
}

return fingerprint, nil
}

Expand Down Expand Up @@ -318,29 +335,19 @@ func listHostImages(remote Remote) ([]api.Image, error) {
// return ifaces, nil
// }

// ProcessInterruptHandler monitors for Ctrl+C keypress in Terminal
func processInterruptHandler(fingerprint string, bravefile *shared.Bravefile, bh *BraveHost) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Interrupting build and cleaning artefacts")
DeleteImageByFingerprint(fingerprint, bh.Remote)
DeleteUnit(bravefile.PlatformService.Name, bh.Remote)

os.Exit(0)
}()
}

func bravefileCopy(copy []shared.CopyCommand, service string, remote Remote) error {
func bravefileCopy(ctx context.Context, copy []shared.CopyCommand, service string, remote Remote) error {
dir, _ := os.Getwd()
for _, c := range copy {
if err := ctx.Err(); err != nil {
return err
}

source := c.Source
source = path.Join(dir, source)
sourcePath := filepath.FromSlash(source)

target := c.Target
_, err := Exec(service, []string{"mkdir", "-p", target}, remote)
_, err := Exec(ctx, service, []string{"mkdir", "-p", target}, remote)
if err != nil {
return errors.New("Failed to create target directory: " + err.Error())
}
Expand Down Expand Up @@ -368,7 +375,7 @@ func bravefileCopy(copy []shared.CopyCommand, service string, remote Remote) err
}

if c.Action != "" {
_, err = Exec(service, []string{"bash", "-c", c.Action}, remote)
_, err = Exec(ctx, service, []string{"bash", "-c", c.Action}, remote)
if err != nil {
return errors.New("Failed to execute action: " + err.Error())
}
Expand All @@ -378,8 +385,12 @@ func bravefileCopy(copy []shared.CopyCommand, service string, remote Remote) err
return nil
}

func bravefileRun(run []shared.RunCommand, service string, remote Remote) (status int, err error) {
func bravefileRun(ctx context.Context, run []shared.RunCommand, service string, remote Remote) (status int, err error) {
for _, c := range run {
if err = ctx.Err(); err != nil {
return 1, err
}

var command string
var content string

Expand All @@ -399,7 +410,7 @@ func bravefileRun(run []shared.RunCommand, service string, remote Remote) (statu
args = append(args, content)
}

status, err = Exec(service, args, remote)
status, err = Exec(ctx, service, args, remote)

}

Expand Down Expand Up @@ -451,36 +462,3 @@ func checkUnits(unitName string, bh *BraveHost) error {

return nil
}

func getImageFingerprint(slice1 []api.Image, slice2 []api.Image) string {
var diff []string
var fingerprint string

// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1.Fingerprint == s2.Fingerprint {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1.Fingerprint)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}

if len(diff) > 0 {
fingerprint = diff[0]
}

return fingerprint
}
Loading