summaryrefslogtreecommitdiff
path: root/extensions/ostree.write
blob: b2b5d6e1bbf7ea1113ae2d6e1394e33d3c2f33c7 (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
#!/usr/bin/python
# Copyright (C) 2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


'''A Baserock write extension for deploying to OSTree repositories.'''


from gi.repository import GLib
from gi.repository import Gio
from gi.repository import OSTree

import os
import subprocess
import tempfile
import urlparse

import writeexts


def ostree_repo_resolve_rev(repo, branch, allow_noent=True):
    '''Return the SHA256 corresponding to 'branch'.'''
    return repo.resolve_rev(branch, allow_noent)[1]


def ostree_repo_commit_tree(repo, tree_dir, branch, commit_subject,
                            commit_body=''):
    '''Commit the contents of 'tree_dir' to 'branch'.'''

    repo.prepare_transaction(None)

    parent = ostree_repo_resolve_rev(repo, branch)

    ostree_mtree = OSTree.MutableTree()
    tree_dir_gfile = Gio.file_new_for_path(tree_dir)
    repo.write_directory_to_mtree(tree_dir_gfile, ostree_mtree, None, None)

    root = repo.write_mtree(ostree_mtree, None)[1]
    checksum = repo.write_commit(
        parent, commit_subject, commit_body, None, root, None)[1]

    repo.transaction_set_ref(None, branch, checksum)

    stats = repo.commit_transaction(None)
    return stats


class OSTreeWriteExtension(writeexts.WriteExtension):
    '''See ostree.write.help for documentation.'''

    def process_args(self, args):
        if len(args) != 2:
            raise writeexts.ExtensionError(
                'Wrong number of command line args')

        temp_root, location = args

        repo = OSTree.Repo.new(Gio.File.new_for_path(location))
        repo.open()

        branch = os.environ.get('OSTREE_BRANCH')
        commit_subject = os.environ.get('OSTREE_COMMIT_SUBJECT')
        commit_body = os.environ.get('OSTREE_COMMIT_BODY')

        try:
            ostree_repo_commit_tree(repo, temp_root, branch, commit_subject, commit_body)
        except GLib.GError as e:
            raise writeexts.ExtensionError(
                '%s. Failed to commit system to repo %s.' %
                (e.message, location))

        self.status(
            msg='Created commit on branch %s in OSTree repo at %s' % (
                branch, location))


OSTreeWriteExtension().run()