~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to .pc/15-fix-default-ramdisk.patch/clc/modules/cluster-manager/src/main/java/com/eucalyptus/cluster/VmInstance.java

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 *Copyright (c) 2009 Eucalyptus Systems, Inc.
 
3
 * 
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation, only version 3 of the License.
 
7
 * 
 
8
 * 
 
9
 * This file is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 
12
 * for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 
16
 * 
 
17
 * Please contact Eucalyptus Systems, Inc., 130 Castilian
 
18
 * Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
 
19
 * if you need additional information or have any questions.
 
20
 * 
 
21
 * This file may incorporate work covered under the following copyright and
 
22
 * permission notice:
 
23
 * 
 
24
 * Software License Agreement (BSD License)
 
25
 * 
 
26
 * Copyright (c) 2008, Regents of the University of California
 
27
 * All rights reserved.
 
28
 * 
 
29
 * Redistribution and use of this software in source and binary forms, with
 
30
 * or without modification, are permitted provided that the following
 
31
 * conditions are met:
 
32
 * 
 
33
 * Redistributions of source code must retain the above copyright notice,
 
34
 * this list of conditions and the following disclaimer.
 
35
 * 
 
36
 * Redistributions in binary form must reproduce the above copyright
 
37
 * notice, this list of conditions and the following disclaimer in the
 
38
 * documentation and/or other materials provided with the distribution.
 
39
 * 
 
40
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
41
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
42
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
43
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 
44
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
45
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
46
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
47
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
48
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
49
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
50
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
 
51
 * THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
 
52
 * LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
 
53
 * SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
 
54
 * IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
 
55
 * BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
 
56
 * THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
57
 * OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
 
58
 * WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
 
59
 * ANY SUCH LICENSES OR RIGHTS.
 
60
 *******************************************************************************/
 
61
/*
 
62
 * Author: chris grzegorczyk <grze@eucalyptus.com>
 
63
 */
 
64
 
 
65
package com.eucalyptus.cluster;
 
66
 
 
67
import java.util.ArrayList;
 
68
import java.util.Date;
 
69
import java.util.HashMap;
 
70
import java.util.List;
 
71
import java.util.Map;
 
72
import java.util.NavigableSet;
 
73
import java.util.Set;
 
74
import java.util.concurrent.ConcurrentSkipListSet;
 
75
import java.util.concurrent.atomic.AtomicMarkableReference;
 
76
import org.apache.commons.lang.time.StopWatch;
 
77
import org.apache.log4j.Logger;
 
78
import com.eucalyptus.bootstrap.Component;
 
79
import com.eucalyptus.component.Configurations;
 
80
import com.eucalyptus.records.EventClass;
 
81
import com.eucalyptus.records.EventRecord;
 
82
import com.eucalyptus.records.EventType;
 
83
import com.eucalyptus.util.HasName;
 
84
import com.eucalyptus.util.LogUtil;
 
85
import com.eucalyptus.vm.SystemState;
 
86
import com.eucalyptus.vm.VmState;
 
87
import com.eucalyptus.vm.SystemState.Reason;
 
88
import com.google.common.base.Predicate;
 
89
import com.google.common.collect.Iterables;
 
90
import com.google.common.collect.Lists;
 
91
import com.google.common.collect.Sets;
 
92
import edu.ucsb.eucalyptus.cloud.Network;
 
93
import edu.ucsb.eucalyptus.cloud.VmImageInfo;
 
94
import edu.ucsb.eucalyptus.cloud.VmKeyInfo;
 
95
import edu.ucsb.eucalyptus.msgs.AttachedVolume;
 
96
import edu.ucsb.eucalyptus.msgs.NetworkConfigType;
 
97
import edu.ucsb.eucalyptus.msgs.RunningInstancesItemType;
 
98
import edu.ucsb.eucalyptus.msgs.VmTypeInfo;
 
