Пример #1
0
def mount (device, mpoint = '/mnt', options=None, make_fs=False, fstype='ext3', auto_mount=False):
	if not os.path.exists(mpoint):
		os.makedirs(mpoint)
	
	options = options or ('-t', 'auto')
	
	if make_fs:
		mkfs(device,fstype)
	
	out, code = system2(['mount'] + list(options) + [device, mpoint], stderr=subprocess.STDOUT, raise_error=False)[0::2]
	if code:
		if out.find("you must specify the filesystem type") != -1:
			raise FstoolError("No filesystem found on device '%s'" % (device), FstoolError.NO_FS)
		raise FstoolError(out)
	
	if " ".join(options).find("loop") == -1:
		mtab = Mtab()		
		if not mtab.contains(device):
			raise FstoolError("Cannot mount device '%s'. %s" % (device, out), FstoolError.CANNOT_MOUNT)
	
	if auto_mount:
		fstab = Fstab()
		if not fstab.contains(device, mpoint=mpoint, reload=True):
			opts = "defaults"
			if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
				opts += ',comment=cloudconfig,nobootwait'
			fstab.append(device, mpoint, options=opts)
Пример #2
0
 def __init__(self):
     initd_script = None
     if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
         initd_script = ("/usr/sbin/service", "mongodb")
     else:
         initd_script = firstmatched(os.path.exists, ("/etc/init.d/mongodb", "/etc/init.d/mongod"))
     initdv2.ParametrizedInitScript.__init__(self, name=SERVICE_NAME, initd_script=initd_script)
Пример #3
0
    def _init_service(self, mpoint, password):
        self.service.stop()

        self.root_user = self.create_linux_user(ROOT_USER, password)
        self.master_user = self.create_linux_user(MASTER_USER, password)

        move_files = not self.cluster_dir.is_initialized(mpoint)
        self.postgresql_conf.data_directory = self.cluster_dir.move_to(
            mpoint, move_files)
        self.postgresql_conf.listen_addresses = '*'
        self.postgresql_conf.wal_level = 'hot_standby'
        self.postgresql_conf.max_wal_senders = 5

        wks = self.postgresql_conf.wal_keep_segments
        if not wks or int(wks) < 32:
            self.postgresql_conf.wal_keep_segments = 32  # [TTM-8]

        if disttool.is_ubuntu() and disttool.version_info() == (
                12, 4) and '9.1' == self.version:
            #SEE: https://bugs.launchpad.net/ubuntu/+source/postgresql-9.1/+bug/1018307
            self.postgresql_conf.ssl_renegotiation_limit = 0

        self.cluster_dir.clean()

        if disttool.is_redhat_based():
            self.config_dir.move_to(self.unified_etc_path)
            make_symlinks(os.path.join(mpoint, STORAGE_DATA_DIR),
                          self.unified_etc_path)
            self.postgresql_conf = PostgresqlConf.find(self.config_dir)
            self.pg_hba_conf = PgHbaConf.find(self.config_dir)

        self.pg_hba_conf.allow_local_connections()
Пример #4
0
def mount_ex(device, 
             mpoint='/mnt',
             options=None,
             make_fs=False,
             fstype='ext3',
             auto_mount=False):
    if not os.path.exists(mpoint):
        os.makedirs(mpoint)
    
    options = options or ('-t', 'auto')
    
    if make_fs:
        from scalarizr import storage2
        storage2.filesystem(fstype).mkfs(device)
    
    out = mount(device, mpoint, *options)[0]
    
    if " ".join(options).find("loop") == -1:
        mtab = mounts()     
        if not mtab.contains(device):
            raise MountError("Cannot mount device '%s'. %s" % (device, out),
                             MountError.CANNOT_MOUNT)
    
    if auto_mount:
        _fstab = fstab()
        if not _fstab.contains(device, mpoint=mpoint, reload=True):
            opts = "defaults"
            if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
                opts += ',comment=cloudconfig,nobootwait'
            _fstab.append(device, mpoint, options=opts)
Пример #5
0
    def __init__(self):
        if 'gce' == node.__node__['platform']:
            self.ensure_pid_directory()

        self.mysql_cli = MySQLClient()


        if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
            initd_script = ('/usr/sbin/service', 'mysql')
        else:
            initd_script = firstmatched(os.path.exists, ('/etc/init.d/mysqld', '/etc/init.d/mysql'))

        pid_file = None
        try:
            out = system2("my_print_defaults mysqld", shell=True, silent=True)
            m = re.search("--pid[-_]file=(.*)", out[0], re.MULTILINE)
            if m:
                pid_file = m.group(1)
            m = re.search("--socket=(.*)", out[0], re.MULTILINE)
            if m:
                self.socket_file = m.group(1)
        except:
            pass

        initdv2.ParametrizedInitScript.__init__(self, SERVICE_NAME,
                        initd_script, pid_file, socks=[initdv2.SockParam(MYSQL_DEFAULT_PORT, timeout=3600)])
Пример #6
0
    def on_init(self, *args, **kwargs):
        bus.on("before_hello", self.on_before_hello)
        bus.on("before_host_init", self.on_before_host_init)
        bus.on("before_restart", self.on_before_restart)
        bus.on("before_reboot_finish", self.on_before_reboot_finish)

        try:
            system(('ntpdate', '-u', '0.amazon.pool.ntp.org'))
        except:
            pass

        msg_service = bus.messaging_service
        producer = msg_service.get_producer()
        producer.on("before_send", self.on_before_message_send)

        if not os_dist.windows_family and not __node__.get('hostname'):
            # Set the hostname to this instance's public hostname
            try:
                hostname_as_pubdns = int(__ec2__['hostname_as_pubdns'])
            except:
                hostname_as_pubdns = True

            if hostname_as_pubdns:
                pub_hostname = self._platform.get_public_hostname()
                self._logger.debug('Setting hostname to %s' % pub_hostname)
                system2("hostname " + pub_hostname, shell=True)

        if disttool.is_ubuntu():
            # Ubuntu cloud-init scripts may disable root ssh login
            for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'):
                if os.path.exists(path):
                    c = None
                    with open(path, 'r') as fp:
                        c = fp.read()
                    c = re.sub(re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c)
                    with open(path, 'w') as fp:
                        fp.write(c)

        if not linux.os.windows_family:
            # Add server ssh public key to authorized_keys
            ssh_key = self._platform.get_ssh_pub_key()
            if ssh_key:
                add_authorized_key(ssh_key)

        # Mount ephemeral devices
        # Seen on eucalyptus:
        #       - fstab contains invalid fstype and `mount -a` fails
        if self._platform.name == 'eucalyptus':
            mtab = mount.mounts()
            fstab = mount.fstab()
            for device in self._platform.instance_store_devices:
                if os.path.exists(device) and device in fstab and device not in mtab:
                    entry = fstab[device]
                    try:
                        mount.mount(device, entry.mpoint, '-o', entry.options)
                    except:
                        self._logger.warn(sys.exc_info()[1])
        else:
            if not os_dist.windows_family:
                system2('mount -a', shell=True, raise_exc=False)
Пример #7
0
    def _init_service(self, mpoint, password):
        self.service.stop()

        self.root_user = self.create_linux_user(ROOT_USER, password)
        self.master_user = self.create_linux_user(MASTER_USER, password)

        move_files = not self.cluster_dir.is_initialized(mpoint)
        self.postgresql_conf.data_directory = self.cluster_dir.move_to(mpoint, move_files)
        self.postgresql_conf.listen_addresses = "*"
        self.postgresql_conf.wal_level = "hot_standby"
        self.postgresql_conf.max_wal_senders = 5

        wks = self.postgresql_conf.wal_keep_segments
        if not wks or int(wks) < 32:
            self.postgresql_conf.wal_keep_segments = 32  # [TTM-8]

        if disttool.is_ubuntu() and disttool.version_info() == (12, 4) and "9.1" == self.version:
            # SEE: https://bugs.launchpad.net/ubuntu/+source/postgresql-9.1/+bug/1018307
            self.postgresql_conf.ssl_renegotiation_limit = 0

        self.cluster_dir.clean()

        if disttool.is_redhat_based():
            self.config_dir.move_to(self.unified_etc_path)
            make_symlinks(os.path.join(mpoint, STORAGE_DATA_DIR), self.unified_etc_path)
            self.postgresql_conf = PostgresqlConf.find(self.config_dir)
            self.pg_hba_conf = PgHbaConf.find(self.config_dir)

        self.pg_hba_conf.allow_local_connections()
Пример #8
0
    def __init__(self):
        if 'gce' == node.__node__['platform'].name:
            self.ensure_pid_directory()

        self.mysql_cli = MySQLClient()

        if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
            initd_script = ('/usr/sbin/service', 'mysql')
        else:
            initd_script = firstmatched(
                os.path.exists, ('/etc/init.d/mysqld', '/etc/init.d/mysql'))

        pid_file = None
        try:
            out = system2("my_print_defaults mysqld", shell=True, silent=True)
            m = re.search("--pid[-_]file=(.*)", out[0], re.MULTILINE)
            if m:
                pid_file = m.group(1)
            m = re.search("--socket=(.*)", out[0], re.MULTILINE)
            if m:
                self.socket_file = m.group(1)
        except:
            pass

        initdv2.ParametrizedInitScript.__init__(
            self,
            SERVICE_NAME,
            initd_script,
            pid_file,
            socks=[initdv2.SockParam(MYSQL_DEFAULT_PORT, timeout=3600)])
