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

« back to all changes in this revision

Viewing changes to clc/modules/msgs/src/main/java/com/eucalyptus/bootstrap/Bootstrap.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
package com.eucalyptus.bootstrap;
 
2
 
 
3
import java.io.File;
 
4
import java.util.Arrays;
 
5
import java.util.List;
 
6
import org.apache.log4j.Logger;
 
7
import com.eucalyptus.bootstrap.transitions.LoadConfigs;
 
8
import com.eucalyptus.component.Component;
 
9
import com.eucalyptus.component.Components;
 
10
import com.eucalyptus.component.Lifecycles;
 
11
import com.eucalyptus.component.Resource;
 
12
import com.eucalyptus.records.EventRecord;
 
13
import com.eucalyptus.records.EventType;
 
14
import com.eucalyptus.system.Ats;
 
15
import com.eucalyptus.system.BaseDirectory;
 
16
import com.eucalyptus.util.Committor;
 
17
import com.eucalyptus.util.LogUtil;
 
18
import com.eucalyptus.util.Transition;
 
19
import com.google.common.collect.Lists;
 
20
 
 
21
public class Bootstrap {
 
22
  private static Logger LOG = Logger.getLogger( Bootstrap.class );
 
23
  
 
24
  public enum Stage {
 
25
    SystemInit,
 
26
    PrivilegedConfiguration,
 
27
    UnprivilegedConfiguration,
 
28
    SystemCredentialsInit, /* <-- this means system credentials, not user. */
 
29
    RemoteConfiguration,
 
30
    DatabaseInit,
 
31
    PersistenceContextInit,
 
32
    DeferredClassInit,
 
33
    RemoteServicesInit,
 
34
    UserCredentialsInit,
 
35
    CloudServiceInit,
 
36
    Verification,
 
37
    Anonymous,
 
38
    Final;
 
39
    public static List<Stage> list( ) {
 
40
      return Arrays.asList( Stage.values( ) );
 
41
    }
 
42
    
 
43
    public <A> Transition<A, Bootstrap.Stage> to( final Bootstrap.Stage s, final Committor<A> c ) throws Exception {
 
44
      return ( Transition<A, Bootstrap.Stage> ) Transition.anonymous( this, s, c );
 
45
    }
 
46
    
 
47
    private List<Bootstrapper> bootstrappers         = Lists.newArrayList( );
 
48
    private List<Resource>     resources             = Lists.newArrayList( );
 
49
    
 
50
    public <A, B extends Comparable<? extends Comparable<?>>> Transition<A,Comparable<? extends Comparable<?>>> to( B s, Committor<A> c ) throws Exception {
 
51
      return ( Transition<A,Comparable<? extends Comparable<?>>> ) Transition.anonymous( this, s, c );
 
52
    }
 
53
    
 
54
    public List<Bootstrapper> getBootstrappers( ) {
 
55
      return this.bootstrappers;
 
56
    }
 
57
    
 
58
    public void addBootstrapper( Bootstrapper b ) {
 
59
      if ( this.bootstrappers.contains( b ) ) {
 
60
        throw BootstrapException.throwFatal( "Duplicate bootstrapper registration: " + b.getClass( ).toString( ) );
 
61
      } else {
 
62
        this.bootstrappers.add( b );
 
63
      }
 
64
    }
 
65
    
 
66
    public void printAgenda( ) {
 
67
      EventRecord.here( Bootstrap.class, EventType.BOOTSTRAP_STAGE_AGENDA, this.name( ), Bootstrap.loading ? "LOAD" : "START" ).info( );
 
68
      for ( Bootstrapper b : this.bootstrappers ) {
 
69
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAP_STAGE_AGENDA, this.name( ), b.getClass( ).getCanonicalName( ) ).info( );
 
70
      }
 
71
    }
 
72
    
 
73
    public void updateBootstrapDependencies( ) {
 
74
      for ( Bootstrapper b : Lists.newArrayList( this.bootstrappers ) ) {
 
75
        if ( !b.checkLocal( ) ) {
 
76
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_SKIPPED, "stage:" + Bootstrap.getCurrentStage( ), this.getClass( ).getSimpleName( ),
 
77
                            "Depends.local=" + b.toString( ), "Component." + b.toString( ) + "=remote" ).info( );
 
78
          this.bootstrappers.remove( b );
 
79
        } else if ( !b.checkRemote( ) ) {
 
80
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_SKIPPED, "stage:" + Bootstrap.getCurrentStage( ), this.getClass( ).getSimpleName( ),
 
81
                            "Depends.remote=" + b.toString( ), "Component." + b.toString( ) + "=local" ).info( );
 
82
          this.bootstrappers.remove( b );
 
83
        }
 
84
      }
 
85
    }
 
