summaryrefslogtreecommitdiff
path: root/actions/ostree_commit_action.go
blob: 85b3fdac9053911755aa14e4a22e8e2d8a10194a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
OstreeCommit Action

Create OSTree commit from rootfs.

Yaml syntax:
 - action: ostree-commit
   repository: repository name
   branch: branch name
   subject: commit message
   collection-id: org.apertis.example
   ref-binding:
     - branch1
     - branch2
   metadata:
     key: value
     vendor.key: somevalue

Mandatory properties:

- repository -- path to repository with OSTree structure; the same path is
used by 'ostree' tool with '--repo' argument.
This path is relative to 'artifact' directory.
Please keep in mind -- you will need a root privileges for 'bare' repository
type (https://ostree.readthedocs.io/en/latest/manual/repo/#repository-types-and-locations).

- branch -- OSTree branch name that should be used for the commit.

Optional properties:

- subject -- one line message with commit description.

- collection-id -- Collection ID ref binding (requires libostree 2018.6).

- ref-binding -- enforce that the commit was retrieved from one of the branch names in this array.
  If 'collection-id' is set and 'ref-binding' is empty, will default to the branch name.

- metadata -- key-value pairs of meta information to be added into commit.
*/
package actions

import (
	"fmt"
	"log"
	"os"
	"path"

	"github.com/go-debos/debos"
	"github.com/sjoerdsimons/ostree-go/pkg/otbuiltin"
)

type OstreeCommitAction struct {
	debos.BaseAction `yaml:",inline"`
	Repository       string
	Branch           string
	Subject          string
	Command          string
	CollectionID     string   `yaml:"collection-id"`
	RefBinding       []string `yaml:"ref-binding"`
	Metadata         map[string]string
}

func emptyDir(dir string) {
	d, _ := os.Open(dir)
	defer d.Close()

	files, err := d.Readdirnames(-1)
	if err != nil {
		log.Fatal(err)
	}

	for _, f := range files {
		err := os.RemoveAll(path.Join(dir, f))
		if err != nil {
			log.Fatalf("Failed to remove file: %v", err)
		}
	}
}

func (ot *OstreeCommitAction) Run(context *debos.DebosContext) error {
	ot.LogStart()
	repoPath := path.Join(context.Artifactdir, ot.Repository)

	emptyDir(path.Join(context.Rootdir, "dev"))

	repo, err := otbuiltin.OpenRepo(repoPath)
	if err != nil {
		return err
	}

	_, err = repo.PrepareTransaction()
	if err != nil {
		return err
	}

	opts := otbuiltin.NewCommitOptions()
	opts.Subject = ot.Subject
	for k, v := range ot.Metadata {
		str := fmt.Sprintf("%s=%s", k, v)
		opts.AddMetadataString = append(opts.AddMetadataString, str)
	}

	if ot.CollectionID != "" {
		opts.CollectionID = ot.CollectionID
		if len(ot.RefBinding) == 0 {
			// Add current branch if not explitely set via 'ref-binding'
			opts.RefBinding = append(opts.RefBinding, ot.Branch)
		}
	}

	// Add values from 'ref-binding' if any
	opts.RefBinding = append(opts.RefBinding, ot.RefBinding...)

	ret, err := repo.Commit(context.Rootdir, ot.Branch, opts)
	if err != nil {
		return err
	} else {
		log.Printf("Commit: %s\n", ret)
	}
	_, err = repo.CommitTransaction()
	if err != nil {
		return err
	}

	return nil
}