Esempio n. 1
0
 def testYumBased(self):
     self.assertFalse(OS.yum_based('ubuntu'))
     self.assertFalse(OS.yum_based('debian'))
     self.assertTrue(OS.yum_based('fedora'))
     self.assertTrue(OS.yum_based('rhel'))
     self.assertTrue(OS.yum_based('centos'))
     self.assertFalse(OS.yum_based('windows'))
Esempio n. 2
0
 def testYumBased(self):
     self.assertFalse(OS.yum_based('ubuntu'))
     self.assertFalse(OS.yum_based('debian'))
     self.assertTrue(OS.yum_based('fedora'))
     self.assertTrue(OS.yum_based('rhel'))
     self.assertTrue(OS.yum_based('centos'))
     self.assertFalse(OS.yum_based('windows'))
Esempio n. 3
0
class Postgresql:

    if OS.yum_based():
        DATADIR = '/var/lib/pgsql/data'
        DAEMON = 'postgresql'
        PSQL_CMD = '/usr/bin/psql'
        PGDUMPALL_CMD = '/usr/bin/pg_dumpall'

        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'yum install -y postgresql-server postgresql'

    elif OS.apt_based() and os.path.isdir('/var/lib/postgresql'):
        VERSION = os.listdir('/var/lib/postgresql')[0]
        DATADIR = '/var/lib/postgresql/' + VERSION + '/main'
        DAEMON = 'postgresql'
        PSQL_CMD = '/usr/bin/psql'
        PGDUMPALL_CMD = '/usr/bin/pg_dumpall'

        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'apt-get install -y postgresql'

    elif OS.is_windows() and os.path.isdir("C:\Program Files\PostgreSQL"):
        VERSION = os.listdir('C:\Program Files\PostgreSQL')[0]
        DATADIR = os.path.join("C:\Program Files\PostgreSQL", VERSION, "data")
        DAEMON = 'postgresql-x64-' + VERSION  # FIXME also support 32 bit
        PSQL_CMD = os.path.join("C:\Program Files\PostgreSQL", VERSION,
                                "bin\psql.exe")
        PGDUMPALL_CMD = os.path.join("C:\Program Files\PostgreSQL", VERSION,
                                     "bin\pg_dumpall.exe")

    else:
        VERSION = None
        DATADIR = None
        DAEMON = None
        PSQL_CMD = None
        PGDUMPALL_CMD = None

    def set_pgpassword_env():
        '''helper to set the postgres password in the env from the config'''
        pgpassword = snap.config.options.service_options['postgresql_password']
        penv = os.environ
        penv['PGPASSWORD'] = pgpassword
        return penv

    set_pgpassword_env = staticmethod(set_pgpassword_env)

    def db_exists(dbname):
        '''helper to return boolean indicating if the db w/ the specified name exists'''
        # get the env containing the postgres password
        penv = Postgresql.set_pgpassword_env()

        # retrieve list of db names from postgres
        c = FileManager.capture_output([
            Postgresql.PSQL_CMD, "--username", "postgres", "-t", "-c",
            "select datname from pg_database"
        ],
                                       env=penv)

        # determine if the specified one is among them
        has_db = len(re.findall(dbname, c))

        return has_db

    db_exists = staticmethod(db_exists)

    def create_db(dbname):
        '''helper to create the specified database'''
        null = open(OSUtils.null_file(), 'w')

        # get env containing the postgres password
        penv = Postgresql.set_pgpassword_env()

        # create the db
        popen = subprocess.Popen([
            Postgresql.PSQL_CMD, "--username", "postgres", "-c",
            "CREATE DATABASE " + dbname
        ],
                                 env=penv,
                                 stdout=null,
                                 stderr=null)
        popen.wait()

    create_db = staticmethod(create_db)

    def drop_db(dbname):
        '''helper to drop the specified database'''
        null = open(OSUtils.null_file(), 'w')

        # get env containing the postgres password
        penv = Postgresql.set_pgpassword_env()

        # destroy the db
        popen = subprocess.Popen([
            Postgresql.PSQL_CMD, "--username", "postgres", "-c",
            "DROP DATABASE " + dbname
        ],
                                 env=penv,
                                 stdout=null,
                                 stderr=null)
        popen.wait()

    drop_db = staticmethod(drop_db)

    def set_root_pass():
        '''helper to set the postgresql root password'''
        # !!!FIXME!!! implement, can be accomplished by su'ing to the postgres user on linux

    set_root_pass = staticmethod(set_root_pass)

    def init_db():
        '''helper to initialize the database server'''

        # db already initialized, just return
        if os.path.isdir(Postgresql.DATADIR) and len(
                os.listdir(Postgresql.DATADIR)) > 0:
            return

        null = open(OSUtils.null_file(), 'w')

        # FIXME should run initdb manually
        popen = subprocess.Popen(["service", "postgresql", "initdb"],
                                 stdout=null)
        popen.wait()

    init_db = staticmethod(init_db)

    def is_available(self):
        '''return true postgres is available locally'''
        return Postgresql.DATADIR and os.path.isdir(Postgresql.DATADIR)

    def install_prereqs(self):
        if OS.is_linux():
            popen = subprocess.Popen(Postgresql.PREREQ_INSTALL_COMMAND.split())
            popen.wait()
        # !!!FIXME!!! it is possible to install postgresql in an automated /
        # non-interactive method on windows, implement this!!!

    def backup(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Postgresql.set_root_pass()

        # check to see if service is running
        already_running = dispatcher.service_running(Postgresql.DAEMON)

        # start the postgresql server
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing postgres password
        penv = Postgresql.set_pgpassword_env()

        outfile = file(basedir + "/dump.psql", "w")
        pipe = subprocess.Popen(
            [Postgresql.PGDUMPALL_CMD, "--username", "postgres"],
            env=penv,
            stdout=outfile,
            stderr=null)
        pipe.wait()

        # if postgresql was running b4hand, start up again
        if not already_running:
            dispatcher.stop_service(Postgresql.DAEMON)

    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()
        null = open(OSUtils.null_file(), 'w')

        if OS.is_linux():
            Postgresql.set_root_pass()

        # init the postgresql db
        Postgresql.init_db()

        # start the postgresql service
        dispatcher.start_service(Postgresql.DAEMON)

        # get env containing the postgresql password
        penv = Postgresql.set_pgpassword_env()

        # use pipe to invoke postgres, restoring database
        infile = file(basedir + "/dump.psql", "r")
        popen = subprocess.Popen(
            [Postgresql.PSQL_CMD, "--username", "postgres"],
            env=penv,
            stdin=infile,
            stdout=null,
            stderr=null)
        popen.wait()
Esempio n. 4
0
class Asterisk:

    DAEMON = 'asterisk'

    DIRS = {
        # All of the configuration files
        'conf': '/etc/asterisk',
        # Call recordings, voicemails, ...
        'spool': '/var/spool/asterisk',
        # Music on Hold files, encryption keys, AGI scripts, ...
        'data': '/usr/share/asterisk',
    }

    PREREQ_INSTALL_COMMAND = None

    if OS.yum_based():
        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'yum install -y asterisk'

    elif OS.apt_based():
        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'apt-get install -y asterisk'

    @classmethod
    def is_available(cls):
        '''return true if we're on a linux and the config dir exists'''
        return OS.is_linux() and os.path.isdir(Asterisk.DIRS['conf'])

    @classmethod
    def install_prereqs(cls):
        if OS.is_linux():
            subprocess.Popen(Asterisk.PREREQ_INSTALL_COMMAND.split()).wait()

    @classmethod
    def backup(cls, basedir):
        # backup the confd
        files = snap.filemanager.FileManager.get_all_files(
                             include=[d for d in Asterisk.DIRS.itervalues()])
        sfiles = [SFile(tfile).copy_to(basedir)
                             for tfile in files if os.access(tfile, os.R_OK)]

        # write record file to basedir
        record = FilesRecordFile(os.path.join(basedir, "service-asterisk.xml"))
        record.write(sfiles)

    @classmethod
    def restore(cls, basedir):
        dispatcher = Dispatcher.os_dispatcher()

        record_file = os.path.join(basedir, "service-asterisk.xml")

        # if files record file isn't found, simply return
        if not os.path.isfile(record_file):
            return

        # stop the service if already running
        if dispatcher.service_running(Asterisk.DAEMON):
            dispatcher.stop_service(Asterisk.DAEMON)

        # read files from the record file
        record = FilesRecordFile(record_file)
        sfiles = record.read()

        # restore those to their original locations
        for sfile in sfiles:
            sfile.copy_to(path_prefix=basedir)

        # start the service
        dispatcher.start_service(Asterisk.DAEMON)
Esempio n. 5
0
class Httpd:

    if OS.yum_based():
        DAEMON = 'httpd'
        CONF_D = '/etc/httpd'
        DOCUMENT_ROOT = '/var/www'

        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'yum install -y httpd'

    elif OS.apt_based():
        DAEMON = 'apache2'
        CONF_D = '/etc/apache2'
        DOCUMENT_ROOT = '/var/www'

        # hack until we re-introduce package system abstraction:
        PREREQ_INSTALL_COMMAND = 'apt-get install -y apache2'

    elif OS.is_windows() and os.path.isdir(
            "C:\Program Files (x86)\Apache Software Foundation"):
        DAEMON = 'Apache2.2'
        CONF_D = "C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\conf"
        DOCUMENT_ROOT = "C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs"

    else:
        DAEMON = None
        CONF_D = None
        DOCUMENT_ROOT = None

    def is_available(self):
        '''return true if we're on a linux system and the init script is available'''
        return (Httpd.CONF_D and os.path.isdir(Httpd.CONF_D)
                and Httpd.DOCUMENT_ROOT and os.path.isdir(Httpd.DOCUMENT_ROOT))

    def install_prereqs(self):
        if OS.is_linux():
            popen = subprocess.Popen(Httpd.PREREQ_INSTALL_COMMAND.split())
            popen.wait()
        # !!!FIXME!!! it is possible to install httpd in an automated /
        # non-interactive method on windows, implement this!!!

    def backup(self, basedir):
        # backup the webroot, confd
        sfiles = []
        files = snap.filemanager.FileManager.get_all_files(
            include=[Httpd.DOCUMENT_ROOT, Httpd.CONF_D])
        for tfile in files:
            if os.access(tfile, os.R_OK):
                sfile = SFile(tfile)
                sfile.copy_to(basedir)
                sfiles.append(sfile)

        # write record file to basedir
        record = FilesRecordFile(os.path.join(basedir, "service-http.xml"))
        record.write(sfiles)

    def restore(self, basedir):
        dispatcher = Dispatcher.os_dispatcher()

        record_file = os.path.join(basedir, "service-http.xml")

        # if files record file isn't found, simply return
        if not os.path.isfile(record_file):
            return

        # stop the httpd service if already running
        if dispatcher.service_running(Httpd.DAEMON):
            dispatcher.stop_service(Httpd.DAEMON)

        # read files from the record file
        record = FilesRecordFile(record_file)
        sfiles = record.read()

        # restore those to their original locations
        for sfile in sfiles:
            sfile.copy_to(path_prefix=basedir)

        # ensure the various subdirs exists even if empty
        if OS.is_linux() and not os.path.isdir(
                os.path.join(Httpd.DOCUMENT_ROOT, "html")):
            os.mkdir(os.path.join(Httpd.DOCUMENT_ROOT, "html"))
        if OS.is_linux() and not os.path.isdir(
                os.path.join(Httpd.CONF_D, "logs")):
            os.mkdir(os.path.join(Httpd.CONF_D, "logs"))
        if OS.is_linux() and not os.path.isdir(
                os.path.join(Httpd.CONF_D, "run")):
            os.mkdir(os.path.join(Httpd.CONF_D, "run"))

        # start the httpd service
        dispatcher.start_service(Httpd.DAEMON)