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)
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)
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()
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 install_prereqs(self): if OS.is_linux(): env = os.environ env['DEBIAN_FRONTEND'] = 'noninteractive' popen = subprocess.Popen(Mysql.PREREQ_INSTALL_COMMAND.split(), env=env) popen.wait()
def backup(self, basedir): dispatcher = Dispatcher.os_dispatcher() null = open(OSUtils.null_file(), 'w') if OS.is_linux(): Mysql.set_root_pass() mysql_password = snap.config.options.service_options['mysql_password'] # check to see if service is running already_running = dispatcher.service_running(Mysql.DAEMON) # start the mysql server dispatcher.start_service(Mysql.DAEMON) # use a pipe to invoke mysqldump and capture output outfile = file(basedir + "/dump.mysql", "w") popen = subprocess.Popen([ Mysql.MYSQLDUMP_CMD, "-u", "root", "-p" + mysql_password, "--all-databases" ], stdout=outfile, stderr=null) popen.wait() # if mysql was stopped b4hand, start up again if not already_running: dispatcher.stop_service(Mysql.DAEMON)
def testIsLinux(self): self.assertTrue(OS.is_linux('fedora')) self.assertTrue(OS.is_linux('rhel')) self.assertTrue(OS.is_linux('centos')) self.assertTrue(OS.is_linux('ubuntu')) self.assertTrue(OS.is_linux('debian')) self.assertTrue(OS.is_linux('mock')) self.assertFalse(OS.is_linux('windows')) self.assertFalse(OS.is_linux('mock_windows'))
def testOSDispatcher(self): if OS.is_windows(): self.assertEqual( snap.backends.services.dispatcher.Dispatcher.os_dispatcher(), snap.backends.services.windowsdispatcher.WindowsDispatcher) elif OS.is_linux(): self.assertEqual( snap.backends.services.dispatcher.Dispatcher.os_dispatcher(), snap.backends.services.linuxdispatcher.LinuxDispatcher)
def testOSDispatcher(self): if OS.is_windows(): self.assertEqual( snap.backends.services.dispatcher.Dispatcher.os_dispatcher(), snap.backends.services.windowsdispatcher.WindowsDispatcher, ) elif OS.is_linux(): self.assertEqual( snap.backends.services.dispatcher.Dispatcher.os_dispatcher(), snap.backends.services.linuxdispatcher.LinuxDispatcher, )
def restore(self, basedir): dispatcher = Dispatcher.os_dispatcher() null = open(OSUtils.null_file(), 'w') if OS.is_linux(): Mysql.set_root_pass() mysql_password = snap.config.options.service_options['mysql_password'] # start the mysql server dispatcher.start_service(Mysql.DAEMON) # use pipe to invoke mysql, restoring database infile = file(basedir + "/dump.mysql", "r") popen = subprocess.Popen([Mysql.MYSQL_CMD, "-u", "root", "-p" + mysql_password], stdin=infile, stdout=null, stderr=null) popen.wait() # flush privileges incase any roles were restored and whatnot Mysql.flush_privileges()
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()
def restore(self, basedir): dispatcher = Dispatcher.os_dispatcher() null = open(OSUtils.null_file(), 'w') if OS.is_linux(): Mysql.set_root_pass() mysql_password = snap.config.options.service_options['mysql_password'] # start the mysql server dispatcher.start_service(Mysql.DAEMON) # use pipe to invoke mysql, restoring database infile = file(basedir + "/dump.mysql", "r") popen = subprocess.Popen( [Mysql.MYSQL_CMD, "-u", "root", "-p" + mysql_password], stdin=infile, stdout=null, stderr=null) popen.wait() # flush privileges incase any roles were restored and whatnot Mysql.flush_privileges()
def backup(self, basedir): dispatcher = Dispatcher.os_dispatcher() null = open(OSUtils.null_file(), 'w') if OS.is_linux(): Mysql.set_root_pass() mysql_password = snap.config.options.service_options['mysql_password'] # check to see if service is running already_running = dispatcher.service_running(Mysql.DAEMON) # start the mysql server dispatcher.start_service(Mysql.DAEMON) # use a pipe to invoke mysqldump and capture output outfile = file(basedir + "/dump.mysql", "w") popen = subprocess.Popen([Mysql.MYSQLDUMP_CMD, "-u", "root", "-p" + mysql_password, "--all-databases"], stdout=outfile, stderr=null) popen.wait() # if mysql was stopped b4hand, start up again if not already_running: dispatcher.stop_service(Mysql.DAEMON)
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 install_prereqs(cls): if OS.is_linux(): subprocess.Popen(Asterisk.PREREQ_INSTALL_COMMAND.split()).wait()
def install_prereqs(self): if OS.is_linux(): env=os.environ env['DEBIAN_FRONTEND']='noninteractive' popen = subprocess.Popen(Mysql.PREREQ_INSTALL_COMMAND.split(), env=env) popen.wait()
def install_prereqs(self): if OS.is_linux(): popen = subprocess.Popen(Postgresql.PREREQ_INSTALL_COMMAND.split()) popen.wait()
def install_prereqs(self): if OS.is_linux(): popen = subprocess.Popen(Httpd.PREREQ_INSTALL_COMMAND.split()) popen.wait()
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'])
class OsRegistryTest(unittest.TestCase): def setUp(self): self.basedir = os.path.join(os.path.dirname(__file__), "data", "osrtest") if not os.path.isdir(self.basedir): os.mkdir(self.basedir) def tearDown(self): if os.path.isdir(self.basedir): shutil.rmtree(self.basedir) def testIsLinux(self): self.assertTrue(OS.is_linux('fedora')) self.assertTrue(OS.is_linux('rhel')) self.assertTrue(OS.is_linux('centos')) self.assertTrue(OS.is_linux('ubuntu')) self.assertTrue(OS.is_linux('debian')) self.assertTrue(OS.is_linux('mock')) self.assertFalse(OS.is_linux('windows')) self.assertFalse(OS.is_linux('mock_windows')) def testIsWindows(self): self.assertFalse(OS.is_windows('fedora')) self.assertFalse(OS.is_windows('rhel')) self.assertFalse(OS.is_windows('centos')) self.assertFalse(OS.is_windows('ubuntu')) self.assertFalse(OS.is_windows('debian')) self.assertFalse(OS.is_windows('mock')) self.assertTrue(OS.is_windows('windows')) self.assertTrue(OS.is_windows('mock_windows')) def testAptBased(self): self.assertTrue(OS.apt_based('ubuntu')) self.assertTrue(OS.apt_based('debian')) self.assertFalse(OS.apt_based('fedora')) self.assertFalse(OS.apt_based('rhel')) self.assertFalse(OS.apt_based('centos')) self.assertFalse(OS.apt_based('windows')) 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')) def testOsRoot(self): self.assertEqual('/', OS.get_root('fedora')) self.assertEqual('/', OS.get_root('ubuntu')) self.assertEqual('C:\\', OS.get_root('windows')) @unittest.skipUnless(OS.is_windows(), "only relevant for windows") def testWindowsRoot(self): self.assertEqual('C:\\', OS.get_root()) @unittest.skipUnless(OS.is_linux(), "only relevant for linux") def testLinuxRoot(self): self.assertEqual('/', OS.get_root()) def testPathSeperator(self): self.assertEqual('/', OS.get_path_seperator('fedora')) self.assertEqual('/', OS.get_path_seperator('ubuntu')) self.assertEqual('\\', OS.get_path_seperator('windows')) @unittest.skipUnless(OS.is_windows(), "only relevant for windows") def testWindowsPathSeperator(self): self.assertEqual('\\', OS.get_path_seperator()) @unittest.skipUnless(OS.is_linux(), "only relevant for linux") def testLinuxPathSeperator(self): self.assertEqual('/', OS.get_path_seperator()) #def testDefaultBackendForTarget(self): @unittest.skipUnless(OS.is_windows(), "only relevant for windows") def testWindowsNullFile(self): self.assertEqual("nul", OSUtils.null_file()) @unittest.skipUnless(OS.is_linux(), "only relevant for linux") def testLinuxNullFile(self): self.assertEqual("/dev/null", OSUtils.null_file()) @unittest.skipUnless(OS.is_windows(), "only relevant for windows") def testWindowsChown(self): basefile = os.path.join(self.basedir, "foo") f = open(basefile, "w") f.write("foo") f.close OSUtils.chown(self.basedir, username="******") null = open(OSUtils.null_file(), 'w') tfile = tempfile.TemporaryFile() popen = subprocess.Popen(["icacls", self.basedir], stdout=tfile, stderr=null) popen.wait() tfile.seek(0) c = tfile.read() self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c))) tfile.close() tfile = tempfile.TemporaryFile() popen = subprocess.Popen(["icacls", basefile], stdout=tfile, stderr=null) popen.wait() tfile.seek(0) c = tfile.read() self.assertNotEqual(1, len(re.findall(".*mmorsi.*(F).*\n.*", c))) tfile.close() @unittest.skipUnless(OS.is_linux(), "only relevant for linux") def testLinuxChown(self): basefile = os.path.join(self.basedir, "foo") f = open(basefile, "w") f.write("foo") f.close OSUtils.chown(self.basedir, uid=100, gid=100) st = os.stat(self.basedir) self.assertEqual(100, st.st_uid) self.assertEqual(100, st.st_gid) st = os.stat(basefile) self.assertEqual(100, st.st_uid) self.assertEqual(100, st.st_gid) import pwd pwo = pwd.getpwnam("nobody") uid = pwo.pw_uid gid = pwo.pw_gid OSUtils.chown(self.basedir, username="******") st = os.stat(self.basedir) self.assertEqual(uid, st.st_uid) self.assertEqual(gid, st.st_gid) st = os.stat(basefile) self.assertEqual(uid, st.st_uid) self.assertEqual(gid, st.st_gid) #@unittest.skipUnless(OS.is_windows(), "only relevant for windows") #def testWindowsIsSuperUser(self): # not sure how to switch users on windows to test this @unittest.skipUnless(OS.is_linux(), "only relevant for linux") def testLinuxIsSuperUser(self): ouid = os.geteuid() self.assertTrue(OSUtils.is_superuser()) os.seteuid(100) self.assertFalse(OSUtils.is_superuser()) os.seteuid(ouid)