Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Omitting deployment variables in a Python script

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

 

1 answer

0 votes
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jan 14, 2019

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. 

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jan 15, 2019

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.

Like Landon Cooper likes this

@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!

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jan 15, 2019 • edited

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.

Like Landon Cooper likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events