onecommons / unfurl-examples

1 stars 1 forks source link

AWS and GCP - machine type auto selection #6

Closed jozo closed 2 years ago

jozo commented 3 years ago

Download all possible machine types for region and then select smallest one with satisfactory attributes.

jozo commented 3 years ago

Note: for GCP you need to pip install google-cloud-compute

aszs commented 3 years ago

This made me realize that we need to make sure that any processing during the parsing and decoration phase is deterministic. So the logic that connects to your cloud account and downloads the machine type metadata for the region you are using should be implemented as a configurator that saves that info as an attribute on its instance. Then the Compute node should compute the machine_type property at deploy time, not decorate time. With the latest code the plan should be smart enough to get the order correct. So assuming the node with the metadata is named something like "gcp_metada" or "aws_metadata" the decorator would look something like this:

  decorators:
   unfurl.nodes.Compute:
    properties:
      machine_type:
        q: # quote this so eval isn't evaluated during decoration
         eval:
           python: helpers.py#choose_machine_type
           args: "{{ NODES.gcp_metdata.machine_types }}"
aszs commented 3 years ago

Actually we don't need a decorator at all, just set that as the default value in the declaration:

unfurl.nodes.Compute:
derived_from: unfurl.nodes.ComputeAbstract
properties:
  machine_type:
    type: string
    default:
      eval:
         python: helpers.py#choose_machine_type
         args: "{{ NODES.gcp_metdata.machine_types }}"