Python REST / TeamCity backup

妖精的绣舞 提交于 2019-12-11 17:57:18

问题


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

here is my code:

#!/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()

cannot figure out why does it fail with

urllib2.HTTPError: HTTP Error 400: Bad Request

and REST logs show

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.

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

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

any pointers would be greatly appreciated


回答1:


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" 

even if urllib2 POST params are passed to Request.

oh well



来源:https://stackoverflow.com/questions/15980461/python-rest-teamcity-backup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!