Пример #9
0
 def __init__(self):
     initd_script = None
     if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
         initd_script = ('/usr/sbin/service', 'redis-server')
     else:
         initd_script = firstmatched(os.path.exists, ('/etc/init.d/redis', '/etc/init.d/redis-server'))
     initdv2.ParametrizedInitScript.__init__(self, name=SERVICE_NAME,
             initd_script=initd_script)
Пример #10
0
 def __init__(self):
     initd_script = None
     if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
         initd_script = ('/usr/sbin/service', 'redis-server')
     else:
         initd_script = firstmatched(os.path.exists, ('/etc/init.d/redis', '/etc/init.d/redis-server'))
     initdv2.ParametrizedInitScript.__init__(self, name=SERVICE_NAME,
             initd_script=initd_script)
Пример #11
0
def is_running(name):
	if not _services.has_key(name):
		raise InitdError("Unknown service '%s'" % (name,))
	cmd = [_services[name]["initd_script"], "status"]
	out, err = system2(cmd)[0:2]
	out += err
	if name == "mysql" and disttool.is_ubuntu() and disttool.linux_dist()[1] == '8.04':
		return out.lower().find("Uptime:") != -1
	else:
		return out.lower().find("running") != -1 or out.lower().find("[ ok ]") != -1 or out.lower().find("done.") != -1
Пример #12
0
	def on_init(self, *args, **kwargs):
		bus.on("before_hello", self.on_before_hello)		
		bus.on("before_host_init", self.on_before_host_init)
		bus.on("before_restart", self.on_before_restart)

		msg_service = bus.messaging_service
		producer = msg_service.get_producer()
		producer.on("before_send", self.on_before_message_send)
		
		# Set the hostname to this instance's public hostname
		cnf = bus.cnf
		try:
			hostname_as_pubdns = int(cnf.rawini.get('ec2', 'hostname_as_pubdns'))
		except ConfigParser.Error:
			hostname_as_pubdns = True
		if hostname_as_pubdns:
			system2("hostname " + self._platform.get_public_hostname(), shell=True)		
		
		if disttool.is_ubuntu():
			# Ubuntu cloud-init scripts may disable root ssh login
			for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'):
				if os.path.exists(path):
					c = filetool.read_file(path)
					c = re.sub(re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c)
					filetool.write_file(path, c)
			
		# Add server ssh public key to authorized_keys
		authorized_keys_path = "/root/.ssh/authorized_keys"
		if os.path.exists(authorized_keys_path):
			c = filetool.read_file(authorized_keys_path)
			ssh_key = self._platform.get_ssh_pub_key()
			idx = c.find(ssh_key)
			if idx == -1:
				if c and c[-1] != '\n':
					c += '\n'
				c += ssh_key + "\n"
				self._logger.debug("Add server ssh public key to authorized_keys")
				filetool.write_file(authorized_keys_path, c)
			elif idx > 0 and c[idx-1] != '\n':
				c = c[0:idx] + '\n' + c[idx:]
				self._logger.warn('Adding new-line character before server SSH key in authorized_keys file')
				filetool.write_file(authorized_keys_path, c)
				
		# Mount ephemeral devices
		# Seen on eucalyptus: 
		# 	- fstab contains invalid fstype and `mount -a` fails  
		mtab = Mtab()
		fstab = Fstab()
		for device in self._platform.instance_store_devices:
			if os.path.exists(device) and fstab.contains(device) and not mtab.contains(device):
				entry = fstab.find(device)[0]
				try:
					mount(device, entry.mpoint, ('-o', entry.options))
				except:
					self._logger.warn(sys.exc_info()[1])
Пример #13
0
def is_running(name):
    if not _services.has_key(name):
        raise InitdError("Unknown service '%s'" % (name, ))
    cmd = [_services[name]["initd_script"], "status"]
    out, err = system2(cmd)[0:2]
    out += err
    if name == "mysql" and disttool.is_ubuntu() and disttool.linux_dist(
    )[1] == '8.04':
        return out.lower().find("Uptime:") != -1
    else:
        return out.lower().find("running") != -1 or out.lower().find(
            "[ ok ]") != -1 or out.lower().find("done.") != -1
