-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjudge-hosts.yaml
199 lines (176 loc) · 4.97 KB
/
judge-hosts.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
AWSTemplateFormatVersion: 2010-09-09
Description: Deploy Judges for DomJudge
Parameters:
## S3
BucketName:
Type: String
Default: judgehost-src
## Secret Manager
SecretName:
Type: String
Default: prod/judgehost/pw
## IAM
RoleName:
Type: String
Default: judgehost
ProfileName:
Type: String
Default: judgehostProfile
BucketPolicy:
Type: String
Default: judgehostSrcRead
SecretPolicy:
Type: String
Default: judgehostGetSecret
## EC2
CidrIpSSH:
Type: String
Default: 0.0.0.0/0
KeyPairName:
Type: String
Default: judgehost-key
LaunchTemplateName:
Type: String
Default: judgehostEC2Template
SecurityGroupName:
Type: String
Default: judgehostSecurityGroup
MachineImage:
Type: String
Default: ami-0a5b5c0ea66ec560d
MachineType:
Type: String
Default: t3.micro
CapacityType:
Type: String
Default: on-demand
SpotCapacity:
Type: Number
Default: 0
OnDemandCapacity:
Type: Number
Default: 0
TotalCapacity:
Type: Number
Default: 0
Resources:
## Secret Manager
JudgeHostSecret:
Type: AWS::SecretsManager::Secret
Properties:
Description: "Login password for the judgehost user"
Name: !Ref SecretName
GenerateSecretString:
SecretStringTemplate: '{"username": "judgehost"}'
GenerateStringKey: password
PasswordLength: 32
ExcludeCharacters: "/@\"'\\"
## IAM
JudgeHostRole:
Type : AWS::IAM::Role
Properties:
RoleName: !Ref RoleName
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: ec2.amazonaws.com
JudgeHostGetSecretPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Ref SecretPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: secretsmanager:GetSecretValue
Resource: !Ref JudgeHostSecret
Roles:
- !Ref JudgeHostRole
JudgeHostSrcReadPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Ref BucketPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:GetObjectVersion'
- 's3:ListBucket'
Resource:
- !Join ['', ['arn:aws:s3:::', !Ref BucketName]]
- !Join ['', ['arn:aws:s3:::', !Ref BucketName, '/*']]
# - arn:aws:s3:::dsa-judgehost-src
# - arn:aws:s3:::dsa-judgehost-src/*
Roles:
- !Ref JudgeHostRole
JudgeHostInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: !Ref ProfileName
Path: '/'
Roles:
- !Ref JudgeHostRole
## EC2
JudgeHostSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Ref SecurityGroupName
GroupDescription: 'Security group for judgehosts'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref CidrIpSSH
JudgeHostKeyPair:
Type: AWS::EC2::KeyPair
Properties:
KeyName: !Ref KeyPairName
KeyType: rsa
JudgeHostLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: !Ref LaunchTemplateName
LaunchTemplateData:
ImageId: !Ref MachineImage
InstanceType: !Ref MachineType
KeyName: !Ref JudgeHostKeyPair
SecurityGroupIds:
- !GetAtt "JudgeHostSecurityGroup.GroupId"
IamInstanceProfile:
Arn: !GetAtt
- JudgeHostInstanceProfile
- Arn
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -eu
# This script is run by root on startup no env variables defined
# the default user on Debian AWS is admin
user=admin
s3_bucket_name=judgehost-src #TODO: Modify if needed
# Fetch judgehost src from s3
sudo -u $user aws s3 cp s3://$s3_bucket_name /home/$user/ --recursive
# Run init script
judge_dir=/home/$user/judgehost
chmod +x $judge_dir/scripts/docker_init.sh
sudo -u $user $judge_dir/scripts/docker_init.sh
JudgeHostEc2Fleet:
Type: AWS::EC2::EC2Fleet
Properties:
# ExcessCapacityTerminationPolicy: no-termination
LaunchTemplateConfigs:
- LaunchTemplateSpecification:
Version: $Latest
LaunchTemplateId: !Ref JudgeHostLaunchTemplate
ReplaceUnhealthyInstances: True
# Here for example specify 5 on demand to be sure and then 5 spot
TargetCapacitySpecification:
DefaultTargetCapacityType: !Ref CapacityType
OnDemandTargetCapacity: !Ref OnDemandCapacity
SpotTargetCapacity: !Ref SpotCapacity
TotalTargetCapacity: !Ref TotalCapacity