def do_configure(): optparser = bus.optparser cnf = bus.cnf cnf.reconfigure( values=CmdLineIni.to_kvals(optparser.values.cnf), silent=optparser.values.import_server, yesall=optparser.values.yesall )
def do_configure(): optparser = bus.optparser cnf = bus.cnf cnf.reconfigure(values=CmdLineIni.to_kvals(optparser.values.cnf), silent=optparser.values.import_server, yesall=optparser.values.yesall)
with open(PID_FILE, 'w') as fp: fp.write(str(pid)) cnf = bus.cnf cnf.on('apply_user_data', _apply_user_data) if optparser.values.configure: do_configure() sys.exit() elif optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" cnf.state = ScalarizrState.IMPORTING # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) cnf.reconfigure(values=values, silent=True, yesall=True) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform # Check that service started after dirty bundle if ini.has_option(config.SECT_GENERAL, config.OPT_SERVER_ID):
def start(self): self._logger.debug("Initialize scalarizr...") _init() _init_environ() # Starting scalarizr daemon initialization globals()['_pid'] = pid = os.getpid() self._logger.info('[pid: %d] Starting scalarizr %s', pid, __version__) __node__['start_time'] = time.time() if not 'Windows' == linux.os['family']: # Check for another running scalarzir if os.path.exists(PID_FILE): try: another_pid = None with open(PID_FILE, 'r') as fp: another_pid = int(fp.read().strip()) except ValueError: pass else: if pid != another_pid and os.path.exists( '/proc/%s/status' % (another_pid, )): self._logger.error( 'Cannot start scalarizr: Another process (pid: %s) already running', another_pid) sys.exit(1) # Write PID with open(PID_FILE, 'w') as fp: fp.write(str(pid)) cnf = bus.cnf optparser = bus.optparser if optparser and optparser.values.configure: do_configure() sys.exit() elif optparser: if optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" __node__['state'] = 'importing' # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) self._logger.info( 'Configuring Scalarizr. This can take a few minutes...') cnf.reconfigure(values=values, silent=True, yesall=True) elif __node__['state'] == 'importing': # Reset state __node__['state'] = 'unknown' if __node__['state'] != 'importing': self._talk_to_updclient() # UpdateClient should fetch meta-data for us. metadata.wait(timeout=60) if linux.os.windows: try: wintool.wait_boot() except wintool.RebootExpected: self._logger.info('Waiting for interruption...') time.sleep(600) try: server_id = __node__['server_id'] except KeyError: server_id = None if optparser and not optparser.values.import_server \ and server_id != metadata.user_data()['serverid']: # This role was bundled with Cloud API call (i.e. CreateImage) # Now we're starting with a new server and should reset it's state self._logger.info(('This image was bundled with cloud API call. ' 'Cleauping ancestor server data')) _cleanup_after_rebundle() __node__['state'] = 'bootstrapping' if __node__['state'] == 'unknown': __node__['state'] = 'bootstrapping' if __node__['state'] == 'rebundling': __node__['state'] == 'running' if __node__['state'] == 'bootstrapping': _apply_user_data(from_scalr=False) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform # Initialize local database _init_db() STATE['global.start_after_update'] = int( bool(STATE['global.version'] and STATE['global.version'] != __version__)) STATE['global.version'] = __version__ if __node__['state'] == 'importing' and not linux.os.windows: try: pkgmgr.updatedb() except: self._logger.warn( 'Failed to update package manager database: %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Check Scalr version if not bus.scalr_version: version_file = cnf.private_path('.scalr-version') if os.path.exists(version_file): bus.scalr_version = None with open(version_file, 'r') as fp: bus.scalr_version = tuple(fp.read().strip().split('.')) else: bus.scalr_version = _detect_scalr_version() with open(version_file, 'w') as fp: fp.write('.'.join(map(str, bus.scalr_version))) # Apply Command-line passed configuration options if optparser: cnf.update(CmdLineIni.to_ini_sections(optparser.values.cnf)) # Validate configuration num_errors = do_validate_cnf() if num_errors or (optparser and optparser.values.validate_cnf): sys.exit(int(not num_errors or 1)) # Initialize scalarizr services self._init_services() if STATE['global.start_after_update'] and __node__[ 'state'] == 'running': self._logger.info('Scalarizr was updated to %s', __version__) __node__['messaging'].send( 'HostUpdate', body={'scalarizr': { 'version': __version__ }}) if __node__['state'] == 'running': # ReSync user-data _apply_user_data(from_scalr=True) try: bus.fire('init') except: self._logger.warn('Caught exception in "init": %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Install signal handlers if not linux.os.windows: signal.signal(signal.SIGTERM, self.onSIGTERM) self._start_services() # Fire start self.running = True try: bus.fire("start") except (BaseException, Exception) as e: if isinstance(e, SystemExit): raise self._logger.warn('Caught exception in "start": %s', e, exc_info=sys.exc_info()) try: while self.running: if linux.os.windows_family: rc = win32event.WaitForSingleObject(self.hWaitStop, 30000) if rc == win32event.WAIT_OBJECT_0: # Service stopped, stop main loop break else: try: select.select([], [], [], 30) except select.error, e: if e.args[0] == 4: # Interrupted syscall continue raise except KeyboardInterrupt: self._logger.debug('Mainloop: KeyboardInterrupt') finally: self._logger.debug('Mainloop: finally') if self.running and os.getpid() == _pid: self._shutdown() self._logger.debug('Mainloop: leave')
def start(self): self._logger.debug("Initialize scalarizr...") _init() # Starting scalarizr daemon initialization globals()['_pid'] = pid = os.getpid() self._logger.info('[pid: %d] Starting scalarizr %s', pid, __version__) if not 'Windows' == linux.os['family']: # Check for another running scalarzir if os.path.exists(PID_FILE): try: another_pid = None with open(PID_FILE, 'r') as fp: another_pid = int(fp.read().strip()) except ValueError: pass else: if pid != another_pid and os.path.exists( '/proc/%s/status' % (another_pid, )): self._logger.error( 'Cannot start scalarizr: Another process (pid: %s) already running', another_pid) sys.exit(1) # Write PID with open(PID_FILE, 'w') as fp: fp.write(str(pid)) cnf = bus.cnf cnf.on('apply_user_data', _apply_user_data) optparser = bus.optparser if optparser and optparser.values.configure: do_configure() sys.exit() elif optparser and optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" cnf.state = ScalarizrState.IMPORTING # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) cnf.reconfigure(values=values, silent=True, yesall=True) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform # Check that service started after dirty bundle if not optparser.values.import_server and \ ini.has_option(config.SECT_GENERAL, config.OPT_SERVER_ID): # XXX: nimbula's user-data is uploaded by ssh server_id = ini.get(config.SECT_GENERAL, config.OPT_SERVER_ID) if pl.name in ('nimbula', 'rackspace', 'openstack' ) and cnf.state != ScalarizrState.IMPORTING: if cnf.state == ScalarizrState.REBUNDLING: # XXX: temporary workaround # XXX: rackspace injects files and boots OS in a parallell. There were situations when # .user-data file was stale and new server started from rebundled image # toughts that he's an old server and continue rebundling time.sleep(30) locs = ('/etc/.scalr-user-data', cnf.private_path('.user-data')) wait_until( lambda: any(map(lambda x: os.path.exists(x), locs)), timeout=60, error_text= "user-data file not found in the following locations: %s" % locs) # When server bundled by Scalr, often new server are spawned in "importing" state # and its important to query user-data first, to override server-id that was bundled. try: ud_server_id = pl.get_user_data(UserDataOptions.SERVER_ID) except: if cnf.state == ScalarizrState.IMPORTING: ud_server_id = None else: raise if server_id and ud_server_id and server_id != ud_server_id: self._logger.info( 'Server was started after rebundle. Performing some cleanups' ) _cleanup_after_rebundle() cnf.state = ScalarizrState.BOOTSTRAPPING if cnf.state == ScalarizrState.UNKNOWN: cnf.state = ScalarizrState.BOOTSTRAPPING ''' if cnf.state == ScalarizrState.REBUNDLING: server_id = ini.get(config.SECT_GENERAL, config.OPT_SERVER_ID) ud_server_id = pl.get_user_data(UserDataOptions.SERVER_ID) if server_id and ud_server_id and server_id != ud_server_id: logger.info('Server was started after rebundle. Performing some cleanups') _cleanup_after_rebundle() cnf.state = ScalarizrState.BOOTSTRAPPING ''' if linux.os.windows: self._wait_sysprep_oobe() # Initialize local database _init_db() STATE['global.start_after_update'] = int( bool(STATE['global.version'] and STATE['global.version'] != __version__)) STATE['global.version'] = __version__ if STATE['global.start_after_update'] and ScalarizrState.RUNNING: self._logger.info('Scalarizr was updated to %s', __version__) if cnf.state == ScalarizrState.UNKNOWN: cnf.state = ScalarizrState.BOOTSTRAPPING # At first startup platform user-data should be applied if cnf.state == ScalarizrState.BOOTSTRAPPING: cnf.fire('apply_user_data', cnf) self._start_update_client() # Check Scalr version if not bus.scalr_version: version_file = cnf.private_path('.scalr-version') if os.path.exists(version_file): bus.scalr_version = None with open(version_file, 'r') as fp: bus.scalr_version = tuple(fp.read().strip().split('.')) else: bus.scalr_version = _detect_scalr_version() with open(version_file, 'w') as fp: fp.write('.'.join(map(str, bus.scalr_version))) # Apply Command-line passed configuration options if optparser: cnf.update(CmdLineIni.to_ini_sections(optparser.values.cnf)) # Validate configuration num_errors = do_validate_cnf() if num_errors or (optparser and optparser.values.validate_cnf): sys.exit(int(not num_errors or 1)) # Initialize scalarizr services self._init_services() if cnf.state == ScalarizrState.RUNNING: # ReSync user-data cnf.fire('apply_user_data', cnf) try: bus.fire('init') except: self._logger.warn('Caught exception in "init": %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Install signal handlers if not linux.os.windows: signal.signal(signal.SIGCHLD, self.onSIGCHILD) signal.signal(signal.SIGTERM, self.onSIGTERM) signal.signal(signal.SIGHUP, self.onSIGHUP) self._start_services() # Fire start self._running = True try: bus.fire("start") except: self._logger.warn('Caught exception in "start": %s', sys.exc_info()[1], exc_info=sys.exc_info()) try: while self._running: if linux.os.windows_family: rc = win32event.WaitForSingleObject(self.hWaitStop, 30000) if rc == win32event.WAIT_OBJECT_0: # Service stopped, stop main loop break else: if self._snmp_pid != -1: # Recover SNMP maybe self._check_snmp() try: select.select([], [], [], 30) except select.error, e: if e.args[0] == 4: # Interrupted syscall continue raise except KeyboardInterrupt: self._logger.debug('Mainloop: KeyboardInterrupt') finally: self._logger.debug('Mainloop: finally') if self._running and os.getpid() == _pid: self._shutdown() self._logger.debug('Mainloop: leave')
# Write PID write_file(PID_FILE, str(pid)) cnf = bus.cnf cnf.on('apply_user_data', _apply_user_data) if optparser.values.configure: do_configure() sys.exit() elif optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" cnf.state = ScalarizrState.IMPORTING # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) cnf.reconfigure(values=values, silent=True, yesall=True) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform # Check that service started after dirty bundle if ini.has_option(config.SECT_GENERAL, config.OPT_SERVER_ID):
def start(self): self._logger.debug("Initialize scalarizr...") _init() # Starting scalarizr daemon initialization globals()['_pid'] = pid = os.getpid() self._logger.info('[pid: %d] Starting scalarizr %s', pid, __version__) if not 'Windows' == linux.os['family']: # Check for another running scalarzir if os.path.exists(PID_FILE): try: another_pid = None with open(PID_FILE, 'r') as fp: another_pid = int(fp.read().strip()) except ValueError: pass else: if pid != another_pid and os.path.exists('/proc/%s/status' % (another_pid,)): self._logger.error('Cannot start scalarizr: Another process (pid: %s) already running', another_pid) sys.exit(1) # Write PID with open(PID_FILE, 'w') as fp: fp.write(str(pid)) cnf = bus.cnf cnf.on('apply_user_data', _apply_user_data) optparser = bus.optparser if optparser and optparser.values.configure: do_configure() sys.exit() elif optparser and optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" cnf.state = ScalarizrState.IMPORTING # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) cnf.reconfigure(values=values, silent=True, yesall=True) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform # Check that service started after dirty bundle if not optparser.values.import_server and \ ini.has_option(config.SECT_GENERAL, config.OPT_SERVER_ID): # XXX: nimbula's user-data is uploaded by ssh server_id = ini.get(config.SECT_GENERAL, config.OPT_SERVER_ID) if pl.name in ('nimbula', 'rackspace', 'openstack') and cnf.state != ScalarizrState.IMPORTING: if cnf.state == ScalarizrState.REBUNDLING: # XXX: temporary workaround # XXX: rackspace injects files and boots OS in a parallell. There were situations when # .user-data file was stale and new server started from rebundled image # toughts that he's an old server and continue rebundling time.sleep(30) locs = ('/etc/.scalr-user-data', cnf.private_path('.user-data')) wait_until(lambda: any(map(lambda x: os.path.exists(x), locs)), timeout=60, error_text="user-data file not found in the following locations: %s" % locs) # When server bundled by Scalr, often new server are spawned in "importing" state # and its important to query user-data first, to override server-id that was bundled. try: ud_server_id = pl.get_user_data(UserDataOptions.SERVER_ID) except: if cnf.state == ScalarizrState.IMPORTING: ud_server_id = None else: raise if server_id and ud_server_id and server_id != ud_server_id: self._logger.info('Server was started after rebundle. Performing some cleanups') _cleanup_after_rebundle() cnf.state = ScalarizrState.BOOTSTRAPPING if cnf.state == ScalarizrState.UNKNOWN: cnf.state = ScalarizrState.BOOTSTRAPPING ''' if cnf.state == ScalarizrState.REBUNDLING: server_id = ini.get(config.SECT_GENERAL, config.OPT_SERVER_ID) ud_server_id = pl.get_user_data(UserDataOptions.SERVER_ID) if server_id and ud_server_id and server_id != ud_server_id: logger.info('Server was started after rebundle. Performing some cleanups') _cleanup_after_rebundle() cnf.state = ScalarizrState.BOOTSTRAPPING ''' if linux.os.windows: self._wait_sysprep_oobe() # Initialize local database _init_db() STATE['global.start_after_update'] = int(bool(STATE['global.version'] and STATE['global.version'] != __version__)) STATE['global.version'] = __version__ if STATE['global.start_after_update'] and ScalarizrState.RUNNING: self._logger.info('Scalarizr was updated to %s', __version__) if cnf.state == ScalarizrState.UNKNOWN: cnf.state = ScalarizrState.BOOTSTRAPPING # At first startup platform user-data should be applied if cnf.state == ScalarizrState.BOOTSTRAPPING: cnf.fire('apply_user_data', cnf) self._start_update_client() # Check Scalr version if not bus.scalr_version: version_file = cnf.private_path('.scalr-version') if os.path.exists(version_file): bus.scalr_version = None with open(version_file, 'r') as fp: bus.scalr_version = tuple(fp.read().strip().split('.')) else: bus.scalr_version = _detect_scalr_version() with open(version_file, 'w') as fp: fp.write('.'.join(map(str, bus.scalr_version))) # Apply Command-line passed configuration options if optparser: cnf.update(CmdLineIni.to_ini_sections(optparser.values.cnf)) # Validate configuration num_errors = do_validate_cnf() if num_errors or (optparser and optparser.values.validate_cnf): sys.exit(int(not num_errors or 1)) # Initialize scalarizr services self._init_services() if cnf.state == ScalarizrState.RUNNING: # ReSync user-data cnf.fire('apply_user_data', cnf) try: bus.fire('init') except: self._logger.warn('Caught exception in "init": %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Install signal handlers if not linux.os.windows: signal.signal(signal.SIGCHLD, self.onSIGCHILD) signal.signal(signal.SIGTERM, self.onSIGTERM) signal.signal(signal.SIGHUP, self.onSIGHUP) self._start_services() # Fire start self._running = True try: bus.fire("start") except: self._logger.warn('Caught exception in "start": %s', sys.exc_info()[1], exc_info=sys.exc_info()) try: while self._running: if linux.os.windows_family: rc = win32event.WaitForSingleObject(self.hWaitStop, 30000) if rc == win32event.WAIT_OBJECT_0: # Service stopped, stop main loop break else: if self._snmp_pid != -1: # Recover SNMP maybe self._check_snmp() try: select.select([], [], [], 30) except select.error, e: if e.args[0] == 4: # Interrupted syscall continue raise except KeyboardInterrupt: self._logger.debug('Mainloop: KeyboardInterrupt') finally: self._logger.debug('Mainloop: finally') if self._running and os.getpid() == _pid: self._shutdown() self._logger.debug('Mainloop: leave')
def start(self): self._logger.debug("Initialize scalarizr...") _init() _init_environ() # Starting scalarizr daemon initialization globals()['_pid'] = pid = os.getpid() self._logger.info('[pid: %d] Starting scalarizr %s', pid, __version__) __node__['start_time'] = time.time() if not 'Windows' == linux.os['family']: # Check for another running scalarzir if os.path.exists(PID_FILE): try: another_pid = None with open(PID_FILE, 'r') as fp: another_pid = int(fp.read().strip()) except ValueError: pass else: if pid != another_pid and os.path.exists('/proc/%s/status' % (another_pid,)): self._logger.error('Cannot start scalarizr: Another process (pid: %s) already running', another_pid) sys.exit(1) # Write PID with open(PID_FILE, 'w') as fp: fp.write(str(pid)) cnf = bus.cnf optparser = bus.optparser if optparser and optparser.values.configure: do_configure() sys.exit() elif optparser and optparser.values.import_server: print "Starting import process..." print "Don't terminate Scalarizr until Scalr will create the new role" __node__['state'] = 'importing' # Load Command-line configuration options and auto-configure Scalarizr values = CmdLineIni.to_kvals(optparser.values.cnf) if not values.get('server_id'): values['server_id'] = str(uuid.uuid4()) self._logger.info('Configuring Scalarizr. This can take a few minutes...') cnf.reconfigure(values=values, silent=True, yesall=True) try: server_id = __node__['server_id'] except KeyError: server_id = None if optparser and not optparser.values.import_server \ and server_id != _meta.user_data()['serverid']: # This role was bundled with Cloud API call (i.e. CreateImage) # Now we're starting with a new server and should reset it's state self._logger.info(('This image was bundled with cloud API call. ' 'Cleauping ancestor server data')) _cleanup_after_rebundle() __node__['state'] = 'bootstrapping' if __node__['state'] == 'unknown': __node__['state'] = 'bootstrapping' if __node__['state'] == 'bootstrapping': _apply_user_data(from_scalr=False) # Load INI files configuration cnf.bootstrap(force_reload=True) ini = cnf.rawini # Initialize platform module _init_platform() pl = bus.platform if linux.os.windows: self._wait_sysprep_oobe() # Initialize local database _init_db() STATE['global.start_after_update'] = int(bool(STATE['global.version'] and STATE['global.version'] != __version__)) STATE['global.version'] = __version__ if __node__['state'] != 'importing': self._talk_to_updclient() if __node__['state'] == 'importing': try: pkgmgr.updatedb() except: self._logger.warn('Failed to update package manager database: %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Check Scalr version if not bus.scalr_version: version_file = cnf.private_path('.scalr-version') if os.path.exists(version_file): bus.scalr_version = None with open(version_file, 'r') as fp: bus.scalr_version = tuple(fp.read().strip().split('.')) else: bus.scalr_version = _detect_scalr_version() with open(version_file, 'w') as fp: fp.write('.'.join(map(str, bus.scalr_version))) # Apply Command-line passed configuration options if optparser: cnf.update(CmdLineIni.to_ini_sections(optparser.values.cnf)) # Validate configuration num_errors = do_validate_cnf() if num_errors or (optparser and optparser.values.validate_cnf): sys.exit(int(not num_errors or 1)) # Initialize scalarizr services self._init_services() if STATE['global.start_after_update'] and __node__['state'] == 'running': self._logger.info('Scalarizr was updated to %s', __version__) __node__['messaging'].send( 'HostUpdate', body={'scalarizr': {'version': __version__}} ) if __node__['state'] == 'running': # ReSync user-data _apply_user_data(from_scalr=True) try: bus.fire('init') except: self._logger.warn('Caught exception in "init": %s', sys.exc_info()[1], exc_info=sys.exc_info()) # Install signal handlers if not linux.os.windows: signal.signal(signal.SIGTERM, self.onSIGTERM) signal.signal(signal.SIGHUP, self.onSIGHUP) self._start_services() # Fire start self._running = True try: bus.fire("start") except: self._logger.warn('Caught exception in "start": %s', sys.exc_info()[1], exc_info=sys.exc_info()) try: while self._running: if linux.os.windows_family: rc = win32event.WaitForSingleObject(self.hWaitStop, 30000) if rc == win32event.WAIT_OBJECT_0: # Service stopped, stop main loop break else: try: select.select([], [], [], 30) except select.error, e: if e.args[0] == 4: # Interrupted syscall continue raise except KeyboardInterrupt: self._logger.debug('Mainloop: KeyboardInterrupt') finally: self._logger.debug('Mainloop: finally') if self._running and os.getpid() == _pid: self._shutdown() self._logger.debug('Mainloop: leave')