def on_host_init_response(self, message): """ Check postgresql data in host init response @type message: scalarizr.messaging.Message @param message: HostInitResponse """ with bus.initialization_op as op: with op.phase(self._phase_postgresql): with op.step(self._step_accept_scalr_conf): if not message.body.has_key(BEHAVIOUR) or message.db_type != BEHAVIOUR: raise HandlerError("HostInitResponse message for PostgreSQL behaviour must have 'postgresql' property and db_type 'postgresql'") ''' if message.postgresql[OPT_REPLICATION_MASTER] != '1' and \ (not message.body.has_key(OPT_ROOT_SSH_PUBLIC_KEY) or not message.body.has_key(OPT_ROOT_SSH_PRIVATE_KEY)): raise HandlerError("HostInitResponse message for PostgreSQL slave must contain both public and private ssh keys") ''' dir = os.path.dirname(self._volume_config_path) if not os.path.exists(dir): os.makedirs(dir) postgresql_data = message.postgresql.copy() root = PgUser(ROOT_USER, self.pg_keys_dir) root.store_keys(postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY], postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY]) del postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY] del postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY] for key, file in ((OPT_VOLUME_CNF, self._volume_config_path), (OPT_SNAPSHOT_CNF, self._snapshot_config_path)): if os.path.exists(file): os.remove(file) if key in postgresql_data: if postgresql_data[key]: Storage.backup_config(postgresql_data[key], file) del postgresql_data[key] root_user= postgresql_data[OPT_ROOT_USER] or ROOT_USER postgresql_data['%s_password' % root_user] = postgresql_data.get(OPT_ROOT_PASSWORD) or cryptotool.pwgen(10) del postgresql_data[OPT_ROOT_PASSWORD] self._logger.debug("Update postgresql config with %s", postgresql_data) self._update_config(postgresql_data)
def on_host_init_response(self, message): """ Check postgresql data in host init response @type message: scalarizr.messaging.Message @param message: HostInitResponse """ with bus.initialization_op as op: with op.phase(self._phase_postgresql): with op.step(self._step_accept_scalr_conf): if not message.body.has_key(BEHAVIOUR) or message.db_type != BEHAVIOUR: raise HandlerError("HostInitResponse message for PostgreSQL behaviour must have 'postgresql' property and db_type 'postgresql'") postgresql_data = message.postgresql.copy() #Extracting service configuration preset from message if 'preset' in postgresql_data: self.initial_preset = postgresql_data['preset'] LOG.debug('Scalr sent current preset: %s' % self.initial_preset) del postgresql_data['preset'] #Extracting or generating postgresql root password postgresql_data['%s_password' % ROOT_USER] = postgresql_data.get(OPT_ROOT_PASSWORD) or cryptotool.pwgen(10) del postgresql_data[OPT_ROOT_PASSWORD] #Extracting replication ssh keys from message root = PgUser(ROOT_USER, self.postgresql.pg_keys_dir) root.store_keys(postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY], postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY]) del postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY] del postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY] if postgresql_data.get('volume'): # New format postgresql_data['compat_prior_backup_restore'] = False postgresql_data['volume'] = storage2.volume(postgresql_data['volume']) LOG.debug("message.pg['volume']:", postgresql_data['volume']) if 'backup' in postgresql_data: postgresql_data['backup'] = backup.backup(postgresql_data['backup']) LOG.debug("message.pg['backup']:", postgresql_data['backup']) if 'restore' in postgresql_data: postgresql_data['restore'] = backup.restore(postgresql_data['restore']) LOG.debug("message.pg['restore']:", postgresql_data['restore']) else: # Compatibility transformation # - volume_config -> volume # - master n'th start, type=ebs - del snapshot_config # - snapshot_config -> restore # - create backup object on master 1'st start postgresql_data['compat_prior_backup_restore'] = True if postgresql_data.get(OPT_VOLUME_CNF): postgresql_data['volume'] = storage2.volume( postgresql_data.pop(OPT_VOLUME_CNF)) elif postgresql_data.get(OPT_SNAPSHOT_CNF): postgresql_data['volume'] = storage2.volume( type=postgresql_data[OPT_SNAPSHOT_CNF]['type']) else: raise HandlerError('No volume config or snapshot config provided') if postgresql_data['volume'].device and \ postgresql_data['volume'].type in ('ebs', 'csvol', 'cinder', 'raid'): LOG.debug("Master n'th start detected. Removing snapshot config from message") postgresql_data.pop(OPT_SNAPSHOT_CNF, None) if postgresql_data.get(OPT_SNAPSHOT_CNF): postgresql_data['restore'] = backup.restore( type='snap_postgresql', snapshot=postgresql_data.pop(OPT_SNAPSHOT_CNF), volume=postgresql_data['volume']) if int(postgresql_data['replication_master']): postgresql_data['backup'] = backup.backup( type='snap_postgresql', volume=postgresql_data['volume']) LOG.debug("Update postgresql config with %s", postgresql_data) __postgresql__.update(postgresql_data) __postgresql__['volume'].mpoint = __postgresql__['storage_dir'] __postgresql__['volume'].tags = self.resource_tags() if 'backup' in __postgresql__: __postgresql__['backup'].tags = self.resource_tags()
def on_host_init_response(self, message): """ Check postgresql data in host init response @type message: scalarizr.messaging.Message @param message: HostInitResponse """ log = bus.init_op.logger log.info('Accept Scalr configuration') if not message.body.has_key(BEHAVIOUR) or message.db_type != BEHAVIOUR: raise HandlerError( "HostInitResponse message for PostgreSQL behaviour must have 'postgresql' property and db_type 'postgresql'" ) postgresql_data = message.postgresql.copy() #Extracting service configuration preset from message if 'preset' in postgresql_data: self.initial_preset = postgresql_data['preset'] LOG.debug('Scalr sent current preset: %s' % self.initial_preset) del postgresql_data['preset'] #Extracting or generating postgresql root password postgresql_data['%s_password' % ROOT_USER] = postgresql_data.get( OPT_ROOT_PASSWORD) or cryptotool.pwgen(10) del postgresql_data[OPT_ROOT_PASSWORD] #Extracting replication ssh keys from message root = PgUser(ROOT_USER, self.postgresql.pg_keys_dir) root.store_keys(postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY], postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY]) del postgresql_data[OPT_ROOT_SSH_PUBLIC_KEY] del postgresql_data[OPT_ROOT_SSH_PRIVATE_KEY] if postgresql_data.get('volume'): # New format postgresql_data['compat_prior_backup_restore'] = False postgresql_data['volume'] = storage2.volume( postgresql_data['volume']) LOG.debug("message.pg['volume']: %s", postgresql_data['volume']) if 'backup' in postgresql_data: postgresql_data['backup'] = backup.backup( postgresql_data['backup']) LOG.debug("message.pg['backup']: %s", postgresql_data['backup']) if 'restore' in postgresql_data: postgresql_data['restore'] = backup.restore( postgresql_data['restore']) LOG.debug("message.pg['restore']: %s", postgresql_data['restore']) else: # Compatibility transformation # - volume_config -> volume # - master n'th start, type=ebs - del snapshot_config # - snapshot_config -> restore # - create backup object on master 1'st start postgresql_data['compat_prior_backup_restore'] = True if postgresql_data.get(OPT_VOLUME_CNF): postgresql_data['volume'] = storage2.volume( postgresql_data.pop(OPT_VOLUME_CNF)) elif postgresql_data.get(OPT_SNAPSHOT_CNF): postgresql_data['volume'] = storage2.volume( type=postgresql_data[OPT_SNAPSHOT_CNF]['type']) else: raise HandlerError( 'No volume config or snapshot config provided') if postgresql_data['volume'].device and \ postgresql_data['volume'].type in ('ebs', 'csvol', 'cinder', 'raid', 'gce_persistent'): LOG.debug( "Master n'th start detected. Removing snapshot config from message" ) postgresql_data.pop(OPT_SNAPSHOT_CNF, None) if postgresql_data.get(OPT_SNAPSHOT_CNF): postgresql_data['restore'] = backup.restore( type='snap_postgresql', snapshot=postgresql_data.pop(OPT_SNAPSHOT_CNF), volume=postgresql_data['volume']) if int(postgresql_data['replication_master']): postgresql_data['backup'] = backup.backup( type='snap_postgresql', volume=postgresql_data['volume']) self._hir_volume_growth = postgresql_data.pop('volume_growth', None) LOG.debug("Update postgresql config with %s", postgresql_data) __postgresql__.update(postgresql_data) __postgresql__['volume'].mpoint = __postgresql__['storage_dir']