2013-04-12 29 views
0

我試圖建立一個python腳本來運行REST備份過程如圖http://confluence.jetbrains.com/display/TW/REST+API+Plugin#RESTAPIPlugin-DataBackupPython的REST/TeamCity的備份

這裏是我的代碼:

#!/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給我:

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

任何指針將不勝感激

回答

0

Python代碼是好的 - 這是不是從TC文檔十分清楚的唯一的事情 - URL仍有待建成

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

即使urllib2 POST params傳遞給Request。

不錯哦