static void createDefault(final OwnerFullName ownerFullName) throws MetadataException { try { try { NetworkGroup net = Transactions.find( new NetworkGroup( AccountFullName.getInstance(ownerFullName.getAccountNumber()), NETWORK_DEFAULT_NAME)); if (net == null) { create(ownerFullName, NETWORK_DEFAULT_NAME, "default group"); } } catch (NoSuchElementException ex) { try { create(ownerFullName, NETWORK_DEFAULT_NAME, "default group"); } catch (ConstraintViolationException ex1) { } } catch (TransactionException ex) { try { create(ownerFullName, NETWORK_DEFAULT_NAME, "default group"); } catch (ConstraintViolationException ex1) { } } } catch (DuplicateMetadataException ex) { } }
public CreateVolumeResponseType CreateVolume(final CreateVolumeType request) throws EucalyptusCloudException, AuthException { Context ctx = Contexts.lookup(); Long volSize = request.getSize() != null ? Long.parseLong(request.getSize()) : null; final String snapId = request.getSnapshotId(); String partition = request.getAvailabilityZone(); if ((request.getSnapshotId() == null && request.getSize() == null)) { throw new EucalyptusCloudException("One of size or snapshotId is required as a parameter."); } if (snapId != null) { try { Transactions.find(Snapshot.named(null, snapId)); } catch (ExecutionException ex) { throw new EucalyptusCloudException( "Failed to create volume because the referenced snapshot id is invalid: " + snapId); } } final Integer newSize = new Integer(request.getSize() != null ? request.getSize() : "-1"); Exception lastEx = null; for (int i = 0; i < VOL_CREATE_RETRIES; i++) { try { final ServiceConfiguration sc = Topology.lookup(Storage.class, Partitions.lookupByName(partition)); final UserFullName owner = ctx.getUserFullName(); Function<Long, Volume> allocator = new Function<Long, Volume>() { @Override public Volume apply(Long size) { try { return Volumes.createStorageVolume( sc, owner, snapId, Ints.checkedCast(size), request); } catch (ExecutionException ex) { throw Exceptions.toUndeclared(ex); } } }; Volume newVol = RestrictedTypes.allocateMeasurableResource(newSize.longValue(), allocator); CreateVolumeResponseType reply = request.getReply(); reply.setVolume(newVol.morph(new edu.ucsb.eucalyptus.msgs.Volume())); return reply; } catch (RuntimeException ex) { LOG.error(ex, ex); if (!(ex.getCause() instanceof ExecutionException)) { throw ex; } else { lastEx = ex; } } } throw new EucalyptusCloudException( "Failed to create volume after " + VOL_CREATE_RETRIES + " because of: " + lastEx, lastEx); }
public ImageInfo resetProductCodes() { try { Transactions.one( ImageInfo.self(this), new Callback<ImageInfo>() { @Override public void fire(final ImageInfo t) { t.getProductCodes().clear(); } }); } catch (final ExecutionException e) { LOG.debug(e, e); } return this; }
public ImageInfo resetPermission() { try { Transactions.one( new ImageInfo(this.displayName), new Callback<ImageInfo>() { @Override public void fire(final ImageInfo t) { t.getPermissions().clear(); t.getPermissions().add(t.getOwnerAccountNumber()); t.setImagePublic(ImageConfiguration.getInstance().getDefaultVisibility()); } }); } catch (final ExecutionException e) { LOG.debug(e, e); } return this; }
public static Volume checkVolumeReady(final Volume vol) throws EucalyptusCloudException { if (vol.isReady()) { return vol; } else { // TODO:GRZE:REMOVE temporary workaround to update the volume state. final ServiceConfiguration sc = Topology.lookup(Storage.class, Partitions.lookupByName(vol.getPartition())); final DescribeStorageVolumesType descVols = new DescribeStorageVolumesType(Lists.newArrayList(vol.getDisplayName())); try { Transactions.one( Volume.named(null, vol.getDisplayName()), new Callback<Volume>() { @Override public void fire(final Volume t) { try { final DescribeStorageVolumesResponseType volState = AsyncRequests.sendSync(sc, descVols); if (!volState.getVolumeSet().isEmpty()) { final State newVolumeState = Volumes.transformStorageState( vol.getState(), volState.getVolumeSet().get(0).getStatus()); vol.setState(newVolumeState); } } catch (final Exception ex) { LOG.error(ex); Logs.extreme().error(ex, ex); throw Exceptions.toUndeclared( "Failed to update the volume state " + vol.getDisplayName() + " not yet ready", ex); } } }); } catch (final ExecutionException ex) { throw new EucalyptusCloudException(ex.getCause()); } if (!vol.isReady()) { throw new EucalyptusCloudException("Volume " + vol.getDisplayName() + " not yet ready"); } return vol; } }
@Override public boolean delete(final AutoScalingMetadata metadata) throws AutoScalingMetadataException { if (metadata instanceof AutoScalingMetadataWithResourceName) { try { return Transactions.delete( exampleWithUuid( AutoScalingResourceName.parse( ((AutoScalingMetadataWithResourceName) metadata).getArn(), type) .getUuid())); } catch (NoSuchElementException e) { return false; } catch (Exception e) { throw new AutoScalingMetadataException( "Error deleting " + typeDescription + " '" + describe(metadata) + "'", e); } } else { return super.delete(metadata); } }
public static Volume createStorageVolume( final ServiceConfiguration sc, final UserFullName owner, final String snapId, final Integer newSize, final BaseMessage request) throws ExecutionException { final String newId = ResourceIdentifiers.generateString(ID_PREFIX); LOG.debug("Creating volume"); final Volume newVol = Transactions.save( Volume.create(sc, owner, snapId, newSize, newId), new Callback<Volume>() { @Override public void fire(final Volume t) { t.setState(State.GENERATING); try { final CreateStorageVolumeType req = new CreateStorageVolumeType(t.getDisplayName(), t.getSize(), snapId, null) .regarding(request); final CreateStorageVolumeResponseType ret = AsyncRequests.sendSync(sc, req); LOG.debug("Volume created"); if (t.getSize() != null && t.getSize() > 0) { fireUsageEvent(t, VolumeEvent.forVolumeCreate()); } } catch (final Exception ex) { LOG.error("Failed to create volume: " + t, ex); t.setState(State.FAIL); throw Exceptions.toUndeclared(ex); } } }); return newVol; }
@Override public void run() { try { SnapshotInfo searchSnap = new SnapshotInfo(); searchSnap.setStatus(StorageProperties.Status.deleting.toString()); List<SnapshotInfo> snapshotsToBeDeleted = null; try { snapshotsToBeDeleted = Transactions.findAll(searchSnap); } catch (Exception e) { LOG.error("Failed to lookup snapshots marked for deletion", e); return; } if (snapshotsToBeDeleted != null && !snapshotsToBeDeleted.isEmpty()) { for (SnapshotInfo snap : snapshotsToBeDeleted) { String snapshotId = snap.getSnapshotId(); LOG.info("Snapshot: " + snapshotId + " was marked for deletion. Cleaning up..."); try { blockManager.deleteSnapshot(snapshotId); } catch (EucalyptusCloudException e1) { LOG.debug("Failed to delete " + snapshotId, e1); LOG.warn("Unable to delete " + snapshotId + ". Will retry later"); continue; } SnapshotInfo snapInfo = new SnapshotInfo(snapshotId); SnapshotInfo foundSnapshotInfo; try (TransactionResource tran = Entities.transactionFor(SnapshotInfo.class)) { foundSnapshotInfo = Entities.uniqueResult(snapInfo); foundSnapshotInfo.setStatus(StorageProperties.Status.deleted.toString()); foundSnapshotInfo.setDeletionTime(new Date()); tran.commit(); } catch (TransactionException | NoSuchElementException e) { LOG.error(e); continue; } if (StringUtils.isNotBlank(foundSnapshotInfo.getSnapshotLocation())) { try { String[] names = SnapshotInfo.getSnapshotBucketKeyNames(foundSnapshotInfo.getSnapshotLocation()); if (snapshotTransfer == null) { snapshotTransfer = new S3SnapshotTransfer(); } snapshotTransfer.setSnapshotId(snapshotId); snapshotTransfer.setBucketName(names[0]); snapshotTransfer.setKeyName(names[1]); snapshotTransfer.delete(); } catch (Exception e) { LOG.warn("Failed to delete snapshot " + snapshotId + " from objectstorage", e); } } else { LOG.debug( "Snapshot location missing for " + snapshotId + ". Skipping deletion from ObjectStorageGateway"); } } } else { LOG.trace("No snapshots marked for deletion"); } } catch (Exception e) { // could catch InterruptedException LOG.warn("Unable to remove snapshots marked for deletion", e); return; } }
public static synchronized void updateNetworkRangeConfiguration() { final AtomicReference<NetworkRangeConfiguration> equalityCheck = new AtomicReference<NetworkRangeConfiguration>(null); try { Transactions.each( new ClusterConfiguration(), new Callback<ClusterConfiguration>() { @Override public void fire(final ClusterConfiguration input) { NetworkRangeConfiguration comparisonConfig = new NetworkRangeConfiguration(); comparisonConfig.setUseNetworkTags(input.getUseNetworkTags()); comparisonConfig.setMinNetworkTag(input.getMinNetworkTag()); comparisonConfig.setMaxNetworkTag(input.getMaxNetworkTag()); comparisonConfig.setMinNetworkIndex(input.getMinNetworkIndex()); comparisonConfig.setMaxNetworkIndex(input.getMaxNetworkIndex()); Logs.extreme() .debug( "Updating cluster config: " + input.getName() + " " + comparisonConfig.toString()); if (equalityCheck.compareAndSet(null, comparisonConfig)) { Logs.extreme().debug("Initialized cluster config check: " + equalityCheck.get()); } else { NetworkRangeConfiguration currentConfig = equalityCheck.get(); List<String> errors = Lists.newArrayList(); if (!currentConfig .getUseNetworkTags() .equals(comparisonConfig.getUseNetworkTags())) { errors.add( input.getName() + " network config mismatch: vlan tagging " + currentConfig.getUseNetworkTags() + " != " + comparisonConfig.getUseNetworkTags()); } else if (!currentConfig .getMinNetworkTag() .equals(comparisonConfig.getMinNetworkTag())) { errors.add( input.getName() + " network config mismatch: min vlan tag " + currentConfig.getMinNetworkTag() + " != " + comparisonConfig.getMinNetworkTag()); } else if (!currentConfig .getMaxNetworkTag() .equals(comparisonConfig.getMaxNetworkTag())) { errors.add( input.getName() + " network config mismatch: max vlan tag " + currentConfig.getMaxNetworkTag() + " != " + comparisonConfig.getMaxNetworkTag()); } else if (!currentConfig .getMinNetworkIndex() .equals(comparisonConfig.getMinNetworkIndex())) { errors.add( input.getName() + " network config mismatch: min net index " + currentConfig.getMinNetworkIndex() + " != " + comparisonConfig.getMinNetworkIndex()); } else if (!currentConfig .getMaxNetworkIndex() .equals(comparisonConfig.getMaxNetworkIndex())) { errors.add( input.getName() + " network config mismatch: max net index " + currentConfig.getMaxNetworkIndex() + " != " + comparisonConfig.getMaxNetworkIndex()); } } } }); } catch (RuntimeException ex) { Logs.extreme().error(ex, ex); throw ex; } catch (TransactionException ex) { LOG.error(ex); Logs.extreme().error(ex, ex); } netConfig = new NetworkRangeConfiguration(); final AtomicBoolean netTagging = new AtomicBoolean(true); try { Transactions.each( new ClusterConfiguration(), new Callback<ClusterConfiguration>() { @Override public void fire(final ClusterConfiguration input) { netTagging.compareAndSet(true, input.getUseNetworkTags()); netConfig.setMinNetworkTag( Ints.max(netConfig.getMinNetworkTag(), input.getMinNetworkTag())); netConfig.setMaxNetworkTag( Ints.min(netConfig.getMaxNetworkTag(), input.getMaxNetworkTag())); netConfig.setMinNetworkIndex( Longs.max(netConfig.getMinNetworkIndex(), input.getMinNetworkIndex())); netConfig.setMaxNetworkIndex( Longs.min(netConfig.getMaxNetworkIndex(), input.getMaxNetworkIndex())); } }); Logs.extreme().debug("Updated network configuration: " + netConfig.toString()); } catch (final TransactionException ex) { Logs.extreme().error(ex, ex); } netConfig.setUseNetworkTags(netTagging.get()); final EntityTransaction db = Entities.get(NetworkGroup.class); try { final List<NetworkGroup> ret = Entities.query(new NetworkGroup()); for (NetworkGroup group : ret) { ExtantNetwork exNet = group.getExtantNetwork(); if (exNet != null && (exNet.getTag() > netConfig.getMaxNetworkTag() || exNet.getTag() < netConfig.getMinNetworkTag())) { exNet.teardown(); Entities.delete(exNet); group.setExtantNetwork(null); } } db.commit(); } catch (final Exception ex) { Logs.extreme().error(ex, ex); LOG.error(ex); db.rollback(); } }