use of org.jclouds.rest.AuthorizationException in project fabric8 by jboss-fuse.
the class CloudContainerInstallationTask method install.
public CreateJCloudsContainerMetadata install() {
LoginCredentials credentials = nodeMetadata.getCredentials();
// For some cloud providers return do not allow shell access to root, so the user needs to be overrided.
if (!Strings.isNullOrEmpty(options.getUser()) && credentials != null) {
credentials = credentials.toBuilder().user(options.getUser()).build();
} else {
credentials = nodeMetadata.getCredentials();
}
String id = nodeMetadata.getId();
Set<String> publicAddresses = nodeMetadata.getPublicAddresses();
// Make a copy of the addresses, because we don't want to return back a guice implementation of Set.
Set<String> copyOfPublicAddresses = new HashSet<String>();
for (String publicAddress : publicAddresses) {
copyOfPublicAddresses.add(publicAddress);
}
CreateJCloudsContainerMetadata jCloudsContainerMetadata = new CreateJCloudsContainerMetadata();
jCloudsContainerMetadata.setCreateOptions(options);
jCloudsContainerMetadata.setNodeId(nodeMetadata.getId());
jCloudsContainerMetadata.setContainerName(containerName);
jCloudsContainerMetadata.setPublicAddresses(copyOfPublicAddresses);
jCloudsContainerMetadata.setHostname(nodeMetadata.getHostname());
if (credentials != null) {
jCloudsContainerMetadata.setIdentity(credentials.identity);
jCloudsContainerMetadata.setCredential(credentials.credential);
}
String publicAddress = "";
Properties addresses = new Properties();
if (publicAddresses != null && !publicAddresses.isEmpty()) {
publicAddress = publicAddresses.iterator().next();
addresses.put(ZkDefs.PUBLIC_IP, publicAddress);
}
options.getSystemProperties().put(ContainerProviderUtils.ADDRESSES_PROPERTY_KEY, addresses);
options.getMetadataMap().put(containerName, jCloudsContainerMetadata);
// Setup firwall for node
try {
FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
if (firewallManager.isSupported()) {
listener.onStateChange("Configuring firewall.");
String source = getOriginatingIp();
Rule httpRule = Rule.create().source("0.0.0.0/0").destination(nodeMetadata).port(8181);
firewallManager.addRules(httpRule);
if (source != null) {
Rule jmxRule = Rule.create().source(source).destination(nodeMetadata).ports(44444, 1099);
Rule sshRule = Rule.create().source(source).destination(nodeMetadata).port(8101);
Rule zookeeperRule = Rule.create().source(source).destination(nodeMetadata).port(2181);
firewallManager.addRules(jmxRule, sshRule, zookeeperRule);
}
// where firewall configuration is shared among nodes of the same groups, e.g. EC2.
if (!Strings.isNullOrEmpty(publicAddress)) {
Rule zookeeperFromTargetRule = Rule.create().source(publicAddress + "/32").destination(nodeMetadata).port(2181);
firewallManager.addRule(zookeeperFromTargetRule);
}
} else {
listener.onStateChange(String.format("Skipping firewall configuration. Not supported for provider %s", options.getProviderName()));
}
} catch (FirewallNotSupportedOnProviderException e) {
LOGGER.warn("Firewall manager not supported. Firewall will have to be manually configured.");
} catch (IOException e) {
LOGGER.warn("Could not lookup originating ip. Firewall will have to be manually configured.", e);
} catch (Throwable t) {
LOGGER.warn("Failed to setup firewall", t);
}
try {
String script = buildInstallAndStartScript(containerName, options);
listener.onStateChange(String.format("Installing fabric agent on container %s. It may take a while...", containerName));
ExecResponse response = null;
String uploadPath = "/tmp/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip";
URL distributionURL = options.getProxyUri().resolve("io/fabric8/fabric8-karaf/" + FabricConstants.FABRIC_VERSION + "/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip").toURL();
try {
if (options.doUploadDistribution()) {
uploadToNode(computeService.getContext(), nodeMetadata, credentials, distributionURL, uploadPath);
}
if (credentials != null) {
response = computeService.runScriptOnNode(id, script, templateOptions.overrideLoginCredentials(credentials).runAsRoot(false));
} else {
response = computeService.runScriptOnNode(id, script, templateOptions);
}
} catch (AuthorizationException ex) {
throw new Exception("Failed to connect to the container via ssh.");
} catch (SshException ex) {
throw new Exception("Failed to connect to the container via ssh.");
}
if (response != null && response.getOutput() != null) {
if (response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
}
String overridenResolverValue = ContainerProviderUtils.parseResolverOverride(response.getOutput());
if (overridenResolverValue != null) {
jCloudsContainerMetadata.setOverridenResolver(overridenResolverValue);
listener.onStateChange("Overriding resolver to " + overridenResolverValue + ".");
}
} else {
jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
}
} catch (Throwable t) {
jCloudsContainerMetadata.setFailure(t);
}
// Cleanup addresses.
options.getSystemProperties().clear();
return jCloudsContainerMetadata;
}
use of org.jclouds.rest.AuthorizationException in project dhis2-core by dhis2.
the class JCloudsFileResourceContentStore method init.
// -------------------------------------------------------------------------
// Life cycle management
// -------------------------------------------------------------------------
@PostConstruct
public void init() {
// ---------------------------------------------------------------------
// Bootstrap config
// ---------------------------------------------------------------------
config = new BlobStoreProperties(configurationProvider.getProperty(ConfigurationKey.FILESTORE_PROVIDER), configurationProvider.getProperty(ConfigurationKey.FILESTORE_LOCATION), configurationProvider.getProperty(ConfigurationKey.FILESTORE_CONTAINER));
Pair<Credentials, Properties> providerConfig = configureForProvider(config.provider, configurationProvider.getProperty(ConfigurationKey.FILESTORE_IDENTITY), configurationProvider.getProperty(ConfigurationKey.FILESTORE_SECRET));
// ---------------------------------------------------------------------
// Set up JClouds context
// ---------------------------------------------------------------------
blobStoreContext = ContextBuilder.newBuilder(config.provider).credentials(providerConfig.getLeft().identity, providerConfig.getLeft().credential).overrides(providerConfig.getRight()).build(BlobStoreContext.class);
blobStore = blobStoreContext.getBlobStore();
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id(config.provider).description(config.provider).build();
try {
blobStore.createContainerInLocation(createRegionLocation(config, provider), config.container);
log.info(String.format("File store configured with provider: '%s', container: '%s' and location: '%s'.", config.provider, config.container, config.location));
} catch (HttpResponseException ex) {
log.error(String.format("Could not configure file store with provider '%s' and container '%s'.\n" + "File storage will not be available.", config.provider, config.container), ex);
} catch (AuthorizationException ex) {
log.error(String.format("Could not authenticate with file store provider '%s' and container '%s'. " + "File storage will not be available.", config.provider, config.location), ex);
}
}
use of org.jclouds.rest.AuthorizationException in project dhis2-core by dhis2.
the class DataValueController method saveDataValueInternal.
private void saveDataValueInternal(String de, String co, String cc, String cp, String pe, String ou, String ds, String value, String comment, Boolean followUp, boolean force, User currentUser) throws WebMessageException {
boolean strictPeriods = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
boolean strictCategoryOptionCombos = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
boolean strictOrgUnits = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
boolean requireCategoryOptionCombo = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
FileResourceRetentionStrategy retentionStrategy = systemSettingManager.getSystemSetting(SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.class);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
Period period = dataValueValidation.getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
dataValueValidation.validateOrganisationUnitPeriod(organisationUnit, period);
DataSet dataSet = dataValueValidation.getAndValidateOptionalDataSet(ds, dataElement);
dataValueValidation.validateInvalidFuturePeriod(period, dataElement);
dataValueValidation.validateAttributeOptionCombo(attributeOptionCombo, period, dataSet, dataElement);
value = dataValueValidation.validateAndNormalizeDataValue(value, dataElement);
dataValueValidation.validateComment(comment);
dataValueValidation.validateOptionSet(value, dataElement.getOptionSet(), dataElement);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, categoryOptionCombo);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, attributeOptionCombo);
if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
throw new WebMessageException(conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
}
if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
throw new WebMessageException(conflict("Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid()));
}
if (strictOrgUnits && !organisationUnit.hasDataElement(dataElement)) {
throw new WebMessageException(conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
}
if (!inputUtils.canForceDataInput(currentUser, force)) {
dataValueValidation.validateDataSetNotLocked(currentUser, dataElement, period, dataSet, organisationUnit, attributeOptionCombo);
}
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
dataValueValidation.validateDataInputPeriodForDataElementAndPeriod(dataElement, dataSet, period);
// ---------------------------------------------------------------------
// Assemble and save data value
// ---------------------------------------------------------------------
String storedBy = currentUser.getUsername();
Date now = new Date();
DataValue persistedDataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
FileResource fileResource = null;
if (persistedDataValue == null) {
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
DataValue newValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
newValue.setFollowup(followUp);
dataValueService.addDataValue(newValue);
} else {
if (value == null && comment == null && followUp == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
dataValueService.deleteDataValue(persistedDataValue);
return;
}
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
if (dataElement.isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE) {
try {
fileResourceService.deleteFileResource(persistedDataValue.getValue());
} catch (AuthorizationException exception) {
// If we fail to delete the fileResource now, mark it as
// unassigned for removal later
fileResourceService.getFileResource(persistedDataValue.getValue()).setAssigned(false);
}
persistedDataValue.setValue(StringUtils.EMPTY);
}
if (value != null) {
persistedDataValue.setValue(StringUtils.trimToNull(value));
}
if (comment != null) {
persistedDataValue.setComment(StringUtils.trimToNull(comment));
}
if (followUp != null) {
persistedDataValue.toggleFollowUp();
}
persistedDataValue.setLastUpdated(now);
persistedDataValue.setStoredBy(storedBy);
dataValueService.updateDataValue(persistedDataValue);
}
if (fileResource != null) {
fileResourceService.updateFileResource(fileResource);
}
}
use of org.jclouds.rest.AuthorizationException in project dhis2-core by dhis2.
the class JCloudsAppStorageService method init.
@PostConstruct
public void init() {
// ---------------------------------------------------------------------
// Bootstrap config
// ---------------------------------------------------------------------
config = new BlobStoreProperties(configurationProvider.getProperty(ConfigurationKey.FILESTORE_PROVIDER), configurationProvider.getProperty(ConfigurationKey.FILESTORE_LOCATION), configurationProvider.getProperty(ConfigurationKey.FILESTORE_CONTAINER));
Pair<Credentials, Properties> providerConfig = configureForProvider(config.provider, configurationProvider.getProperty(ConfigurationKey.FILESTORE_IDENTITY), configurationProvider.getProperty(ConfigurationKey.FILESTORE_SECRET));
// ---------------------------------------------------------------------
// Set up JClouds context
// ---------------------------------------------------------------------
blobStoreContext = ContextBuilder.newBuilder(config.provider).credentials(providerConfig.getLeft().identity, providerConfig.getLeft().credential).overrides(providerConfig.getRight()).build(BlobStoreContext.class);
blobStore = blobStoreContext.getBlobStore();
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id(config.provider).description(config.provider).build();
try {
blobStore.createContainerInLocation(createRegionLocation(config, provider), config.container);
log.info(String.format("File store configured with provider: '%s', container: '%s' and location: '%s'.", config.provider, config.container, config.location));
} catch (HttpResponseException ex) {
log.error(String.format("Could not configure file store with provider '%s' and container '%s'.\n" + "File storage will not be available.", config.provider, config.container), ex);
} catch (AuthorizationException ex) {
log.error(String.format("Could not authenticate with file store provider '%s' and container '%s'. " + "File storage will not be available.", config.provider, config.location), ex);
}
}
Aggregations