def on_before_host_up(self, message): LOG.debug("on_before_host_up") """ Configure MySQL __mysql__['behavior'] @type message: scalarizr.messaging.Message @param message: HostUp message """ self.generate_datadir() self.mysql.service.stop('Configuring MySQL') # On Debian/GCE we've got 'Another MySQL daemon already running with the same unix socket.' socket_file = mysql2_svc.my_print_defaults('mysqld').get('socket') if socket_file: coreutils.remove(socket_file) if 'Amazon' == linux.os['name']: self.mysql.my_cnf.pid_file = os.path.join(__mysql__['data_dir'], 'mysqld.pid') repl = 'master' if int(__mysql__['replication_master']) else 'slave' bus.fire('before_mysql_configure', replication=repl) if repl == 'master': self._init_master(message) else: self._init_slave(message) # Force to resave volume settings __mysql__['volume'] = storage2.volume(__mysql__['volume']) bus.fire('service_configured', service_name=__mysql__['behavior'], replication=repl, preset=self.initial_preset)
def _fix_percona_debian_cnf(self): if __mysql__['behavior'] == 'percona' and \ os.path.exists(__mysql__['debian.cnf']): LOG.info('Fixing socket options in %s', __mysql__['debian.cnf']) debian_cnf = metaconf.Configuration('mysql') debian_cnf.read(__mysql__['debian.cnf']) sock = mysql2_svc.my_print_defaults('mysqld')['socket'] debian_cnf.set('client/socket', sock) debian_cnf.set('mysql_upgrade/socket', sock) debian_cnf.write(__mysql__['debian.cnf'])
def generate_datadir(self): try: datadir = mysql2_svc.my_print_defaults('mysqld').get('datadir') if datadir and \ os.path.isdir(datadir) and \ not os.path.isdir(os.path.join(datadir, 'mysql')): self.mysql.service.start() self.mysql.service.stop('Autogenerating datadir') except: #TODO: better error handling pass
def _init_master(self, message): """ Initialize MySQL master @type message: scalarizr.messaging.Message @param message: HostUp message """ LOG.info("Initializing MySQL master") with bus.initialization_op as op: with op.step(self._step_create_storage): if 'restore' in __mysql__ and \ __mysql__['restore'].type == 'snap_mysql': __mysql__['restore'].run() else: __mysql__['volume'].ensure(mount=True, mkfs=True) LOG.debug('MySQL volume config after ensure: %s', dict(__mysql__['volume'])) self.mysql.flush_logs(__mysql__['data_dir']) with op.step(self._step_move_datadir): storage_valid = self._storage_valid() user_creds = self.get_user_creds() datadir = mysql2_svc.my_print_defaults('mysqld').get('datadir', '/var/lib/mysql') self.mysql.my_cnf.datadir = datadir if not storage_valid and datadir.find(__mysql__['data_dir']) == 0: # When role was created from another mysql role it contains modified my.cnf settings self.mysql.my_cnf.datadir = '/var/lib/mysql' self.mysql.my_cnf.log_bin = None # Patch configuration self.mysql.my_cnf.expire_logs_days = 10 self.mysql.my_cnf.skip_locking = False self.mysql.move_mysqldir_to(__mysql__['storage_dir']) self._change_selinux_ctx() with op.step(self._step_patch_conf): # Init replication self.mysql._init_replication(master=True) if 'restore' in __mysql__ and \ __mysql__['restore'].type == 'xtrabackup': __mysql__['restore'].run() if __mysql__['restore'].features['master_binlog_reset']: self.mysql.service.start() self.mysql.service.stop() log_file, log_pos = mysql2_svc.mysqlbinlog_head() __mysql__['restore'].log_file = log_file __mysql__['restore'].log_pos = log_pos # If It's 1st init of mysql master storage if not storage_valid: with op.step(self._step_create_users): if os.path.exists(__mysql__['debian.cnf']): LOG.debug("Copying debian.cnf file to mysql storage") shutil.copy(__mysql__['debian.cnf'], __mysql__['storage_dir']) # Add system users self.create_users(**user_creds) # If volume has mysql storage directory structure (N-th init) else: with op.step(self._step_innodb_recovery): self._copy_debian_cnf_back() if 'restore' in __mysql__ \ and __mysql__['restore'].type != 'xtrabackup': self._innodb_recovery() self.mysql.service.start() with op.step(self._step_create_data_bundle): if 'backup' in __mysql__: __mysql__['restore'] = __mysql__['backup'].run() with op.step(self._step_collect_hostup_data): # Update HostUp message md = dict( replication_master=__mysql__['replication_master'], root_password=__mysql__['root_password'], repl_password=__mysql__['repl_password'], stat_password=__mysql__['stat_password'], ) if __mysql__['compat_prior_backup_restore']: if 'restore' in __mysql__: md.update(dict( log_file=__mysql__['restore'].log_file, log_pos=__mysql__['restore'].log_pos, snapshot_config=dict(__mysql__['restore'].snapshot))) elif 'log_file' in __mysql__: md.update(dict( log_file=__mysql__['log_file'], log_pos=__mysql__['log_pos'])) md.update(dict( volume_config=dict(__mysql__['volume']))) else: md.update(dict( volume=dict(__mysql__['volume']) )) for key in ('backup', 'restore'): if key in __mysql__: md[key] = dict(__mysql__[key]) message.db_type = __mysql__['behavior'] setattr(message, __mysql__['behavior'], md)