|
13 | 13 | CLEAN_DASHBOARD = 'False'
|
14 | 14 |
|
15 | 15 | from config import *
|
| 16 | +from fleet import * |
16 | 17 |
|
17 | 18 | WAIT_TIME = 60
|
18 | 19 | MONITOR_TIME = 60
|
@@ -557,26 +558,84 @@ def startCluster():
|
557 | 558 | print('Use: run.py startCluster configFile')
|
558 | 559 | sys.exit()
|
559 | 560 |
|
560 |
| - thistime = datetime.datetime.now().replace(microsecond=0) |
561 |
| - #Step 1: set up the configuration files |
| 561 | + #Step 1: Configure the Spot Fleet Request |
| 562 | + ec2client=boto3.client('ec2') |
562 | 563 | s3client = boto3.client('s3')
|
| 564 | + iam=boto3.client('iam') |
| 565 | + |
563 | 566 | ecsConfigFile=generateECSconfig(ECS_CLUSTER,APP_NAME,AWS_BUCKET,s3client)
|
564 | 567 | spotfleetConfig=loadConfig(sys.argv[2])
|
565 |
| - spotfleetConfig['ValidFrom']=thistime |
566 |
| - spotfleetConfig['ValidUntil']=(thistime+datetime.timedelta(days=365)).replace(microsecond=0) |
567 |
| - spotfleetConfig['TargetCapacity']= CLUSTER_MACHINES |
568 |
| - spotfleetConfig['SpotPrice'] = '%.2f' %MACHINE_PRICE |
| 568 | + |
| 569 | + # Still need to figure out how these fit in |
| 570 | + """ |
569 | 571 | DOCKER_BASE_SIZE = int(round(float(EBS_VOL_SIZE)/int(TASKS_PER_MACHINE))) - 2
|
570 | 572 | userData=generateUserData(ecsConfigFile,DOCKER_BASE_SIZE)
|
571 | 573 | for LaunchSpecification in range(0,len(spotfleetConfig['LaunchSpecifications'])):
|
572 | 574 | spotfleetConfig['LaunchSpecifications'][LaunchSpecification]["UserData"]=userData
|
573 | 575 | spotfleetConfig['LaunchSpecifications'][LaunchSpecification]['BlockDeviceMappings'][1]['Ebs']["VolumeSize"]= EBS_VOL_SIZE
|
574 |
| - spotfleetConfig['LaunchSpecifications'][LaunchSpecification]['InstanceType'] = MACHINE_TYPE[LaunchSpecification] |
| 576 | + """ |
575 | 577 |
|
| 578 | + ecsInstanceArn = iam.get_role(RoleName='ecsInstanceRole')['Role']['Arn'] |
| 579 | + fleetRoleArn = iam.get_role(RoleName='aws-ec2-spot-fleet-tagging-role')['Role']['Arn'] |
576 | 580 |
|
577 |
| - # Step 2: make the spot fleet request |
578 |
| - ec2client=boto3.client('ec2') |
579 |
| - requestInfo = ec2client.request_spot_fleet(SpotFleetRequestConfig=spotfleetConfig) |
| 581 | + LaunchTemplateData={"ImageId": ImageId, |
| 582 | + "KeyName": KeyName, |
| 583 | + "IamInstanceProfile": {"Arn": ecsInstanceArn}, |
| 584 | + "BlockDeviceMappings": [ |
| 585 | + { |
| 586 | + "DeviceName": "/dev/xvda", |
| 587 | + "Ebs": { |
| 588 | + "DeleteOnTermination": True, |
| 589 | + "VolumeType": "gp2", |
| 590 | + "VolumeSize": 8, |
| 591 | + "SnapshotId": SnapshotId |
| 592 | + } |
| 593 | + }, |
| 594 | + { |
| 595 | + "DeviceName": "/dev/xvdcz", |
| 596 | + "Ebs": { |
| 597 | + "DeleteOnTermination": True, |
| 598 | + "VolumeType": "gp2" |
| 599 | + } |
| 600 | + } |
| 601 | + ], |
| 602 | + "NetworkInterfaces": [ |
| 603 | + { |
| 604 | + "DeviceIndex": 0, |
| 605 | + "SubnetId": SubnetId, |
| 606 | + "DeleteOnTermination": True, |
| 607 | + "AssociatePublicIpAddress": True, |
| 608 | + "Groups": [SecurityGroup] |
| 609 | + } |
| 610 | + ] |
| 611 | + } |
| 612 | + |
| 613 | + TemplateName=f'{APP_NAME}_LaunchTemplate' |
| 614 | + try: |
| 615 | + launch_template = ec2client.describe_launch_templates(LaunchTemplateNames=[TemplateName])['LaunchTemplates'][0] |
| 616 | + except ec2client.exceptions.ClientError: |
| 617 | + launch_template = ec2client.create_launch_template(LaunchTemplateName=TemplateName,LaunchTemplateData=LaunchTemplateData)['LaunchTemplate'] |
| 618 | + |
| 619 | + SpotOptions = {"AllocationStrategy": "lowestPrice"} |
| 620 | + LaunchTemplateConfigs=[{'LaunchTemplateSpecification':{'LaunchTemplateId':launch_template['LaunchTemplateId'], |
| 621 | + 'Version':'$Latest'}, |
| 622 | + 'Overrides':[{'InstanceType':MACHINE_TYPE, |
| 623 | + 'MaxPrice':'%.2f' %MACHINE_PRICE, |
| 624 | + 'SubnetId':SubnetId}]}] |
| 625 | + TargetCapacitySpecification={'TotalTargetCapacity':CLUSTER_MACHINES, |
| 626 | + 'OnDemandTargetCapacity':0, |
| 627 | + 'DefaultTargetCapacityType':'spot'} |
| 628 | + |
| 629 | + # Step 2: Make the spot fleet request |
| 630 | + thistime = datetime.datetime.now().replace(microsecond=0) |
| 631 | + requestInfo = ec2client.create_fleet(DryRun=False, |
| 632 | + SpotOptions=SpotOptions, |
| 633 | + LaunchTemplateConfigs=LaunchTemplateConfigs, |
| 634 | + TargetCapacitySpecification=TargetCapacitySpecification, |
| 635 | + TerminateInstancesWithExpiration=True, |
| 636 | + Type="maintain", |
| 637 | + ValidFrom=thistime, |
| 638 | + ValidUntil=(thistime+datetime.timedelta(days=365)).replace(microsecond=0),) |
580 | 639 | print('Request in process. Wait until your machines are available in the cluster.')
|
581 | 640 | print('SpotFleetRequestId',requestInfo['SpotFleetRequestId'])
|
582 | 641 |
|
|
0 commit comments