File: plugin-podman-sdk-provider.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (498 lines) | stat: -rw-r--r-- 17,216 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* plugin-podman-sdk-provider.c
 *
 * Copyright 2024 Christian Hergert <chergert@redhat.com>
 *
 * 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.1 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 General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <stdio.h>

#include <json-glib/json-glib.h>
#include <libdex.h>

#include "plugin-distrobox-sdk.h"
#include "plugin-podman-sdk-provider.h"
#include "plugin-podman-sdk.h"
#include "plugin-toolbox-sdk.h"

#define PODMAN_RELOAD_DELAY_SECONDS 3

typedef struct _LabelToType
{
  const char *label;
  const char *value;
  GType type;
} LabelToType;

struct _PluginPodmanSdkProvider
{
  FoundrySdkProvider  parent_instance;
  GFileMonitor       *storage_monitor;
  GFileMonitor       *monitor;
  GArray             *label_to_type;
  guint               queued_update;
};

G_DEFINE_FINAL_TYPE (PluginPodmanSdkProvider, plugin_podman_sdk_provider, FOUNDRY_TYPE_SDK_PROVIDER)

static void
plugin_podman_sdk_provider_set_type_for_label (PluginPodmanSdkProvider *self,
                                               const char              *key,
                                               const char              *value,
                                               GType                    container_type)
{
  LabelToType map;

  g_return_if_fail (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));
  g_return_if_fail (key != NULL);
  g_return_if_fail (g_type_is_a (container_type, PLUGIN_TYPE_PODMAN_SDK));

  map.label = g_intern_string (key);
  map.value = g_intern_string (value);
  map.type = container_type;

  g_array_append_val (self->label_to_type, map);
}

static void
plugin_podman_sdk_provider_storage_dir_changed (PluginPodmanSdkProvider *self,
                                                GFile                   *file,
                                                GFile                   *other_file,
                                                GFileMonitorEvent        event,
                                                GFileMonitor            *monitor)
{
  g_autofree char *name = NULL;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));
  g_assert (G_IS_FILE (file));
  g_assert (G_IS_FILE_MONITOR (monitor));

  name = g_file_get_basename (file);

  if (g_strcmp0 (name, "db.sql") == 0)
    plugin_podman_sdk_provider_queue_update (self);
}

static DexFuture *
plugin_podman_sdk_provider_load_fiber (gpointer user_data)
{
  PluginPodmanSdkProvider *self = user_data;
  g_autoptr(GFile) file = NULL;
  g_autoptr(GFile) storage_dir = NULL;
  g_autofree char *data_dir = NULL;
  g_autofree char *parent_dir = NULL;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  g_set_str (&data_dir, g_get_user_data_dir ());
  if (data_dir == NULL)
    data_dir = g_build_filename (g_get_home_dir (), ".local", "share", NULL);

  g_assert (data_dir != NULL);

  storage_dir = g_file_new_build_filename (data_dir, "containers", "storage", NULL);
  parent_dir = g_build_filename (data_dir, "containers", "storage", "overlay-containers", NULL);
  file = g_file_new_build_filename (parent_dir, "containers.json", NULL);

  /* If our parent directory does not exist, we won't be able to monitor
   * for changes to the podman json file. Just create it upfront in the
   * same form that it'd be created by podman (mode 0700).
   */
  g_mkdir_with_parents (parent_dir, 0700);

  /* We have two files to monitor for potential updates. The containers.json
   * file is primarily how we've done it. But if we have hopes to track the
   * creation of containers via quadlet, we must monitor db.sql for changes.
   *
   * Since db.sql might not exist if we're the first to set things up, we
   * track changes to the @storage_dir directory. We could filter on what
   * files are changed there, but it isn't frequently changed and we delay
   * three seconds, so that doesn't seem necessary.
   */

  if ((self->monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL)))
    g_signal_connect_object (self->monitor,
                             "changed",
                             G_CALLBACK (plugin_podman_sdk_provider_queue_update),
                             self,
                             G_CONNECT_SWAPPED);

  if ((self->storage_monitor = g_file_monitor_directory (storage_dir, G_FILE_MONITOR_NONE, NULL, NULL)))
    g_signal_connect_object (self->storage_monitor,
                             "changed",
                             G_CALLBACK (plugin_podman_sdk_provider_storage_dir_changed),
                             self,
                             G_CONNECT_SWAPPED);

  return plugin_podman_sdk_provider_update (self);
}

