aboutsummaryrefslogtreecommitdiff
path: root/src/cmd_create.c
blob: a68fcb7cb6913f844f438a8330f47a5ce40c4e06 (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
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>

#include "dbx.h"

int dbx_create(struct dbx_engine *engine, struct dbx_options const *options) {
  char ssh_pubkey[PATH_MAX];
  if (!dbx_ssh_init(options->name, ssh_pubkey)) {
    dbx_printerr("failed to generate SSH key");
    return EX_CANTCREAT;
  }

  if (!dbx_engine_create(engine, options->image, options->name, ssh_pubkey,
                         options->ports.host)) {
    dbx_printerr("failed to create devbox");
    return EX_CANTCREAT;
  }

  int result = EXIT_SUCCESS;
  if (options->post_install != NULL) {
    char hostname[HOSTNAME_MAX];
    uint16_t sshd_port;
    if (!dbx_engine_start(engine, options->name, hostname, &sshd_port)) {
      return EX_CONFIG;
    }

    char tmp[PATH_MAX];
    if (dbx_fpath(tmp, "%s:/tmp/post-install", options->name) < 0 ||
        !dbx_ssh_config(options->name, hostname, sshd_port) ||
        !dbx_ssh_cp(options->name, options->post_install, tmp) ||
        !dbx_ssh_sys(options->name,
                     "chmod +x /tmp/post-install && /tmp/post-install")) {
      result = EX_CONFIG;
    }

    if (!dbx_engine_stop(engine, options->name)) {
      result = EX_CONFIG;
    }
  }

  return result;
}