/
Allocations.java
373 lines (335 loc) · 13.6 KB
/
Allocations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
package com.eucalyptus.cloud.run;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.persistence.EntityTransaction;
import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.Base64;
import com.eucalyptus.auth.principal.UserFullName;
import com.eucalyptus.cloud.ResourceToken;
import com.eucalyptus.cloud.util.MetadataException;
import com.eucalyptus.cloud.util.NotEnoughResourcesException;
import com.eucalyptus.component.Partition;
import com.eucalyptus.context.Context;
import com.eucalyptus.context.Contexts;
import com.eucalyptus.entities.Entities;
import com.eucalyptus.entities.TransientEntityException;
import com.eucalyptus.images.Emis;
import com.eucalyptus.images.Emis.BootableSet;
import com.eucalyptus.keys.KeyPairs;
import com.eucalyptus.keys.SshKeyPair;
import com.eucalyptus.network.ExtantNetwork;
import com.eucalyptus.network.NetworkGroup;
import com.eucalyptus.records.Logs;
import com.eucalyptus.util.UniqueIds;
import com.eucalyptus.vm.VmInstance;
import com.eucalyptus.vm.VmInstances;
import com.eucalyptus.vm.VmType;
import com.eucalyptus.vm.VmTypes;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import edu.ucsb.eucalyptus.msgs.HasRequest;
import edu.ucsb.eucalyptus.msgs.RunInstancesType;
import edu.ucsb.eucalyptus.msgs.VmTypeInfo;
public class Allocations {
private static Logger LOG = Logger.getLogger( Allocations.class );
public static class Allocation implements HasRequest {
/** to be eliminated **/
private final Context context;
private final RunInstancesType request;
/** values determined by the request **/
private final UserFullName ownerFullName;
private byte[] userData;
private final int minCount;
private final int maxCount;
private final boolean usePrivateAddressing;
/** verified references determined by the request **/
private Partition partition;
private SshKeyPair sshKeyPair;
private BootableSet bootSet;
private VmType vmType;
private NetworkGroup primaryNetwork;
private Map<String, NetworkGroup> networkGroups;
/** intermediate allocation state **/
private final String reservationId;
private final List<ResourceToken> allocationTokens = Lists.newArrayList( );
private final Long reservationIndex;
private final Map<Integer, String> instanceIds;
private Date expiration;
private Allocation( final RunInstancesType request ) {
super( );
this.context = Contexts.lookup( );
this.instanceIds = Maps.newHashMap( );
this.request = request;
this.minCount = request.getMinCount( );
this.maxCount = request.getMaxCount( );
this.usePrivateAddressing = "private".equals(request.getAddressingType());
this.ownerFullName = this.context.getUserFullName( );
if ( ( this.request.getInstanceType( ) == null ) || "".equals( this.request.getInstanceType( ) ) ) {
this.request.setInstanceType( VmTypes.defaultTypeName( ) );
}
this.reservationIndex = UniqueIds.nextIndex( VmInstance.class, ( long ) request.getMaxCount( ) );
this.reservationId = VmInstances.getId( this.reservationIndex, 0 ).replaceAll( "i-", "r-" );
if ( this.request.getUserData( ) != null ) {
try {
this.userData = Base64.decode( this.request.getUserData( ) );
this.request.setUserData( new String( Base64.encode( this.userData ) ) );
} catch ( Exception e ) {}
} else {
try {
this.request.setUserData( new String( Base64.encode( new byte[0] ) ) );
} catch ( Exception ex ) {
LOG.error( ex, ex );
}
}
}
private Allocation( final String reservationId,
final String instanceId,
final byte[] userData,
final Date expiration,
final Partition partition,
final SshKeyPair sshKeyPair,
final BootableSet bootSet,
final VmType vmType,
final Set<NetworkGroup> networkGroups,
final boolean isUsePrivateAddressing ) {
super( );
this.context = Contexts.lookup( );
this.minCount = 1;
this.maxCount = 1;
this.usePrivateAddressing = isUsePrivateAddressing;
this.ownerFullName = this.context.getUserFullName( );
this.reservationId = reservationId;
this.reservationIndex = UniqueIds.nextIndex( VmInstance.class, ( long ) this.maxCount );
this.instanceIds = Maps.newHashMap( );
this.instanceIds.put( 0, instanceId );
this.userData = userData;
this.partition = partition;
this.sshKeyPair = ( sshKeyPair != null ? sshKeyPair : KeyPairs.noKey( ) );
this.bootSet = bootSet;
this.expiration = expiration;
this.vmType = vmType;
this.networkGroups = new HashMap<String, NetworkGroup>( ) {
{
for ( NetworkGroup g : networkGroups ) {
if ( Allocation.this.primaryNetwork == null ) {
Allocation.this.primaryNetwork = g;
}
put( g.getDisplayName( ), g );
}
}
};
this.request = new RunInstancesType( ) {
{
this.setMinCount( 1 );
this.setMaxCount( 1 );
this.setImageId( bootSet.getMachine( ).getDisplayName( ) );
this.setAvailabilityZone( partition.getName( ) );
this.getGroupSet( ).addAll( Allocation.this.networkGroups.keySet( ) );
this.setInstanceType( vmType.getName( ) );
}
};
}
@Override
public RunInstancesType getRequest( ) {
return this.request;
}
public NetworkGroup getPrimaryNetwork( ) {
return this.primaryNetwork;
}
public ExtantNetwork getExtantNetwork( ) {
final EntityTransaction db = Entities.get( ExtantNetwork.class );
try {
final NetworkGroup net = Entities.merge( this.primaryNetwork );
final ExtantNetwork ex = net.extantNetwork( );
db.commit( );
return ex;
} catch ( final TransientEntityException ex ) {
LOG.error( ex, ex );
db.rollback( );
throw new RuntimeException( ex );
} catch ( final NotEnoughResourcesException ex ) {
db.rollback( );
return ExtantNetwork.bogus( this.getPrimaryNetwork( ) );
}
}
public void commit( ) throws Exception {
try {
for ( final ResourceToken t : this.getAllocationTokens( ) ) {
VmInstance.Create.INSTANCE.apply( t );
}
} catch ( final Exception ex ) {
this.abort( );
throw ex;
}
}
public void abort( ) {
for ( final ResourceToken token : this.allocationTokens ) {
LOG.error( "Aborting resource token: " + token, new RuntimeException( ) );
final EntityTransaction db = Entities.get( VmInstance.class );
try {
token.abort( );
db.commit( );
} catch ( final Exception ex ) {
LOG.warn( ex.getMessage( ) );
Logs.exhaust( ).error( ex, ex );
db.rollback( );
}
}
}
public List<NetworkGroup> getNetworkGroups( ) {
return Lists.newArrayList( this.networkGroups.values( ) );
}
public VmType getVmType( ) {
return this.vmType;
}
public Partition getPartition( ) {
return this.partition;
}
public void setBootableSet( final BootableSet bootSet ) {
this.bootSet = bootSet;
}
public void setVmType( final VmType vmType ) {
this.vmType = vmType;
}
public UserFullName getOwnerFullName( ) {
return this.ownerFullName;
}
public List<ResourceToken> getAllocationTokens( ) {
return this.allocationTokens;
}
public byte[] getUserData( ) {
return this.userData;
}
public Long getReservationIndex( ) {
return this.reservationIndex;
}
public String getReservationId( ) {
return this.reservationId;
}
public BootableSet getBootSet( ) {
return this.bootSet;
}
public Context getContext( ) {
return this.context;
}
public void setPartition( final Partition partition2 ) {
this.partition = partition2;
}
public SshKeyPair getSshKeyPair( ) {
return this.sshKeyPair;
}
public void setSshKeyPair( final SshKeyPair sshKeyPair ) {
this.sshKeyPair = sshKeyPair;
}
public void setNetworkRules( final Map<String, NetworkGroup> networkRuleGroups ) {
final Entry<String, NetworkGroup> ent = networkRuleGroups.entrySet( ).iterator( ).next( );
this.primaryNetwork = ent.getValue( );
this.networkGroups = networkRuleGroups;
}
public VmTypeInfo getVmTypeInfo( ) throws MetadataException {
return this.bootSet.populateVirtualBootRecord( this.vmType );
}
public int getMinCount( ) {
return this.minCount;
}
public int getMaxCount( ) {
return this.maxCount;
}
public boolean isUsePrivateAddressing() {
return usePrivateAddressing;
}
public String getInstanceId( int index ) {
while ( this.instanceIds.size( ) < index + 1 ) {
this.instanceIds.put( index, VmInstances.getId( ( long ) this.getReservationIndex( ), index ) );
}
return this.instanceIds.get( index );
}
public Date getExpiration( ) {
return this.expiration;
}
public void setExpiration(final Date expiration) {
this.expiration = expiration;
}
}
public static Allocation run( final RunInstancesType request ) {
return new Allocation( request );
}
public static Allocation start( final VmInstance vm ) {
BootableSet bootSet = Emis.recreateBootableSet( vm );
return new Allocation( vm.getReservationId( ),
vm.getInstanceId( ),
vm.getUserData( ),
vm.getExpiration( ),
vm.lookupPartition( ),
vm.getKeyPair( ),
bootSet,
vm.getVmType( ),
vm.getNetworkGroups( ),
vm.isUsePrivateAddressing() );
}
}