Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Mar 4, 2024. It is now read-only.
/geoengineerPublic archive

DEPRECATED — Infrastructure As Code

License

NotificationsYou must be signed in to change notification settings

coinbase/geoengineer

Repository files navigation

CircleCI

Mantle_of_Responsibility

GeoEngineer provides a Ruby DSL and command line tool (geo) tocodify then plan and execute changes to cloud resources.

GeoEngineer's goals/requirements/features are:

  1. DSL based on Terraform: GeoEngineer usesTerraform to plan and execute changes, so the DSL to describe resources is similar to Terraform's. GeoEngineer's DSL also provides programming and object oriented features like inheritance, abstraction, branching and looping.
  2. Development Workflow: GeoEngineer is built to be used within existing development workflows, e.g. branching, creating pull requests, code reviewing and merging. To simplify these workflows, GeoEngineer dynamically generates Terraform state files using cloud APIs and opinionated tagging.
  3. Extensible Validation: Every team has their own standards when managing cloud resources e.g. naming patterns, tagging requirements, security rules. GeoEngineer resources can have custom validations added to ensure the resources conform to required standards.
  4. Describe Existing Resources: Existing resources can be described with GeoEngineer without having to destroy and recreate them.
  5. Principle of Least Astonishment: show the exact plan before execution; do nothing without confirmation; do not allow a plan to be executed with failing validations; by default do not allow deletions; show warnings and hints to make code better.
  6. One File per Project: Managing dozens of projects with hundreds of files is difficult and error-prone, especially if a single project's resources are described across many files. Projects are easier to manage when they are each described in one file.
  7. Dependencies: resources have dependencies on other resources, projects have dependencies on other projects. Using Ruby'srequire file that describe resources can be included and referenced without having to hard-code any values.

Getting Started

Install Terraform

Instructions to install Terraform can be foundhere.

brew install terraform

Install Ruby

Instructions to install Ruby can be foundhere.

rbenv install `cat .ruby-version`

Install GeoEngineer

Build the gem locally and then refer to it withgeo on the command line.

bundle installgem build geoengineer.gemspecgem install geoengineer-version.gemgeo --help

Running GeoEngineer

Install and configureassume-role.

assume-role <account-id> <role>./geo --help

First GeoEngineer Project

GeoEngineer can use the folder structure where projects and environments are in theprojects andenvironments directories respectively, however everything can also be defined in a single file, e.g.first_project.rb:

# First define the environment which is available with the variable `env`# This is where project invariants are stored, e.g. subnets, vpc ...environment("staging"){account_id"1"subnet"1"vpc_id"1"allow_destroytrue## Defaults to false.  Set to true to support `geo destroy ...`}# Create the first_project to be in the `staging` environmentproject=project('org','first_project'){environments'staging'}# Define the security group for the ELB to allow HTTPelb_sg=project.resource("aws_security_group","allow_http"){name"allow_http"description"Allow All HTTP"vpc_idenv.vpc_idingress{from_port80to_port80protocol"tcp"cidr_blocks["0.0.0.0/0"]}tags{Name"allow_http"}}# Define the security group for EC2 to allow ingress from the ELBec2_sg=project.resource("aws_security_group","allow_elb"){name"allow_elb"description"Allow ELB to 80"vpc_idenv.vpc_idingress{from_port8000to_port8000protocol"tcp"security_groups[elb_sg]}tags{Name"allow_elb"}}# cloud_config to run webserveruser_data=%{#cloud-configruncmd:  - docker run -d --name nginx -p 8000:80 nginx}# Create an EC2 instance to run nginx serverinstance=project.resource("aws_instance","web"){ami"ami-1c94e10b"# COREOS AMIinstance_type"t1.micro"subnet_idenv.subnetuser_datauser_datatags{Name"ec2_instance"}}# Create the ELB connected to the instanceproject.resource("aws_elb","main-web-app"){name"main-app-elb"security_groups[elb_sg]subnets[env.subnet]instances[instance]listener{instance_port8000instance_protocol"http"lb_port80lb_protocol"http"}}

The GeoEngineer command line toolgeo can:

  1. Create a plan withgeo plan -e staging first_project.rb
  2. Execute the plan withgeo apply -e staging first_project.rb
  3. Create a graph withgeo graph -e staging --quiet first_project.rb | dot -Tpng > graph.png && open graph.png
  4. Status of Codified Resources withgeo status first_project.rb -e staging
  5. Query GPS Resource Graph withgeo query "*:*:*:*:*"

There are more examples in theexamples folder.

Customizations

GeoEngineer's DSL can be customized to your needs using validations, GPS and reusable methods on resources.

Validations

Below is an example which will add the validation to ensure that all listeners on all ELB's must be HTTPS, for security reasons.

classGeoEngineer::Resources::AwsElb <GeoEngineer::Resourcevalidate:validate_listeners_must_be_httpsdefvalidate_listeners_must_be_httpserrors=[]all_listener.select{ |i|i.lb_protocol !='https'}.eachdoerrors <<"ELB must use https protocol#{for_resource}"endreturnerrorsendend

Geo Planning System (GPS)

GeoEngineer describes resources in thecloud domain, notyour application domain. For example, security group ingress is the "cloud" way of defining "what can call your service". The friction between these two domains makes communication with others (e.g. developers) difficult.

GPS is an abstraction that helps you describe your cloud in the language of your domain. GPS:

  1. Uses Higher Level Vocabulary to build configurations.
  2. Explicit Configurations means no tricks; What you see is what you get.
  3. YAML and JSON Schema to strictly configure using known standards.
  4. Extensible Configuration lets GPS express any domain.
  5. Backwards Compatible: GPS is built to work with current GeoEngineer resources.

GPS files look likegps/org/first-project.yml:

<environment>:<configuration>:<node_type>:<node_name>:<attributes>:

The filename is used to define the project. Theenvironment andconfiguration are used to group nodes. Each configuration has multiple nodes, defined under their types. You can define your own node types that can allow multipleattributes.

For example, the file./gps/org/first-project.yml describes a nodeservice namedapi with configurationstaging in thedevelopment environment:

development:staging:service:api:ports:"80:80"

If you have multiple environments and wish something to be applied to all of them evenly, you can use_default as a special environment keyword. This will be applied to all known environments, unless they are already defined. For example, if you had a project that was deployed to all environments except one namedinternal, you could use the following example:

_default:common:service:api:ports:"80:80"internal:{}

Theservice node type is defined to take a string of ports and build a Load balancer:

# Load Balancer NodeclassGeoEngineer::GPS::Nodes::Service <GeoEngineer::GPS::Node# explicity define the exposed resources from this nodedefine_resource"aws_elb",:elb# define the types of attributes using JSON schemadefjson_schema{"type":"object","additionalProperties"=>false,"properties":{"ports":{"type":"string","default":"80:80"}}}end# called by GPS when creating resourcesdefcreate_resources(project)create_elb(project)# method created with `define_resource`setup_elbenddefsetup_elb# Set the values of the resource hereelb.ports=attributes["ports"]endend

To integrate with a project use:

project=gps.project("org","first-project",env)do |nodes|# query for api filling in the default env, config, project...nodes.find(":::service:api")end# Find the service# query syntax is `<project>:<environment>:<config>:<type>:<name>`service=gps.find("org/first-project:development:staging:service:api")# method to get the GeoEngineer resource ELBservice.elb# method to get the terraform reference to the resourceservice.elb_ref# return all service nodesgps.where("org/first-project:*:*:service:*").eachdo |node|node.elb.tags{ ...}end

Methods

Define methods to be used in your own resources, e.g. a custom method to security group to add a rule:

classGeoEngineer::Resources::AwsSecurityGroup <GeoEngineer::Resource# ...defall_egress_everywhereegress{from_port0to_port0protocol"-1"cidr_blocks["0.0.0.0/0"]}end# ...endproject.resource('aws_security_group','all_egress'){all_egress_everywhere# use the method to add egress}

