import json import requests octopus_server_uri = 'https://octopus.delphi.zone/' octopus_api_key = '' headers = {'X-Octopus-ApiKey': octopus_api_key} def get_octopus_resource(uri): response = requests.get(uri, headers=headers) response.raise_for_status() return json.loads(response.content.decode('utf-8')) space_name = 'Default' worker_pool_id = '' spaces = get_octopus_resource('{0}/api/spaces/all'.format(octopus_server_uri)) space = next((x for x in spaces if x['Name'] == space_name), None) unupdatable_projects = [] failed_projects = [] #['Template project', 'delphi.tiktok-chartmetrics-sync-snowflake', 'delphi.crm-sources-snowflake'] #filtered_projects = ['delphi.crm-sources-snowflake'] #projects = [x for x in get_octopus_resource('{0}/api/{1}/projects/all'.format(octopus_server_uri, space['Id'])) if x['Name'] in filtered_projects] projects = get_octopus_resource('{0}/api/{1}/projects/all'.format(octopus_server_uri, space['Id'])) for project in projects: deploymentprocess_link = project['Links']['DeploymentProcess'] if project['IsVersionControlled'] or project['IsDisabled']: # Version controlled projects are not covered by this script continue uri = '{0}{1}'.format(octopus_server_uri, deploymentprocess_link) process = get_octopus_resource(uri) update_process = False for step in process['Steps']: properties = step['Properties'] for action in step['Actions']: if 'Octopus.Action.RunOnServer' not in action['Properties']: unupdatable_projects.append(project['Name']) if 'Octopus.Action.RunOnServer' in action['Properties'] and \ action['Properties']['Octopus.Action.RunOnServer'] == 'false': print('Step \'{0}\' of project \'{1}\' is not running on the server'.format(step['Name'], project['Name'])) # Trigger update request update_process = True # Set action to run on WorkerPool action['Properties']['Octopus.Action.RunOnServer'] = 'true' action['WorkerPoolId'] = worker_pool_id # Remove target roles from the step step['Properties']['Octopus.Action.TargetRoles'] = {} if update_process and project['Name'] in unupdatable_projects: failed_projects.append(project['Name']) if update_process and project['Name'] not in unupdatable_projects: print('Updating deployment process \'{0}\' for project \'{1}\'...'.format(process['Id'], project['Name'])) response = requests.put(uri, headers=headers, json=process) if response.status_code != 200: failed_projects.append(project['Name']) print(set(unupdatable_projects)) print(set(failed_projects))