Omitting deployment variables in a Python script
Asked by Landon Cooper:

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 = ()
```


Answers:
author: Daniel Santos
body: Hi [@Landon Cooper](/t5/user/viewprofilepage/user-id/867754),

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)

comments: author: Landon Cooper
body: Hi [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125)  

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.


author: Daniel Santos
body: 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.


author: Landon Cooper
body: [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125) 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!


author: Daniel Santos
body: 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.


Tags: python