Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better reporting from install #1349

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 90 additions & 71 deletions src/main/java/org/myrobotlab/framework/repo/IvyWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void rawlog(String msg, int level) {
static String ivysettingsXmlTemplate = null;

static String ivyXmlTemplate = null;

transient static IvyWrapper localInstance = null;

public static final Filter NO_FILTER = NoFilter.INSTANCE;
Expand Down Expand Up @@ -404,97 +405,119 @@ public void installDependency(String location, ServiceDependency library) {
}

@Override
synchronized public void install(String location, String[] serviceTypes) {

try {
synchronized public void install(String location, String[] serviceTypes) throws IOException {

Set<ServiceDependency> targetLibraries = getUnfulfilledDependencies(serviceTypes);
Set<ServiceDependency> targetLibraries = getUnfulfilledDependencies(serviceTypes);

if (targetLibraries.size() == 0) {
StringBuilder sb = new StringBuilder();
for (String type : serviceTypes) {
if (type.lastIndexOf(".") > 0) {
sb.append(type.substring(type.lastIndexOf(".") + 1));
} else {
sb.append(type);
}
sb.append(" ");
if (targetLibraries.size() == 0) {
StringBuilder sb = new StringBuilder();
for (String type : serviceTypes) {
if (type.lastIndexOf(".") > 0) {
sb.append(type.substring(type.lastIndexOf(".") + 1));
} else {
sb.append(type);
}
info("%s already installed", sb.toString());
return;
sb.append(" ");
}
info("%s already installed", sb.toString());
return;
}

publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_START, String.format("starting install of %s", (Object[]) serviceTypes)));
publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_START, String.format("starting install of %s", (Object[]) serviceTypes)));

log.info("installing {} services into {}", serviceTypes.length, location);
log.info("installing {} services into {}", serviceTypes.length, location);

// create build files - generates appropriate ivy.xml and settings files
// this service file should be marked as dependencies all others
// should be marked as provided
// ??? do "provided" get incorporate in the resolve ?
createBuildFiles(location, serviceTypes);
// create build files - generates appropriate ivy.xml and settings files
// this service file should be marked as dependencies all others
// should be marked as provided
// ??? do "provided" get incorporate in the resolve ?
createBuildFiles(location, serviceTypes);

Platform platform = Platform.getLocalInstance();
Platform platform = Platform.getLocalInstance();

// templates [originalname](-[classifier])(-[revision]).[ext] parens are "optional"
String[] cmd = new String[] { "-settings", location + "/ivysettings.xml", "-ivy", location + "/ivy.xml", "-retrieve", location + "/jar" + "/[originalname].[ext]" };
// templates [originalname](-[classifier])(-[revision]).[ext] parens are
// "optional"

StringBuilder sb = new StringBuilder();
sb.append("wget https://repo1.maven.org/maven2/org/apache/ivy/ivy/" + IVY_VERSION + "/ivy-" + IVY_VERSION + ".jar\n");
sb.append("java -jar ivy-" + IVY_VERSION + ".jar");
for (String s : cmd) {
sb.append(" ");
sb.append(s);
}
List<String> cmd = new ArrayList<>();
cmd.add("-settings");
cmd.add(location + "/ivysettings.xml");
cmd.add("-ivy");
cmd.add(location + "/ivy.xml");
cmd.add("-retrieve");
cmd.add(location + "/jar" + "/[originalname].[ext]");

sb.append("\n");
int msgLevel = Message.MSG_WARN;
if (log.isWarnEnabled() || log.isErrorEnabled()) {
msgLevel = Message.MSG_WARN;
cmd.add("-warn");
} else {
msgLevel = Message.MSG_INFO;
}

log.info("cmd {}", sb);
FileIO.toFile("libraries/install.sh", sb.toString().getBytes());
StringBuilder sb = new StringBuilder();
sb.append("wget https://repo1.maven.org/maven2/org/apache/ivy/ivy/" + IVY_VERSION + "/ivy-" + IVY_VERSION + ".jar\n");
sb.append("java -jar ivy-" + IVY_VERSION + ".jar");
for (String s : cmd) {
sb.append(" ");
sb.append(s);
}

Ivy ivy = Ivy.newInstance(); // <-- for future 2.5.x release
ivy.getLoggerEngine().pushLogger(new IvyWrapperLogger(Message.MSG_INFO));
sb.append("\n");

ResolveReport report = null;
List<String> err = new ArrayList<>();
try {
report = Main.run(cmd);
} catch(Exception e) {
err.add(e.toString());
}
log.info("cmd {}", sb);
FileIO.toFile("libraries/install.sh", sb.toString().getBytes());

// if no errors -h
// mark "service" as installed
// mark all libraries as installed
Ivy ivy = Ivy.newInstance(); // <-- for future 2.5.x release
ivy.getLoggerEngine().pushLogger(new IvyWrapperLogger(msgLevel));

if (report != null) {
List<String> problems = report.getAllProblemMessages();
for (String problem : problems) {
if (!problem.startsWith("WARN: symlinkmass")) {
err.add(problem);
}
ResolveReport report = null;
List<String> err = new ArrayList<>();
try {
report = Main.run(cmd.toArray(new String[0]));
} catch (Exception e) {
err.add(e.toString());
}

// if no errors -h
// mark "service" as installed
// mark all libraries as installed

if (report != null) {
List<String> problems = report.getAllProblemMessages();
for (String problem : problems) {
if (!problem.startsWith("WARN: symlinkmass")) {
err.add(problem);
}
}
}

if (err.size() > 0) {
log.error("had errors - repo will not be updated. Errors:\n{}", err);
} else {

// TODO - promote to Repo.setInstalled
for (ServiceDependency library : targetLibraries) {
// set as installed & save state
library.setInstalled(true);
installedLibraries.put(library.toString(), library);
info("installed %s platform %s", library, platform.getPlatformId());
}
save();
if (err.size() > 0) {
log.error("had errors - repo will not be updated. Errors:\n{}", err);
} else {

// TODO - promote to Repo.setInstalled
for (ServiceDependency library : targetLibraries) {
// set as installed & save state
library.setInstalled(true);
installedLibraries.put(library.toString(), library);
info("installed %s platform %s", library, platform.getPlatformId());
}
save();
}

if (report == null) {
String errorDetail = String.format("There were problems resolving dependencies %s", (Object[]) serviceTypes);
log.error(errorDetail);
publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.ERROR, Repo.INSTALL_FINISHED, errorDetail));
throw new RuntimeException(errorDetail);
} else {

ArtifactDownloadReport[] artifacts = report.getAllArtifactsReports();
for (int i = 0; i < artifacts.length; ++i) {
ArtifactDownloadReport ar = artifacts[i];
Artifact artifact = ar.getArtifact();
// String filename = IvyPatternHelper.substitute("[originalname].[ext]",
// String filename =
// IvyPatternHelper.substitute("[originalname].[ext]",
// artifact);

File file = ar.getLocalFile();
Expand All @@ -508,17 +531,13 @@ synchronized public void install(String location, String[] serviceTypes) {
info("unzipped %s", filename);
} catch (Exception e) {
log.error("unable to unzip file {}", filename, e);
throw new IOException(String.format("unable to unzip file %s", filename));
}
}
}

publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_FINISHED, String.format("finished install of %s", (Object[]) serviceTypes)));

} catch (Exception e) {
error(e.getMessage());
log.error(e.getMessage(), e);
}

}

private void publishStatus(String msg, int level) {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/myrobotlab/framework/repo/Repo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public abstract class Repo {
*/
protected static String LOCATION = null;

List<Status> errors = new ArrayList<Status>();
protected List<Status> errors = new ArrayList<Status>();

Map<String, ServiceDependency> installedLibraries = new TreeMap<String, ServiceDependency>();
protected Map<String, ServiceDependency> installedLibraries = new TreeMap<String, ServiceDependency>();

public String getRepoPath() {
return LOCATION + File.separator + REPO_STATE_FILE_NAME;
Expand Down Expand Up @@ -360,19 +360,19 @@ static public void info(String format, Object... args) {
publishStatus(Status.info(format, args));
}

synchronized public void install() {
synchronized public void install() throws Exception {
// if a runtime exits we'll broadcast we are starting to install
ServiceData sd = ServiceData.getLocalInstance();
info("starting installation of %s services", sd.getServiceTypeNames().length);
install(sd.getServiceTypeNames());
info("finished installing %d services", sd.getServiceTypeNames().length);
}

synchronized public void install(String serviceType) {
synchronized public void install(String serviceType) throws Exception {
install(getInstallDir(), serviceType);
}

synchronized public void install(String location, String serviceType) {
synchronized public void install(String location, String serviceType) throws Exception {

String[] types = null;
if (serviceType == null) {
Expand Down Expand Up @@ -403,18 +403,18 @@ synchronized public void installDependency(String libraries, String[] installDep

abstract public void installDependency(String location, ServiceDependency serviceTypes);

abstract public void install(String location, String[] serviceTypes);
abstract public void install(String location, String[] serviceTypes) throws IOException;

synchronized public void install(String[] serviceTypes) {
synchronized public void install(String[] serviceTypes) throws Exception {
install(getInstallDir(), serviceTypes);
}

public void installEach() {
public void installEach() throws Exception {
String workDir = String.format(String.format("libraries.ivy.services.%d", System.currentTimeMillis()));
installEachTo(workDir);
}

public void installEachTo(String location) {
public void installEachTo(String location) throws Exception {
// if a runtime exits we'll broadcast we are starting to install
ServiceData sd = ServiceData.getLocalInstance();
String[] serviceNames = sd.getServiceTypeNames();
Expand All @@ -423,7 +423,7 @@ public void installEachTo(String location) {
info("finished installing %d services", sd.getServiceTypeNames().length);
}

public void installTo(String location) {
public void installTo(String location) throws Exception {
// if a runtime exits we'll broadcast we are starting to install
ServiceData sd = ServiceData.getLocalInstance();
info("starting installation of %s services", sd.getServiceTypeNames().length);
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/org/myrobotlab/framework/repo/RepoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Set;

Expand Down Expand Up @@ -50,7 +48,7 @@ public void setUp() throws Exception {
}

@Test
public void testAddStatusListener() throws ParseException, IOException {
public void testAddStatusListener() throws Exception {
Repo repo = Repo.getInstance();
repo.addStatusPublisher(this);
repo.install("Arduino");
Expand Down Expand Up @@ -81,7 +79,7 @@ public void testGetUnfulfilledDependencies() {
}

@Test
public void testIsInstalled() throws ParseException, IOException {
public void testIsInstalled() throws Exception {
Repo repo = Repo.getInstance();
repo.clear();
repo.install("Arduino");
Expand Down