本文介绍了Python REST/TeamCity备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发python脚本来运行REST备份过程,如 http://confluence.jetbrains.com/display/TW/REST+API+Plugin#RESTAPIPlugin-DataBackup

I am trying to develop a python script to run REST backup procedure as shown in http://confluence.jetbrains.com/display/TW/REST+API+Plugin#RESTAPIPlugin-DataBackup

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib
import urllib2

"""
Data Backup
+++++++++++
Start backup: POST http://teamcity:8111/httpAuth/app/rest/
server/backup?includeConfigs=true&includeDatabase=true&
includeBuildLogs=true&fileName=<fileName>
where <fileName> is the prefix of the file to save backup to.
The file will be created in the default backup directory (see more).
"""

url = "http://localhost/httpAuth/app/rest/server/backup"

# === EDITED code = now working (see my answer below) ===
url = "http://localhost/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&
includeBuildLogs=true&fileName=TCBACKUP"
# === /EDITED code (see my answer below) ===

params = {
'fileName':'TCBACKUP',
'includeBuildLogs':'false',
'includeDatabase':'true',
'includeConfigs':'true'
}

post_data = urllib.urlencode(params)

req = urllib2.Request(url, post_data, headers={'Content-Type': 'application/xml'})

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, 'user', 'pass')

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)

urllib2.install_opener(opener)

handler = urllib2.urlopen(req)
handler.close()

无法弄清为什么会失败

urllib2.HTTPError: HTTP Error 400: Bad Request

和REST日志显示

WARN [hon-urllib/2.7 ] - est.jersey.ExceptionMapperUtil -
Error 'Invalid request. Please check the request URL and
data are correct.' for request http://server/app/rest/server/backup.
Sending Bad Request error in response: jetbrains.buildServer.server.rest.errors.BadRequestException:
No target file name specified.

没有目标文件名?真的吗?打印post_data给我:

No target file name? Really?Printing post_data gives me:

includeConfigs=true&includeDatabase=true&includeBuildLogs=false&fileName=TCBACKUP

任何指针将不胜感激

推荐答案

Python代码很好-TC文档中唯一不清楚的唯一内容-URL仍必须构建为

Python code was good - the only thing that was not exactly clear from TC docs - the URL still has to be built as

"POST http://teamcity:8111/httpAuth/app/rest/server/backup?
includeConfigs=true&includeDatabase=true&includeBuildLogs=true&
fileName=backup"

即使将urllib2 POST参数传递给Request.

even if urllib2 POST params are passed to Request.

好吧

这篇关于Python REST/TeamCity备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:20