Adding New Resources

The best way to contribute is to add resources that exist in Terraform but are not yet described in GeoEngineer.

To define a resource:

  1. checkout and fork/branch GeoEngineer
  2. create a file./lib/geoengineer/resources/<provider_type>/<resource_type>.rb
  3. define a classclass GeoEngineer::Resources::<ResourceType> < GeoEngineer::Resource
  4. define_terraform_id, and potentially_geo_id andself._fetch_remote_resources method (more below).
  5. write a test file for the resource that follows the style of other similar resources

Codified to Remote Resources

A fundamental problem with codifying resources is matching the in code resource to the real remote resource. Terraform does this by maintaining anid in a state file which is matched to a remote resources attribute. This attribute is different per resource, e.g. for ELB's it is theirname, for security groups it is theirgroup_name that is generated so cannot be codified.

Without a state file GeoEngineer uses API's to match resources, this makes generatedid's likes security groups difficult. For these generated ids GeoEngineer uses tags e.g. for ELB's the GeoEngineer id is itsname (just like Terraform) and for security groups it is theirName tag.

In a GeoEngineer resource the_terraform_id is the id used by Terraform and the_geo_id is GeoEngineer ID. By default a resources_geo_id is the same as the_terraform_id, so for most resources only the_terraform_id is required.

If_terraform_id is generated then the remote resource needed to be fetched via API and matched to the codified resource with_geo_id. This is done by implementing theself._fetch_remote_resources method to use the API and return a list of resources as an array of hashes each containing keys_terraform_id and_geo_id, then GeoEngineer will automatically match them.

For example, inaws_security_group's the resource is matched based on theName tag, implements as:

classGeoEngineer::Resources::AwsSecurityGroup <GeoEngineer::Resourceafter:initialize,->{_terraform_id->{NullObject.maybe(remote_resource)._terraform_id}}after:initialize,->{_geo_id->{NullObject.maybe(tags)[:Name]}}defself._fetch_remote_resources(provider)AwsClients.ec2(provider).describe_security_groups['security_groups'].map(&:to_h).mapdo |sg|sg[:name]=sg[:group_name]sg[:_terraform_id]=sg[:group_id]sg[:_geo_id]=sg[:tags] ?sg[:tags].select{ |x|x[:key] =="Name"}.first[:value] :nilsgendendend

Adding a New Provider

Adding resources for a new provider requires creating a new subfolder and resources referencing the provider name inlib/geoengineer/resources/. If necessary, utility methods for the new provider client are stored atlib/geoengineer/utils/. Once the resources files are defined, no further setup is needed as provider information is pulled in from resource definitions in the project files being planned and applied.

Validations

Terraform does not validate a lot of attributes before they are sent to the cloud. This means that often plans will fail for reasons that could have been initially validated. When creating a resource think about what validations could be done to ensure a plan is successful.

For example, a security groups needs aName tag, requires aname anddescription, and a more complicated example is that itscidr_blocks should be valid:

classGeoEngineer::Resources::AwsSecurityGroup <GeoEngineer::Resource# ...validate:validate_correct_cidr_blocksvalidate->{validate_required_attributes([:name,:description])}validate->{validate_has_tag(:Name)}defvalidate_correct_cidr_blockserrors=[](self.all_ingress +self.all_egress).eachdo |in_eg|nextunlessin_eg.cidr_blocksin_eg.cidr_blocks.eachdo |cidr|beginNetAddr::IPv4Net.parse(cidr)rescueNetAddr::ValidationErrorerrors <<"Bad cidr block\"#{cidr}\"#{for_resource}"endendenderrorsend# ...end

Terraform State

Terraform by default will attempt to sync its resources with the API so that its state file is up to date with the real world. Given that GeoEngineer uses Terraform in a different way this sometimes causes plans to list changes that have already happened.

