use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class PluginSpec method readExternal.
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
type = in.readUTF();
name = in.readUTF();
properties = (Map<String, String>) in.readObject();
boolean artifactExists = in.readBoolean();
if (artifactExists) {
String artifactName = in.readUTF();
ArtifactVersion artifactVersion = new ArtifactVersion(in.readUTF());
ArtifactScope artifactScope = ArtifactScope.valueOf(in.readUTF());
artifact = new ArtifactId(artifactName, artifactVersion, artifactScope);
}
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class ApplicationLifecycleService method getLatestAppArtifactForUpgrade.
/**
* Finds latest application artifact for given application and current artifact for upgrading application.
* If no artifact found then returns current artifact as the candidate.
*
* @param appId application Id to find latest app artifact for.
* @param currentArtifactId current artifact used by application.
* @param allowedArtifactScopes artifact scopes to search in for finding candidate artifacts.
* @param allowSnapshot whether to consider snapshot version of artifacts or not for upgrade.
* @return {@link ArtifactSummary} for the artifact to be used for upgrade purpose.
* @throws NotFoundException if there is no artifact available for given artifact.
* @throws Exception if there was an exception during finding candidate artifact.
*/
private ArtifactSummary getLatestAppArtifactForUpgrade(ApplicationId appId, ArtifactId currentArtifactId, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot) throws Exception {
List<ArtifactSummary> availableArtifacts = new ArrayList<>();
// At the least, current artifact should be in the set of available artifacts.
availableArtifacts.add(ArtifactSummary.from(currentArtifactId));
// Find candidate artifacts from all scopes we need to consider.
for (ArtifactScope scope : allowedArtifactScopes) {
NamespaceId artifactNamespaceToConsider = ArtifactScope.SYSTEM.equals(scope) ? NamespaceId.SYSTEM : appId.getParent();
List<ArtifactSummary> artifacts;
try {
artifacts = artifactRepository.getArtifactSummaries(artifactNamespaceToConsider, currentArtifactId.getName(), Integer.MAX_VALUE, ArtifactSortOrder.ASC);
} catch (ArtifactNotFoundException e) {
// This can happen if we are looking for candidate artifact in multiple namespace.
continue;
}
for (ArtifactSummary artifactSummary : artifacts) {
ArtifactVersion artifactVersion = new ArtifactVersion(artifactSummary.getVersion());
// Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
if ((artifactVersion.isSnapshot() && allowSnapshot) || !artifactVersion.isSnapshot()) {
availableArtifacts.add(artifactSummary);
}
}
}
// Find the artifact with latest version.
Optional<ArtifactSummary> newArtifactCandidate = availableArtifacts.stream().max(Comparator.comparing(artifactSummary -> new ArtifactVersion(artifactSummary.getVersion())));
io.cdap.cdap.proto.id.ArtifactId currentArtifact = new io.cdap.cdap.proto.id.ArtifactId(appId.getNamespace(), currentArtifactId.getName(), currentArtifactId.getVersion().getVersion());
return newArtifactCandidate.orElseThrow(() -> new ArtifactNotFoundException(currentArtifact));
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class RemoteArtifactRepositoryReader method newInputStream.
/**
* Returns an input stream for reading the artifact bytes. If no such artifact exists, or an error occurs during
* reading, an exception is thrown.
*
* @param artifactId the id of the artifact to get
* @return an InputStream for the artifact bytes
* @throws IOException if there as an exception reading from the store.
* @throws NotFoundException if the given artifact does not exist
*/
@Override
public InputStream newInputStream(Id.Artifact artifactId) throws IOException, NotFoundException {
String namespaceId = artifactId.getNamespace().getId();
ArtifactScope scope = ArtifactScope.USER;
// as long as it exists. Using default because it will always be there
if (NamespaceId.SYSTEM.getNamespace().equalsIgnoreCase(namespaceId)) {
namespaceId = NamespaceId.DEFAULT.getNamespace();
scope = ArtifactScope.SYSTEM;
}
String url = String.format("namespaces/%s/artifacts/%s/versions/%s/download?scope=%s", namespaceId, artifactId.getName(), artifactId.getVersion(), scope);
HttpURLConnection urlConn = remoteClient.openConnection(HttpMethod.GET, url);
throwIfError(artifactId, urlConn);
// Use FilterInputStream and override close to ensure the connection is closed once the input stream is closed
return new FilterInputStream(urlConn.getInputStream()) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
urlConn.disconnect();
}
}
};
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class SetArtifactPropertiesCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
String scopeStr = arguments.get(ArgumentName.SCOPE.toString());
ArtifactScope scope = ArtifactScope.valueOf(scopeStr.toUpperCase());
NamespaceId namespace = scope == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : cliConfig.getCurrentNamespace();
ArtifactId artifactId = namespace.artifact(artifactName, artifactVersion);
String propertiesFilePath = arguments.get(ArgumentName.LOCAL_FILE_PATH.toString());
File propertiesFile = resolver.resolvePathToFile(propertiesFilePath);
try (Reader reader = new FileReader(propertiesFile)) {
ArtifactProperties properties;
try {
properties = GSON.fromJson(reader, ArtifactProperties.class);
} catch (Exception e) {
throw new RuntimeException("Error parsing file contents. Please check that it is a valid JSON object, " + "and that it contains a 'properties' key whose value is a JSON object of the " + "artifact properties.", e);
}
artifactClient.writeProperties(artifactId, properties.properties);
}
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class UpdateAppCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String appName = arguments.get(ArgumentName.APP.toString());
ApplicationId appId = cliConfig.getCurrentNamespace().app(appName);
String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
ArtifactScope artifactScope = ArtifactScope.valueOf(arguments.get(ArgumentName.SCOPE.toString()).toUpperCase());
ArtifactSummary artifact = new ArtifactSummary(artifactName, artifactVersion, artifactScope);
JsonObject config = new JsonObject();
String configPath = arguments.getOptional(ArgumentName.APP_CONFIG_FILE.toString());
if (configPath != null) {
File configFile = resolver.resolvePathToFile(configPath);
try (FileReader reader = new FileReader(configFile)) {
AppRequest<JsonObject> appRequest = GSON.fromJson(reader, CONFIG_TYPE);
config = appRequest.getConfig();
}
}
AppRequest<JsonObject> appRequest = new AppRequest<>(artifact, config);
applicationClient.update(appId, appRequest);
output.println("Successfully updated application");
}
Aggregations