Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from mesosphere/marathon-http-auth
Browse files Browse the repository at this point in the history
Marathon http auth
  • Loading branch information
brndnmtthws committed Feb 9, 2016
2 parents 12d34eb + 0302901 commit 8e3b8ae
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
19 changes: 12 additions & 7 deletions bluegreen_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def query_yes_no(question, default="yes"):

def get_app_info(args, deployment_group, alt_port):
url = args.marathon + "/v2/apps"
response = requests.get(url)
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
apps = response.json()
existing_app = None
Expand Down Expand Up @@ -128,12 +128,12 @@ def find_tasks_to_kill(tasks, hostports):
def check_if_tasks_drained(args, app, existing_app):
time.sleep(args.step_delay)
url = args.marathon + "/v2/apps" + existing_app['id']
response = requests.get(url)
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
existing_app = response.json()['app']

url = args.marathon + "/v2/apps" + app['id']
response = requests.get(url)
response = requests.get(url, auth=get_marathon_auth_params(args))
response.raise_for_status()
app = response.json()['app']

Expand Down Expand Up @@ -252,7 +252,8 @@ def check_if_tasks_drained(args, app, existing_app):
logger.info("About to delete old app {}".format(existing_app['id']))
if args.force or query_yes_no("Continue?"):
url = args.marathon + "/v2/apps" + existing_app['id']
response = requests.delete(url)
response = requests.delete(url,
auth=get_marathon_auth_params(args))
response.raise_for_status()
return True
else:
Expand All @@ -267,15 +268,17 @@ def check_if_tasks_drained(args, app, existing_app):
url = args.marathon + "/v2/apps" + app['id']
data = json.dumps({'instances': instances})
headers = {'Content-Type': 'application/json'}
response = requests.put(url, headers=headers, data=data)
response = requests.put(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()

# Scale old app down
logger.info("Scaling down old app by {} instances"
.format(len(tasks_to_kill)))
data = json.dumps({'ids': tasks_to_kill})
url = args.marathon + "/v2/tasks/delete?scale=true"
response = requests.post(url, headers=headers, data=data)
response = requests.post(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()

return check_if_tasks_drained(args,
Expand All @@ -289,7 +292,8 @@ def start_deployment(args, app, existing_app, resuming):
url = args.marathon + "/v2/apps"
data = json.dumps(app)
headers = {'Content-Type': 'application/json'}
response = requests.post(url, headers=headers, data=data)
response = requests.post(url, headers=headers, data=data,
auth=get_marathon_auth_params(args))
response.raise_for_status()
if existing_app is not None:
return check_if_tasks_drained(args,
Expand Down Expand Up @@ -419,6 +423,7 @@ def get_arg_parser():
action="store_true"
)
parser = set_logging_args(parser)
parser = set_marathon_auth_args(parser)
return parser


Expand Down
24 changes: 24 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ def setup_logging(logger, syslog_socket, log_format):
logger.addHandler(syslogHandler)


def set_marathon_auth_args(parser):
parser.add_argument("--marathon-auth-credential-file",
help="Path to file containing a user/pass for "
"the Marathon HTTP API in the format of 'user:pass'."
)

return parser


def get_marathon_auth_params(args):
if args.marathon_auth_credential_file is None:
return None

line = None
with open(args.marathon_auth_credential_file, 'r') as f:
line = f.readline().rstrip('\r\n')

if line is not None:
splat = line.split(':')
return (splat[0], splat[1])

return None


def set_logging_args(parser):
default_log_socket = "/dev/log"
if sys.platform == "darwin":
Expand Down
15 changes: 10 additions & 5 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,13 @@ def __eq__(self, other):

class Marathon(object):

def __init__(self, hosts, health_check):
def __init__(self, hosts, health_check, auth):
# TODO(cmaloney): Support getting master list from zookeeper
self.__hosts = hosts
self.__health_check = health_check
self.__auth = auth

def api_req_raw(self, method, path, body=None, **kwargs):
def api_req_raw(self, method, path, auth, body=None, **kwargs):
for host in self.__hosts:
path_str = os.path.join(host, 'v2')

Expand All @@ -473,6 +474,7 @@ def api_req_raw(self, method, path, body=None, **kwargs):
response = requests.request(
method,
path_str,
auth=auth,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json'
Expand All @@ -491,7 +493,7 @@ def api_req_raw(self, method, path, body=None, **kwargs):
return response

def api_req(self, method, path, **kwargs):
return self.api_req_raw(method, path, **kwargs).json()
return self.api_req_raw(method, path, self.__auth, **kwargs).json()

def create(self, app_json):
return self.api_req('POST', ['apps'], app_json)
Expand Down Expand Up @@ -529,7 +531,7 @@ def get_event_stream(self):
url = self.__hosts[0]+"/v2/events"
logger.info(
"SSE Active, trying fetch events from from {0}".format(url))
return SSEClient(url)
return SSEClient(url, auth=self.__auth)


def has_group(groups, app_groups):
Expand Down Expand Up @@ -1188,6 +1190,7 @@ def get_arg_parser():
help="Only print configuration to console",
action="store_true")
parser = set_logging_args(parser)
parser = set_marathon_auth_args(parser)
return parser


Expand Down Expand Up @@ -1282,7 +1285,9 @@ def process_sse_events(marathon, config_file, groups,
setup_logging(logger, args.syslog_socket, args.log_format)

# Marathon API connector
marathon = Marathon(args.marathon, args.health_check)
marathon = Marathon(args.marathon,
args.health_check,
get_marathon_auth_params(args))

# If in listening mode, spawn a webserver waiting for events. Otherwise
# just write the config.
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ requests
six
sseclient
python-dateutil
mock
3 changes: 2 additions & 1 deletion tests/test_bluegreen_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Arguments:
marathon = "http://marathon"
dry_run = True
initial_instances = 1
marathon_auth_credential_file = None


class MyResponse:
Expand All @@ -28,7 +29,7 @@ class TestBluegreenDeploy(unittest.TestCase):
from mock import patch

@mock.patch('requests.get',
mock.Mock(side_effect=lambda k:
mock.Mock(side_effect=lambda k, auth:
MyResponse('tests/bluegreen_app_blue.json')))
def test_simple(self):
# This test just checks the output of the program against
Expand Down

0 comments on commit 8e3b8ae

Please sign in to comment.