Пример #14
0
    def __init__(self):
        self._logger = logging.getLogger(__name__)

        if disttool.is_redhat_based():
            init_script = ('/sbin/service', 'sshd')
        elif disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
            init_script = ('/usr/sbin/service', 'ssh')
        else:
            init_script = firstmatched(os.path.exists, ('/etc/init.d/ssh', '/etc/init.d/sshd'))
        self._sshd_init = ParametrizedInitScript('sshd', init_script)

        bus.on(init=self.on_init)
Пример #15
0
    def disable_requiretty(self):
        """
		requiretty      If set, sudo will only run when the user is logged in to a real tty.  
		When this flag is set, sudo can only be  run from a login session and not via other means 
		such  as cron(8) or cgi-bin scripts.  This flag is off by default on all systems but CentOS5.
		"""
        path = "/etc/sudoers"
        self._logger.debug("Disabling requiretty in %s" % path)
        if not disttool.is_ubuntu():
            orig = read_file(path)
            new = re.sub("Defaults\s+requiretty", "\n", orig)
            if new != orig:
                write_file(path, new)
Пример #16
0
class ClusterDir(object):
    
    base_path = glob.glob('/var/lib/p*sql/9.*/')[0]
    default_path = os.path.join(base_path, 'main' if disttool.is_ubuntu() else 'data')
    
    def __init__(self, path=None):
        self.path = path
        self.user = DEFAULT_USER

    @classmethod
    def find(cls, postgresql_conf):
        return cls(postgresql_conf.data_directory or cls.default_path)

    def move_to(self, dst, move_files=True):
        new_cluster_dir = os.path.join(dst, STORAGE_DATA_DIR)
        
        if not os.path.exists(dst):
            LOG.debug('Creating directory structure for postgresql cluster: %s' % dst)
            os.makedirs(dst)
        
        if move_files:
            source = self.path 
            if not os.path.exists(self.path):
                source = self.default_path
                LOG.debug('data_directory in postgresql.conf points to non-existing location, using %s instead' % source)
            if source != new_cluster_dir:
                LOG.debug("copying cluster files from %s into %s" % (source, new_cluster_dir))
                shutil.copytree(source, new_cluster_dir)    
        LOG.debug("changing directory owner to %s" % self.user)
        chown_r(dst, self.user)
        
        LOG.debug("Changing postgres user`s home directory")
        if disttool.is_redhat_based():
            #looks like ubuntu doesn`t need this
            system2([USERMOD, '-d', new_cluster_dir, self.user]) 
            
        self.path = new_cluster_dir
    
        return new_cluster_dir
    
    def clean(self):
        fnames = ('recovery.conf','recovery.done','postmaster.pid')
        for fname in fnames:
            exclude = os.path.join(self.path, fname)
            if os.path.exists(exclude):
                LOG.debug('Deleting file: %s' % exclude)
                os.remove(exclude)
    
    def is_initialized(self, path):
        # are the pgsql files already in place? 
        return os.path.exists(path) and STORAGE_DATA_DIR in os.listdir(path)
Пример #17
0
 def disable_requiretty(self):
     '''
     requiretty      If set, sudo will only run when the user is logged in to a real tty.  
     When this flag is set, sudo can only be  run from a login session and not via other means 
     such  as cron(8) or cgi-bin scripts.  This flag is off by default on all systems but CentOS5.
     '''
     path = '/etc/sudoers'
     self._logger.debug('Disabling requiretty in %s' % path)
     if not disttool.is_ubuntu():
         orig = None
         with open(path, 'r') as fp:
             orig = fp.read()
         new = re.sub('Defaults\s+requiretty', '\n', orig)
         if new != orig:
             with open(path, 'w') as fp:
                 fp.write(new)
Пример #18
0
MONGOS = software.which('mongos')

ROUTER_DEFAULT_PORT = 27017
ARBITER_DEFAULT_PORT = 27020
REPLICA_DEFAULT_PORT = 27018
CONFIG_SERVER_DEFAULT_PORT = 27019

SERVICE_NAME = BuiltinBehaviours.MONGODB
STORAGE_PATH = "/mnt/mongodb-storage"

LOG_DIR = glob.glob('/var/log/mongo*')[0]
LOG_PATH_DEFAULT = os.path.join(LOG_DIR, 'mongodb.shardsrv.log')
DEFAULT_UBUNTU_DB_PATH = '/var/lib/mongodb'
DEFAULT_CENTOS_DB_PATH = '/var/lib/mongo'
LOCK_FILE = 'mongod.lock'
DEFAULT_USER = '******' if disttool.is_ubuntu() else 'mongod'
SCALR_USER = '******'
STORAGE_DATA_DIR = os.path.join(STORAGE_PATH, 'data')

