def test_paasta_args_mixer_clusters_and_instances(mock_cluster_instance_map): PaastaArgs = namedtuple( 'PaastaArgs', ['soa_dir', 'clusters', 'instances', 'deploy_group']) args = PaastaArgs(soa_dir='/fake/soa/dir', deploy_group=None, clusters='cluster1', instances='instance1,instance3') pargs = paasta_args_mixer(args, 'fake_service') assert pargs.instance_whitelist == ['instance1', 'instance3'] assert pargs.cluster_whitelist == ['cluster1'] assert not mock_cluster_instance_map.called
def test_paasta_args_mixer_clusters_return_none_when_instance_not_in_deploy_group( mock_cluster_instance_map): PaastaArgs = namedtuple( 'PaastaArgs', ['soa_dir', 'clusters', 'instances', 'deploy_group']) args = PaastaArgs(soa_dir='/fake/soa/dir', deploy_group='fake_deploy_group', clusters=None, instances='instance5') mock_cluster_instance_map.return_value = { 'cluster1': { 'instances': ['instance1', 'instance2'] }, 'cluster2': { 'instances': ['instance3'] } } assert paasta_args_mixer(args, 'fake_service') is None
def test_paasta_args_mixer_clusters_do_not_whitelist_anything_by_default( mock_cluster_instance_map): PaastaArgs = namedtuple( 'PaastaArgs', ['soa_dir', 'clusters', 'instances', 'deploy_group']) args = PaastaArgs(soa_dir='/fake/soa/dir', deploy_group=None, clusters=None, instances=None) mock_cluster_instance_map.return_value = { 'cluster1': { 'instances': ['instance1', 'instance2'] }, 'cluster2': { 'instances': ['instance3'] } } pargs = paasta_args_mixer(args, 'fake_service') assert len(pargs.instance_whitelist) == 0 assert len(pargs.cluster_whitelist) == 0
def test_paasta_args_mixer_clusters_and_instances_clusters_instances_deploy_group( mock_cluster_instance_map): PaastaArgs = namedtuple( 'PaastaArgs', ['soa_dir', 'clusters', 'instances', 'deploy_group']) args = PaastaArgs(soa_dir='/fake/soa/dir', deploy_group='fake_deploy_group', clusters='cluster1', instances='instance1,instance3') mock_cluster_instance_map.return_value = { 'cluster1': { 'instances': ['instance1', 'instance2'] }, 'cluster2': { 'instances': ['instance3'] } } pargs = paasta_args_mixer(args, 'fake_service') assert pargs.instance_whitelist == ['instance1'] assert pargs.cluster_whitelist == ['cluster1']
def paasta_start_or_stop(args, desired_state): """Requests a change of state to start or stop given branches of a service.""" soa_dir = args.soa_dir service = figure_out_service_name(args=args, soa_dir=soa_dir) pargs = paasta_args_mixer(args, service) if pargs is None: return 1 instances = pargs.instance_whitelist if pargs.instance_whitelist else None # assert that each of the clusters that the user specifies are 'valid' # for the instance list provided; that is, assert that at least one of the instances # provided in the -i argument is deployed there. # if there are no instances defined in the args, then assert that the service # is deployed to that cluster. # If args.clusters is falsey, then default to *all* clusters that a service is deployed to, # and we figure out which ones are needed for each service later. if instances: instance_clusters = [ list_clusters(service, soa_dir, instance) for instance in instances ] valid_clusters = sorted( list({ cluster for cluster_list in instance_clusters for cluster in cluster_list })) else: valid_clusters = list_clusters(service, soa_dir) if pargs.cluster_whitelist: clusters = pargs.cluster_whitelist invalid_clusters = [ cluster for cluster in clusters if cluster not in valid_clusters ] if invalid_clusters: paasta_print( "Invalid cluster name(s) specified: %s." % " ".join(invalid_clusters), "Valid options: %s" % " ".join(valid_clusters), ) return 1 else: clusters = valid_clusters try: remote_refs = remote_git.list_remote_refs( utils.get_git_url(service, soa_dir)) except remote_git.LSRemoteException as e: msg = ( "Error talking to the git server: %s\n" "This PaaSTA command requires access to the git server to operate.\n" "The git server may be down or not reachable from here.\n" "Try again from somewhere where the git server can be reached, " "like your developer environment.") % str(e) paasta_print(msg) return 1 invalid_deploy_groups = [] marathon_message_printed, chronos_message_printed = False, False for cluster in clusters: # If they haven't specified what instances to act on, do it for all of them. # If they have specified what instances, only iterate over them if they're # actually within this cluster. if instances is None: cluster_instances = list_all_instances_for_service( service, clusters=[cluster], soa_dir=soa_dir) paasta_print( "no instances specified; restarting all instances for service") else: all_cluster_instances = list_all_instances_for_service( service, clusters=[cluster], soa_dir=soa_dir) cluster_instances = all_cluster_instances.intersection( set(instances)) for instance in cluster_instances: service_config = get_instance_config( service=service, cluster=cluster, instance=instance, soa_dir=soa_dir, load_deployments=False, ) deploy_group = service_config.get_deploy_group() (deploy_tag, _) = get_latest_deployment_tag(remote_refs, deploy_group) if deploy_tag not in remote_refs: invalid_deploy_groups.append(deploy_group) else: force_bounce = utils.format_timestamp( datetime.datetime.utcnow()) if isinstance(service_config, MarathonServiceConfig ) and not marathon_message_printed: print_marathon_message(desired_state) marathon_message_printed = True elif isinstance( service_config, ChronosJobConfig) and not chronos_message_printed: print_chronos_message(desired_state) chronos_message_printed = True issue_state_change_for_service( service_config=service_config, force_bounce=force_bounce, desired_state=desired_state, ) return_val = 0 if invalid_deploy_groups: paasta_print("No branches found for %s in %s." % (", ".join(invalid_deploy_groups), remote_refs)) paasta_print("Has %s been deployed there yet?" % service) return_val = 1 return return_val