use of aQute.jpm.lib.ServiceData in project bnd by bndtools.
the class Main method _update.
@Description("Perform updates for installed commands and services")
public void _update(UpdateOptions opts) throws Exception {
if (!jpm.hasAccess()) {
error("No write acces, might require administrator or root privileges (sudo in *nix)");
return;
}
ArrayList<String> refs = new ArrayList<String>();
for (CommandData data : jpm.getCommands()) {
refs.add(data.name);
}
for (ServiceData data : jpm.getServices()) {
refs.add(data.name);
}
ArrayList<UpdateMemo> notFound = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
ArrayList<UpdateMemo> upToDate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
ArrayList<UpdateMemo> toUpdate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
ArrayList<CommandData> datas = new ArrayList<CommandData>();
if (opts._arguments().size() == 0) {
datas.addAll(jpm.getCommands());
datas.addAll(jpm.getServices());
} else {
for (String pattern : opts._arguments()) {
Glob glob = new Glob(pattern);
for (String name : refs) {
if (glob.matcher(name).matches()) {
CommandData data = jpm.getCommand(name);
if (data == null) {
Service service = jpm.getService(name);
if (service != null) {
data = service.getServiceData();
}
}
if (data != null) {
datas.add(data);
}
}
}
}
}
for (CommandData data : datas) {
jpm.listUpdates(notFound, upToDate, toUpdate, data, opts.staged());
}
if (opts.all() || opts._arguments().size() > 0) {
for (UpdateMemo memo : toUpdate) {
jpm.update(memo);
}
out.format("%d command(s) updated%n", toUpdate.size());
} else {
Justif justif = new Justif(100, 20, 50);
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb);
if (upToDate.size() > 0) {
f.format("Up to date:%n");
for (UpdateMemo memo : upToDate) {
if (memo.current instanceof ServiceData) {
f.format(" - %s (service) \t0- %s%n", memo.current.name, memo.current.version);
} else {
f.format(" - %s \t0- %s%n", memo.current.name, memo.current.version);
}
}
f.format("%n");
}
if (toUpdate.size() > 0) {
f.format("Update available:%n");
for (UpdateMemo memo : toUpdate) {
if (memo.current instanceof ServiceData) {
f.format(" - %s (service) \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
} else {
f.format(" - %s \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
}
}
f.format("%n");
}
if (notFound.size() > 0) {
if (opts.staged()) {
f.format("Information not found (local install ?):%n");
} else {
f.format("Information not found (try including staging versions with the --staged (-s) flag)%n");
}
for (UpdateMemo memo : notFound) {
if (memo.current instanceof ServiceData) {
f.format(" - %s (service)%n", memo.current.name);
} else {
f.format(" - %s%n", memo.current.name);
}
}
}
if (toUpdate.size() > 0) {
f.format("%nIn order to apply all possible updates, run jpm update again with the --all (or -a) flag.%n");
}
f.flush();
justif.wrap(sb);
out.println(sb.toString());
f.close();
}
}
use of aQute.jpm.lib.ServiceData in project bnd by bndtools.
the class Main method _service.
@Description("Manage the jpm4j services")
public void _service(ServiceOptions opts) throws Exception {
if (opts._arguments().isEmpty()) {
for (ServiceData sd : jpm.getServices()) print(sd);
return;
}
List<String> cmdline = opts._arguments();
String name = cmdline.remove(0);
Service s = jpm.getService(name);
if (opts.remove()) {
if (!jpm.hasAccess()) {
error("No write access to remove service %s", name);
return;
}
if (s == null) {
error("No such service %s to remove", name);
return;
}
s.stop();
s.remove();
return;
}
if (opts.create() != null) {
logger.debug("create service");
if (s != null) {
error("Service already exists, cannot be created: %s. Update or remove it first", name);
return;
}
ArtifactData target = jpm.getCandidate(opts.create());
if (target == null) {
error("Cannot find candidate for coordinates", opts.create());
return;
}
ServiceData data = new ServiceData();
CommandData cmd = jpm.parseCommandData(target);
for (Field f : cmd.getClass().getFields()) {
f.set(data, f.get(cmd));
}
logger.debug("service data {}", cmd);
data.name = name;
updateServiceData(data, opts);
logger.debug("update service data");
String result = jpm.createService(data, false);
if (result != null)
error("Create service failed: %s", result);
return;
}
if (s == null) {
error("No such service: %s", name);
return;
}
ServiceData data = s.getServiceData();
if (updateServiceData(data, opts) || opts.coordinates() != null || opts.update()) {
if (!jpm.hasAccess()) {
error("No write access to update service %s", name);
return;
}
if (opts.coordinates() != null || opts.update()) {
String coordinates = opts.coordinates();
if (coordinates == null) {
error("No coordinate found in old service record");
return;
}
int n = coordinates.indexOf('@');
if (n > 0)
coordinates = coordinates.substring(0, n);
logger.debug("Updating from coordinate: {}", coordinates);
ArtifactData target = jpm.getCandidate(coordinates);
if (target == null) {
error("No candidates found for %s (%s)", coordinates, opts.staged() ? "staged" : "only masters");
return;
}
CommandData cmd = jpm.parseCommandData(target);
for (Field f : cmd.getClass().getFields()) {
f.set(data, f.get(cmd));
}
data.name = name;
}
String result = jpm.createService(data, true);
if (result != null)
error("Update service failed: %s", result);
else if (s.isRunning())
warning("Changes will not affect the currently running process");
}
Data.details(data, out);
}
use of aQute.jpm.lib.ServiceData in project bnd by bndtools.
the class Main method _log.
@Description("Show the service log")
public void _log(logOptions opts) throws Exception {
String s = opts._arguments().isEmpty() ? null : opts._arguments().get(0);
if (s == null) {
error("No such service %s", s);
return;
}
Service service = jpm.getService(s);
if (service == null) {
error("No such service %s", s);
return;
}
ServiceData data = service.getServiceData();
File logFile = new File(data.log);
if (!logFile.isFile()) {
error("Log file %s for service %s is not a file", logFile, s);
return;
}
if (opts.clear()) {
IO.delete(logFile);
logFile.createNewFile();
}
try (RandomAccessFile raf = new RandomAccessFile(logFile, "r")) {
long start = Math.max(logFile.length() - 2000, 0);
while (true) {
long l = raf.length();
byte[] buffer = new byte[(int) (l - start)];
raf.seek(start);
raf.read(buffer);
out.write(buffer);
start = l;
if (!service.isRunning() || !opts.tail())
return;
if (l == raf.length())
Thread.sleep(100);
}
}
}
use of aQute.jpm.lib.ServiceData in project bnd by bndtools.
the class Main method _start.
/**
* Start a service.
*
* @param options
* @throws Exception
*/
@Description("Start a service")
public void _start(startOptions options) throws Exception {
if (!jpm.hasAccess()) {
error("No write acces, might require administrator or root privileges (sudo in *nix)");
return;
}
for (String s : options._arguments()) {
Service service = jpm.getService(s);
if (service == null)
error("Non existent service %s", s);
else {
if (!service.isRunning()) {
try {
ServiceData d = service.getServiceData();
logger.debug("starting {} as user {}, lock={}, log={}", d.name, d.user, d.lock, d.log);
if (options.clean())
service.clear();
String result = service.start();
if (result != null)
error("Failed to start: %s", result);
} catch (Exception e) {
exception(e, "Could not start service %s due to %s", s, e);
}
} else
warning("Service %s already running", s);
}
}
}
use of aQute.jpm.lib.ServiceData in project bnd by bndtools.
the class Main method _remove.
@Description("Remove a command or a service from the system")
public void _remove(UninstallOptions opts) throws Exception {
if (!jpm.hasAccess()) {
error("No write acces, might require administrator or root privileges (sudo in *nix)");
return;
}
ArrayList<String> toDelete = new ArrayList<String>();
ArrayList<String> names = new ArrayList<String>();
List<CommandData> commands = jpm.getCommands();
for (CommandData command : commands) {
names.add(command.name);
}
List<ServiceData> services = jpm.getServices();
for (ServiceData service : services) {
names.add(service.name);
}
for (String pattern : opts._arguments()) {
Glob glob = new Glob(pattern);
for (String name : names) {
if (glob.matcher(name).matches()) {
toDelete.add(name);
}
}
}
int ccount = 0, scount = 0;
for (String name : toDelete) {
Service s = null;
if (jpm.getCommand(name) != null) {
// Try command first
logger.debug("Corresponding command found, removing");
jpm.deleteCommand(name);
ccount++;
} else if ((s = jpm.getService(name)) != null) {
// No command
// matching, try
// service
logger.debug("Corresponding service found, removing");
s.remove();
scount++;
} else {
// No match amongst commands & services
error("No matching command or service found for: %s", name);
}
}
out.format("%d command(s) removed and %d service(s) removed%n", ccount, scount);
}
Aggregations