99
 
 
100
public class VmInstance implements HasName {
 
101
  private static Logger LOG          = Logger.getLogger( VmInstance.class );
 
102
  public static String  DEFAULT_IP   = "0.0.0.0";
 
103
  public static String  DEFAULT_TYPE = "m1.small";
 
104
  
 
105
  private final String                                reservationId;
 
106
  private final int                                   launchIndex;
 
107
  private final String                                instanceId;
 
108
  private final String                                ownerId;
 
109
  private final String                                placement;
 
110
  private final String                                userData;
 
111
  private final List<Network>                         networks      = Lists.newArrayList( );
 
112
  private final NetworkConfigType                     networkConfig = new NetworkConfigType( );
 
113
  private VmImageInfo                                 imageInfo;
 
114
  private VmKeyInfo                                   keyInfo;
 
115
  private VmTypeInfo                                  vmTypeInfo;
 
116
  
 
117
  private final AtomicMarkableReference<VmState>      state         = new AtomicMarkableReference<VmState>( VmState.PENDING, false );
 
118
  private final ConcurrentSkipListSet<AttachedVolume> volumes       = new ConcurrentSkipListSet<AttachedVolume>( );
 
119
  private final StopWatch                             stopWatch     = new StopWatch( );
 
120
  
 
121
  private Date                                        launchTime    = new Date( );
 
122
  private String                                      serviceTag;
 
123
  private SystemState.Reason                          reason;
 
124
  private final List<String>                          reasonDetails = Lists.newArrayList( );
 
125
  private StringBuffer                                consoleOutput = new StringBuffer( );
 
126
  private String                                      passwordData;
 
127
  private Boolean                                     privateNetwork;
 
128
  
 
129
  public VmInstance( final String reservationId, final int launchIndex, final String instanceId, final String ownerId, final String placement,
 
130
                     final String userData, final VmImageInfo imageInfo, final VmKeyInfo keyInfo, final VmTypeInfo vmTypeInfo, final List<Network> networks,
 
131
                     final String networkIndex ) {
 
132
    this.reservationId = reservationId;
 
133
    this.launchIndex = launchIndex;
 
134
    this.instanceId = instanceId;
 
135
    this.ownerId = ownerId;
 
136
    this.placement = placement;
 
137
    this.userData = userData;
 
138
    this.imageInfo = imageInfo;
 
139
    this.keyInfo = keyInfo;
 
140
    this.vmTypeInfo = vmTypeInfo;
 
141
    this.networks.addAll( networks );
 
142
    this.networkConfig.setMacAddress( "d0:0d:" + VmInstances.asMacAddress( this.instanceId ) );
 
143
    this.networkConfig.setIpAddress( DEFAULT_IP );
 
144
    this.networkConfig.setIgnoredPublicIp( DEFAULT_IP );
 
145
    this.networkConfig.setNetworkIndex( Integer.parseInt( networkIndex ) );
 
146
    this.stopWatch.start( );
 
147
    this.updateDns( );
 
148
  }
 
149
  
 
150
  public void updateNetworkIndex( Integer newIndex ) {
 
151
    if ( this.getNetworkConfig( ).getNetworkIndex( ) > 0 && newIndex > 0
 
152
         && ( VmState.RUNNING.equals( this.getState( ) ) || VmState.PENDING.equals( this.getState( ) ) ) ) {
 
153
      this.getNetworkConfig( ).setNetworkIndex( newIndex );
 
154
    }
 
155
  }
 
156
  
 
157
  public void updateAddresses( String privateAddr, String publicAddr ) {
 
158
    this.updatePrivateAddress( privateAddr );
 
159
    this.updatePublicAddress( publicAddr );
 
160
  }
 
161
  
 
162
  public void updatePublicAddress( String publicAddr ) {
 
163
    if ( !VmInstance.DEFAULT_IP.equals( publicAddr ) && !"".equals( publicAddr )
 
164
         && publicAddr != null ) {
 
165
      this.getNetworkConfig( ).setIgnoredPublicIp( publicAddr );
 
166
    }
 
167
  }
 
168
  
 
169
  private void updateDns( ) {
 
170
    String dnsDomain = "dns-disabled";
 
171
    try {
 
172
      dnsDomain = edu.ucsb.eucalyptus.cloud.entities.SystemConfiguration.getSystemConfiguration( ).getDnsDomain( );
 
173
    } catch ( Exception e ) {}
 
174
    this.getNetworkConfig( ).updateDns( dnsDomain );
 
175
  }
 
176
  
 
177
  public void updatePrivateAddress( String privateAddr ) {
 
178
    if ( !VmInstance.DEFAULT_IP.equals( privateAddr ) && !"".equals( privateAddr ) && privateAddr != null ) {
 
179
      this.getNetworkConfig( ).setIpAddress( privateAddr );
 
180
    }
 
181
    updateDns( );
 
182
  }
 
183
  
 
184
  public boolean clearPending( ) {
 
185
    if ( this.state.isMarked( ) && this.getState( ).ordinal( ) > VmState.RUNNING.ordinal( ) ) {
 
186
      this.state.set( this.getState( ), false );
 
187
      VmInstances.cleanUp( this );
 
188
      return true;
 
189
    } else {
 
190
      this.state.set( this.getState( ), false );
 
191
      return false;
 
192
    }
 
193
  }
 
194
  
 
195
  public VmState getState( ) {
 
196
    return this.state.getReference( );
 
197
  }
 
198
  
 
199
  public void setState( final VmState state ) {
 
200
    this.setState( state, SystemState.Reason.NORMAL );
 
201
  }
 
202
  
 
203
  public String getReason( ) {
 
204
    if ( this.reason == null ) {
 
205
      this.reason = Reason.NORMAL;
 
206
    }
 
207
    return this.reason.name( ) + ": " + this.reason + ( this.reasonDetails != null ? " -- " + this.reasonDetails : "" );
 
208
  }
 
209
  
 
210
  private int           stateCounter        = 0;
 
211
  private static String SEND_USER_TERMINATE = "SIGTERM";
 
212
  
 
213
  private void addReasonDetail( String... extra ) {
 
214
    for ( String s : extra ) {
 
215
      this.reasonDetails.add( s );
 
216
    }
 
217
  }
 
218
  
 
219
  public void setState( final VmState newState, SystemState.Reason reason, String... extra ) {
 
220
    this.resetStopWatch( );
 
221
    VmState oldState = this.state.getReference( );
 
222
    if ( VmState.SHUTTING_DOWN.equals( newState ) && VmState.SHUTTING_DOWN.equals( oldState ) && Reason.USER_TERMINATED.equals( reason ) ) {
 
223
      VmInstances.cleanUp( this );
 
224
      if ( !this.reasonDetails.contains( SEND_USER_TERMINATE ) ) {
 
225
        this.addReasonDetail( SEND_USER_TERMINATE );
 
226
      }
 
227
    } else if ( VmState.TERMINATED.equals( newState ) && VmState.TERMINATED.equals( oldState ) ) {
 
228
      VmInstances.getInstance( ).deregister( this.getName( ) );
 
229
    } else if ( !this.getState( ).equals( newState ) ) {
 
230
      if ( Reason.APPEND.equals( reason ) ) {
 
231
        reason = this.reason;
 
232
      }
 
233
      this.addReasonDetail( extra );
 
234
      LOG.info( String.format( "%s state change: %s -> %s", this.getInstanceId( ), this.getState( ), newState ) );
 
235
      this.reason = reason;
 
236
      if ( this.state.isMarked( ) && VmState.PENDING.equals( this.getState( ) ) ) {
 
237
        if ( VmState.SHUTTING_DOWN.equals( newState ) || VmState.PENDING.equals( newState ) ) {
 
238
          this.state.set( newState, true );
 
239
        } else {
 
240
          this.state.set( newState, false );
 
241
        }
 
242
      } else if ( this.state.isMarked( ) && VmState.SHUTTING_DOWN.equals( this.getState( ) ) ) {
 
243
        LOG.debug( "Ignoring events for state transition because the instance is marked as pending: " + oldState + " to " + this.getState( ) );
 
244
      } else if ( !this.state.isMarked( ) ) {
 
245
        if ( oldState.ordinal( ) <= VmState.RUNNING.ordinal( ) && newState.ordinal( ) > VmState.RUNNING.ordinal( ) ) {
 
246
          this.state.set( newState, false );
 
247
          VmInstances.cleanUp( this );
 
248
        } else if ( VmState.PENDING.equals( oldState ) && VmState.RUNNING.equals( newState ) ) {
 
249
          this.state.set( newState, false );
 
250
        } else if ( VmState.TERMINATED.equals( newState ) && oldState.ordinal( ) <= VmState.RUNNING.ordinal( ) ) {
 
251
          this.state.set( newState, false );
 
252
          VmInstances.getInstance( ).disable( this.getName( ) );
 
253
          VmInstances.cleanUp( this );
 
254
        } else if ( VmState.TERMINATED.equals( newState ) && oldState.ordinal( ) > VmState.RUNNING.ordinal( ) ) {
 
255
          this.state.set( newState, false );
 
256
          VmInstances.getInstance( ).disable( this.getName( ) );
 
257
        } else if ( oldState.ordinal( ) > VmState.RUNNING.ordinal( ) && newState.ordinal( ) <= VmState.RUNNING.ordinal( ) ) {
 
258
          this.state.set( oldState, false );
 
259
          VmInstances.cleanUp( this );
 
260
        } else if ( newState.ordinal( ) > oldState.ordinal( ) ) {
 
261
          this.state.set( newState, false );
 
262
        }
 
263
        EventRecord.here( VmInstance.class, EventClass.VM, EventType.VM_STATE,  "user="+this.getOwnerId( ), "instance="+this.getInstanceId( ), "type="+this.getVmTypeInfo( ).getName( ), "state="+ this.state.getReference( ).name( ), "details="+this.reasonDetails.toString( )  ).info();
 
264
      } else {
 
265
        LOG.debug( "Ignoring events for state transition because the instance is marked as pending: " + oldState + " to " + this.getState( ) );
 
266
      }
 
267
      if ( !this.getState( ).equals( oldState ) ) {
 
268
        EventRecord.caller( VmInstance.class, EventType.VM_STATE, this.instanceId, this.ownerId, this.state.getReference( ).name( ), this.launchTime );
 
269
      }
 
270
    }
 
271
  }
 
272
  
 
273
  public String getByKey( String path ) {
 
274
    Map<String, String> m = getMetadataMap( );
 
275
    if ( path == null ) path = "";
 
276
    LOG.debug( "Servicing metadata request:" + path + " -> " + m.get( path ) );
 
277
    if ( m.containsKey( path + "/" ) ) path += "/";
 
278
    return m.get( path ).replaceAll( "\n*\\z", "" );
 
279
  }
 
280
  
 
281
  private Map<String, String> getMetadataMap( ) {
 
282
    Map<String, String> m = new HashMap<String, String>( );
 
283
    m.put( "ami-id", this.getImageInfo( ).getImageId( ) );
 
284
    m.put( "product-codes", this.getImageInfo( ).getProductCodes( ).toString( ).replaceAll( "[\\Q[]\\E]", "" ).replaceAll( ", ", "\n" ) );
 
285
    m.put( "ami-launch-index", "" + this.getLaunchIndex( ) );
 
286
    m.put( "ancestor-ami-ids", this.getImageInfo( ).getAncestorIds( ).toString( ).replaceAll( "[\\Q[]\\E]", "" ).replaceAll( ", ", "\n" ) );
 
287
    
 
288
    m.put( "ami-manifest-path", this.getImageInfo( ).getImageLocation( ) );
 
289
    m.put( "hostname", this.getPublicAddress( ) );
 
290
    m.put( "instance-id", this.getInstanceId( ) );
 
291
    m.put( "instance-type", this.getVmTypeInfo( ).getName( ) );
 
292
    if ( Component.dns.isLocal( ) ) {
 
293
      m.put( "local-hostname", this.getNetworkConfig( ).getPrivateDnsName( ) );
 
294
    } else {
 
295
      m.put( "local-hostname", this.getNetworkConfig( ).getIpAddress( ) );
 
296
    }
 
297
    m.put( "local-ipv4", this.getNetworkConfig( ).getIpAddress( ) );
 
298
    if ( Component.dns.isLocal( ) ) {
 
299
      m.put( "public-hostname", this.getNetworkConfig( ).getPublicDnsName( ) );
 
300
    } else {
 
301
      m.put( "public-hostname", this.getPublicAddress( ) );
 
302
    }
 
303
    m.put( "public-ipv4", this.getPublicAddress( ) );
 
304
    m.put( "reservation-id", this.getReservationId( ) );
 
305
    m.put( "kernel-id", this.getImageInfo( ).getKernelId( ) );
 
306
    if ( this.getImageInfo( ).getRamdiskId( ) != null ) {
 
307
      m.put( "ramdisk-id", this.getImageInfo( ).getRamdiskId( ) );
 
308
    }
 
309
    m.put( "security-groups", this.getNetworkNames( ).toString( ).replaceAll( "[\\Q[]\\E]", "" ).replaceAll( ", ", "\n" ) );
 
310
    
 
311
    m.put( "block-device-mapping/", "emi\nephemeral\nephemeral0\nroot\nswap" );
 
312
    m.put( "block-device-mapping/emi", "sda1" );
 
313
    m.put( "block-device-mapping/ami", "sda1" );
 
314
    m.put( "block-device-mapping/ephemeral", "sda2" );
 
315
    m.put( "block-device-mapping/ephemeral0", "sda2" );
 
316
    m.put( "block-device-mapping/swap", "sda3" );
 
317
    m.put( "block-device-mapping/root", "/dev/sda1" );
 
318
    
 
319
    m.put( "public-keys/", "0=" + this.getKeyInfo( ).getName( ) );
 
320
    m.put( "public-keys/0", "openssh-key" );
 
321
    m.put( "public-keys/0/", "openssh-key" );
 
322
    m.put( "public-keys/0/openssh-key", this.getKeyInfo( ).getValue( ) );
 
323
    
 
324
    m.put( "placement/", "availability-zone" );
 
325
    m.put( "placement/availability-zone", this.getPlacement( ) );
 
326
    String dir = "";
 
327
    for ( String entry : m.keySet( ) ) {
 
328
      if ( entry.contains( "/" ) && !entry.endsWith( "/" ) ) continue;
 
329
      dir += entry + "\n";
 
330
    }
 
331
    m.put( "", dir );
 
332
    return m;
 
333
  }
 
334
  
 
335
  public int compareTo( final Object o ) {
 
336
    VmInstance that = ( VmInstance ) o;
 
337
    return this.getName( ).compareTo( that.getName( ) );
 
338
  }
 
339
  
 
340
  public synchronized long resetStopWatch( ) {
 
341
    this.stopWatch.stop( );
 
342
    long ret = this.stopWatch.getTime( );
 
343
    this.stopWatch.reset( );
 
344
    this.stopWatch.start( );
 
345
    return ret;
 
346
  }
 
347
  
 
348
  public synchronized long getSplitTime( ) {
 
349
    this.stopWatch.split( );
 
350
    long ret = this.stopWatch.getSplitTime( );
 
351
    this.stopWatch.unsplit( );
 
352
    return ret;
 
353
  }
 
354
  
 
355
  public RunningInstancesItemType getAsRunningInstanceItemType( boolean dns ) {
 
356
    RunningInstancesItemType runningInstance = new RunningInstancesItemType( );
 
357
    
 
358
    runningInstance.setAmiLaunchIndex( Integer.toString( this.launchIndex ) );
 
359
    runningInstance.setStateCode( Integer.toString( this.state.getReference( ).getCode( ) ) );
 
360
    runningInstance.setStateName( this.state.getReference( ).getName( ) );
 
361
    runningInstance.setInstanceId( this.instanceId );
 
362
    runningInstance.setImageId( this.imageInfo.getImageId( ) );
 
363
    runningInstance.setKernel( this.imageInfo.getKernelId( ) );
 
364
    runningInstance.setRamdisk( this.imageInfo.getRamdiskId( ) );
 
365
    runningInstance.setProductCodes( this.imageInfo.getProductCodes( ) );
 
366
    
 
367
    if ( dns ) {
 
368
      runningInstance.setDnsName( this.getNetworkConfig( ).getPublicDnsName( ) );
 
369
      runningInstance.setPrivateDnsName( this.getNetworkConfig( ).getPrivateDnsName( ) );
 
370
    } else {
 
371
      runningInstance.setPrivateDnsName( this.getNetworkConfig( ).getIpAddress( ) );
 
372
      if ( !VmInstance.DEFAULT_IP.equals( this.getPublicAddress( ) ) ) {
 
373
        runningInstance.setDnsName( this.getPublicAddress( ) );
 
374
      } else {
 
375
        runningInstance.setDnsName( this.getNetworkConfig( ).getIpAddress( ) );
 
376
      }
 
377
    }
 
378
    runningInstance.setReason( this.getReason( ) );
 
379
    
 
380
    if ( this.getKeyInfo( ) != null )
 
381
      runningInstance.setKeyName( this.getKeyInfo( ).getName( ) );
 
382
    else runningInstance.setKeyName( "" );
 
383
    
 
384
    runningInstance.setInstanceType( this.getVmTypeInfo( ).getName( ) );
 
385
    runningInstance.setPlacement( this.placement );
 
386
    
 
387
    runningInstance.setLaunchTime( this.launchTime );
 
388
    
 
389
    return runningInstance;
 
390
  }
 
391
  
 
392
  public void setServiceTag( String serviceTag ) {
 
393
    this.serviceTag = serviceTag;
 
394
  }
 
395
  
 
396
  public String getServiceTag( ) {
 
397
    return serviceTag;
 
398
  }
 
399
  
 
400
  public boolean hasPublicAddress( ) {
 
401
    NetworkConfigType conf = getNetworkConfig( );
 
402
    return conf != null && !( DEFAULT_IP.equals( conf.getIgnoredPublicIp( ) ) || conf.getIpAddress( ).equals( conf.getIgnoredPublicIp( ) ) );
 
403
  }
 
404
  
 
405
  public String getName( ) {
 
406
    return this.instanceId;
 
407
  }
 
408
  
 
409
  public void setLaunchTime( final Date launchTime ) {
 
410
    this.launchTime = launchTime;
 
411
  }
 
412
  
 
413
  public String getReservationId( ) {
 
414
    return reservationId;
 
415
  }
 
416
  
 
417
  public String getInstanceId( ) {
 
418
    return instanceId;
 
419
  }
 
420
  
 
421
  public String getOwnerId( ) {
 
422
    return ownerId;
 
423
  }
 
424
  
 
425
  public int getLaunchIndex( ) {
 
426
    return launchIndex;
 
427
  }
 
428
  
 
429
  public String getPlacement( ) {
 
430
    return placement;
 
431
  }
 
432
  
 
433
  public Date getLaunchTime( ) {
 
434
    return launchTime;
 
435
  }
 
436
  
 
437
  public String getUserData( ) {
 
438
    return userData;
 
439
  }
 
440
  
 
441
  public VmKeyInfo getKeyInfo( ) {
 
442
    return keyInfo;
 
443
  }
 
444
  
 
445
  public StringBuffer getConsoleOutput( ) {
 
446
    return consoleOutput;
 
447
  }
 
448
  
 
449
  public void setConsoleOutput( final StringBuffer consoleOutput ) {
 
450
    this.consoleOutput = consoleOutput;
 
451
    if ( this.passwordData == null ) {
 
452
      String tempCo = consoleOutput.toString( ).replaceAll( "[\r\n]*", "" );
 
453
      if ( tempCo.matches( ".*<Password>[\\w=+/]*</Password>.*" ) ) {
 
454
        this.passwordData = tempCo.replaceAll( ".*<Password>", "" ).replaceAll( "</Password>.*", "" );
 
455
      }
 
456
    }
 
457
  }
 
458
  
 
459
  public void setKeyInfo( final VmKeyInfo keyInfo ) {
 
460
    this.keyInfo = keyInfo;
 
461
  }
 
462
  
 
463
  public VmTypeInfo getVmTypeInfo( ) {
 
464
    return vmTypeInfo;
 
465
  }
 
466
  
 
467
  public void setVmTypeInfo( final VmTypeInfo vmTypeInfo ) {
 
468
    this.vmTypeInfo = vmTypeInfo;
 
469
  }
 
470
  
 
471
  public List<Network> getNetworks( ) {
 
472
    return networks;
 
473
  }
 
474
  
 
475
  public List<String> getNetworkNames( ) {
 
476
    List<String> nets = new ArrayList<String>( );
 
477
    for ( Network net : this.getNetworks( ) )
 
478
      nets.add( net.getNetworkName( ) );
 
479
    return nets;
 
480
  }
 
481
  
 
482
  public String getPrivateAddress( ) {
 
483
    return networkConfig.getIpAddress( );
 
484
  }
 
485
  
 
486
  public String getPublicAddress( ) {
 
487
    return networkConfig.getIgnoredPublicIp( );
 
488
  }
 
489
  
 
490
  public NetworkConfigType getNetworkConfig( ) {
 
491
    return networkConfig;
 
492
  }
 
493
  
 
494
  public VmImageInfo getImageInfo( ) {
 
495
    return imageInfo;
 
496
  }
 
497
  
 
498
  public void setImageInfo( final VmImageInfo imageInfo ) {
 
499
    this.imageInfo = imageInfo;
 
500
  }
 
501
  
 
502
  public void updateVolumeState( final String volumeId, String state ) {
 
503
    AttachedVolume v = Iterables.find( this.volumes, new Predicate<AttachedVolume>( ) {
 
504
      @Override
 
505
      public boolean apply( AttachedVolume arg0 ) {
 
506
        return arg0.getVolumeId( ).equals( volumeId );
 
507
      }
 
508
    } );
 
509
    v.setStatus( state );
 
510
  }
 
511
  
 
512
  public NavigableSet<AttachedVolume> getVolumes( ) {
 
513
    return this.volumes;
 
514
  }
 
515
  
 
516
  public void setVolumes( final List<AttachedVolume> newVolumes ) {
 
517
    for ( AttachedVolume vol : newVolumes ) {
 
518
      vol.setInstanceId( this.getInstanceId( ) );
 
519
      vol.setStatus( "attached" );
 
520
    }
 
521
    Set<AttachedVolume> oldVolumes = Sets.newHashSet( this.getVolumes( ) );
 
522
    this.volumes.retainAll( volumes );
 
523
    this.volumes.addAll( volumes );
 
524
    for ( AttachedVolume v : oldVolumes ) {
 
525
      if ( "attaching".equals( v.getStatus( ) ) && !this.volumes.contains( v ) ) {
 
526
        this.volumes.add( v );
 
527
      }
 
528
    }
 
529
  }
 
530
  
 
531
  public String getPasswordData( ) {
 
532
    return this.passwordData;
 
533
  }
 
534
  
 
535
  public void setPasswordData( String passwordData ) {
 
536
    this.passwordData = passwordData;
 
537
  }
 
538
  
 
539
  @Override
 
540
  public boolean equals( Object o ) {
 
541
    if ( this == o ) return true;
 
542
    if ( o == null || getClass( ) != o.getClass( ) ) return false;
 
543
    
 
544
    VmInstance vmInstance = ( VmInstance ) o;
 
545
    
 
546
    if ( !instanceId.equals( vmInstance.instanceId ) ) return false;
 
547
    
 
548
    return true;
 
549
  }
 
550
  
 
551
  @Override
 
552
  public int hashCode( ) {
 
553
    return instanceId.hashCode( );
 
554
  }
 
555
  
 
556
  @Override
 
557
  public String toString( ) {
 
558
    return String
 
559
                 .format(
 
560
                          "VmInstance [imageInfo=%s, instanceId=%s, keyInfo=%s, launchIndex=%s, launchTime=%s, networkConfig=%s, networks=%s, ownerId=%s, placement=%s, privateNetwork=%s, reason=%s, reservationId=%s, state=%s, stopWatch=%s, userData=%s, vmTypeInfo=%s, volumes=%s]",
 
561
                          this.imageInfo, this.instanceId, this.keyInfo, this.launchIndex, this.launchTime, this.networkConfig, this.networks, this.ownerId,
 
562
                          this.placement, this.privateNetwork, this.reason, this.reservationId, this.state, this.stopWatch, this.userData, this.vmTypeInfo,
 
563
                          this.volumes );
 
564
  }
 
565
  
 
566
  public int getNetworkIndex( ) {
 
567
    return this.getNetworkConfig( ).getNetworkIndex( );
 
568
  }
 
569
  
 
570
}