Last active
January 25, 2022 10:09
-
-
Save junedkhatri31/29cccac0aea40b2fbf45f5cdbdf603fb to your computer and use it in GitHub Desktop.
This is a script helps to add propagateTags property to AWS ECS service by re-creating it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This is a script helps to add propagateTags property to AWS ECS service by re-creating it. | |
Before you run the script, | |
Make sure you understand script | |
Change tags, cluster and service | |
It is recommented to set desired tasks to 0 to service | |
Requirements: | |
python 3.6+ | |
boto3 | |
PyYAML | |
USAGE: python3 recreate-service.py | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
''' | |
import boto3 | |
import yaml | |
import logging | |
from pathlib import Path | |
import os | |
logging.basicConfig( | |
level=logging.INFO, | |
format= '[%(asctime)s] [%(pathname)s:%(lineno)d] %(levelname)s - %(message)s', | |
datefmt='%H:%M:%S' | |
) | |
log = logging.getLogger(__name__) | |
client = boto3.client('ecs') | |
tags = { | |
# Add tags here | |
# { | |
# "key1": "value1", | |
# "key2": "value2", | |
# } | |
} | |
def get_awsecs_service(cluster_name, service_name): | |
response = client.describe_services( | |
cluster=cluster_name, | |
services=[ | |
service_name, | |
] | |
) | |
service = response['services'][0] | |
keys_to_remove = [ | |
'events', | |
'createdAt', | |
'deployments', | |
'createdBy', | |
'pendingCount', | |
'runningCount', | |
'serviceArn', | |
'status', | |
'clusterArn', | |
'roleArn', | |
] | |
for key in keys_to_remove: | |
del service[key] | |
return service | |
def add_tags(service): | |
service['propagateTags'] = 'SERVICE' | |
tags_list = [{'key': key, 'value': value} for key, value in tags.items()] | |
service['tags'] = tags_list | |
return service | |
def delete_service(cluster_name, service): | |
client.delete_service( | |
cluster=cluster_name, | |
service=service, | |
force=True, | |
) | |
def create_awsecs_service(cluster_name, service): | |
service['cluster'] = cluster_name | |
client.create_service(**service) | |
def save_to_file(file_name, content): | |
# create directory if not exists | |
directory_path = os.path.dirname(file_name) | |
Path(directory_path).mkdir(parents=True, exist_ok=True) | |
# store to file | |
with open(file_name, 'w') as service_file: | |
yaml.dump(content, stream=service_file) | |
def get_file_content(file_name): | |
with open(file_name, 'r') as service_file: | |
return yaml.load(service_file) | |
def wait_for_service_to_drain(cluster_name, service_name): | |
waiter = client.get_waiter('services_inactive') | |
waiter.wait( | |
cluster=cluster_name, | |
services=[ | |
service_name, | |
], | |
WaiterConfig={ | |
'MaxAttempts': 40 # 10 minutes | |
} | |
) | |
def main(cluster_name, service_name): | |
backup_file_name = f'services_backup/{cluster_name}/{service_name}.yml' | |
log.info(f'Fetching service {service_name}') | |
service = get_awsecs_service(cluster_name, service_name) | |
log.info('Got service') | |
log.info(f'Adding tags - {tags}') | |
new_service = add_tags(service) | |
log.info('Tags added') | |
log.info(f'creating backup of service {service_name} in file') | |
save_to_file(backup_file_name, new_service) | |
log.info('Done storing') | |
log.info(f'Deleting service {service_name}') | |
delete_service(cluster_name, service_name) | |
log.info('Service deleted') | |
log.info(f'Waiting for service {service_name} to drain - max 10 mins') | |
wait_for_service_to_drain(cluster_name, service_name) | |
# new_service = get_file_content(file_name) | |
log.info(f'creating service {service_name}') | |
create_awsecs_service(cluster_name, new_service) | |
if __name__ == '__main__': | |
cluster_name = '<cluster-name>' | |
service_name = '<service-name>' | |
main(cluster_name, service_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment