Skip to content

Instantly share code, notes, and snippets.

@Rehzende
Last active April 27, 2021 00:17
Show Gist options
  • Save Rehzende/d2725dc83a8c5c8cc0212c4621c33341 to your computer and use it in GitHub Desktop.
Save Rehzende/d2725dc83a8c5c8cc0212c4621c33341 to your computer and use it in GitHub Desktop.
Pyhton script to deploying Azure Web Jobs
import requests, time, argparse, zipfile, sys
parser = argparse.ArgumentParser(description='Azure Webjob deploy script!')
parser.add_argument("--AUTH", required=True, type=str, help="Basic Auth, base64. Ex: 'Basic LjsdI...JGSJH")
parser.add_argument("--APPNAME", required=True, type=str, help="AppService Name")
parser.add_argument("--WEBJOBNAME", required=True, type=str, help="WebJob Name")
parser.add_argument("--ZIPFILEPATH", required=True, type=str, help="Zip file to deploy")
args = parser.parse_args()
AUTH = args.AUTH
APPNAME = args.APPNAME
WEBJOBNAME = args.WEBJOBNAME
ZIPFILEPATH = args.ZIPFILEPATH
URL_BASE = "https://" + APPNAME + ".scm.azurewebsites.net/api/continuouswebjobs/" + WEBJOBNAME
def chekZipValid():
print("Testing zip file: %s" % ZIPFILEPATH)
the_zip_file = zipfile.ZipFile(ZIPFILEPATH)
ret = the_zip_file.testzip()
if ret is not None:
print("Arquivo Zip inválido: %s" % ret)
sys.exit(1)
else:
return print("Arquivo ZIP valido.")
def getStatus():
payload={}
headers = {
'Cache-Control': 'no-cache',
'Authorization': AUTH
}
response = requests.request("GET", URL_BASE, headers=headers, data=payload)
data = response.json()
return data['status']
def jobStop():
payload={}
headers = {
'Authorization': AUTH
}
URL = URL_BASE + "/stop"
response = requests.request("POST", URL, headers=headers, data=payload)
status_code = response.status_code
if status_code == 200:
return status_code
else:
print(status_code)
def jobStart():
payload={}
headers = {
'Authorization': AUTH
}
URL = URL_BASE + "/start"
response = requests.request("POST", URL, headers=headers, data=payload)
data = response.text
return data
def jobDeploy():
data = open(ZIPFILEPATH, 'rb').read()
headers = {
'Content-Type': 'application/zip',
'Content-Disposition': 'attachement; filename=run.sh',
'Authorization': AUTH
}
response = requests.request("PUT", URL_BASE, headers=headers, data=data)
status_code = response.status_code
return status_code
def main():
chekZipValid()
jobStop()
time.sleep(3)
success= False
retry= 0
while (success != True) and ( retry <= 5):
status = getStatus()
if status == "Stopped":
print("Realizando o Deploy")
status_code = jobDeploy()
if status_code == 200:
success= True
print("Iniciando o WebJob: " + WEBJOBNAME)
jobStart()
time.sleep(5)
status = getStatus()
if status == "Running":
print("WebJob status: " + status)
break
else:
time.sleep(5)
retry = retry + 1
if __name__ == '__main__':
main()
@Rehzende
Copy link
Author

usage:

py webjob_deploy.py --help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment