本文整理汇总了Python中zstacklib.utils.jsonobject.dumps函数的典型用法代码示例。如果您正苦于以下问题:Python dumps函数的具体用法?Python dumps怎么用?Python dumps使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dumps函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scan_host
def scan_host(self, req):
rsp = ScanRsp()
success = 0
cmd = jsonobject.loads(req[http.REQUEST_BODY])
for i in range(0, cmd.times):
if shell.run("nmap -sP -PI %s | grep 'Host is up'" % cmd.ip) == 0:
success += 1
time.sleep(cmd.interval)
if success == cmd.successTimes:
rsp.result = self.RET_SUCCESS
return jsonobject.dumps(rsp)
if success == 0:
rsp.result = self.RET_FAILURE
return jsonobject.dumps(rsp)
# WE SUCCEED A FEW TIMES, IT SEEMS THE CONNECTION NOT STABLE
success = 0
for i in range(0, cmd.successTimes):
if shell.run("nmap -sP -PI %s | grep 'Host is up'" % cmd.ip) == 0:
success += 1
time.sleep(cmd.successInterval)
if success == cmd.successTimes:
rsp.result = self.RET_SUCCESS
return jsonobject.dumps(rsp)
rsp.result = self.RET_NOT_STABLE
return jsonobject.dumps(rsp)
示例2: create_empty_volume
def create_empty_volume(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = CreateEmptyVolumeResponse()
try:
dirname = os.path.dirname(cmd.installUrl)
if not os.path.exists(dirname):
os.makedirs(dirname)
linux.qcow2_create(cmd.installUrl, cmd.size)
except Exception as e:
logger.warn(linux.get_exception_stacktrace())
rsp.error = 'unable to create empty volume[uuid:%s, name:%s], %s' % (cmd.uuid, cmd.name, str(e))
rsp.success = False
return jsonobject.dumps(rsp)
meta = VolumeMeta()
meta.account_uuid = cmd.accountUuid
meta.hypervisor_type = cmd.hypervisorType
meta.name = cmd.name
meta.uuid = cmd.volumeUuid
meta.size = cmd.size
meta_path = self._json_meta_file_name(cmd.installUrl)
with open(meta_path, 'w') as fd:
fd.write(jsonobject.dumps(meta, pretty=True))
self._set_capacity_to_response(cmd.uuid, rsp)
logger.debug('successfully create empty volume[uuid:%s, name:%s, size:%s] at %s' % (cmd.uuid, cmd.name, cmd.size, cmd.installUrl))
return jsonobject.dumps(rsp)
示例3: setup_heartbeat_file
def setup_heartbeat_file(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = SetupMountablePrimaryStorageHeartbeatResponse()
for hb in cmd.heartbeatFilePaths:
hb_dir = os.path.dirname(hb)
mount_path = os.path.dirname(hb_dir)
if not linux.is_mounted(mount_path):
rsp.error = '%s is not mounted, setup heartbeat file[%s] failed' % (mount_path, hb)
rsp.success = False
return jsonobject.dumps(rsp)
for hb in cmd.heartbeatFilePaths:
t = self.heartbeat_timer.get(hb, None)
if t:
t.cancel()
hb_dir = os.path.dirname(hb)
if not os.path.exists(hb_dir):
os.makedirs(hb_dir, 0755)
t = thread.timer(cmd.heartbeatInterval, self._heartbeat_func, args=[hb], stop_on_exception=False)
t.start()
self.heartbeat_timer[hb] = t
logger.debug('create heartbeat file at[%s]' % hb)
return jsonobject.dumps(rsp)
示例4: connect
def connect(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = ConnectResponse()
# page table extension
if shell.run('lscpu | grep -q -w GenuineIntel') == 0:
new_ept = False if cmd.pageTableExtensionDisabled else True
rsp.error = self._set_intel_ept(new_ept)
if rsp.error is not None:
rsp.success = False
return jsonobject.dumps(rsp)
self.host_uuid = cmd.hostUuid
self.config[kvmagent.HOST_UUID] = self.host_uuid
self.config[kvmagent.SEND_COMMAND_URL] = cmd.sendCommandUrl
Report.serverUuid = self.host_uuid
Report.url = cmd.sendCommandUrl
logger.debug(http.path_msg(self.CONNECT_PATH, 'host[uuid: %s] connected' % cmd.hostUuid))
rsp.libvirtVersion = self.libvirt_version
rsp.qemuVersion = self.qemu_version
# create udev rule
self.handle_usb_device_events()
ignore_msrs = 1 if cmd.ignoreMsrs else 0
shell.run("/bin/echo %s > /sys/module/kvm/parameters/ignore_msrs" % ignore_msrs)
vm_plugin.cleanup_stale_vnc_iptable_chains()
apply_iptables_result = self.apply_iptables_rules(cmd.iptablesRules)
rsp.iptablesSucc = apply_iptables_result
return jsonobject.dumps(rsp)
示例5: ping
def ping(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = PingRsp()
facts = bash_o('ceph -s -f json')
mon_facts = jsonobject.loads(facts)
found = False
for mon in mon_facts.monmap.mons:
if cmd.monAddr in mon.addr:
found = True
break
if not found:
rsp.success = False
rsp.failure = "MonAddrChanged"
rsp.error = 'The mon addr is changed on the mon server[uuid:%s], not %s anymore.' \
'Reconnect the ceph primary storage' \
' may solve this issue' % (cmd.monUuid, cmd.monAddr)
return jsonobject.dumps(rsp)
create_img = shell.ShellCmd('rbd create %s --image-format 2 --size 1' % cmd.testImagePath)
create_img(False)
if create_img.return_code != 0:
rsp.success = False
rsp.failure = 'UnableToCreateFile'
rsp.error = "%s %s" % (create_img.stderr, create_img.stdout)
else:
rm_img = shell.ShellCmd('rbd rm %s' % cmd.testImagePath)
rm_img(False)
return jsonobject.dumps(rsp)
示例6: migrate_volume_segment
def migrate_volume_segment(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = AgentResponse()
src_install_path = self._normalize_install_path(cmd.srcInstallPath)
dst_install_path = self._normalize_install_path(cmd.dstInstallPath)
src_size = self._get_file_size(src_install_path)
dst_size = self._get_dst_volume_size(dst_install_path, cmd.dstMonHostname, cmd.dstMonSshUsername, cmd.dstMonSshPassword, cmd.dstMonSshPort)
if dst_size > src_size:
if cmd.isXsky:
# xsky / ceph -> xsky, size must be equal
rsp.success = False
rsp.error = "Failed to migrate volume segment because dst size: %s > src size: %s" % (dst_size, src_size)
return jsonobject.dumps(rsp)
elif isXsky() == False:
# ceph -> ceph, don't check size
rsp.success = True
else:
# xsky -> ceph, not supported
rsp.success = False
rsp.error = "Failed to migrate volume segment because xsky migrate to ceph is not supported now"
return jsonobject.dumps(rsp)
if dst_size < src_size:
ret = self._resize_dst_volume(dst_install_path, src_size, cmd.dstMonHostname, cmd.dstMonSshUsername, cmd.dstMonSshPassword, cmd.dstMonSshPort)
if ret != 0:
rsp.success = False
rsp.error = "Failed to resize volume before migrate."
return jsonobject.dumps(rsp)
ret = self._migrate_volume_segment(cmd.parentUuid, cmd.resourceUuid, cmd.srcInstallPath, cmd.dstInstallPath, cmd.dstMonHostname, cmd.dstMonSshUsername, cmd.dstMonSshPassword, cmd.dstMonSshPort)
if ret != 0:
rsp.success = False
rsp.error = "Failed to migrate volume segment from one ceph primary storage to another."
self._set_capacity_to_response(rsp)
return jsonobject.dumps(rsp)
示例7: ping
def ping(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = PingRsp()
facts = bash_o('ceph -s -f json')
mon_facts = jsonobject.loads(facts)
found = False
for mon in mon_facts.monmap.mons:
if cmd.monAddr in mon.addr:
found = True
break
if not found:
rsp.success = False
rsp.failure = "MonAddrChanged"
rsp.error = 'The mon addr is changed on the mon server[uuid:%s], not %s anymore.' \
'Reconnect the ceph primary storage' \
' may solve this issue' % (cmd.monUuid, cmd.monAddr)
return jsonobject.dumps(rsp)
pool, objname = cmd.testImagePath.split('/')
create_img = shell.ShellCmd("echo zstack | rados -p '%s' put '%s' -" % (pool, objname))
create_img(False)
if create_img.return_code != 0:
rsp.success = False
rsp.failure = 'UnableToCreateFile'
rsp.error = "%s %s" % (create_img.stderr, create_img.stdout)
else:
shell.run("rados -p '%s' rm '%s'" % (pool, objname))
return jsonobject.dumps(rsp)
示例8: set_bootstrap_info
def set_bootstrap_info(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
info = jsonobject.dumps(cmd.info, True)
socket_path = cmd.socketPath
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1)
buf_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
info_len = len(info)
if info_len < buf_size:
# as there is no fflush() in python, we have to create a message
# matching to the socket buffer to force it to send the message immediately
padding_len = buf_size - info_len
padding = ' ' * padding_len
info = '%s%s' % (info, padding)
try:
logger.debug('send appliance vm bootstrap info to %s\n%s' % (socket_path, info))
s.connect(socket_path)
s.sendall(info)
finally:
s.close()
rsp = PrepareBootstrapInfoRsp()
return jsonobject.dumps(rsp)
示例9: _add_zone
def _add_zone(zone, zone_duplication):
action = api_actions.CreateZoneAction()
action.sessionUuid = session_uuid
if zone_duplication == 0:
action.name = zone.name_
action.description = zone.description__
if zone.uuid__:
action.resourceUuid = zone.uuid__
else:
action.name = generate_dup_name(zone.name_, zone_duplication, 'z')
action.description = generate_dup_name(zone.description__, zone_duplication, 'zone')
try:
evt = action.run()
deploy_logger(jsonobject.dumps(evt))
zinv = evt.inventory
except:
exc_info.append(sys.exc_info())
if xmlobject.has_element(zone, 'backupStorageRef'):
for ref in xmlobject.safe_list(zone.backupStorageRef):
bss = res_ops.get_resource(res_ops.BACKUP_STORAGE, session_uuid, name=ref.text_)
bs = get_first_item_from_list(bss, 'Backup Storage', ref.text_, 'attach backup storage to zone')
action = api_actions.AttachBackupStorageToZoneAction()
action.sessionUuid = session_uuid
action.backupStorageUuid = bs.uuid
action.zoneUuid = zinv.uuid
try:
evt = action.run()
deploy_logger(jsonobject.dumps(evt))
except:
exc_info.append(sys.exc_info())
示例10: download_image
def download_image(self, req):
#TODO: report percentage to mgmt server
def percentage_callback(percent, url):
logger.debug('Downloading %s ... %s%%' % (url, percent))
def use_wget(url, name, workdir, timeout):
return linux.wget(url, workdir=workdir, rename=name, timeout=timeout, interval=2, callback=percentage_callback, callback_data=url)
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = DownloadResponse()
supported_schemes = [self.URL_HTTP, self.URL_HTTPS, self.URL_FILE]
if cmd.urlScheme not in supported_schemes:
rsp.success = False
rsp.error = 'unsupported url scheme[%s], SimpleSftpBackupStorage only supports %s' % (cmd.urlScheme, supported_schemes)
return jsonobject.dumps(rsp)
path = os.path.dirname(cmd.installPath)
if not os.path.exists(path):
os.makedirs(path, 0777)
image_name = os.path.basename(cmd.installPath)
install_path = cmd.installPath
timeout = cmd.timeout if cmd.timeout else 7200
if cmd.urlScheme in [self.URL_HTTP, self.URL_HTTPS]:
try:
ret = use_wget(cmd.url, image_name, path, timeout)
if ret != 0:
rsp.success = False
rsp.error = 'http/https download failed, [wget -O %s %s] returns value %s' % (image_name, cmd.url, ret)
return jsonobject.dumps(rsp)
except linux.LinuxError as e:
traceback.format_exc()
rsp.success = False
rsp.error = str(e)
return jsonobject.dumps(rsp)
elif cmd.urlScheme == self.URL_FILE:
src_path = cmd.url.lstrip('file:')
src_path = os.path.normpath(src_path)
if not os.path.isfile(src_path):
raise Exception('cannot find the file[%s]' % src_path)
logger.debug("src_path is: %s" % src_path)
shell.call('yes | cp %s %s' % (src_path, install_path))
os.chmod(cmd.installPath, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)
image_format = bash_o("qemu-img info %s | grep -w '^file format' | awk '{print $3}'" % install_path).strip('\n')
size = os.path.getsize(install_path)
md5sum = 'not calculated'
logger.debug('successfully downloaded %s to %s' % (cmd.url, install_path))
(total, avail) = self.get_capacity()
rsp.md5Sum = md5sum
rsp.actualSize = size
rsp.size = linux.qcow2_virtualsize(install_path)
rsp.totalCapacity = total
rsp.availableCapacity = avail
rsp.format = image_format
return jsonobject.dumps(rsp)
示例11: create_root_volume_from_template
def create_root_volume_from_template(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
rsp = CreateRootVolumeFromTemplateResponse()
if not os.path.exists(cmd.templatePathInCache):
rsp.error = self.ERR_UNABLE_TO_FIND_IMAGE_IN_CACHE
rsp.success = False
return jsonobject.dumps(rsp)
try:
dirname = os.path.dirname(cmd.installUrl)
if not os.path.exists(dirname):
os.makedirs(dirname, 0775)
linux.qcow2_clone(cmd.templatePathInCache, cmd.installUrl)
logger.debug('successfully create root volume[%s] from template in cache[%s]' % (cmd.installUrl, cmd.templatePathInCache))
meta = VolumeMeta()
meta.account_uuid = cmd.accountUuid
meta.hypervisor_type = cmd.hypervisorType
meta.name = cmd.name
meta.uuid = cmd.volumeUuid
meta.size = os.path.getsize(cmd.templatePathInCache)
meta_path = self._json_meta_file_name(cmd.installUrl)
with open(meta_path, 'w') as fd:
fd.write(jsonobject.dumps(meta, pretty=True))
self._set_capacity_to_response(cmd.uuid, rsp)
logger.debug('successfully create root volume[%s] from template in cache[%s]' % (cmd.installUrl, cmd.templatePathInCache))
except Exception as e:
content = traceback.format_exc()
logger.warn(content)
err = 'unable to clone qcow2 template[%s] to %s' % (cmd.templatePathInCache, cmd.installUrl)
rsp.error = err
rsp.success = False
return jsonobject.dumps(rsp)
示例12: remove_vip
def remove_vip(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
for vip in cmd.vips:
linux.delete_vip_by_ip_if_exists(vip.ip)
logger.debug('removed vip %s' % jsonobject.dumps(vip))
rsp = RemoveVipRsp()
return jsonobject.dumps(rsp)
示例13: create_vip
def create_vip(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
for vip in cmd.vips:
linux.create_vip_if_not_exists(vip.ownerEthernetMac, vip.ip, vip.netmask)
logger.debug('created vip %s' % jsonobject.dumps(vip))
rsp = CreateVipRsp()
return jsonobject.dumps(rsp)
示例14: ping
def ping(self, req):
cmd = jsonobject.loads(req[http.REQUEST_BODY])
facts = bash_o('ceph -s -f json')
mon_facts = jsonobject.loads(facts)
found = False
for mon in mon_facts.monmap.mons:
if cmd.monAddr in mon.addr:
found = True
break
rsp = PingRsp()
if not found:
rsp.success = False
rsp.failure = "MonAddrChanged"
rsp.error = 'The mon addr is changed on the mon server[uuid:%s], not %s anymore.' \
'Reconnect the ceph primary storage' \
' may solve this issue' % (cmd.monUuid, cmd.monAddr)
return jsonobject.dumps(rsp)
def retry(times=3, sleep_time=3):
def wrap(f):
@functools.wraps(f)
def inner(*args, **kwargs):
for i in range(0, times):
try:
return f(*args, **kwargs)
except Exception as e:
logger.error(e)
time.sleep(sleep_time)
rsp.error = ("Still failed after retry. Below is detail:\n %s" % e)
return inner
return wrap
@retry()
def doPing():
# try to delete test file, ignore the result
pool, objname = cmd.testImagePath.split('/')
bash_r("rados -p '%s' rm '%s'" % (pool, objname))
r, o, e = bash_roe("echo zstack | timeout 60 rados -p '%s' put '%s' -" % (pool, objname))
if r != 0:
rsp.success = False
rsp.failure = "UnableToCreateFile"
if r == 124:
# timeout happened
rsp.error = 'failed to create heartbeat object on ceph, timeout after 60s, %s %s' % (e, o)
raise Exception(rsp.error)
else:
rsp.error = "%s %s" % (e, o)
doPing()
self._set_capacity_to_response(rsp)
return jsonobject.dumps(rsp)
示例15: callback
def callback(self, req):
rsp = jsonobject.loads(req[http.REQUEST_BODY])
print jsonobject.dumps(rsp)
cmd = vm_plugin.RebootVmCmd()
cmd.uuid = self.uuid
cmd.timeout = 30
url = kvmagent._build_url_for_test([vm_plugin.KVM_REBOOT_VM_PATH])
rsp = http.json_dump_post(url, cmd, headers={http.TASK_UUID:uuidhelper.uuid(), http.CALLBACK_URI:self.CALLBACK_URL2})