Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Spot fleets #149

Draft
wants to merge 34 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
64b0652
Remove deprecated mkFixStrictness call
roberth Jul 12, 2021
a6ea0ba
update IRC reference to Matrix
Aug 8, 2021
4e638bf
Regenerate ec2-properties, but retain instance types that no longer e…
rehno-lindeque Aug 3, 2021
588b7ca
boto3, boto3-stubs: upgrade to 1.18.50
rehno-lindeque Sep 29, 2021
201771c
Fix type errors for more specific argument overrides
rehno-lindeque Sep 30, 2021
4aeef38
Fix escape sequences that needs to be doubly escaped
rehno-lindeque Sep 30, 2021
14040cb
Fix string formatting
rehno-lindeque Sep 30, 2021
755af51
aws: add aws-ec2-launch-template resource
PsyanticY Feb 14, 2020
3e00fd0
fix mypy issues
rehno-lindeque Oct 2, 2021
40d9d3d
formatting with black
rehno-lindeque Oct 2, 2021
43a1c16
Try to satisfy flake8
rehno-lindeque Oct 3, 2021
b08c9b5
simplify function as demanded by flake8
rehno-lindeque Oct 3, 2021
43d7ea6
Add types to request / response and fix resulting type errors
rehno-lindeque Oct 3, 2021
bcc853f
directly set tags for launch template creation
rehno-lindeque Oct 3, 2021
b5d7c00
Tag type for sub-resources
rehno-lindeque Oct 3, 2021
f82189c
apply different tags to the template and instances launched using the…
rehno-lindeque Oct 3, 2021
8f5cd78
whitespace
rehno-lindeque Oct 3, 2021
15242a5
mypy-boto3: update poetry.lock
rehno-lindeque Sep 30, 2021
d838ce0
move boto3-stubs to production dependencies
rehno-lindeque Oct 1, 2021
2f69594
wip
rehno-lindeque Sep 30, 2021
0df93ea
launch template: add options type and fix resulting type errors
rehno-lindeque Oct 4, 2021
cc7765a
launch template: merge name and templateName
rehno-lindeque Oct 4, 2021
fb8a1e2
launch template: tags is an immutable mapping
rehno-lindeque Oct 4, 2021
d5804c2
launch template: options must use Sequence not List
rehno-lindeque Oct 4, 2021
6d76724
launch template: security group ids is a sequence
rehno-lindeque Oct 4, 2021
9086abd
launch template: rearrange options to reflect ec2 instance options
rehno-lindeque Oct 4, 2021
a597a8d
launch template: type check describe_images arguments
rehno-lindeque Oct 4, 2021
c5e9375
launch template: correctly fetch aws access key id
rehno-lindeque Oct 4, 2021
87dd293
launch template: formatting
rehno-lindeque Oct 4, 2021
e8d8d87
spot-fleet: wip
rehno-lindeque Oct 5, 2021
b5d37ba
launch template: fix block device mapping from ami
rehno-lindeque Oct 6, 2021
968c58b
launch template: rename to _to_launch_template_data (minor refactor)
rehno-lindeque Oct 6, 2021
8639911
launch template: instance type may remain unspecified in template
rehno-lindeque Oct 6, 2021
d8ae3f4
spot fleet: wip
rehno-lindeque Oct 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This repo contains the NixOps AWS Plugin.
* [Source code](https://github.com/NixOS/nixops)
* [Issue Tracker](https://github.com/NixOS/nixops/issues)
* [Mailing list / Google group](https://groups.google.com/forum/#!forum/nixops-users)
* [IRC - #nixos on freenode.net](irc://irc.freenode.net/#nixos)
* [Matrix - #nix:nixos.org](https://matrix.to/#/#nix:nixos.org)

## Developing

Expand Down
2 changes: 1 addition & 1 deletion coverage-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"--cover-package=nixops",
"--nocapture",
"-e",
"^tests\.py$",
"^tests\\.py$",
]
+ sys.argv[1:]
)
124 changes: 124 additions & 0 deletions nixops_aws/nix/aws-ec2-launch-template.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{ config, lib, uuid, name, ... }:

with import ./lib.nix lib;
with lib;

{
imports = [ ./common-ec2-auth-options.nix ];

options = {

name = mkOption {
default = "nixops-${uuid}-${name}";
type = types.str;
description = "Name of the launch template.";
};

templateId = mkOption {
default = "";
type = types.str;
description = "ec2 launch template ID (set by NixOps)";
};

versionDescription = mkOption {
default = "";
type = types.str;
description = "A description for the version of the launch template";
};


# we might want to make this in a way similar to ec2.nix
ebsOptimized = mkOption {
default = true;
description = ''
Whether the EC2 instance should be created as an EBS Optimized instance.
'';
type = types.bool;
};

userData = mkOption {
default = null;
type = types.nullOr types.str;
description = ''
The user data to make available to the instance.
It should be valid nix expressions.
'';
};

# add support for ec2 then move to common
disableApiTermination = mkOption {
default = false;
type = types.bool;
description = ''
If set to true , you can't terminate the instance
using the Amazon EC2 console, CLI, or API.
'';
};

# add support for ec2 then move to common
instanceInitiatedShutdownBehavior = mkOption {
default = "terminate";
type = types.enum ["stop" "terminate"];
description = ''
Indicates whether an instance stops or terminates
when you initiate shutdown from the instance (using
the operating system command for system shutdown).
'';
};
# add support for ec2 then move to common
networkInterfaceId = mkOption {
default = "";
# must get the id fro mthe name
type = with types; either str (resource "vpc-network-interface");
apply = x: if builtins.isString x then x else "res-" + x._name "." + x._type;
description = ''
The ID of the network interface.
'';
};

privateIpAddresses = mkOption {
default = null;
type = with types; (nullOr (listOf str));
description = ''
One or more secondary private IPv4 addresses.
'';
};
secondaryPrivateIpAddressCount = mkOption {
default = null;
type = types.nullOr types.int;
description = ''
The number of secondary private IPv4 addresses to assign to a network interface.
When you specify a number of secondary IPv4 addresses, Amazon EC2 selects these
IP addresses within the subnet's IPv4 CIDR range.
You can't specify this option and specify privateIpAddresses in the same time.
'';
};

instanceTags = mkOption {
default = { };
example = { foo = "bar"; xyzzy = "bla"; };
type = types.attrsOf types.str;
description = ''
Default tags assigned to each instance launched using the template. Each tag
name can be at most 128 characters, and each tag value can be at most 256
characters. There can be at most 10 tags.
'';
};

volumeTags = mkOption {
default = { };
example = { foo = "bar"; xyzzy = "bla"; };
type = types.attrsOf types.str;
description = ''
Default tags assigned to each volume created using the template. Each tag
name can be at most 128 characters, and each tag value can be at most 256
characters. There can be at most 10 tags.
'';
};

}
// (import ./common-ec2-options.nix { inherit lib; })
// (import ./common-ec2-instance-options.nix { inherit lib; });

config._type = "aws-ec2-launch-template";
}
208 changes: 208 additions & 0 deletions nixops_aws/nix/aws-spot-fleet-request.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{ config, lib, uuid, name, ... }:

with lib;


let
cfg = config.awsSpotFleetRequest;

launchSpecificationOptions = {
options = {
};
};

fleetLaunchTemplateSpecificationOptions = {
options = {
launchTemplateName = mkOption {
type = types.str;
description = "The name of the launch template. If you specify the template name, you can't specify the template ID.";
};

version = mkOption {
type = types.str;
description = ''
The launch template version number, <literal>"$Latest"</literal>, or <literal>"$Default"</literal>.
If the value is <literal>"$Latest"</literal>, Amazon EC2 uses the latest version of the launch template.
If the value is <literal>"$Default"</literal>, Amazon EC2 uses the default version of the launch template.
'';
};
};
};

launchTemplateOverridesOptions = {
options = {
spotPrice = mkOption {
default = null;
# example = ;
type = with types; nullOr str;
description = "The maximum price per unit hour that you are willing to pay for a Spot Instance.";
};

subnetId = mkOption {
default = null;
# example = ;
type = with types; nullOr str;
# description = "The ID of the subnet in which to launch the instances.";
};

availabilityZone = mkOption {
default = null;
# example = ;
type = with types; nullOr str;
description = "The Availability Zone in which to launch the instances.";
};

weightedCapacity = mkOption {
default = null;
# example = ;
type = with types; nullOr float;
description = "The number of units provided by the specified instance type.";
};

priority = mkOption {
default = null;
# example = ;
type = with types; nullOr float;
description = ''
The priority for the launch template override. The highest priority is launched first.
If <code>OnDemandAllocationStrategy</code> is set to <literal>"prioritized"</literal>, Spot Fleet uses priority to determine which launch template override to use first in fulfilling On-Demand capacity.
If the Spot <code>AllocationStrategy</code> is set to <literal>"capacityOptimizedPrioritized"</literal>, Spot Fleet uses priority on a best-effort basis to determine which launch template override to use in fulfilling Spot capacity, but optimizes for capacity first.
Valid values are whole numbers starting at <literal>0</literal>. The lower the number, the higher the priority. If no number is set, the launch template override has the lowest priority. You can set the same priority for different launch template overrides.
'';
};

# Common EC2 instance options
instanceType = mkOption {
default = null;
# example = ;
type = with types; nullOr str;
description = "The instance type.";
};
};
};

launchTemplateConfigOptions = {
options = {
launchTemplateSpecification = mkOption {
type = types.submodule fleetLaunchTemplateSpecificationOptions;
description = "The launch template.";
};

overrides = mkOption {
default = [ ]; # Optional
example = [
{
instanceType = "m1.small";
weightedCapacity = 1.;
}
{
instanceType = "m3.medium";
weightedCapacity = 1.;
}
{
instanceType = "m1.medium";
weightedCapacity = 1.;
}
];
type = with types; listOf (types.submodule launchTemplateOverridesOptions);
description = "Any parameters that you specify override the same parameters in the launch template.";
};
};
};
in
{
imports = [ ./common-ec2-auth-options.nix ];

options = {

spotFleetRequestId = mkOption {
default = "";
type = types.str;
description = "Spot fleet request ID (set by NixOps)";
};

iamFleetRole = mkOption {
type = types.str;
description = ''
The Amazon Resource Name (ARN) of an Identity and Access Management
(IAM) role that grants the Spot Fleet the permission to request,
launch, terminate, and tag instances on your behalf.

Spot Fleet can terminate Spot Instances on your behalf when you cancel
its Spot Fleet request or when the Spot Fleet request expires, if you
set <code>TerminateInstancesWithExpiration</code>.
'';
};

launchTemplateConfigs = mkOption
{
type = with types; listOf (submodule launchTemplateConfigOptions);
description = ''
The launch template and overrides. If you specify <code>LaunchTemplateConfigs</code>, you can't specify <code>LaunchSpecifications</code>. If you include On-Demand capacity in your request, you must use <code>LaunchTemplateConfigs</code>.
'';
};

spotPrice = mkOption {
type = with types; nullOr str;
description = ''
The maximum price per unit hour that you are willing to pay for a Spot Instance. The default is the On-Demand price.
'';
};

spotMaxTotalPrice = mkOption {
type = with types; nullOr str;
description = "The maximum amount per hour for Spot Instances that you're willing to pay. You can use the <code>spotdMaxTotalPrice</code> parameter, the <code>onDemandMaxTotalPrice</code> parameter, or both parameters to ensure that your fleet cost does not exceed your budget. If you set a maximum price per hour for the On-Demand Instances and Spot Instances in your request, Spot Fleet will launch instances until it reaches the maximum amount you're willing to pay. When the maximum amount you're willing to pay is reached, the fleet stops launching instances even if it hasn’t met the target capacity.";
};

type = mkOption {
default = "maintain";
example = "request";
type =
types.enum [
"request"
"maintain"
# "instant" # instant is listed but is not used by Spot Fleet.
];
description = ''
The type of request. Indicates whether the Spot Fleet only requests
the target capacity or also attempts to maintain it. When this
value is <code>request</code>, the Spot Fleet only places the
required requests. It does not attempt to replenish Spot Instances if
capacity is diminished, nor does it submit requests in alternative Spot
pools if capacity is not available. When this value is
<code>maintain</code>, the Spot Fleet maintains the target capacity.
The Spot Fleet places the required requests to meet capacity and
automatically replenishes any interrupted instances.
'';
};

iamFleetRole = mkOption {
# example = "rolename"; # TODO
type = types.str;
description = ''
The Amazon Resource Name (ARN) of an Identity and Access Management
(IAM) role that grants the Spot Fleet the permission to request,
launch, terminate, and tag instances on your behalf.

Spot Fleet can terminate Spot Instances on your behalf when you cancel
its Spot Fleet request or when the Spot Fleet request expires, if you
set <code>TerminateInstancesWithExpiration</code>.
'';
};

launchTemplateConfigs = mkOption
{
type = with types; listOf (submodule launchTemplateConfigOptions);
description = ''
The launch template and overrides. If you specify <code>LaunchTemplateConfigs</code>, you can't specify <code>LaunchSpecifications</code>. If you include On-Demand capacity in your request, you must use <code>LaunchTemplateConfigs</code>.
'';
};

}
// (import ./common-ec2-options.nix { inherit lib; });

config._type = "aws-spot-fleet-request";

}


Loading