static DexFuture *
plugin_podman_sdk_provider_load (FoundrySdkProvider *sdk_provider)
{
  PluginPodmanSdkProvider *self = (PluginPodmanSdkProvider *)sdk_provider;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  return dex_scheduler_spawn (NULL, 0,
                              plugin_podman_sdk_provider_load_fiber,
                              g_object_ref (self),
                              g_object_unref);
}

static DexFuture *
plugin_podman_sdk_provider_unload (FoundrySdkProvider *sdk_provider)
{
  PluginPodmanSdkProvider *self = (PluginPodmanSdkProvider *)sdk_provider;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  g_clear_object (&self->monitor);
  g_clear_object (&self->storage_monitor);
  g_clear_handle_id (&self->queued_update, g_source_remove);

  return FOUNDRY_SDK_PROVIDER_CLASS (plugin_podman_sdk_provider_parent_class)->unload (sdk_provider);
}

static void
plugin_podman_sdk_provider_finalize (GObject *object)
{
  PluginPodmanSdkProvider *self = (PluginPodmanSdkProvider *)object;

  g_clear_pointer (&self->label_to_type, g_array_unref);

  G_OBJECT_CLASS (plugin_podman_sdk_provider_parent_class)->finalize (object);
}

static void
plugin_podman_sdk_provider_class_init (PluginPodmanSdkProviderClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FoundrySdkProviderClass *sdk_provider_class = FOUNDRY_SDK_PROVIDER_CLASS (klass);

  object_class->finalize = plugin_podman_sdk_provider_finalize;

  sdk_provider_class->load = plugin_podman_sdk_provider_load;
  sdk_provider_class->unload = plugin_podman_sdk_provider_unload;
}

static void
plugin_podman_sdk_provider_init (PluginPodmanSdkProvider *self)
{
  self->label_to_type = g_array_new (FALSE, FALSE, sizeof (LabelToType));

  /* Prioritize "manager":"distrobox" above toolbox because it erroniously
   * can add com.github.containers.toolbox too! See chergert/ptyxis#245
   * for details.
   */
  plugin_podman_sdk_provider_set_type_for_label (self,
                                                 "manager",
                                                 "distrobox",
                                                 PLUGIN_TYPE_DISTROBOX_SDK);
  plugin_podman_sdk_provider_set_type_for_label (self,
                                                 "com.github.containers.toolbox",
                                                 NULL,
                                                 PLUGIN_TYPE_TOOLBOX_SDK);
}

static gboolean
container_is_infra (JsonObject *object)
{
  JsonNode *is_infra;
  g_assert (object != NULL);

  return json_object_has_member (object, "IsInfra") &&
      (is_infra = json_object_get_member (object, "IsInfra")) &&
      json_node_get_value_type (is_infra) == G_TYPE_BOOLEAN &&
      json_node_get_boolean (is_infra);
}

static gboolean
label_matches (JsonNode          *node,
               const LabelToType *l_to_t)
{
  if (l_to_t->value != NULL)
    return JSON_NODE_HOLDS_VALUE (node) &&
           json_node_get_value_type (node) == G_TYPE_STRING &&
           g_strcmp0 (l_to_t->value, json_node_get_string (node)) == 0;

  return TRUE;
}

