mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(start): give each instance its own Docker resource namespace and base URL (#31)
This commit is contained in:
1 parent
788eb470fb
commit
bf9ee4037b
14 files changed
+310
-8
No files matched your search
@@ -68,6 +68,9 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
@Option(names = {"--services"}, description = "create: comma-separated services to enable", paramLabel = "<csv>")
|
||||
String services;
|
||||
|
||||
@Option(names = {"--namespace"}, description = "create: Docker resource namespace for the containers this instance launches (default: derived from --container)", paramLabel = "<name>")
|
||||
String namespace;
|
||||
|
||||
@Spec
|
||||
CommandSpec spec;
|
||||
|
||||
@@ -104,6 +107,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
row.put("image", p.image);
|
||||
row.put("persistDir", p.persistDir);
|
||||
row.put("services", p.services);
|
||||
row.put("namespace", p.namespace);
|
||||
out.add(row);
|
||||
}
|
||||
printer.structured(out);
|
||||
@@ -143,6 +147,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
if (p.port != null) printer.println(" port: " + p.port);
|
||||
if (p.persistDir != null) printer.println(" persistDir: " + p.persistDir);
|
||||
if (p.services != null) printer.println(" services: " + p.services);
|
||||
if (p.namespace != null) printer.println(" namespace: " + p.namespace);
|
||||
return 0;
|
||||
} catch (IOException e) {
|
||||
printer.error("Could not read profile: " + e.getMessage());
|
||||
@@ -171,8 +176,9 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
if (typed("--image")) p.image = image;
|
||||
if (typed("--persist")) p.persistDir = persistDir;
|
||||
if (typed("--services")) p.services = services;
|
||||
if (typed("--namespace")) p.namespace = namespace;
|
||||
|
||||
for (String warning : collisions(store.list(), p)) {
|
||||
for (String warning : collisions(profile, store.list(), p)) {
|
||||
printer.warn(warning);
|
||||
}
|
||||
store.save(p);
|
||||
@@ -191,25 +197,38 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
|
||||
/**
|
||||
* Two profiles that share a container are the same instance, and two that share a port on the
|
||||
* same image cannot run at once. Both are legitimate (one instance, used at different times),
|
||||
* same image cannot run at once. Two of one emulator that share a resource namespace remove
|
||||
* each other's child containers. All are legitimate (one instance, used at different times),
|
||||
* so they are warnings, not errors.
|
||||
*/
|
||||
static List<String> collisions(List<Profile> existing, Profile created) {
|
||||
static List<String> collisions(ProductProfile product, List<Profile> existing, Profile created) {
|
||||
List<String> warnings = new ArrayList<>();
|
||||
for (Profile other : existing) {
|
||||
if (other.name == null || other.name.equals(created.name)) continue;
|
||||
if (created.container != null && created.container.equals(other.container)) {
|
||||
warnings.add("Profile '" + other.name + "' already uses container '" + created.container + "'.\n"
|
||||
+ "Pick another --container, or start only one of them at a time.");
|
||||
} else if (created.port != null && created.port.equals(other.port)
|
||||
&& repository(created.image).equals(repository(other.image))) {
|
||||
continue; // one instance: its port and namespace are the same by definition
|
||||
}
|
||||
boolean sameEmulator = repository(created.image).equals(repository(other.image));
|
||||
if (sameEmulator && created.port != null && created.port.equals(other.port)) {
|
||||
warnings.add("Profile '" + other.name + "' already uses port " + created.port + ".\n"
|
||||
+ "Pick another --port, or start only one of them at a time.");
|
||||
}
|
||||
// Reported separately: a new port alone does not stop shared child containers.
|
||||
String ns = sameEmulator ? namespaceOf(product, created) : null;
|
||||
if (ns != null && ns.equals(namespaceOf(product, other))) {
|
||||
warnings.add("Profile '" + other.name + "' already uses resource namespace '" + ns + "'.\n"
|
||||
+ "Pass --namespace to give this instance its own.");
|
||||
}
|
||||
}
|
||||
return warnings;
|
||||
}
|
||||
|
||||
private static String namespaceOf(ProductProfile product, Profile p) {
|
||||
return p.namespace != null && !p.namespace.isBlank() ? p.namespace : product.resourceNamespace(p.container);
|
||||
}
|
||||
|
||||
// floci/floci-az:latest -> floci/floci-az, so two tags of one emulator still collide on a port.
|
||||
private static String repository(String image) {
|
||||
if (image == null) return "";
|
||||
|
||||
Reference in new issue
Block a user