86
    
 
87
    public void load( ) {
 
88
      this.updateBootstrapDependencies( );
 
89
      this.printAgenda( );
 
90
      for ( Bootstrapper b : this.bootstrappers ) {
 
91
        try {
 
92
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_LOAD, this.name( ), b.getClass( ).getCanonicalName( ) ).info( );
 
93
          boolean result = b.load( this );
 
94
          if ( !result ) {
 
95
            throw BootstrapException.throwFatal( b.getClass( ).getSimpleName( ) + " returned 'false' from load( ): terminating bootstrap." );
 
96
          }
 
97
        } catch ( Throwable e ) {
 
98
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_ERROR, this.name( ), b.getClass( ).getCanonicalName( ) ).info( );
 
99
          throw BootstrapException.throwFatal( b.getClass( ).getSimpleName( ) + " threw an error in load( ): " + e.getMessage( ), e );
 
100
        }
 
101
      }
 
102
    }
 
103
 
 
104
    public void start( ) {
 
105
      this.updateBootstrapDependencies( );
 
106
      this.printAgenda( );
 
107
      for ( Bootstrapper b : this.bootstrappers ) {
 
108
        try {
 
109
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_START, this.name( ), b.getClass( ).getCanonicalName( ) ).info( );
 
110
          boolean result = b.start( );
 
111
          if ( !result ) {
 
112
            throw BootstrapException.throwFatal( b.getClass( ).getSimpleName( ) + " returned 'false' from start( ): terminating bootstrap." );
 
113
          }
 
114
        } catch ( Throwable e ) {
 
115
          EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_ERROR, this.name( ), b.getClass( ).getCanonicalName( ) ).info( );
 
116
          throw BootstrapException.throwFatal( b.getClass( ).getSimpleName( ) + " threw an error in start( ): " + e.getMessage( ), e );
 
117
        }
 
118
      }
 
119
    }
 
120
        
 
121
    public String describe( ) {
 
122
      StringBuffer buf = new StringBuffer( );
 
123
      for ( Bootstrapper b : this.bootstrappers ) {
 
124
        buf.append( EventRecord.caller( Component.class, EventType.COMPONENT_INFO, this.name( ), b.getClass( ).getSimpleName( ) ) ).append( "\n" );
 
125
      }
 
126
      return buf.toString( );
 
127
    }
 
128
    
 
129
    public String getResourceName( ) {
 
130
      return String.format( "com.eucalyptus.%sProvider", this.name( ).replaceAll( "Init\\Z", "" ) );
 
131
    }
 
132
    
 
133
    public List<Resource> getResources( ) {
 
134
      return this.resources;
 
135
    }
 
136
    
 
137
  }
 
138
  
 
139
  private static Boolean loading      = false;
 
140
  private static Boolean starting     = false;
 
141
  private static Boolean finished     = false;
 
142
  private static Stage   currentStage = Stage.SystemInit;
 
143
  
 
144
  public static Stage getCurrentStage( ) {
 
145
    return currentStage;
 
146
  }
 
147
  
 
148
  private static void doDiscovery( ) {
 
149
    File libDir = new File( BaseDirectory.LIB.toString( ) );
 
150
    for ( File f : libDir.listFiles( ) ) {
 
151
      if ( f.getName( ).startsWith( com.eucalyptus.bootstrap.Component.eucalyptus.name( ) ) && f.getName( ).endsWith( ".jar" )
 
152
           && !f.getName( ).matches( ".*-ext-.*" ) ) {
 
153
        LOG.debug( "Found eucalyptus component jar: " + f.getName( ) );
 
154
        try {
 
155
          ServiceJarDiscovery.processFile( f );
 
156
        } catch ( Throwable e ) {
 
157
          LOG.error( e.getMessage( ) );
 
158
          continue;
 
159
        }
 
160
      }
 
161
    }
 
162
    ServiceJarDiscovery.runDiscovery( );
 
163
  }
 
164
  
 
165
  @SuppressWarnings( "deprecation" )
 
166
  public static void initBootstrappers( ) {
 
167
    for ( Bootstrapper bootstrap : BootstrapperDiscovery.getBootstrappers( ) ) {//these have all been checked at discovery time
 
168
      com.eucalyptus.bootstrap.Component comp, old = com.eucalyptus.bootstrap.Component.any;
 
169
      String bc = bootstrap.getClass( ).getCanonicalName( );
 
170
      Bootstrap.Stage stage = Ats.from( bootstrap ).get( RunDuring.class ).value( );
 
171
      Provides p = Ats.from( bootstrap ).get( Provides.class );
 
172
      comp = ( p.value( ) == null ? old : p.value( ) );//TODO: remap orphan bootstrapper to 'any'
 
173
      if ( Components.delegate.any.equals( comp ) ) {
 
174
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_ADDED, currentStage.name( ), bc, "Provides", comp.name( ),
 
175
                          "Component." + comp.name( ) + ".isEnabled", "true" ).info( );
 
176
        stage.addBootstrapper( bootstrap );
 
177
      } else if ( !comp.isSingleton( ) && !comp.isEnabled( ) && Components.contains( comp ) ) { //report skipping a bootstrapper for an enabled component
 
178
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_SKIPPED, currentStage.name( ), bc, "Provides", comp.name( ),
 
179
                          "Component." + comp.name( ) + ".isEnabled", comp.isEnabled( ).toString( ) ).info( );
 
180
      } else if ( !bootstrap.checkLocal( ) ) {
 
181
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_SKIPPED, currentStage.name( ), bc, "DependsLocal", comp.name( ),
 
182
                          "Component." + comp.name( ) + ".isLocal", comp.isLocal( ).toString( ) ).info( );
 