static PluginPodmanSdk *
plugin_podman_sdk_provider_deserialize (PluginPodmanSdkProvider *self,
                                        JsonObject              *object)
{
  g_autoptr(PluginPodmanSdk) container = NULL;
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GError) error = NULL;
  JsonObject *labels_object;
  JsonNode *labels;
  GType gtype;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));
  g_assert (object != NULL);

  if (!(context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (self))))
    return NULL;

  gtype = PLUGIN_TYPE_PODMAN_SDK;

  if (json_object_has_member (object, "Labels") &&
      (labels = json_object_get_member (object, "Labels")) &&
      JSON_NODE_HOLDS_OBJECT (labels) &&
      (labels_object = json_node_get_object (labels)))
    {
      for (guint i = 0; i < self->label_to_type->len; i++)
        {
          const LabelToType *l_to_t = &g_array_index (self->label_to_type, LabelToType, i);

          if (json_object_has_member (labels_object, l_to_t->label))
            {
              JsonNode *match = json_object_get_member (labels_object, l_to_t->label);

              if (label_matches (match, l_to_t))
                {
                  gtype = l_to_t->type;
                  break;
                }
            }
        }
    }

  container = g_object_new (gtype,
                            "context", context,
                            NULL);

  if (!plugin_podman_sdk_deserialize (container, object, &error))
    {
      g_warning ("Failed to deserialize container JSON: %s", error->message);
      return NULL;
    }

  return g_steal_pointer (&container);
}

