aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/JshipIT.java
blob: afcf5410da13688a315a08fd1d7f78c1a54a9a6b (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-only

package io.github.jshipit;

import com.beust.jcommander.JCommander;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class JshipIT {

    public JshipIT(String[] args) {
        CommandShell commandShell = new CommandShell();
        CommandStart commandStart = new CommandStart();
        CommandCreate commandCreate = new CommandCreate();
        CommandPull commandPull = new CommandPull();
        CommandDelete commandDelete = new CommandDelete();
        JCommander commands = JCommander.newBuilder()
                .addCommand("create", commandCreate)
                .addCommand("pull", commandPull)
                .addCommand("start", commandStart)
                .addCommand("shell", commandShell)
                .addCommand("delete", commandDelete)
                .build();

        commands.parse(args);

        OCIDataStore dataStore = new OCIDataStore(System.getenv("HOME") + "/.local/share/jshipit/dataStore");

        if (commands.getParsedCommand() == null) {
            commands.usage();
            System.exit(1);
        } else if (commands.getParsedCommand().equals("create")) {
            if (commandCreate.containerName == null) {
                System.out.println("Container name is required");
                System.exit(1);
            }

            // Parse the image name into the registry, repo, image name and tag
            List<String> image = new ArrayList<String>(Arrays.asList(commandCreate.containerImage.split("/")));
            String containerImage = image.get(image.size() - 1).split(":")[0];
            String apiRepo = image.get(0);
            image.remove(0);
            image.remove(image.size() - 1);
            String containerRepo = String.join("/", image);

            // Convert the registry name to the OCI registry name
            if (apiRepo.equalsIgnoreCase("docker.io")) {
                apiRepo = "registry.docker.io";
            }

            ContainerManager containerManager = new ContainerManager(
                    commandCreate.containerName,
                    containerImage,
                    commandCreate.containerImage.split(":")[1],
                    apiRepo,
                    containerRepo,
                    commandCreate.containerIsolated,
                    dataStore
            );

            containerManager.createContainer();
        } else if (commands.getParsedCommand().equals("pull")) {

            // Parse the image name into the registry, repo, image name and tag
            List<String> image = new ArrayList<String>(Arrays.asList(commandPull.containerImage.split("/")));
            String containerImage = image.get(image.size() - 1).split(":")[0];
            String apiRepo = image.get(0);
            image.remove(0);
            image.remove(image.size() - 1);
            String containerRepo = String.join("/", image);

            // Convert the registry name to the OCI registry name
            if (apiRepo.equalsIgnoreCase("docker.io")) {
                apiRepo = "registry.docker.io";
            }

            System.out.println("Pulling image " + containerImage + " from " + apiRepo + "/" + containerRepo);
            dataStore.createImage(
                    apiRepo,
                    containerRepo,
                    containerImage,
                    commandPull.containerImage.split(":")[1]
            );

        } else if (commands.getParsedCommand().equals("start")) {
            ContainerManager containerManager = new ContainerManager(
                    commandStart.containerName,
                    commandStart.containerCommand,
                    commandStart.containerMount,
                    dataStore
            );

            containerManager.runCommand();
        } else if (commands.getParsedCommand().equals("shell")) {
            ContainerManager containerManager = new ContainerManager(
                    commandShell.containerName,
                    "/bin/sh", // A proper linux system should always have /bin/sh, skill issue if it doesn't
                    commandShell.containerMount,
                    dataStore
            );

            containerManager.runCommand();
        } else if (commands.getParsedCommand().equals("delete")) {
            ContainerManager containerManager = new ContainerManager(
                    commandDelete.containerName,
                    null,
                    null,
                    dataStore
            );

            containerManager.deleteContainer();
        }
    }
}