To fix this issue a resource can overrideto_terraform_state method, e.g.aws_db_instance has issues withfinal_snapshot_identifier updating:

classGeoEngineer::Resources::AwsDbInstance <GeoEngineer::Resource# ...defto_terraform_statetfstate=supertfstate[:primary][:attributes]={'final_snapshot_identifier'=>final_snapshot_identifier,}tfstateend# ...end

GeoEngineer Reference

The core models in GeoEngineer are:

 +-------------+ 1 | Environment +-----------+ +-------------+           |       | 1                 |       |                   |       v *                 v * +-----+-------+ 1  * +-------------+ 1  *  +-------------+ | Project     +----->+ Resource    +------>+ SubResource | +-------------+      +-------------+       +-------------+
  1. Environment contains many resources that may exist outside of a project, like VPCs or routing tables. Also every project defined to be in the environment, for example thetest_www project is instaging butmonorail is instaging andproduction environments.
  2. Project contains many resources and services grouped together into a name.
  3. Resource andSubResource are based off of how terraform models cloud resources. AResource instance can have manySubResource instances, but aSubResource instance belongs to only oneResource instance, e.g. a load balancer resource may have ahealth_check sub-resource to only allow specific incoming ports.

All these models can have arbitrary attributes assigned to them either by directly assigning on the instance, or through passing a block to the constructor. For example:

resource=Resource.new('type','id'){ |res|# CORRECTres.hello='hey'putsres.hello# 'hey'hello'hey again'#putsres.hello# 'hey again'# INCORRECT way of assigning variablesgoodbye='nooo'# This assigns a local variable, not an attribute on the resourceputsres.goodbye# nil}putsresource.hello# 'hey again'resource.goodbye='see ya'putsresource.goodbye# 'see ya'

Additionally, if the value is expensive to calculate or requires other attributes not yet assigned, an attribute can be assigned aProc orlambda which will be calculated lazily:

resource=Resource.new('type','id')resource.lazy_attr=->{puts"CALCULATING THE VALUE";'value'}# ...putsresource.lazy_attr#$ "CALCULATING THE VALUE"#$ "value"

Environment

The top level class in GeoEngineer is theenvironment: it contains all projects, resources and services, and there should only ever be one initialized at a time.

An environment can mean many things to different people, e.g. an AWS account, an AWS region, or a specific AWS VPC. The only real constraint is that a resource has one instance per environment, e.g. a load balancer that is defined to be instaging andproduction environments, will have an instance in each.

The functionenvironment is provided as a factory to build an environment:

environment=environment("environment_name"){ |e|e.attr_1=[1,2,3]attr_2'value'}environment.attr_3="another value"

Project

A project is a group of resources typically provisioned to deploy one code base. A project has anorganization andname, to mimic the githubusername/organiztion andrepository structure.

A project is defined like:

project=project('org','project_name'){environments'staging','production'}

This projects organization isorg, its nameproject_name and will be provisioned in thestaging andproduction environments. Theorg andname must be unique across all other projects.

The methodproject will automatically add the project to the instantiated environment objectonly if that environment's name is in the list of environments, otherwise it is ignored.

Resources and SubResources

Resources are defined to be similar to theterraform resource configuration. The main difference is to not use= as this will create a local ruby variable and not assign the value.

AResource can be created with andenvironment orproject object (this will add that resource to that object):

environment.resource('type','identifier'){name"resource_name"subresource{attribute"attribute"}}project.resource('type','identifier'){# ...}

Thetype of a resource must be a valid terraform type, where AWS types are listedhere. Some resources are not supported yet by GeoEngineer.

identifier is used by GeoEngineer and terraform to reference this resource must be unique, however it is not stored in the cloud so can be changed without affecting a plan.

A resource also has a ruby block sent to it that contains parameters and sub-resources. These values are defined by terraform so for reference to what values are required please refer to theterraform docs.


[8]ページ先頭

©2009-2025 Movatter.jp