def _init_master(self, message): """ Initialize postgresql master @type message: scalarizr.messaging.Message @param message: HostUp message """ LOG.info("Initializing PostgreSQL master") with bus.initialization_op as op: with op.step(self._step_create_storage): # Plug storage if 'restore' in __postgresql__ and\ __postgresql__['restore'].type == 'snap_postgresql': __postgresql__['restore'].run() else: if __node__['platform'] == 'idcf': if __postgresql__['volume'].id: LOG.info('Cloning volume to workaround reattachment limitations of IDCF') __postgresql__['volume'].snap = __postgresql__['volume'].snapshot() __postgresql__['volume'].ensure(mount=True, mkfs=True) LOG.debug('Postgres volume config after ensure: %s', dict(__postgresql__['volume'])) with op.step(self._step_init_master): self.postgresql.init_master(mpoint=STORAGE_PATH, password=self.root_password) with op.step(self._step_create_data_bundle): if 'backup' in __postgresql__: __postgresql__['restore'] = __postgresql__['backup'].run() with op.step(self._step_collect_host_up_data): # Update HostUp message msg_data = dict({OPT_REPLICATION_MASTER: str(int(self.is_replication_master)), OPT_ROOT_USER: self.postgresql.root_user.name, OPT_ROOT_PASSWORD: self.root_password, OPT_ROOT_SSH_PRIVATE_KEY: self.postgresql.root_user.private_key, OPT_ROOT_SSH_PUBLIC_KEY: self.postgresql.root_user.public_key, OPT_CURRENT_XLOG_LOCATION: None}) if __postgresql__['compat_prior_backup_restore']: if 'restore' in __postgresql__: msg_data.update(dict( snapshot_config=dict(__postgresql__['restore'].snapshot))) msg_data.update(dict( volume_config=dict(__postgresql__['volume']))) else: msg_data.update(dict( volume=dict(__postgresql__['volume']) )) for key in ('backup', 'restore'): if key in __postgresql__: msg_data[key] = dict(__postgresql__[key]) message.db_type = BEHAVIOUR message.postgresql = msg_data.copy() try: del msg_data[OPT_SNAPSHOT_CNF], msg_data[OPT_VOLUME_CNF] except KeyError: pass __postgresql__.update(msg_data)
STORAGE_VOLUME_CNF = 'postgresql.json' STORAGE_SNAPSHOT_CNF = 'postgresql-snap.json' OPT_VOLUME_CNF = 'volume_config' OPT_SNAPSHOT_CNF = 'snapshot_config' OPT_ROOT_USER = '******' OPT_ROOT_PASSWORD = "******" OPT_ROOT_SSH_PUBLIC_KEY = "root_ssh_public_key" OPT_ROOT_SSH_PRIVATE_KEY = "root_ssh_private_key" OPT_CURRENT_XLOG_LOCATION = 'current_xlog_location' OPT_REPLICATION_MASTER = postgresql_svc.OPT_REPLICATION_MASTER __postgresql__.update({ 'port': 5432, 'storage_dir': '/mnt/pgstorage', 'root_user': '******', 'pgdump_chunk_size': 200 * 1024 * 1024, }) def get_handlers(): return PostgreSqlHander(), SSH_KEYGEN_SELINUX_MODULE = """ module local 1.0; require { type initrc_tmp_t; type ssh_keygen_t; type initrc_t;
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 _init_master(self, message): """ Initialize postgresql master @type message: scalarizr.messaging.Message @param message: HostUp message """ log = bus.init_op.logger log.info("Initializing PostgreSQL master") log.info('Create storage') # Plug storage if 'restore' in __postgresql__ and\ __postgresql__['restore'].type == 'snap_postgresql': __postgresql__['restore'].run() else: if __node__['platform'].name == 'idcf': if __postgresql__['volume'].id: LOG.info( 'Cloning volume to workaround reattachment limitations of IDCF' ) __postgresql__['volume'].snap = __postgresql__[ 'volume'].snapshot() if self._hir_volume_growth: #Growing maser storage if HIR message contained "growth" data LOG.info( "Attempting to grow data volume according to new data: %s" % str(self._hir_volume_growth)) grown_volume = __postgresql__['volume'].grow( **self._hir_volume_growth) grown_volume.mount() __postgresql__['volume'] = grown_volume else: __postgresql__['volume'].ensure(mount=True, mkfs=True) LOG.debug('Postgres volume config after ensure: %s', dict(__postgresql__['volume'])) log.info('Initialize Master') self.postgresql.init_master(mpoint=STORAGE_PATH, password=self.root_password) if self.postgresql.first_start and 'backup' in __postgresql__: log.info('Create data bundle') __postgresql__['restore'] = __postgresql__['backup'].run() else: log.debug('No need to create data bundle') log.info('Collect HostUp data') # Update HostUp message msg_data = dict({ OPT_REPLICATION_MASTER: str(int(self.is_replication_master)), OPT_ROOT_USER: self.postgresql.root_user.name, OPT_ROOT_PASSWORD: self.root_password, OPT_ROOT_SSH_PRIVATE_KEY: self.postgresql.root_user.private_key, OPT_ROOT_SSH_PUBLIC_KEY: self.postgresql.root_user.public_key, OPT_CURRENT_XLOG_LOCATION: None }) if self._hir_volume_growth: msg_data['volume_template'] = dict( __postgresql__['volume'].clone()) if __postgresql__['compat_prior_backup_restore']: if 'restore' in __postgresql__: msg_data.update( dict(snapshot_config=dict( __postgresql__['restore'].snapshot))) msg_data.update(dict(volume_config=dict(__postgresql__['volume']))) else: msg_data.update(dict(volume=dict(__postgresql__['volume']))) for key in ('backup', 'restore'): if key in __postgresql__: msg_data[key] = dict(__postgresql__[key]) message.db_type = BEHAVIOUR message.postgresql = msg_data.copy() try: del msg_data[OPT_SNAPSHOT_CNF], msg_data[OPT_VOLUME_CNF] except KeyError: pass __postgresql__.update(msg_data)
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']
STORAGE_PATH = postgresql_api.STORAGE_PATH STORAGE_VOLUME_CNF = 'postgresql.json' STORAGE_SNAPSHOT_CNF = 'postgresql-snap.json' OPT_VOLUME_CNF = 'volume_config' OPT_SNAPSHOT_CNF = postgresql_api.OPT_SNAPSHOT_CNF OPT_ROOT_USER = '******' OPT_ROOT_PASSWORD = "******" OPT_ROOT_SSH_PUBLIC_KEY = "root_ssh_public_key" OPT_ROOT_SSH_PRIVATE_KEY = "root_ssh_private_key" OPT_CURRENT_XLOG_LOCATION = 'current_xlog_location' OPT_REPLICATION_MASTER = postgresql_svc.OPT_REPLICATION_MASTER __postgresql__.update({ 'port': 5432, 'storage_dir': '/mnt/pgstorage', 'root_user': '******', 'pgdump_chunk_size': 200 * 1024 * 1024, }) def get_handlers(): return [PostgreSqlHander()] SSH_KEYGEN_SELINUX_MODULE = """ module local 1.0; require { type initrc_tmp_t; type ssh_keygen_t; type initrc_t;
def _init_master(self, message): """ Initialize postgresql master @type message: scalarizr.messaging.Message @param message: HostUp message """ LOG.info("Initializing PostgreSQL master") with bus.initialization_op as op: with op.step(self._step_create_storage): # Plug storage if 'restore' in __postgresql__ and\ __postgresql__['restore'].type == 'snap_postgresql': __postgresql__['restore'].run() else: if __node__['platform'] == 'idcf': if __postgresql__['volume'].id: LOG.info( 'Cloning volume to workaround reattachment limitations of IDCF' ) __postgresql__['volume'].snap = __postgresql__[ 'volume'].snapshot() __postgresql__['volume'].ensure(mount=True, mkfs=True) LOG.debug('Postgres volume config after ensure: %s', dict(__postgresql__['volume'])) with op.step(self._step_init_master): self.postgresql.init_master(mpoint=STORAGE_PATH, password=self.root_password) with op.step(self._step_create_data_bundle): if 'backup' in __postgresql__: __postgresql__['restore'] = __postgresql__['backup'].run() with op.step(self._step_collect_host_up_data): # Update HostUp message msg_data = dict({ OPT_REPLICATION_MASTER: str(int(self.is_replication_master)), OPT_ROOT_USER: self.postgresql.root_user.name, OPT_ROOT_PASSWORD: self.root_password, OPT_ROOT_SSH_PRIVATE_KEY: self.postgresql.root_user.private_key, OPT_ROOT_SSH_PUBLIC_KEY: self.postgresql.root_user.public_key, OPT_CURRENT_XLOG_LOCATION: None }) if __postgresql__['compat_prior_backup_restore']: if 'restore' in __postgresql__: msg_data.update( dict(snapshot_config=dict( __postgresql__['restore'].snapshot))) msg_data.update( dict(volume_config=dict(__postgresql__['volume']))) else: msg_data.update( dict(volume=dict(__postgresql__['volume']))) for key in ('backup', 'restore'): if key in __postgresql__: msg_data[key] = dict(__postgresql__[key]) message.db_type = BEHAVIOUR message.postgresql = msg_data.copy() try: del msg_data[OPT_SNAPSHOT_CNF], msg_data[OPT_VOLUME_CNF] except KeyError: pass __postgresql__.update(msg_data)
def _init_master(self, message): """ Initialize postgresql master @type message: scalarizr.messaging.Message @param message: HostUp message """ log = bus.init_op.logger log.info("Initializing PostgreSQL master") log.info('Create storage') # Plug storage if 'restore' in __postgresql__ and\ __postgresql__['restore'].type == 'snap_postgresql': __postgresql__['restore'].run() else: if __node__['platform'].name == 'idcf': if __postgresql__['volume'].id: LOG.info('Cloning volume to workaround reattachment limitations of IDCF') __postgresql__['volume'].snap = __postgresql__['volume'].snapshot() if self._hir_volume_growth: #Growing maser storage if HIR message contained "growth" data LOG.info("Attempting to grow data volume according to new data: %s" % str(self._hir_volume_growth)) grown_volume = __postgresql__['volume'].grow(**self._hir_volume_growth) grown_volume.mount() __postgresql__['volume'] = grown_volume else: __postgresql__['volume'].ensure(mount=True, mkfs=True) LOG.debug('Postgres volume config after ensure: %s', dict(__postgresql__['volume'])) log.info('Initialize Master') self.postgresql.init_master(mpoint=STORAGE_PATH, password=self.root_password) log.info('Create data bundle') if 'backup' in __postgresql__: __postgresql__['restore'] = __postgresql__['backup'].run() log.info('Collect HostUp data') # Update HostUp message msg_data = dict({OPT_REPLICATION_MASTER: str(int(self.is_replication_master)), OPT_ROOT_USER: self.postgresql.root_user.name, OPT_ROOT_PASSWORD: self.root_password, OPT_ROOT_SSH_PRIVATE_KEY: self.postgresql.root_user.private_key, OPT_ROOT_SSH_PUBLIC_KEY: self.postgresql.root_user.public_key, OPT_CURRENT_XLOG_LOCATION: None}) if self._hir_volume_growth: msg_data['volume_template'] = dict(__postgresql__['volume'].clone()) if __postgresql__['compat_prior_backup_restore']: if 'restore' in __postgresql__: msg_data.update(dict( snapshot_config=dict(__postgresql__['restore'].snapshot))) msg_data.update(dict( volume_config=dict(__postgresql__['volume']))) else: msg_data.update(dict( volume=dict(__postgresql__['volume']) )) for key in ('backup', 'restore'): if key in __postgresql__: msg_data[key] = dict(__postgresql__[key]) message.db_type = BEHAVIOUR message.postgresql = msg_data.copy() try: del msg_data[OPT_SNAPSHOT_CNF], msg_data[OPT_VOLUME_CNF] except KeyError: pass __postgresql__.update(msg_data)