aboutsummaryrefslogtreecommitdiff
path: root/src/engine_container.c
blob: 89d37069b8e5b60a0e5e328fa51d623c16314c6d (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
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
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dbx.h"

struct dbx_engine {
  char container[PATH_MAX];
};

struct dbx_engine *dbx_engine_init(void) {
  struct dbx_engine *engine = malloc(sizeof(struct dbx_engine));
  if (engine == NULL) {
    dbx_perror("calloc", errno);
    return NULL;
  }

  int result = dbx_proc_find("container", engine->container);
  if (result) {
    dbx_perror("unable to find `container` executable", result);
    goto error;
  }

  int status_exit;
  const char *const status[] = {"system", "status", NULL};
  result = dbx_proc_run(status, DBXFD_NONE, &status_exit);
  if (result) {
    dbx_perror("unable to check `container` status", result);
    goto error;
  }
  if (status_exit != 0) {
    dbx_printerr("container engine must started with `container system start`");
    goto error;
  }

  return engine;

error:
  free(engine);
  return NULL;
}

bool dbx_engine_create(struct dbx_engine *engine, const char *image,
                       const char *name, const char *ssh_pubkey,
                       uint16_t sshd_port) {
  // The container machine has its own network and does not bind-mount a port
  // on the host. We can therefore ignore the `sshd_port` setting.
  (void)sshd_port;

  int create_exit;
  const char *const status[] = {
      engine->container, "machine",           "create", "--name", name,
      "--no-boot",       "--home-mount=none", image,    NULL};
  int result = dbx_proc_run(status, DBXFD_STDOUT | DBXFD_STDERR, &status_exit);
  if (result) {
    dbx_perror("unable to create `container`", result);
    return false;
  }
  if (create_exit != 0) {
    dbx_printerr("container creation failed");
    return false;
  }

  // Unfortunately, there is no way to access files from the container machine
  // while it is stopped; instead boot it and run a small script that can
  // authorize our SSH key.
  char *key;
  result = dbx_readfile(ssh_pubkey, &key);
  if (result) {
    dbx_perror("unable to read SSH public key", result);
    return false;
  }
  char script[1024];
  int n = snprintf(script, sizeof(script),
                   "echo '%s' > /etc/ssh/authorized_keys", key);
  if (n < 0 || n >= (int)sizeof(script)) {
    dbx_printerr("invalid SSH public key format");
    return false;
  }

  // Note that `container machine run` _requires_ `stdin` for some reason (I
  // assume it is to attach to the VM process), make sure to not close it.

  if (dbx_proc_run(DBX_CMD(engine->container, "machine", "run", "--name", name,
                           "--root", script),
                   DBXFD_ALL) != 0) {
    return false;
  }

  // Make sure to shut down the machine, this will restart `SSHD` to ensure the
  // port configuration is correctly picked up.
  return dbx_engine_stop(engine, name);
}

static bool read_ipaddr(char *json, char hostname[HOSTNAME_MAX]) {
  assert(json != NULL && hostname != NULL);

  // We only need to extract the `ipAddress` field from the JSON, so abuse
  // `strtok_r` to read tokens until we get there. Assume that the JSON is valid
  // and that there is only a single `ipAddress` field in the document.

  char *tok, *save;
  const char *sep = " \t\r\n\":";
  for (tok = strtok_r(json, sep, &save); tok != NULL;
       tok = strtok_r(NULL, sep, &save)) {
    if (strcmp(tok, "ipAddress") == 0) {
      // The next token is going to be the IP address value.
      tok = strtok_r(NULL, sep, &save);
      break;
    }
  }

  if (tok == NULL) {
    return false;
  }

  int n = snprintf(hostname, HOSTNAME_MAX, "%s", tok);
  return n >= 0 && n < HOSTNAME_MAX;
}

bool dbx_engine_start(struct dbx_engine *engine, const char *name,
                      char hostname[HOSTNAME_MAX], uint16_t *sshd_port) {
  // The only way to start a comand is with `run` - so run something that
  // does nothing but return `0` exit code.
  if (dbx_proc_run(DBX_CMD(engine->container, "machine", "run", "--name", name,
                           "--root", "true"),
                   DBXFD_STDERR) != 0) {
    return false;
  }

  if (hostname != NULL) {
    char json[8192];
    if (dbx_proc_output(DBX_CMD(engine->container, "machine", "inspect", name),
                        json, sizeof(json)) != 0) {
      return false;
    }

    if (!read_ipaddr(json, hostname)) {
      return false;
    }
  }

  if (sshd_port != NULL) {
    *sshd_port = 22;
  }

  return true;
}

bool dbx_engine_stop(struct dbx_engine *engine, const char *name) {
  return dbx_proc_run(DBX_CMD(engine->container, "machine", "stop", name),
                      DBXFD_STDERR) == 0;
}

void dbx_engine_destroy(struct dbx_engine *engine) { free(engine); }