summaryrefslogtreecommitdiff
path: root/extensions/ostree.write
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/ostree.write')
-rw-r--r--extensions/ostree.write88
1 files changed, 88 insertions, 0 deletions
diff --git a/extensions/ostree.write b/extensions/ostree.write
new file mode 100644
index 00000000..b2b5d6e1
--- /dev/null
+++ b/extensions/ostree.write
@@ -0,0 +1,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()