183
      } else if ( !bootstrap.checkRemote( ) ) {
 
184
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_SKIPPED, currentStage.name( ), bc, "DependsRemote", comp.name( ),
 
185
                          "Component." + comp.name( ) + ".isLocal", comp.isLocal( ).toString( ) ).info( );
 
186
      } else if ( !Components.contains( comp ) ) {
 
187
        throw BootstrapException.throwFatal( "Bootstrap class provides a component for which registration failed: " + bc + " provides " + comp.name( ) );
 
188
      } else {
 
189
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAPPER_ADDED, currentStage.name( ), bc, "Provides", comp.name( ),
 
190
                          "Component." + comp.name( ) + ".isEnabled", comp.isEnabled( ).toString( ) ).info( );
 
191
        Component realComponent = Components.lookup( comp );
 
192
        realComponent.getConfiguration( ).addBootstrapper( bootstrap );
 
193
        stage.addBootstrapper( bootstrap );
 
194
      }
 
195
    }
 
196
  }
 
197
  
 
198
  public static Stage transition( ) {
 
199
    if ( currentStage == Stage.SystemInit && !loading && !starting && !finished ) {
 
200
      loading = true;
 
201
      starting = false;
 
202
      finished = false;
 
203
    } else if ( currentStage != null ) {
 
204
      EventRecord.here( Bootstrap.class, EventType.BOOTSTRAP_STAGE_COMPLETE, currentStage.toString( ) ).info( );
 
205
      if ( Stage.Final.equals( currentStage ) ) {
 
206
        currentStage = null;
 
207
        if ( loading && !starting && !finished ) {
 
208
          loading = true;
 
209
          starting = true;
 
210
          finished = false;
 
211
        } else if ( loading && starting && !finished ) {
 
212
          loading = true;
 
213
          starting = true;
 
214
          finished = true;
 
215
        }
 
216
        return currentStage;
 
217
      }
 
218
    }
 
219
    int currOrdinal = currentStage != null ? currentStage.ordinal( ) : -1;
 
220
    for ( int i = currOrdinal + 1; i <= Stage.Final.ordinal( ); i++ ) {
 
221
      currentStage = Stage.values( )[i];
 
222
      if ( currentStage.bootstrappers.isEmpty( ) ) {
 
223
        EventRecord.here( Bootstrap.class, EventType.BOOTSTRAP_STAGE_SKIPPED, currentStage.name( ) ).info( );
 
224
        continue;
 
225
      } else {
 
226
        return currentStage;
 
227
      }
 
228
    }
 
229
    return currentStage;
 
230
  }
 
231
  
 
232
  public static Boolean isFinished( ) {
 
233
    return finished;
 
234
  }
 
235
 
 
236
  public static void describeComponents( String message ) {
 
237
    LOG.info( LogUtil.header( message ) );
 
238
    
 
239
  }
 
240
  
 
241
  public static void initialize( ) throws Throwable {
 
242
    LOG.info( LogUtil.header( "Initializing component resources." ) );
 
243
    Transition.anonymous( LoadConfigs.class ).transition( Stage.list( ) );
 
244
    for ( Component c : Components.list( ) ) {
 
245
      LOG.info( c.toString( ) );
 
246
    }
 
247
        
 
248
    LOG.info( LogUtil.header( "Initializing discoverable bootstrap resources." ) );
 
249
    Bootstrap.doDiscovery( );
 
250
 
 
251
    LOG.info( LogUtil.header( "Initializing local singleton component services." ) );
 
252
    Lifecycles.State.PRIMORDIAL.to( Lifecycles.State.INITIALIZED, new Committor<Component>( ) {
 
253
      @Override
 
254
      public void commit( Component comp ) throws Exception {
 
255
        if( comp.isSingleton( ) && ( comp.isLocal( ) || comp.getPeer().equals(com.eucalyptus.bootstrap.Component.dns) ) ) {
 
256
          comp.buildService( );
 
257
        }
 
258
      }
 
259
    } ).transition( Components.list( ) );
 
260
 
 
261
    Bootstrap.describeComponents( "Preparing to initialize the system." );
 
262
 
 
263
    LOG.info( LogUtil.header( "Initializing bootstrappers." ) );
 
264
    Bootstrap.initBootstrappers( );
 
265
    
 
266
    Bootstrap.describeComponents( "Initialized system: ready to start bootstrap." );
 
267
  }
 
268
  
 
269
}