UBUNTU_CONFIG_PATH = '/etc/mongodb.conf'
CENTOS_CONFIG_PATH = '/etc/mongod.conf'
CONFIG_PATH_DEFAULT = '/etc/mongodb.shardsrv.conf'
ARBITER_DATA_DIR = '/tmp/arbiter'
ARBITER_LOG_PATH = os.path.join(LOG_DIR, 'mongodb.arbiter.log')
ARBITER_CONF_PATH = '/etc/mongodb.arbiter.conf'

CONFIG_SERVER_DATA_DIR = os.path.join(STORAGE_PATH, 'config_server')
CONFIG_SERVER_CONF_PATH = '/etc/mongodb.configsrv.conf'
CONFIG_SERVER_LOG_PATH = os.path.join(LOG_DIR, 'mongodb.configsrv.log')

ROUTER_LOG_PATH = os.path.join(LOG_DIR, 'mongodb.router.log')
Пример #19
0
 def find(cls, version=None):
     cls.version = version or '9.0'
     path = cls.get_sysconfig_pgdata()
     if not path:
         path = '/etc/postgresql/%s/main' % version if disttool.is_ubuntu() else '/var/lib/pgsql/%s/data/' % version
     return cls(path, version)
Пример #20
0
    def on_init(self, *args, **kwargs):
        bus.on("before_hello", self.on_before_hello)
        bus.on("before_host_init", self.on_before_host_init)
        bus.on("before_restart", self.on_before_restart)

        msg_service = bus.messaging_service
        producer = msg_service.get_producer()
        producer.on("before_send", self.on_before_message_send)

        # Set the hostname to this instance's public hostname
        try:
            hostname_as_pubdns = int(__ec2__['hostname_as_pubdns'])
        except:
            hostname_as_pubdns = True

        if hostname_as_pubdns:
            pub_hostname = self._platform.get_public_hostname()
            self._logger.debug('Setting hostname to %s' % pub_hostname)
            system2("hostname " + pub_hostname, shell=True)

        if disttool.is_ubuntu():
            # Ubuntu cloud-init scripts may disable root ssh login
            for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'):
                if os.path.exists(path):
                    c = None
                    with open(path, 'r') as fp:
                        c = fp.read()
                    c = re.sub(re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c)
                    with open(path, 'w') as fp:
                        fp.write(c)

        # Add server ssh public key to authorized_keys
        authorized_keys_path = "/root/.ssh/authorized_keys"
        if os.path.exists(authorized_keys_path):
            c = None
            with open(authorized_keys_path, 'r') as fp:
                c = fp.read()
            ssh_key = self._platform.get_ssh_pub_key()
            idx = c.find(ssh_key)
            if idx == -1:
                if c and c[-1] != '\n':
                    c += '\n'
                c += ssh_key + "\n"
                self._logger.debug("Add server ssh public key to authorized_keys")
            elif idx > 0 and c[idx-1] != '\n':
                c = c[0:idx] + '\n' + c[idx:]
                self._logger.warn('Adding new-line character before server SSH key in authorized_keys file')
            with open(authorized_keys_path, 'w') as fp:
                fp.write(c)

        # Mount ephemeral devices
        # Seen on eucalyptus:
        #       - fstab contains invalid fstype and `mount -a` fails
        if self._platform.name == 'eucalyptus':
            mtab = mount.mounts()
            fstab = mount.fstab()
            for device in self._platform.instance_store_devices:
                if os.path.exists(device) and device in fstab and device not in mtab:
                    entry = fstab[device]
                    try:
                        mount.mount(device, entry.mpoint, '-o', entry.options)
                    except:
                        self._logger.warn(sys.exc_info()[1])
        else:
            system2('mount -a', shell=True, raise_exc=False)
Пример #21
0
    def on_init(self, *args, **kwargs):
        bus.on("before_hello", self.on_before_hello)
        bus.on("before_host_init", self.on_before_host_init)
        bus.on("before_restart", self.on_before_restart)

        msg_service = bus.messaging_service
        producer = msg_service.get_producer()
        producer.on("before_send", self.on_before_message_send)

        # Set the hostname to this instance's public hostname
        try:
            hostname_as_pubdns = int(__ec2__['hostname_as_pubdns'])
        except:
            hostname_as_pubdns = True

        if hostname_as_pubdns:
            pub_hostname = self._platform.get_public_hostname()
            self._logger.debug('Setting hostname to %s' % pub_hostname)
            system2("hostname " + pub_hostname, shell=True)

        if disttool.is_ubuntu():
            # Ubuntu cloud-init scripts may disable root ssh login
            for path in ('/etc/ec2-init/ec2-config.cfg',
                         '/etc/cloud/cloud.cfg'):
                if os.path.exists(path):
                    c = None
                    with open(path, 'r') as fp:
                        c = fp.read()
                    c = re.sub(
                        re.compile(r'^disable_root[^:=]*([:=]).*', re.M),
                        r'disable_root\1 0', c)
                    with open(path, 'w') as fp:
                        fp.write(c)

        # Add server ssh public key to authorized_keys
        authorized_keys_path = "/root/.ssh/authorized_keys"
        if os.path.exists(authorized_keys_path):
            c = None
            with open(authorized_keys_path, 'r') as fp:
                c = fp.read()
            ssh_key = self._platform.get_ssh_pub_key()
            idx = c.find(ssh_key)
            if idx == -1:
                if c and c[-1] != '\n':
                    c += '\n'
                c += ssh_key + "\n"
                self._logger.debug(
                    "Add server ssh public key to authorized_keys")
            elif idx > 0 and c[idx - 1] != '\n':
                c = c[0:idx] + '\n' + c[idx:]
                self._logger.warn(
                    'Adding new-line character before server SSH key in authorized_keys file'
                )
            with open(authorized_keys_path, 'w') as fp:
                fp.write(c)

        # Mount ephemeral devices
        # Seen on eucalyptus:
        #       - fstab contains invalid fstype and `mount -a` fails
        if self._platform.name == 'eucalyptus':
            mtab = mount.mounts()
            fstab = mount.fstab()
            for device in self._platform.instance_store_devices:
                if os.path.exists(
                        device) and device in fstab and device not in mtab:
                    entry = fstab[device]
                    try:
                        mount.mount(device, entry.mpoint, '-o', entry.options)
                    except:
                        self._logger.warn(sys.exc_info()[1])
        else:
            system2('mount -a', shell=True, raise_exc=False)
Пример #22
0
 def find(cls, mongo_conf):
     dir = mongo_conf.dbpath
     if not dir:
         dir = DEFAULT_UBUNTU_DB_PATH if disttool.is_ubuntu(
         ) else DEFAULT_CENTOS_DB_PATH
     return cls(dir)
Пример #23
0
from scalarizr.libs.metaconf import Configuration, NoPathError


__redis__ = node.__node__['redis']
__redis__.update({
        'storage_dir': '/mnt/redisstorage'
})

SERVICE_NAME = CNF_SECTION = DEFAULT_USER = '******'

SU_EXEC = '/bin/su'
BASH    = '/bin/bash'

UBUNTU_BIN_PATH          = '/usr/bin/redis-server'
CENTOS_BIN_PATH          = '/usr/sbin/redis-server'
BIN_PATH = UBUNTU_BIN_PATH if disttool.is_ubuntu() else CENTOS_BIN_PATH

PRESET_FNAME = 'redis.conf'
UBUNTU_CONFIG_DIR = '/etc/redis'
CENTOS_CONFIG_DIR = '/etc/'
CONFIG_DIR = UBUNTU_CONFIG_DIR if disttool.is_ubuntu() else CENTOS_CONFIG_DIR
DEFAULT_CONF_PATH = os.path.join(CONFIG_DIR, 'redis.conf')


OPT_REPLICATION_MASTER  = "replication_master"

REDIS_CLI_PATH = '/usr/bin/redis-cli'
DEFAULT_DIR_PATH = '/var/lib/redis'

DEFAULT_PID_DIR = '/var/run/redis' if os.path.isdir('/var/run/redis') else '/var/run'
CENTOS_DEFAULT_PIDFILE = os.path.join(DEFAULT_PID_DIR, 'redis.pid')
Пример #24
0
MONGOS = software.which("mongos")

ROUTER_DEFAULT_PORT = 27017
ARBITER_DEFAULT_PORT = 27020
REPLICA_DEFAULT_PORT = 27018
CONFIG_SERVER_DEFAULT_PORT = 27019

SERVICE_NAME = BuiltinBehaviours.MONGODB
STORAGE_PATH = "/mnt/mongodb-storage"

LOG_DIR = glob.glob("/var/log/mongo*")[0]
LOG_PATH_DEFAULT = os.path.join(LOG_DIR, "mongodb.shardsrv.log")
DEFAULT_UBUNTU_DB_PATH = "/var/lib/mongodb"
DEFAULT_CENTOS_DB_PATH = "/var/lib/mongo"
LOCK_FILE = "mongod.lock"
DEFAULT_USER = "******" if disttool.is_ubuntu() else "mongod"
SCALR_USER = "******"
STORAGE_DATA_DIR = os.path.join(STORAGE_PATH, "data")