static DexFuture *
plugin_podman_sdk_provider_update_cb (DexFuture *completed,
                                      gpointer   user_data)
{
  PluginPodmanSdkProvider *self = user_data;
  g_autoptr(JsonParser) parser = NULL;
  g_autoptr(GPtrArray) sdks = NULL;
  g_autoptr(GError) error = NULL;
  g_autofree char *stdout_buf = NULL;
  JsonArray *root_array;
  JsonNode *root;

  g_assert (DEX_IS_FUTURE (completed));
  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  parser = json_parser_new ();

  if (!(stdout_buf = dex_await_string (dex_ref (completed), &error)) ||
      !json_parser_load_from_data (parser, stdout_buf, -1, &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  sdks = g_ptr_array_new_with_free_func (g_object_unref);

  if ((root = json_parser_get_root (parser)) &&
      JSON_NODE_HOLDS_ARRAY (root) &&
      (root_array = json_node_get_array (root)))
    {
      guint n_elements = json_array_get_length (root_array);

      for (guint i = 0; i < n_elements; i++)
        {
          g_autoptr(PluginPodmanSdk) container = NULL;
          JsonNode *element = json_array_get_element (root_array, i);
          JsonObject *element_object;

          if (JSON_NODE_HOLDS_OBJECT (element) &&
              (element_object = json_node_get_object (element)) &&
              !container_is_infra (element_object) &&
              (container = plugin_podman_sdk_provider_deserialize (self, element_object)))
            g_ptr_array_add (sdks, g_steal_pointer (&container));
        }
    }

  foundry_sdk_provider_merge (FOUNDRY_SDK_PROVIDER (self), sdks);

  return dex_future_new_true ();
}

DexFuture *
plugin_podman_sdk_provider_update (PluginPodmanSdkProvider *self)
{
  const GSubprocessFlags flags = G_SUBPROCESS_FLAGS_STDERR_SILENCE|G_SUBPROCESS_FLAGS_STDOUT_PIPE;
  g_autoptr(FoundryProcessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subprocess = NULL;
  g_autoptr(GError) error = NULL;
  DexFuture *future;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  launcher = foundry_process_launcher_new ();

  foundry_process_launcher_push_host (launcher);

  foundry_process_launcher_append_argv (launcher, "podman");
  foundry_process_launcher_append_argv (launcher, "ps");
  foundry_process_launcher_append_argv (launcher, "--all");
  foundry_process_launcher_append_argv (launcher, "--format=json");

  /* Ignore failures if podman fails to launch */
  if (!(subprocess = foundry_process_launcher_spawn_with_flags (launcher, flags, &error)))
    return dex_future_new_true ();

  future = foundry_subprocess_communicate_utf8 (subprocess, NULL);
  future = dex_future_then (future,
                            plugin_podman_sdk_provider_update_cb,
                            g_object_ref (self),
                            g_object_unref);

  return future;
}

static gboolean
plugin_podman_sdk_provider_update_source_func (gpointer user_data)
{
  PluginPodmanSdkProvider *self = user_data;

  g_assert (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  self->queued_update = 0;
  dex_future_disown (plugin_podman_sdk_provider_update (self));
  return G_SOURCE_REMOVE;
}

void
plugin_podman_sdk_provider_queue_update (PluginPodmanSdkProvider *self)
{
  g_return_if_fail (PLUGIN_IS_PODMAN_SDK_PROVIDER (self));

  if (self->queued_update == 0)
    self->queued_update = g_timeout_add_seconds_full (G_PRIORITY_LOW,
                                                      PODMAN_RELOAD_DELAY_SECONDS,
                                                      plugin_podman_sdk_provider_update_source_func,
                                                      self, NULL);
}

static DexFuture *
plugin_podman_sdk_provider_get_version_fiber (gpointer user_data)
{
  const GSubprocessFlags flags = G_SUBPROCESS_FLAGS_STDERR_SILENCE|G_SUBPROCESS_FLAGS_STDOUT_PIPE;
  g_autoptr(FoundryProcessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subprocess = NULL;
  g_autoptr(JsonParser) parser = NULL;
  g_autoptr(GError) error = NULL;
  g_autofree char *stdout_buf = NULL;
  JsonObject *obj;
  JsonNode *node;

  launcher = foundry_process_launcher_new ();

  foundry_process_launcher_push_host (launcher);

  foundry_process_launcher_append_argv (launcher, "podman");
  foundry_process_launcher_append_argv (launcher, "version");
  foundry_process_launcher_append_argv (launcher, "--format=json");

  if (!(subprocess = foundry_process_launcher_spawn_with_flags (launcher, flags, &error)))
    return dex_future_new_for_error (g_steal_pointer (&error));

  if (!(stdout_buf = dex_await_string (foundry_subprocess_communicate_utf8 (subprocess, NULL), &error)))
    return dex_future_new_for_error (g_steal_pointer (&error));

  parser = json_parser_new ();
  if (!json_parser_load_from_data (parser, stdout_buf, -1, &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  if ((node = json_parser_get_root (parser)) &&
      JSON_NODE_HOLDS_OBJECT (node) &&
      (obj = json_node_get_object (node)) &&
      json_object_has_member (obj, "Client") &&
      (node = json_object_get_member (obj, "Client")) &&
      JSON_NODE_HOLDS_OBJECT (node) &&
      (obj = json_node_get_object (node)) &&
      json_object_has_member (obj, "Version") &&
      (node = json_object_get_member (obj, "Version")) &&
      JSON_NODE_HOLDS_VALUE (node))
    return dex_future_new_take_string (g_strdup (json_node_get_string (node)));

  return dex_future_new_reject (G_IO_ERROR,
                                G_IO_ERROR_INVALID_DATA,
                                "Unknown JSON format");
}

static DexFuture *
plugin_podman_sdk_provider_get_version (void)
{
  return dex_scheduler_spawn (NULL, 0,
                              plugin_podman_sdk_provider_get_version_fiber,
                              NULL, NULL);
}

typedef struct
{
  guint major;
  guint minor;
  guint micro;
} Version;

static DexFuture *
plugin_podman_sdk_provider_check_version_cb (DexFuture *completed,
                                             gpointer   user_data)
{
  g_autoptr(GError) error = NULL;
  g_autofree char *version = NULL;
  Version *v = user_data;
  guint pmaj, pmin, pmic;

  g_assert (FOUNDRY_IS_MAIN_THREAD ());
  g_assert (DEX_IS_FUTURE (completed));
  g_assert (v != NULL);

  if (!(version = dex_await_string (dex_ref (completed), &error)))
    return dex_future_new_for_error (g_steal_pointer (&error));

  if (sscanf (version, "%u.%u.%u", &pmaj, &pmin, &pmic) != 3)
    return dex_future_new_reject (G_IO_ERROR,
                                  G_IO_ERROR_INVALID_DATA,
                                  "Invalid data returned from podman");

  if ((pmaj > v->major) ||
      ((pmaj == v->major) && (pmin > v->minor)) ||
      ((pmaj == v->major) && (pmin == v->minor) && (pmic >= v->micro)))
    return dex_future_new_true ();

  return dex_future_new_false ();
}

DexFuture *
plugin_podman_sdk_provider_check_version (guint major,
                                          guint minor,
                                          guint micro)
{
  Version version = {major, minor, micro};

  return dex_future_then (plugin_podman_sdk_provider_get_version (),
                          plugin_podman_sdk_provider_check_version_cb,
                          g_memdup2 (&version, sizeof version),
                          g_free);
}