def docker_start(): _exec_system("sysctl -e kernel.core_pattern=/tmp/core/core-%e-%p-%s") for i in dock_list: _exec_system('docker start %s' % (i)) __docker_start()
def mount_dev(self, dev, force = 0): self.dev = dev try: self.dir = "/mnt/snap" _exec_system("mkdir -p %s" % self.dir) except: pass if not stat.S_ISBLK(os.lstat(os.path.realpath(self.dev)).st_mode): raise Exp('not a block device', dev) try: (out, err) = _exec_pipe1(["mount", self.dev, self.dir], 0, self.verbose, 0) except Exp, e: out = e.out err = e.err for line in err.splitlines(): m1 = re.search('you must specify the filesystem type', line) m2 = re.search('wrong fs type, bad option', line) if m1 is not None or m2 is not None: if force: self.disk.dev_format(self.dev, self.verbose) self.disk.dev_mount(self.dev, self.dir, self.verbose) else: _derror('wrong fs type, you must specify the filesystem type or use --force') exit(e.errno)
def docker_run(): for i in dock_list: #_exec_system('docker run --name %s -h %s -d -t %s /bin/bash' % (i, i, tpl)) _exec_system( 'docker run --name %s -h %s -v /opt/sdfs --privileged=true -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -d -t %s' % (i, i, tpl)) __docker_start()
def __docker_start(): _exec_system("echo '' > /tmp/hosts") (distro, release, codename) = lsb.lsb_release() for i in dock_list: if distro == 'CentOS': errno = _exec_system( "docker exec %s sed -i 's/^UsePAM yes$/UsePAM no/g' /etc/ssh/sshd_config" % (i)) if errno: _derror("Please install docker last version") exit(errno) errno = _exec_system( "docker exec %s sed -i 's/^#PermitRootLogin yes$/PermitRootLogin yes/g' /etc/ssh/sshd_config" % (i)) if errno: _derror("Please install docker last version") exit(errno) errno = _exec_system( "docker exec %s bash -c \"echo 'mdsmds' | passwd root --stdin\"" % (i)) if errno: _derror("Please install docker last version") exit(errno) _exec_system( r"docker exec %s sed -i 's/\\h \\W/\\H \\W/g' /etc/bashrc" % (i)) """ errno = _exec_system('docker exec %s /etc/init.d/sshd start' % (i)) if errno: _derror("Please install docker last version") exit(errno) """ errno = _exec_system( 'docker exec %s /usr/bin/rsync --daemon --config=/etc/rsyncd.conf' % (i)) if errno: _derror("Please install docker last version") exit(errno) _exec_system("echo %s %s >> /tmp/hosts" % (get_addr(i), i)) for i in dock_list: try: _put_remote(get_addr(i), "/tmp/hosts", "/etc/hosts", user='******', password='******', timeout=10) except Exception, e: _derror( "put file /tmp/hosts to %s failed, please check sshd is running normal" % i) #_exec_system('docker exec %s bash -c \ # "sed -i \'s/session required pam_loginuid.so/#session required pam_loginuid.so/\' /etc/pam.d/sshd"' % i) exit(1)
def backup(self, lun, path, filename, keep, force, v): filepath = "" snap = "" if (filename == ""): name = "snapfile" else: name = filename if path.startswith('/dev/'): self.dev = path self.mount_dev(path, force) filepath = self.dir elif os.path.isdir(path): filepath = path elif path.startswith('/'): filepath = os.path.dirname(path) if not os.path.exists(filepath): try: self.dir = filepath _exec_system("mkdir -p %s" % self.dir) except: pass #name = os.path.basename(path) if path.split('/')[-1] != "": name = path.split('/')[-1] else: self.end(keep, snap) _derror('path %s was wrong' % path) exit(errno.EINVAL) if (self.is_snap(lun, v)): snapath = self.defaultpath + lun elif (self.is_lun(lun, v)): snap = self.create_snap(lun) snapath = snap else: self.end(keep, snap) _derror('lun %s was wrong' % lun) exit(errno.EINVAL) self.check(snapath, filepath, keep, snap) cmd = [self.lich_snapshot, '--copy', snapath, ':' + filepath + '/' + name] try: (out, err) = _exec_pipe1(cmd, 0, self.verbose) except Exp, e: if e.err: self.end(keep, snap) _derror(str(e.err).strip()) exit(e.errno)
def sync(self): newpath = os.path.abspath(path + '/../') cmd = 'rsync -varz --progress --no-o --no-g --exclude-from=%s/.gitignore %s root@%s::sdfs' % ( newpath, newpath, self.addr) errno = _exec_system(cmd) if errno: _derror("cmd %s fail:%d" % (cmd, errno)) exit(errno)
def end(self, keep = 0, snap = ""): try: if self.dev != "": self.disk.dev_umount(self.dev, self.verbose) except: pass if self.dir != "" and self.dev != "": _exec_system("rm -rf %s" % self.dir) if keep == 0 and snap != "": cmd = [self.lich_snapshot, "--remove", snap] try: (out, err) = _exec_pipe1(cmd, 0, False) except Exp, e: if e.err: _derror(str(e.err)) exit(e.errno)
def set_lun(self, path, ext): if ext == 'ssd': num = 0 elif ext == 'hdd': num = 1 cmd = [self.config.inspect, "--tier", path, str(num)] try: (out, err) = _exec_pipe1(cmd, 0, False) ret = _exec_system("echo $?", False, False) if ret == 0: _dmsg('set %s %s successful' % (path, ext)) except Exp, e: if e.err: _derror(str(e.err)) exit(e.errno)
def getstat(self, path): cmd = [self.lichfs, "--stat", path] try: (out, err) = _exec_pipe1(cmd, 0, False) ret = _exec_system("echo $?", False, False) if ret == 0: self.success += 1 #_dmsg('check %s successful' % (path)) else: self.fail += 1 self.fail_lun.append(path) except Exp, e: self.fail += 1 self.fail_lun.append(path) if e.err: _derror(str(e.err)) exit(e.errno)
def restore(self, path, lun, filename, v): if (path.startswith('/dev/')): self.dev = path self.mount_dev(path) filepath = self.dir + '/' + filename else: filepath = path + '/' + filename if not os.path.exists(filepath): self.end() _derror( '%s filename is not exist' % (filepath)) exit(errno.EINVAL) if (self.is_target(lun, v) == 1): if (self.is_lun(lun, v) == 1): self.end() _derror( '%s is exist, please rename a new lun' % (lun)) exit(errno.EINVAL) else: tgt = os.path.dirname(lun) cmd = "lichbd mkpool %s -p iscsi" % (tgt) if (self.verbose): print cmd os.system(cmd) cmd = [self.lichfs, "--copy", ":" + filepath, self.defaultpath + lun] if (self.verbose): print cmd try: (out, err) = _exec_pipe1(cmd, 0, False) print out except Exp, e: ret = _exec_system("echo $?", False, False) if ret and e.err: self.end() _derror(str(e.err)) exit(e.errno)
def docker_build(): userdir = os.path.expanduser('~') if not os.path.exists(userdir + "/.ssh/id_dsa")\ or not os.path.exists(userdir + "/.ssh/id_dsa.pub"): _derror("Please generate ssh key file use 'ssh-keygen -t dsa'") exit(1) _exec_system("cat ~/.ssh/id_dsa > %s/id_dsa" % (path)) _exec_system("cat ~/.ssh/id_dsa.pub > %s/id_dsa.pub" % (path)) _exec_system("cat ~/.ssh/id_dsa.pub > %s/authorized_keys" % (path)) #_exec_system("cp %s/../rpms/libisal-2.14.0-1.el7.centos.x86_64.rpm %s/libisal-2.14.0-1.el7.centos.x86_64.rpm" % (path, path)) #_exec_system("cp %s/../rpms/libisal-devel-2.14.0-1.el7.centos.x86_64.rpm %s/libisal-devel-2.14.0-1.el7.centos.x86_64.rpm" % (path, path)) file_object = open("%s/rsyncd.conf" % (path), 'w') file_object.write("[sdfs]\n path=/tmp\n readonly=no\n list=yes") file_object.close() errno = _exec_system("docker build --rm -t %s %s" % (tpl, path)) if errno: _derror("Please install docker last version") exit(errno) _exec_system("rm %s/id_dsa" % (path)) _exec_system("rm %s/id_dsa.pub" % (path)) _exec_system("rm %s/authorized_keys" % (path)) _exec_system("rm %s/rsyncd.conf" % (path))
def docker_pull(): errno = _exec_system("docker pull centos:7") if errno: _derror("Please install docker last version") exit(errno)
def docker_add_config(): for i in dock_list: _exec_system("docker exec %s mkdir /boot" % (i), False) _exec_system( "scp /boot/config-`uname -r` `docker inspect --format='{{.NetworkSettings.IPAddress}}' %s`:/boot" % (i), False)
def docker_list(): for i in dock_list: _exec_system( "docker inspect --format='{{.NetworkSettings.IPAddress}}' %s" % (i), False)
def docker_remove(): for i in dock_list: _exec_system('docker rm %s' % (i))
def docker_stop(): for i in dock_list: _exec_system('docker stop %s' % (i))