[ostree] admin: Add set-origin command



commit 6bbfa5f85a936adeb31d88fa4b27f106fc2b38cf
Author: Colin Walters <walters verbum org>
Date:   Fri Jan 16 13:07:26 2015 -0500

    admin: Add set-origin command
    
    See projectatomic/rpm-ostree#42 for rationale. There are two high
    level use cases:
    
     - If the OS comes unconfigured, this is a way to point it at a repo of your choice.
     - To switch between repositories while keeping the same branch easily.

 Makefile-ostree.am                       |    1 +
 doc/Makefile.am                          |    2 +-
 doc/ostree-admin-set-origin.xml          |   96 ++++++++++++++++++++
 doc/ostree-admin.xml                     |    1 +
 src/ostree/ot-admin-builtin-set-origin.c |  146 ++++++++++++++++++++++++++++++
 src/ostree/ot-admin-builtins.h           |    1 +
 src/ostree/ot-builtin-admin.c            |    1 +
 tests/admin-test.sh                      |   10 ++
 8 files changed, 257 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-ostree.am b/Makefile-ostree.am
index b82932c..8aed75b 100644
--- a/Makefile-ostree.am
+++ b/Makefile-ostree.am
@@ -60,6 +60,7 @@ ostree_SOURCES += \
        src/ostree/ot-admin-builtin-instutil.c \
        src/ostree/ot-admin-builtin-cleanup.c \
        src/ostree/ot-admin-builtin-os-init.c \
+       src/ostree/ot-admin-builtin-set-origin.c \
        src/ostree/ot-admin-builtin-status.c \
        src/ostree/ot-admin-builtin-switch.c \
        src/ostree/ot-admin-builtin-upgrade.c \
diff --git a/doc/Makefile.am b/doc/Makefile.am
index 9e6dd15..1179a91 100644
--- a/doc/Makefile.am
+++ b/doc/Makefile.am
@@ -128,7 +128,7 @@ include $(top_srcdir)/gtk-doc.make
 man1_MANS =
 
 if ENABLE_GTK_DOC
-man1_MANS += ostree.1 ostree.repo.5 ostree.repo-config.5 ostree-admin-cleanup.1 ostree-admin-config-diff.1 
ostree-admin-deploy.1 ostree-admin-init-fs.1 ostree-admin-instutil.1 ostree-admin-os-init.1 
ostree-admin-status.1 ostree-admin-switch.1 ostree-admin-undeploy.1 ostree-admin-upgrade.1 ostree-admin.1 
ostree-cat.1 ostree-checkout.1 ostree-checksum.1 ostree-commit.1 ostree-config.1 ostree-diff.1 ostree-fsck.1 
ostree-init.1 ostree-log.1 ostree-ls.1 ostree-prune.1 ostree-pull-local.1 ostree-pull.1 ostree-refs.1 
ostree-remote.1 ostree-reset.1 ostree-rev-parse.1 ostree-show.1 ostree-summary.1 ostree-static-delta.1 
ostree-trivial-httpd.1
+man1_MANS += ostree.1 ostree.repo.5 ostree.repo-config.5 ostree-admin-cleanup.1 ostree-admin-config-diff.1 
ostree-admin-deploy.1 ostree-admin-init-fs.1 ostree-admin-instutil.1 ostree-admin-os-init.1 
ostree-admin-status.1 ostree-admin-set-origin.1 ostree-admin-switch.1 ostree-admin-undeploy.1 
ostree-admin-upgrade.1 ostree-admin.1 ostree-cat.1 ostree-checkout.1 ostree-checksum.1 ostree-commit.1 
ostree-config.1 ostree-diff.1 ostree-fsck.1 ostree-init.1 ostree-log.1 ostree-ls.1 ostree-prune.1 
ostree-pull-local.1 ostree-pull.1 ostree-refs.1 ostree-remote.1 ostree-reset.1 ostree-rev-parse.1 
ostree-show.1 ostree-summary.1 ostree-static-delta.1 ostree-trivial-httpd.1
 
 
 XSLTPROC_FLAGS = \
diff --git a/doc/ostree-admin-set-origin.xml b/doc/ostree-admin-set-origin.xml
new file mode 100644
index 0000000..86984da
--- /dev/null
+++ b/doc/ostree-admin-set-origin.xml
@@ -0,0 +1,96 @@
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+    "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd";>
+
+<!--
+Copyright 2015 Colin Walters <walters verbum org>
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2 of the License, or (at your option) any later version.
+
+This library 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
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the
+Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.
+-->
+
+<refentry id="ostree">
+
+    <refentryinfo>
+        <title>ostree admin set-origin</title>
+        <productname>OSTree</productname>
+
+        <authorgroup>
+            <author>
+                <contrib>Developer</contrib>
+                <firstname>Colin</firstname>
+                <surname>Walters</surname>
+                <email>walters verbum org</email>
+            </author>
+        </authorgroup>
+    </refentryinfo>
+
+    <refmeta>
+        <refentrytitle>ostree admin set-origin</refentrytitle>
+        <manvolnum>1</manvolnum>
+    </refmeta>
+
+    <refnamediv>
+        <refname>ostree-admin-set-origin</refname>
+        <refpurpose>Change the "origin" (location for upgrades)</refpurpose>
+    </refnamediv>
+
+    <refsynopsisdiv>
+            <cmdsynopsis>
+                <command>ostree admin set-origin </command> <arg choice="req">REMOTENAME</arg> <arg 
choice="req">URL</arg> <arg choice="opt">BRANCH</arg>
+            </cmdsynopsis>
+    </refsynopsisdiv>
+
+    <refsect1>
+        <title>Description</title>
+
+        <para>
+            Add a new remote named
+            <replaceable>REMOTENAME</replaceable> (if it does not
+            already exist).  Then change the origin file for the
+            current deployment.  This is the ref that will be
+            "tracked" and upgraded with <command>ostree admin
+            upgrade</command>.
+        </para>
+    </refsect1>
+
+    <refsect1>
+        <title>Options</title>
+
+        <variablelist>
+            <varlistentry>
+                <term><option>--set</option>=KEY=VALUE</term>
+
+                <listitem><para>
+                 Set an option for the remote.
+                </para></listitem>
+               
+           </varlistentry>
+           <varlistentry>
+                <term><option>--index</option>=INDEX</term>
+
+                <listitem><para> Change the origin of the deployment
+                numbered <replaceable>INDEX</replaceable> (starting
+                from 0).  </para></listitem>
+            </varlistentry>
+        </variablelist>
+    </refsect1>
+
+    <refsect1>
+        <title>Example</title>
+
+        <para><command>$ ostree admin set-origin exampleos http://os.example.com/repo 
exampleos/10.0/master/router</command></para>
+    </refsect1>    
+</refentry>
diff --git a/doc/ostree-admin.xml b/doc/ostree-admin.xml
index fd75b56..62cbbe9 100644
--- a/doc/ostree-admin.xml
+++ b/doc/ostree-admin.xml
@@ -72,6 +72,7 @@ Boston, MA 02111-1307, USA.
             <listitem><para><command>instutil</command></para></listitem>
             <listitem><para><command>os-init</command></para></listitem>
             <listitem><para><command>status</command></para></listitem>
+            <listitem><para><command>set-origin</command></para></listitem>
             <listitem><para><command>switch</command></para></listitem>
             <listitem><para><command>undeploy</command></para></listitem>
             <listitem><para><command>upgrade</command></para></listitem>            
diff --git a/src/ostree/ot-admin-builtin-set-origin.c b/src/ostree/ot-admin-builtin-set-origin.c
new file mode 100644
index 0000000..3beed86
--- /dev/null
+++ b/src/ostree/ot-admin-builtin-set-origin.c
@@ -0,0 +1,146 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2015 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "ot-main.h"
+#include "ot-admin-builtins.h"
+#include "ot-admin-functions.h"
+#include "ostree.h"
+#include "ot-tool-util.h"
+#include "otutil.h"
+#include "libgsystem.h"
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <glib/gi18n.h>
+
+static int opt_index = -1;
+static char **opt_set;
+
+static GOptionEntry options[] = {
+  { "set", 's', 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", 
"KEY=VALUE" },
+  { "index", 0, 0, G_OPTION_ARG_INT, &opt_index, "Operate on the deployment INDEX, starting from zero", 
"INDEX" },
+  { NULL }
+};
+
+gboolean
+ot_admin_builtin_set_origin (int argc, char **argv, GCancellable *cancellable, GError **error)
+{
+  gboolean ret = FALSE;
+  GOptionContext *context;
+  const char *remotename = NULL;
+  const char *url = NULL;
+  const char *branch = NULL;
+  gs_unref_object OstreeRepo *repo = NULL;
+  gs_unref_object OstreeSysroot *sysroot = NULL;
+  OstreeDeployment *target_deployment = NULL;
+
+  context = g_option_context_new ("REMOTENAME URL [BRANCH]");
+
+  if (!ostree_admin_option_context_parse (context, options, &argc, &argv, &sysroot, cancellable, error))
+    goto out;
+
+  if (argc < 3)
+    {
+      ot_util_usage_error (context, "REMOTENAME and URL must be specified", error);
+      goto out;
+    }
+
+  remotename = argv[1];
+  url = argv[2];
+  if (argc > 3)
+    branch = argv[3];
+
+  if (!ostree_sysroot_load (sysroot, cancellable, error))
+    goto out;
+
+  if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error))
+    goto out;
+
+  if (opt_index == -1)
+    {
+      target_deployment = ostree_sysroot_get_booted_deployment (sysroot);
+      if (target_deployment == NULL)
+        {
+          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Not currently booted into an OSTree system");
+          goto out;
+        }
+    }
+  else
+    {
+      target_deployment = ot_admin_get_indexed_deployment (sysroot, opt_index, error);
+      if (!target_deployment)
+        goto out;
+    }
+
+  { char **iter;
+    gs_unref_variant_builder GVariantBuilder *optbuilder =
+      g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
+
+    for (iter = opt_set; iter && *iter; iter++)
+      {
+        const char *keyvalue = *iter;
+        gs_free char *subkey = NULL;
+        gs_free char *subvalue = NULL;
+
+        if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error))
+          goto out;
+
+        g_variant_builder_add (optbuilder, "{s v}",
+                               subkey, g_variant_new_variant (g_variant_new_string (subvalue)));
+      }
+    
+    if (!ostree_repo_remote_change (repo, NULL,
+                                    OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS, 
+                                    remotename, url,
+                                    g_variant_builder_end (optbuilder),
+                                    cancellable, error))
+      goto out;
+  }
+  
+  { GKeyFile *old_origin = ostree_deployment_get_origin (target_deployment);
+    gs_free char *origin_refspec = g_key_file_get_string (old_origin, "origin", "refspec", NULL);
+    gs_free char *new_refspec = NULL;
+    gs_free char *origin_remote = NULL;
+    gs_free char *origin_ref = NULL;
+  
+    if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
+      goto out;
+
+    { gs_free char *new_refspec = g_strconcat (remotename, ":", branch ? branch : origin_ref, NULL);
+      gs_unref_keyfile GKeyFile *new_origin = NULL;
+      gs_unref_object GFile *origin_path = NULL;
+      
+      new_origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec);
+
+      if (!ostree_sysroot_write_origin_file (sysroot, target_deployment, new_origin,
+                                             cancellable, error))
+        goto out;
+    }
+  }
+
+  ret = TRUE;
+ out:
+  if (context)
+    g_option_context_free (context);
+  return ret;
+}
diff --git a/src/ostree/ot-admin-builtins.h b/src/ostree/ot-admin-builtins.h
index 9858f5b..5918aa2 100644
--- a/src/ostree/ot-admin-builtins.h
+++ b/src/ostree/ot-admin-builtins.h
@@ -35,6 +35,7 @@ gboolean ot_admin_builtin_undeploy (int argc, char **argv, GCancellable *cancell
 gboolean ot_admin_builtin_deploy (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_admin_builtin_cleanup (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_admin_builtin_status (int argc, char **argv, GCancellable *cancellable, GError **error);
+gboolean ot_admin_builtin_set_origin (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_admin_builtin_diff (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_admin_builtin_switch (int argc, char **argv, GCancellable *cancellable, GError **error);
 gboolean ot_admin_builtin_upgrade (int argc, char **argv, GCancellable *cancellable, GError **error);
diff --git a/src/ostree/ot-builtin-admin.c b/src/ostree/ot-builtin-admin.c
index 47cf1b7..b194143 100644
--- a/src/ostree/ot-builtin-admin.c
+++ b/src/ostree/ot-builtin-admin.c
@@ -44,6 +44,7 @@ static OstreeAdminCommand admin_subcommands[] = {
   { "init-fs", ot_admin_builtin_init_fs },
   { "instutil", ot_admin_builtin_instutil },
   { "os-init", ot_admin_builtin_os_init },
+  { "set-origin", ot_admin_builtin_set_origin },
   { "status", ot_admin_builtin_status },
   { "switch", ot_admin_builtin_switch },
   { "undeploy", ot_admin_builtin_undeploy },
diff --git a/tests/admin-test.sh b/tests/admin-test.sh
index f0e2e64..49596ba 100755
--- a/tests/admin-test.sh
+++ b/tests/admin-test.sh
@@ -148,6 +148,16 @@ ostree admin --sysroot=sysroot status
 
 echo "ok upgrade"
 
+originfile=$(ostree admin --sysroot=sysroot --print-current-dir).origin
+cp ${originfile} saved-origin
+ostree admin --sysroot=sysroot set-origin --index=0 bacon --set=gpg-verify=false http://tasty.com
+assert_file_has_content "${originfile}" "bacon:testos/buildmaster/x86_64-runtime"
+ostree --repo=sysroot/ostree/repo remote list -u > remotes.txt
+assert_file_has_content remotes.txt 'bacon.*http://tasty.com'
+cp saved-origin ${originfile}
+
+echo "ok set-origin"
+
 assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/etc/os-release 'NAME=TestOS'
 assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0/etc/os-release 'NAME=TestOS'
 ostree admin --sysroot=sysroot undeploy 1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]