I am building s simple python script to check a canary service is up and validate a bamboo build # . I have all the script logic complete except for one condition. The script will be used in multiple projects. Some require a canary credential ( auth ) while others do not. I can get one or the other working but not account for both. Is there anyway to ignore a deploy variable ( using a python script ) ?
canary_username = sys.argv[1]
canary_password = sys.argv[2]
canary_host = sys.argv[3]
bamboo_deploy_version = os.environ["bamboo_deploy_version"]
bamboo_build_number = os.environ["bamboo_buildNumber"]
s = requests.Session()
s.auth = () #ONLY works WITHOUT credentials
s.auth = (canary_username, canary_password) #DOES NOT WORK WITHOUT credentials
# TODO: ensure all canary services require authentication
if canary_username == "null":
s.auth = ()
Hi @Landon Cooper,
Thank you for sharing all those details, but I'm still not quite clear of what you are trying to achieve.
What do you mean by:
Is there anyway to ignore a deploy variable ( using a python script ) ?
Are you trying to omit canary_username and canary_password ?
Have you added the variable canary_password in your deployment environment or in the global variables page? (the name of this variable should trigger the feature in Bamboo that hides its content from the logs)
Hi @Daniel Santos
thanks for taking a look at this ( I'll try to add more detail). So the canary check is a rest call to an end point. In some projects I need:
http://USER:PASSWORD@SERVER:8181/canary/build/endpoint
Others there is NO credential for the canary call:
http://SERVER:8181/canary/build/endpoint
So in the python script I would like to simply use a condition to define the s.auth for either case:
i.e.
if canary_username != "null":
s.auth = (canary_username, canary_password) # s.auth will default to () if not defined)
The problem is that there is error when attempting to have the python script modify or replace, or omit the sys_argv[1] ( bamboo variable). I've tried casting the variable as a specific type, putting it in a function, creating a second "buffer" var as a replacement, etc. I would like the script to use the variable when a condition is true and omit it when not true.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't know how you plan to call your script for each plan. If you intend to call it differently depending on what variables you have available, one of the possible approaches is checking the number of arguments.
The following inline script will show you how you could check the number or arguments to decide which authentication to use:
#!/usr/bin/python
import os
import sys
if len(sys.argv)==2:
canary_host =sys.argv[1]
# Just a random request with NO authentication
os.system("curl -X GET \'http://"+canary_host+":8085/bamboo/rest/api/latest/server\'")
elif len(sys.argv)==4:
canary_username = sys.argv[1]
canary_password = sys.argv[2]
canary_host = sys.argv[3]
# Just a random request using authentication
os.system("curl -u "+canary_username+":"+canary_password+" -X GET \'http://"+canary_host+":8085/bamboo/rest/api/latest/search/users?searchTerm=a\'")
else:
print "len(sys.argv)={}", len(sys.argv)
print "Invalid arguments ERROR"
calling the script with NO authentication:
authentication.py $bamboo_canary_host
calling the script with authentication:
authentication.py $bamboo_canary_username $bamboo_canary_password $canary_canary_host
you could even call the script with empty variables and it would call the authentication method without user and password:
authentication.py $bamboo_empty_var $bamboo_empty_var $bamboo_canary_host
Please let me know if this make sense to you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Daniel Santos I thought about a form of this but was not sure how to go about it. I will try it and let you know. Thanks for your thoughts!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is a really ugly example just used as a test to try understanding what problems you were facing. I shared that small script just to give you some ideas. The real solution will depend on your company process, if you will declare or not the variables for all plans and how that script will be called.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.