Skip to content

Commit

Permalink
Allow using elastic IP resources by direct reference
Browse files Browse the repository at this point in the history
Example:

  resources.elasticIPs.my-ip =
    { region = "eu-west-1"; ...
    };

  machine =
    { config, pkgs, resources, ... }:
    { deployment.targetEnv = "ec2"; ...
      deployment.ec2.elasticIPv4 = resources.elasticIPs.my-ip;
    };
  • Loading branch information
edolstra committed Nov 12, 2013
1 parent 98b65e1 commit e91d56f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nix/ec2.nix
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ in
deployment.ec2.elasticIPv4 = mkOption {
default = "";
example = "203.0.113.123";
type = types.str;
type = union types.str (resource "elastic-ip");
apply = x: if builtins.isString x then x else "res-" + x._name;
description = ''
Elastic IPv4 address to be associated with this machine.
'';
Expand Down
2 changes: 2 additions & 0 deletions nix/elastic-ip.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ with pkgs.lib;

};

config._type = "elastic-ip";

}
13 changes: 12 additions & 1 deletion nixops/backends/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from nixops.backends import MachineDefinition, MachineState
from nixops.nix_expr import Function, RawValue
from nixops.resources.ebs_volume import EBSVolumeState
from nixops.resources.elastic_ip import ElasticIPState
import nixops.util
import nixops.ec2_utils
import nixops.known_hosts
Expand Down Expand Up @@ -811,7 +812,17 @@ def create(self, defn, check, allow_reboot, allow_recreate):
# TODO: remove obsolete tags?
self.tags = tags

self.assign_elastic_ip(defn.elastic_ipv4, instance, check)
# Assign the elastic IP. If necessary, dereference the resource.
elastic_ipv4 = defn.elastic_ipv4
if elastic_ipv4.startswith("res-"):
res_name = elastic_ipv4[4:]
res = self.depl.active_resources.get(res_name, None)
if not res:
raise Exception("resource ‘{0}’ does not exist".format(res_name))
if not isinstance(res, ElasticIPState):
raise Exception("resource ‘{0}’ is not an elastic IP address".format(res_name))
elastic_ipv4 = res.public_ipv4
self.assign_elastic_ip(elastic_ipv4, instance, check)

# Wait for the IP address.
if not self.public_ipv4 or check:
Expand Down

0 comments on commit e91d56f

Please sign in to comment.