UBUNTU_CONFIG_PATH = "/etc/mongodb.conf"
CENTOS_CONFIG_PATH = "/etc/mongod.conf"
CONFIG_PATH_DEFAULT = "/etc/mongodb.shardsrv.conf"
ARBITER_DATA_DIR = "/tmp/arbiter"
ARBITER_LOG_PATH = os.path.join(LOG_DIR, "mongodb.arbiter.log")
ARBITER_CONF_PATH = "/etc/mongodb.arbiter.conf"

CONFIG_SERVER_DATA_DIR = os.path.join(STORAGE_PATH, "config_server")
CONFIG_SERVER_CONF_PATH = "/etc/mongodb.configsrv.conf"
CONFIG_SERVER_LOG_PATH = os.path.join(LOG_DIR, "mongodb.configsrv.log")

ROUTER_LOG_PATH = os.path.join(LOG_DIR, "mongodb.router.log")
Пример #25
0
from scalarizr.util.filetool import rchown
from scalarizr.libs import metaconf
import sys


from scalarizr import linux, storage2
from scalarizr.linux import coreutils, pkgmgr
from scalarizr.services import backup
from scalarizr.node import __node__


LOG = logging.getLogger(__name__)

MYSQL_DEFAULT_PORT = 3306
MYSQL_PATH = "/usr/bin/mysql"  # old mysql_path
MYCNF_PATH = "/etc/mysql/my.cnf" if disttool.is_ubuntu() else "/etc/my.cnf"
MYSQLD_PATH = firstmatched(lambda x: os.access(x, os.X_OK), ("/usr/sbin/mysqld", "/usr/libexec/mysqld"))
MYSQLDUMP_PATH = "/usr/bin/mysqldump"
DEFAULT_DATADIR = "/var/lib/mysql"

STORAGE_DATA_DIR = "mysql-data"
STORAGE_BINLOG = "mysql-misc/binlog"

BEHAVIOUR = SERVICE_NAME = BuiltinBehaviours.MYSQL


class MySQL(BaseService):

    service = None
    my_cnf = None
    _instance = None
Пример #26
0
class MysqlInitScript(initdv2.ParametrizedInitScript):

    socket_file = None
    cli = None
    sgt_pid_path = '/tmp/mysqld-sgt.pid'

    @lazy
    def __new__(cls, *args, **kws):
        obj = super(MysqlInitScript, cls).__new__(cls, *args, **kws)
        cls.__init__(obj)
        return obj

    def __init__(self):
        if 'gce' == node.__node__['platform'].name:
            self.ensure_pid_directory()

        self.mysql_cli = MySQLClient()

        if disttool.is_ubuntu() and disttool.version_info() >= (10, 4):
            initd_script = ('/usr/sbin/service', 'mysql')
        else:
            initd_script = firstmatched(
                os.path.exists, ('/etc/init.d/mysqld', '/etc/init.d/mysql'))

        pid_file = None
        try:
            out = system2("my_print_defaults mysqld", shell=True, silent=True)
            m = re.search("--pid[-_]file=(.*)", out[0], re.MULTILINE)
            if m:
                pid_file = m.group(1)
            m = re.search("--socket=(.*)", out[0], re.MULTILINE)
            if m:
                self.socket_file = m.group(1)
        except:
            pass

        initdv2.ParametrizedInitScript.__init__(
            self,
            SERVICE_NAME,
            initd_script,
            pid_file,
            socks=[initdv2.SockParam(MYSQL_DEFAULT_PORT, timeout=3600)])

    def _start_stop_reload(self, action):
        ''' XXX: Temporary ugly hack (Ubuntu 1004 upstart problem - Job is already running)'''
        try:
            args = [self.initd_script] \
                            if isinstance(self.initd_script, basestring) \
                            else list(self.initd_script)
            args.append(action)
            out, err, returncode = system2(args,
                                           close_fds=True,
                                           preexec_fn=os.setsid)
        except PopenError, e:
            if 'Job is already running' not in str(e):
                raise InitdError("Popen failed with error %s" % (e, ))

        if action == 'restart':
            if err and 'stop: Job has already been stopped: mysql' in err:
                return True
            else:
                LOG.debug('waiting for mysql process')
                wait_until(lambda: MYSQLD_PATH in system2(
                    ('ps', '-G', DEFAULT_OWNER, '-o', 'command', '--no-headers'
                     ))[0],
                           timeout=10,
                           sleep=1)

        if action == 'start' and disttool.is_ubuntu(
        ) and disttool.version_info() >= (10, 4):
            try:
                LOG.debug('waiting for mysql process')
                wait_until(lambda: MYSQLD_PATH in system2(
                    ('ps', '-G', DEFAULT_OWNER, '-o', 'command', '--no-headers'
                     ))[0],
                           timeout=10,
                           sleep=1)
                LOG.debug('waiting for debian-start finish')
                out = system2(
                    'ps axo pid,command --noheaders | grep /etc/mysql/debian-start',
                    shell=True)[0].strip()
                if out:
                    pid = out.split('\n')[0].split(' ')[0]
                    wait_until(lambda: not os.path.exists('/proc/%s' % pid),
                               sleep=1)
            except:
                self._start_stop_reload('restart')
                return True

        if self.socks and (action != "stop"
                           and not (action == 'reload' and not self.running)):
            for sock in self.socks:
                wait_sock(sock)

        return True
