use of com.hashicorp.nomad.apimodel.Port in project twister2 by DSC-SPIDAL.
the class NomadController method getShellDriver.
private Task getShellDriver(JobAPI.Job job) {
String taskName = job.getJobId();
Task task = new Task();
// get the job working directory
String workingDirectory = NomadContext.workingDirectory(config);
String jobWorkingDirectory = Paths.get(workingDirectory, job.getJobId()).toString();
String configDirectoryName = Paths.get(workingDirectory, job.getJobId(), SchedulerContext.clusterType(config)).toString();
String corePackageFile = SchedulerContext.temporaryPackagesPath(config) + "/" + SchedulerContext.corePackageFileName(config);
String jobPackageFile = SchedulerContext.temporaryPackagesPath(config) + "/" + SchedulerContext.jobPackageFileName(config);
String nomadScriptContent = getNomadScriptContent(config, configDirectoryName);
task.setName(taskName);
task.setDriver("raw_exec");
task.addConfig(NomadContext.NOMAD_TASK_COMMAND, NomadContext.SHELL_CMD);
String[] args = workerProcessCommand(workingDirectory, job);
task.addConfig(NomadContext.NOMAD_TASK_COMMAND_ARGS, args);
Template template = new Template();
template.setEmbeddedTmpl(nomadScriptContent);
template.setDestPath(NomadContext.NOMAD_SCRIPT_NAME);
task.addTemplates(template);
Resources resourceReqs = new Resources();
String portNamesConfig = NomadContext.networkPortNames(config);
String[] portNames = portNamesConfig.split(",");
// configure nomad to allocate dynamic ports
Port[] ports = new Port[portNames.length];
int i = 0;
for (String p : portNames) {
ports[i] = new Port().setLabel(p);
i++;
}
NetworkResource networkResource = new NetworkResource();
networkResource.addDynamicPorts(ports);
resourceReqs.addNetworks(networkResource);
JobAPI.ComputeResource computeResource = JobUtils.getComputeResource(job, 0);
if (computeResource == null) {
LOG.log(Level.SEVERE, "Error: there is no compute resource");
return null;
}
int cpu = (int) computeResource.getCpu();
int disk = (int) computeResource.getDiskGigaBytes();
int memory = computeResource.getRamMegaBytes();
resourceReqs.setCpu(cpu * 200);
resourceReqs.setMemoryMb(memory);
resourceReqs.setDiskMb(disk * 1024);
LOG.log(Level.INFO, "Compute resources are " + cpu + " " + memory + " " + disk);
Map<String, String> envVars = new HashMap<>();
envVars.put(NomadContext.WORKING_DIRECTORY_ENV, NomadContext.workingDirectory(config));
if (!NomadContext.sharedFileSystem(config)) {
envVars.put(NomadContext.DOWNLOAD_PACKAGE_ENV, "false");
} else {
envVars.put(NomadContext.DOWNLOAD_PACKAGE_ENV, "true");
}
// we are putting the core packages as env variable
envVars.put(NomadContext.CORE_PACKAGE_ENV, corePackageFile);
envVars.put(NomadContext.JOB_PACKAGE_ENV, jobPackageFile);
task.setEnv(envVars);
task.setResources(resourceReqs);
return task;
}
Aggregations