def rename_volume(self, lv_name, new_name): "Change the name of an existing volume." try: self.cmd.execute('lvrename', self.vg_name, lv_name, new_name) except Exception, e: raise CustomException("Error: %s" % str(e))
class Http : def __init__(self) : self.http_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5", "Accept": "text/html"} self.timeout = 30 self.readTimeout = 50 socket.setdefaulttimeout(10.0) #get方式提交 def doGet(self, url, params, accept="text/html") : _url = "" if params not in (None, ''): _params = urllib.urlencode(params) if '?' in url > 0 : _url = "%s&%s" % (url,_params) else : _url = "%s?%s" % (url,_params) else : _url = url try: #time.sleep(self.readTimeout) req = urllib2.Request(_url, headers=self.http_headers) req.add_header('Accept', accept) req.get_method = lambda: 'GET' page = urllib2.urlopen(req)#, timeout=self.timeout) code = page.getcode() ret_data = page.read() return ret_data except urllib2.HTTPError, e: raise CustomException(e) except urllib2.URLError, e: raise CustomException(e)
def vg_size(self): "Return: vg totle_size ;vg free_size" try: out = self.cmd.execute("vgs | grep %s | awk {'print $6,$7'} " % self.vg_name) except Exception, e: raise CustomException("Error: %s" % str(e))
def __init__(self, confFile): if not os.path.isfile(confFile): raise CustomException("The file %s does not exist!" % confFile) self.confFile = confFile self.config = ConfigParser() #self.config.readfp(codecs.open(confFile, 'r',"utf_16")) self.config.read(confFile)
def remove_volume(self, lv_dir): """Creates a logical volume on the object's VG. param lv_dir: Name to lv dir """ try: self.cmd.execute('lvremove', '-f', lv_dir) except Exception, e: raise CustomException("Error: %s" % str(e))
def create_volume(self, name, size): """Creates a logical volume on the object's VG. param name: Name to use when creating Logical Volume param size: Size to use when creating Logical Volume """ try: self.cmd.execute('lvcreate', '-n', name, self.vg_name, '-L', size) except Exception, e: raise CustomException("Error: %s" % str(e))
def extend_volume(self, lv_name, new_size): "Extend the size of an existing volume." try: self.cmd.execute( 'lvextend', '-L', new_size, '%s/%s' % (self.vg_name, lv_name), ) except Exception, e: raise CustomException("Error: %s" % str(e))
def create_table(self, sql): '''创建数据库表:student''' if sql is not None and sql != '': if SHOW_SQL: print('执行sql:[{0}]'.format(sql)) self.cu.execute(sql) self.conn.commit() if SHOW_SQL: print('创建数据库表[student]成功!') else: raise CustomException( 'the [{0}] is empty or equal None!'.format(sql))
def save(self, sql, data): '''插入数据''' if sql is not None and sql != '': if data is not None: for d in data: if SHOW_SQL: print('执行sql:[{0}],参数:[{1}]'.format(sql, d)) self.cu.execute(sql, d) self.conn.commit() else: raise CustomException( 'the [{0}] is empty or equal None!'.format(sql))
def get_lvm_version(self): """Static method to get LVM version from system. :param root_helper: root_helper to use for execute :returns: version 3-tuple """ try: out = self.cmd.execute('env', 'LC_ALL=C', 'vgs', '--version') except Exception, e: raise CustomException("Error: %s" % str(e))
def drop_table(self, table): '''如果表存在,则删除表,如果表中存在数据的时候,使用该 方法的时候要慎用!''' if table is not None and table != '': sql = 'DROP TABLE IF EXISTS ' + table if SHOW_SQL: print('执行sql:[{0}]'.format(sql)) self.cu.execute(sql) self.conn.commit() if SHOW_SQL: print('删除数据库表[{0}]成功!'.format(table)) else: raise CustomException('table is empty or equal None!')
def get_lvm_path(self, lv_name): """Static method to get LVM version from system. :param root_helper: root_helper to use for execute :returns: lvm path """ try: cmd = ('lvdisplay | grep "LV Path" | grep %s | awk %s' % (lv_name + '$', ' \'{print $3}\' ')) out = os.popen(cmd, 'r').readline() #out = self.cmd.execute('lvdisplay | grep "LV Path" | grep %s | awk %s'%(lv_name+'$',' \'{print $3}\' ')) except Exception, e: raise CustomException("Error: %s" % str(e))
def get_all_physical_volumes(self, vg_name=None): """Static method to get all PVs on a system. :param root_helper: root_helper to use for execute :param vg_name: optional, gathers info for only the specified VG :returns: List of Dictionaries with PV info """ try: out = self.cmd.execute('env', 'LC_ALL=C', 'pvs', '--noheadings', '--unit=g', '-o', 'vg_name,name,size,free', '--separator', ':', '--nosuffix') except Exception, e: raise CustomException("Error: %s" % str(e))
def doPost(self, url, params, accept="text/plain") : _params = {} if params not in (None, ''): _params = urllib.urlencode(params) try: req = urllib2.Request(url, data=_params, headers=self.http_headers) req.add_header('Accept', accept) req.get_method = lambda: 'POST' page = urllib2.urlopen(req,timeout=self.timeout) code = page.getcode() ret_data = page.read() return ret_data except urllib2.HTTPError, e: raise CustomException(e)
def __init__(self, dbFile): '''获取到数据库的连接对象,参数为数据库文件的绝对路径 如果传递的参数是存在,并且是文件,那么就返回硬盘上面改 路径下的数据库文件的连接对象;否则,返回内存中的数据接 连接对象''' conn = sqlite3.connect(dbFile) if os.path.exists(dbFile) and os.path.isfile(dbFile): if SHOW_SQL: print('硬盘上面:[{0}]'.format(dbFile)) self.conn = conn '''该方法是获取数据库的游标对象,参数为数据库的连接对象 如果数据库的连接对象不为None,则返回数据库连接对象所创 建的游标对象;否则返回一个游标对象,该对象是内存中数据 库连接对象所创建的游标对象''' self.cu = conn.cursor() else: raise CustomException("The file %s create failed!" % dbFile)
def fetchone(self, sql, data): '''查询一条数据''' if sql is not None and sql != '': if data is not None: #Do this instead d = (data, ) if SHOW_SQL: print('执行sql:[{0}],参数:[{1}]'.format(sql, data)) self.cu.execute(sql, d) r = self.cu.fetchall() if len(r) > 0: for e in range(len(r)): return (r[e]) else: print('the [{0}] equal None!'.format(data)) else: raise CustomException( 'the [{0}] is empty or equal None!'.format(sql))
def fetchall(self, sql, data=None): '''查询所有数据''' reslut = [] if sql is not None and sql != '': if SHOW_SQL: print('执行sql:[{0}]'.format(sql)) if data is None: self.cu.execute(sql) else: self.cu.execute(sql, data) r = self.cu.fetchall() if len(r) > 0: for e in range(len(r)): reslut.append(r[e]) return reslut else: raise CustomException( 'the [{0}] is empty or equal None!'.format(sql))
def get_all_volumes(self, vg_name=None): """Static method to get all LV's on a system. :param vg_name: optional, gathers info for only the specified VG :returns: List of Dictionaries with LV info """ try: if vg_name is not None: out = self.cmd.execute('env', 'LC_ALL=C', 'lvs', '--noheadings', '--unit=g', '-o', 'vg_name,name,size', '--nosuffix', vg_name) else: out = self.cmd.execute('env', 'LC_ALL=C', 'lvs', '--noheadings', '--unit=g', '-o', 'vg_name,name,size', '--nosuffix') except Exception, e: raise CustomException("Error: %s" % str(e))
def doGet(self, url, params, accept="text/html") : _url = "" if params not in (None, ''): _params = urllib.urlencode(params) if '?' in url > 0 : _url = "%s&%s" % (url,_params) else : _url = "%s?%s" % (url,_params) else : _url = url try: #time.sleep(self.readTimeout) req = urllib2.Request(_url, headers=self.http_headers) req.add_header('Accept', accept) req.get_method = lambda: 'GET' page = urllib2.urlopen(req)#, timeout=self.timeout) code = page.getcode() ret_data = page.read() return ret_data except urllib2.HTTPError, e: raise CustomException(e)
if params not in (None, ''): _params = urllib.urlencode(params) try: req = urllib2.Request(url, data=_params, headers=self.http_headers) req.add_header('Accept', accept) req.get_method = lambda: 'POST' page = urllib2.urlopen(req,timeout=self.timeout) code = page.getcode() ret_data = page.read() return ret_data except urllib2.HTTPError, e: raise CustomException(e) except urllib2.URLError, e: raise CustomException(e) except socket.timeout, e: raise CustomReqTimeoutException(e) def download(self, url, fileName, params=None) : try: _url = "" if params not in (None, ''): _params = urllib.urlencode(params) if '?' in url > 0 : _url = "%s&%s" % (url,_params) else : _url = "%s?%s" % (url,_params) else : _url = url #print _url
def create_vg(self, vg_name, pv_list): "Create volume group." try: self.cmd.execute('vgcreate', self.vg_name, ' '.join(pv_list)) except Exception, e: raise CustomException("Error: %s" % str(e))