-
-
Save dooyeoung/ca95d8cc504dd9071f710bb15eefafb2 to your computer and use it in GitHub Desktop.
헬름 업그레이드 on ec2
This file contains 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
import argparse | |
import time | |
import traceback | |
from boto3 import client as boto_client | |
def get_command_status(command_id: str, instance_id: str, client): | |
response = client.get_command_invocation( | |
CommandId=command_id, | |
InstanceId=instance_id, | |
) | |
return response.get("Status") | |
def get_repo_name(repository_url): | |
return repository_url.split('/')[-1].split('.')[0] | |
def get_github_clone_url(repository_url): | |
repo_name = get_repo_name(repository_url) | |
return f"https://__your_name__:__developer_token__@github.com/team/{repo_name}.git" # noqa | |
def main(repository_url, commit_id, ci_number, env, image_id, image_repository, chart_name): # noqa | |
print(f'repository_url: {repository_url}') | |
print(f'commit_id: {commit_id}') | |
print(f'ci_number: {ci_number}') | |
print(f'env: {env}') | |
print(f'image_repository: {image_repository}') | |
print(f'image_id: {image_id}') | |
print(f'chart_name: {chart_name}') | |
ssm_client = boto_client( | |
"ssm", | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
region_name=AWS_DEFAULT_REGION, | |
) | |
repository_url = get_github_clone_url(repository_url) | |
dir_name = get_repo_name(repository_url) | |
commands = [ | |
'cd /home/ec2-user', | |
# 깃헙에서 레포지토리 다운로드 | |
f''' | |
if [ ! -d "/home/ec2-user/{dir_name}" ] | |
then | |
git clone "{repository_url}" /home/ec2-user | |
fi | |
''', | |
# 폴더 이동 | |
f"cd /home/ec2-user/{dir_name}", | |
# helm 준비 | |
f"cp helm/{env}.values.yaml helm/values.yaml", | |
# ec2-user 권한으로 helm 실행 | |
f''' | |
runuser -l ec2-user -c "helm upgrade '{chart_name}' \ | |
/home/ec2-user/{dir_name}/helm/ \ | |
--set image.repository='{image_repository}' \ | |
--set image.id='{image_id}' \ | |
--description 'commit {commit_id} from CircleCI {ci_number}' " | |
''' | |
] | |
run_crawl_job = ssm_client.send_command( | |
DocumentName="AWS-RunShellScript", | |
Parameters={"commands": commands}, | |
InstanceIds=[instance_id], | |
CloudWatchOutputConfig={ | |
'CloudWatchLogGroupName': 'session-manager-command', | |
'CloudWatchOutputEnabled': True | |
} | |
) | |
command_id = run_crawl_job.get("Command").get("CommandId") | |
print(f'send_command :{command_id}') | |
command_status = "Pending" | |
while ( | |
command_status == "Pending" | |
or command_status == "InProgress" | |
or command_status == "Delayed" | |
): | |
time.sleep(3) | |
command_status = get_command_status( | |
command_id, | |
instance_id, | |
ssm_client, | |
) | |
print(command_status) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--repository_url") | |
parser.add_argument("--commit_id") | |
parser.add_argument("--ci_number") | |
parser.add_argument("--image_id") | |
parser.add_argument("--image_repository") | |
parser.add_argument("--chart_name") | |
parser.add_argument("--env", default="test") | |
if __name__ == "__main__": | |
try: | |
args = parser.parse_args() | |
main( | |
repository_url=args.repository_url, | |
commit_id=args.commit_id, | |
ci_number=args.ci_number, | |
env=args.env, | |
image_repository=args.image_repository, | |
image_id=args.image_id, | |
chart_name=args.chart_name, | |
) | |
except Exception: | |
traceback.print_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment