def import_block(request): try: #验证input configs = get_config() block_name = request.POST.get('name') compute_group = request.POST.get('compute_group') if block_name == '': raise ValueError('block name field is empty') if compute_group == '': raise ValueError('compute group field is empty') #筛选出一个合适的host res = ansible.runner.Runner(module_name='ping', module_args='', pattern=compute_group, forks=10).run() compute_nodes = res['contacted'] des_node = compute_nodes.keys()[int(random.random() * len(compute_nodes))] #验证block 是否存在 args = 'ls ' + configs['block_path'] + '/' + block_name res = ansible.runner.Runner(module_name='shell', module_args=args, pattern=des_node, forks=10).run() if res['contacted'][des_node]['stderr'] != '': raise ValueError('the block is not exist!') #如果已经导入过返回ERROR result = Storage.objects.filter(uuid=block_name) if len(result) > 0: raise ValueError('已经导入过,不能重复导入') #得到block 的虚拟大小、实际使用大小、类型 args = '/usr/bin/qemu-img info ' + configs['block_path'] + '/' + block_name res = ansible.runner.Runner(module_name='shell', module_args=args, pattern=des_node, forks=10).run() d = {} for s in res['contacted'][des_node]['stdout'].split('\n'): item = s.split(":") if item[0].strip() == 'file format' or item[0].strip() == 'virtual size' \ or item[0].strip() == 'disk size': key, value = item d[key.strip()] = value.strip() #如果成功就将此条数据,插入数据库,失败返回 ERROR block = Storage() block.block_name = block_name block.block_path = configs['block_path'] + '/' + block_name block.size = d['virtual size'].split('(')[0] block.type = d['file format'] block.used_size = d['disk size'] block.crated_at = timezone.now() block.is_mounted = False block.uuid = block_name block.compute_group = compute_group block.save() #log name = '' mesStr = request.session.get('username', 'root') + ' import block ' + d['virtual size'].split('(')[0] + ' named ' + block_name logger = getLogger(name) logger.info(mesStr) return HttpResponseRedirect(reverse('blockmanager:index')) except Exception, e: name = request.session.get('username', 'root') logger = getLogger(name) logger.error(e) return HttpResponse(e)
def create_block(request): try: #验证input configs = get_config() block_name = request.POST['name'] if block_name == '': raise ValueError('block name field is empty') size = request.POST['size'] if(size == ''): raise ValueError('size field is empty') size = int(size) block_type = request.POST['type'] if block_type == '': raise ValueError('block_type field is empty') if block_type not in configs['block_type']: raise ValueError('block type is undefined') compute_group = request.POST.get('compute_group') if compute_group == '': raise ValueError('compute group field is empty') #筛选出一个合适的host res = ansible.runner.Runner(module_name='ping', module_args='', pattern=compute_group, forks=10).run() compute_nodes = res['contacted'] des_node = compute_nodes.keys()[int(random.random() * len(compute_nodes))] #得到block 的uuid, 并确保唯一性 block_uuid = uuid.uuid1().hex #调用API,在筛选出的host上创建block if block_type == 'qcow2': args = '/usr/bin/qemu-img create -f qcow2 -o preallocation=metadata ' + configs['block_path'] + '/' + block_uuid + '.qcow2 ' + str(size) + 'G' res = ansible.runner.Runner(module_name='shell', module_args=args, pattern=des_node, forks=10).run() if res['contacted'][des_node]['stderr'] != '': raise ValueError(res['contacted'][des_node]['stderr']) #如果成功就将此条数据,插入数据库,失败返回 ERROR block = Storage() block.block_name = block_name block.block_path = configs['block_path'] + '/' + block_uuid + '.' + block_type block.size = str(size) + 'G' block.type = block_type block.crated_at = timezone.now() block.is_mounted = False block.uuid = block_uuid block.compute_group = compute_group block.save() #log name = '' mesStr = request.session.get('username', 'root') + ' create block ' + str(size) + 'G named ' + block_name logger = getLogger(name) logger.info(mesStr) #success return HttpResponseRedirect(reverse('blockmanager:index')) except Exception, e: name = request.session.get('username', 'root') logger = getLogger(name) logger.error(e) return HttpResponse(e)