Пример #27
0
 def find(cls, mongo_conf):
     dir = mongo_conf.dbpath
     if not dir:
         dir = DEFAULT_UBUNTU_DB_PATH if disttool.is_ubuntu() else DEFAULT_CENTOS_DB_PATH
     return cls(dir)
Пример #28
0
    def on_init(self, *args, **kwargs):
        bus.on("before_hello", self.on_before_hello)
        bus.on("before_host_init", self.on_before_host_init)
        bus.on("before_restart", self.on_before_restart)
        bus.on("before_reboot_finish", self.on_before_reboot_finish)

        try:
            system(('ntpdate', '-u', '0.amazon.pool.ntp.org'))
        except:
            pass

        msg_service = bus.messaging_service
        producer = msg_service.get_producer()
        producer.on("before_send", self.on_before_message_send)

        if not os_dist.windows_family and not __node__.get('hostname'):
            # Set the hostname to this instance's public hostname
            try:
                hostname_as_pubdns = int(__ec2__['hostname_as_pubdns'])
            except:
                hostname_as_pubdns = True

            if hostname_as_pubdns:
                pub_hostname = self._platform.get_public_hostname()
                self._logger.debug('Setting hostname to %s' % pub_hostname)
                system2("hostname " + pub_hostname, shell=True)

        if disttool.is_ubuntu():
            # Ubuntu cloud-init scripts may disable root ssh login
            for path in ('/etc/ec2-init/ec2-config.cfg',
                         '/etc/cloud/cloud.cfg'):
                if os.path.exists(path):
                    c = None
                    with open(path, 'r') as fp:
                        c = fp.read()
                    c = re.sub(
                        re.compile(r'^disable_root[^:=]*([:=]).*', re.M),
                        r'disable_root\1 0', c)
                    with open(path, 'w') as fp:
                        fp.write(c)

        if not linux.os.windows_family:
            # Add server ssh public key to authorized_keys
            ssh_key = self._platform.get_ssh_pub_key()
            if ssh_key:
                add_authorized_key(ssh_key)

        # Mount ephemeral devices
        # Seen on eucalyptus:
        #       - fstab contains invalid fstype and `mount -a` fails
        if self._platform.name == 'eucalyptus':
            mtab = mount.mounts()
            fstab = mount.fstab()
            for device in self._platform.instance_store_devices:
                if os.path.exists(
                        device) and device in fstab and device not in mtab:
                    entry = fstab[device]
                    try:
                        mount.mount(device, entry.mpoint, '-o', entry.options)
                    except:
                        self._logger.warn(sys.exc_info()[1])
        else:
            if not os_dist.windows_family:
                system2('mount -a', shell=True, raise_exc=False)
Пример #29
0
from pymysql import cursors

from scalarizr.config import BuiltinBehaviours
from scalarizr.services import BaseService, ServiceError, BaseConfig, lazy, PresetProvider
from scalarizr.util import system2, disttool, firstmatched, initdv2, wait_until, PopenError, software
from scalarizr.util.initdv2 import wait_sock, InitdError
from scalarizr.linux.coreutils import chown_r
from scalarizr.libs import metaconf
from scalarizr.linux.rsync import rsync

LOG = logging.getLogger(__name__)

MYSQL_DEFAULT_PORT = 3306
MYSQL_PATH = '/usr/bin/mysql'  # old mysql_path
MYCNF_PATH = '/etc/mysql/my.cnf' if disttool.is_ubuntu() else '/etc/my.cnf'
MYSQLD_PATH = firstmatched(lambda x: os.access(x, os.X_OK),
                           ('/usr/sbin/mysqld', '/usr/libexec/mysqld'))
MYSQLDUMP_PATH = '/usr/bin/mysqldump'
DEFAULT_DATADIR = "/var/lib/mysql"
DEFAULT_OWNER = "mysql"
STORAGE_DATA_DIR = "mysql-data"
STORAGE_BINLOG = "mysql-misc/binlog"
SU_EXEC = '/bin/su'
BASH = '/bin/bash'
PRESET_FNAME = 'my.cnf'

BEHAVIOUR = SERVICE_NAME = BuiltinBehaviours.MYSQL


class MySQL(BaseService):