|
13 | 13 | )
|
14 | 14 |
|
15 | 15 | from stacker.blueprints.base import Blueprint
|
| 16 | +from stacker.blueprints.variables.types import TroposphereType |
16 | 17 |
|
17 | 18 | from .policies import ecs_task_execution_policy
|
18 | 19 |
|
@@ -281,3 +282,188 @@ def create_template(self):
|
281 | 282 | self.create_task_execution_role_policy()
|
282 | 283 | self.create_task_definition()
|
283 | 284 | self.create_service()
|
| 285 | + |
| 286 | + |
| 287 | +class SimpleECSService(Blueprint): |
| 288 | + VARIABLES = { |
| 289 | + "ServiceName": { |
| 290 | + "type": str, |
| 291 | + "description": "A simple name for the service.", |
| 292 | + }, |
| 293 | + "Image": { |
| 294 | + "type": str, |
| 295 | + "description": "The docker image to use for the task.", |
| 296 | + }, |
| 297 | + "Command": { |
| 298 | + "type": list, |
| 299 | + "description": "A list of the command and it's arguments to run " |
| 300 | + "inside the container. If not provided, will " |
| 301 | + "default to the default command defined in the " |
| 302 | + "image.", |
| 303 | + "default": [], |
| 304 | + }, |
| 305 | + "Cluster": { |
| 306 | + "type": str, |
| 307 | + "description": "The name or Amazon Resource Name (ARN) of the " |
| 308 | + "ECS cluster that you want to run your tasks on.", |
| 309 | + }, |
| 310 | + "CPU": { |
| 311 | + "type": int, |
| 312 | + "description": "The relative CPU shares used by each instance of " |
| 313 | + "the task.", |
| 314 | + }, |
| 315 | + "Memory": { |
| 316 | + "type": int, |
| 317 | + "description": "The amount of memory (in megabytes) to reserve " |
| 318 | + "for each instance of the task.", |
| 319 | + }, |
| 320 | + "Count": { |
| 321 | + "type": int, |
| 322 | + "description": "The number of instances of the task to create.", |
| 323 | + "default": 1, |
| 324 | + }, |
| 325 | + "Environment": { |
| 326 | + "type": dict, |
| 327 | + "description": "A dictionary representing the environment of the " |
| 328 | + "task.", |
| 329 | + "default": {}, |
| 330 | + }, |
| 331 | + "LogConfiguration": { |
| 332 | + "type": TroposphereType(ecs.LogConfiguration, optional=True), |
| 333 | + "description": "An optional log configuration object.", |
| 334 | + "default": None, |
| 335 | + }, |
| 336 | + } |
| 337 | + |
| 338 | + @property |
| 339 | + def service_name(self): |
| 340 | + return self.get_variables()["ServiceName"] |
| 341 | + |
| 342 | + @property |
| 343 | + def image(self): |
| 344 | + return self.get_variables()["Image"] |
| 345 | + |
| 346 | + @property |
| 347 | + def command(self): |
| 348 | + return self.get_variables()["Command"] or NoValue |
| 349 | + |
| 350 | + @property |
| 351 | + def cluster(self): |
| 352 | + return self.get_variables()["Cluster"] |
| 353 | + |
| 354 | + @property |
| 355 | + def cpu(self): |
| 356 | + return self.get_variables()["CPU"] |
| 357 | + |
| 358 | + @property |
| 359 | + def memory(self): |
| 360 | + return self.get_variables()["Memory"] |
| 361 | + |
| 362 | + @property |
| 363 | + def count(self): |
| 364 | + return self.get_variables()["Count"] |
| 365 | + |
| 366 | + @property |
| 367 | + def environment(self): |
| 368 | + env_dict = self.get_variables()["Environment"] |
| 369 | + if not env_dict: |
| 370 | + return NoValue |
| 371 | + |
| 372 | + env_list = [] |
| 373 | + for k, v in env_dict.items(): |
| 374 | + env_list.append(ecs.Environment(Name=str(k), Value=str(v))) |
| 375 | + |
| 376 | + return env_list |
| 377 | + |
| 378 | + @property |
| 379 | + def log_configuration(self): |
| 380 | + log_config = self.get_variables()["LogConfiguration"] |
| 381 | + return log_config or NoValue |
| 382 | + |
| 383 | + def add_output(self, key, value): |
| 384 | + self.template.add_output(Output(key, Value=value)) |
| 385 | + |
| 386 | + def create_role(self): |
| 387 | + t = self.template |
| 388 | + |
| 389 | + self.role = t.add_resource( |
| 390 | + iam.Role( |
| 391 | + "Role", |
| 392 | + AssumeRolePolicyDocument=get_ecs_task_assumerole_policy(), |
| 393 | + Path="/", |
| 394 | + ) |
| 395 | + ) |
| 396 | + |
| 397 | + self.add_output("RoleName", self.role.Ref()) |
| 398 | + self.add_output("RoleArn", self.role.GetAtt("Arn")) |
| 399 | + self.add_output("RoleId", self.role.GetAtt("RoleId")) |
| 400 | + |
| 401 | + def generate_policy_document(self): |
| 402 | + return None |
| 403 | + |
| 404 | + def create_policy(self): |
| 405 | + t = self.template |
| 406 | + |
| 407 | + policy_doc = self.generate_policy_document() |
| 408 | + if not policy_doc: |
| 409 | + return |
| 410 | + |
| 411 | + self.policy = t.add_resource( |
| 412 | + iam.ManagedPolicy( |
| 413 | + "ManagedPolicy", |
| 414 | + PolicyDocument=self.generate_policy(), |
| 415 | + Roles=[self.role.Ref()], |
| 416 | + ) |
| 417 | + ) |
| 418 | + |
| 419 | + self.add_output("ManagedPolicyArn", self.policy.Ref()) |
| 420 | + |
| 421 | + def generate_container_definition(self): |
| 422 | + return ecs.ContainerDefinition( |
| 423 | + Command=self.command, |
| 424 | + Cpu=self.cpu, |
| 425 | + Environment=self.environment, |
| 426 | + Essential=True, |
| 427 | + Image=self.image, |
| 428 | + LogConfiguration=self.log_configuration, |
| 429 | + Memory=self.memory, |
| 430 | + Name=self.service_name, |
| 431 | + ) |
| 432 | + |
| 433 | + def create_task_definition(self): |
| 434 | + t = self.template |
| 435 | + |
| 436 | + self.task_definition = t.add_resource( |
| 437 | + ecs.TaskDefinition( |
| 438 | + "TaskDefinition", |
| 439 | + Cpu=str(self.cpu), |
| 440 | + Family=self.service_name, |
| 441 | + Memory=str(self.memory), |
| 442 | + TaskRoleArn=self.role.GetAtt("Arn"), |
| 443 | + ContainerDefinitions=[self.generate_container_definition()] |
| 444 | + ) |
| 445 | + ) |
| 446 | + |
| 447 | + self.add_output("TaskDefinitionArn", self.task_definition.Ref()) |
| 448 | + |
| 449 | + def create_service(self): |
| 450 | + t = self.template |
| 451 | + self.service = t.add_resource( |
| 452 | + ecs.Service( |
| 453 | + "Service", |
| 454 | + Cluster=self.cluster, |
| 455 | + DesiredCount=self.count, |
| 456 | + LaunchType="EC2", |
| 457 | + ServiceName=self.service_name, |
| 458 | + TaskDefinition=self.task_definition.Ref(), |
| 459 | + ) |
| 460 | + ) |
| 461 | + |
| 462 | + self.add_output("ServiceArn", self.service.Ref()) |
| 463 | + self.add_output("ServiceName", self.service.GetAtt("Name")) |
| 464 | + |
| 465 | + def create_template(self): |
| 466 | + self.create_role() |
| 467 | + self.create_policy() |
| 468 | + self.create_task_definition() |
| 469 | + self.create_service() |
0 commit comments