Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #231 from DJRH/add-opsworks
Browse files Browse the repository at this point in the history
Add OpsWorks support (stacks and custom layers)
  • Loading branch information
dtan4 authored Jun 26, 2016
2 parents 131facb + eeffadf commit e049e00
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/terraforming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
require "terraforming/resource/internet_gateway"
require "terraforming/resource/network_acl"
require "terraforming/resource/network_interface"
require "terraforming/resource/opsworks_custom_layer"
require "terraforming/resource/opsworks_stack"
require "terraforming/resource/rds"
require "terraforming/resource/redshift"
require "terraforming/resource/route_table"
Expand Down
10 changes: 10 additions & 0 deletions lib/terraforming/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ def nif
execute(Terraforming::Resource::NetworkInterface, options)
end

desc "opwcl", "OpsWorks Custom Layer"
def opwcl
execute(Terraforming::Resource::OpsWorksCustomLayer, options)
end

desc "opws", "OpsWorks Stack"
def opws
execute(Terraforming::Resource::OpsWorksStack, options)
end

desc "r53r", "Route53 Record"
def r53r
execute(Terraforming::Resource::Route53Record, options)
Expand Down
119 changes: 119 additions & 0 deletions lib/terraforming/resource/opsworks_custom_layer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module Terraforming
module Resource
class OpsWorksCustomLayer
include Terraforming::Util

def self.tf(client: Aws::OpsWorks::Client.new(region: 'us-east-1'))
self.new(client).tf
end

def self.tfstate(client: Aws::OpsWorks::Client.new(region: 'us-east-1'))
self.new(client).tfstate
end

def initialize(client)
@client = client
end

def tf
apply_template(@client, "tf/opsworks_custom_layer")
end

def tfstate
stacks.inject({}) do |resources, stack|

stack_layers(stack.stack_id).each do |layer|
attributes = {
"auto_assign_elastic_ips" => layer.auto_assign_elastic_ips.to_s,
"auto_assign_public_ips" => layer.auto_assign_public_ips.to_s,
"auto_healing" => layer.enable_auto_healing.to_s,
"custom_instance_profile_arn" => layer.custom_instance_profile_arn,
"custom_security_group_ids" => layer.custom_security_group_ids.join(","),
"drain_elb_on_shutdown" => layer.lifecycle_event_configuration.shutdown.delay_until_elb_connections_drained.to_s,
"install_updates_on_boot" => layer.install_updates_on_boot.to_s,
"instance_shutdown_timeout" => layer.lifecycle_event_configuration.shutdown.execution_timeout.to_s,
"name" => layer.name,
"short_name" => layer.shortname,
"stack_id" => layer.stack_id,
"use_ebs_optimized_instances" => layer.use_ebs_optimized_instances.to_s,
}

attributes["custom_security_group_ids.#"] = layer.custom_security_group_ids.count.to_s
layer.custom_security_group_ids.each do |sg|
attributes["custom_security_group_ids.#{Zlib.crc32(sg)}"] = sg
end

attributes["system_packages.#"] = layer.packages.count.to_s
layer.packages.each do |package|
attributes["system_packages.#{Zlib.crc32(package)}"] = package
end

attributes["custom_setup_recipes.#"] = layer.custom_recipes.setup.count.to_s
layer.custom_recipes.setup.each_with_index do |recipe, index|
attributes["custom_setup_recipes.#{index}"] = recipe
end

attributes["custom_configure_recipes.#"] = layer.custom_recipes.configure.count.to_s
layer.custom_recipes.configure.each_with_index do |recipe, index|
attributes["custom_configure_recipes.#{index}"] = recipe
end

attributes["custom_deploy_recipes.#"] = layer.custom_recipes.deploy.count.to_s
layer.custom_recipes.deploy.each_with_index do |recipe, index|
attributes["custom_deploy_recipes.#{index}"] = recipe
end

attributes["custom_undeploy_recipes.#"] = layer.custom_recipes.undeploy.count.to_s
layer.custom_recipes.undeploy.each_with_index do |recipe, index|
attributes["custom_undeploy_recipes.#{index}"] = recipe
end

attributes["custom_shutdown_recipes.#"] = layer.custom_recipes.shutdown.count.to_s
layer.custom_recipes.shutdown.each_with_index do |recipe, index|
attributes["custom_shutdown_recipes.#{index}"] = recipe
end

attributes["ebs_volume.#"] = layer.volume_configurations.count.to_s
layer.volume_configurations.each do |volume|
index = Zlib.crc32(volume.mount_point)
attributes["ebs_volume.#{index}.mount_point"] = volume.mount_point
attributes["ebs_volume.#{index}.size"] = volume.size.to_s
attributes["ebs_volume.#{index}.number_of_disks"] = volume.number_of_disks.to_s
attributes["ebs_volume.#{index}.raid_level"] = volume.raid_level.to_s
attributes["ebs_volume.#{index}.type"] = volume.volume_type
attributes["ebs_volume.#{index}.iops"] = volume.iops.to_s
end

resources["aws_opsworks_custom_layer.#{module_name_of(stack)}_#{module_name_of(layer)}"] = {
"type" => "aws_opsworks_custom_layer",
"primary" => {
"id" => layer.layer_id,
"attributes" => attributes,
"meta" => {
"schema_version" => "1"
}
}
}
end

resources
end
end

private

def stacks
@client.describe_stacks.stacks
end

def stack_layers(stack_id)
@client.describe_layers({stack_id: stack_id}).layers
end

def module_name_of(object)
normalize_module_name(object.name)
end

end
end
end
81 changes: 81 additions & 0 deletions lib/terraforming/resource/opsworks_stack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module Terraforming
module Resource
class OpsWorksStack
include Terraforming::Util

def self.tf(client: Aws::OpsWorks::Client.new(region: 'us-east-1'))
self.new(client).tf
end

def self.tfstate(client: Aws::OpsWorks::Client.new(region: 'us-east-1'))
self.new(client).tfstate
end

def initialize(client)
@client = client
end

def tf
apply_template(@client, "tf/opsworks_stack")
end

def tfstate
stacks.inject({}) do |resources, stack|
attributes = {
"color" => stack.attributes["Color"],
"default_availability_zone" => stack.default_availability_zone,
"default_instance_profile_arn" => stack.default_instance_profile_arn,
"default_os" => stack.default_os,
"default_root_device_type" => stack.default_root_device_type,
"default_ssh_key_name" => stack.default_ssh_key_name,
"default_subnet_id" => stack.default_subnet_id,
"hostname_theme" => stack.hostname_theme,
"name" => stack.name,
"region" => stack.region,
"service_role_arn" => stack.service_role_arn,
"use_custom_cookbooks" => stack.use_custom_cookbooks.to_s,
"use_opsworks_security_groups" => stack.use_opsworks_security_groups.to_s,
"vpc_id" => stack.vpc_id,

"configuration_manager_name" => stack.configuration_manager.name,
"configuration_manager_version" => stack.configuration_manager.version,

"berkshelf_version" => stack.chef_configuration.berkshelf_version,
"manage_berkshelf" => stack.chef_configuration.manage_berkshelf.to_s,

"custom_cookbooks_source.password" => stack.custom_cookbooks_source.password,
"custom_cookbooks_source.revision" => stack.custom_cookbooks_source.revision,
"custom_cookbooks_source.ssh_key" => stack.custom_cookbooks_source.ssh_key,
"custom_cookbooks_source.type" => stack.custom_cookbooks_source.type,
"custom_cookbooks_source.url" => stack.custom_cookbooks_source.url,
"custom_cookbooks_source.username" => stack.custom_cookbooks_source.username
}

resources["aws_opsworks_stack.#{module_name_of(stack)}"] = {
"type" => "aws_opsworks_stack",
"primary" => {
"id" => stack.stack_id,
"attributes" => attributes,
"meta" => {
"schema_version" => "1"
}
}
}

resources
end
end

private

def stacks
@client.describe_stacks.stacks
end

def module_name_of(stack)
normalize_module_name(stack.name)
end

end
end
end
40 changes: 40 additions & 0 deletions lib/terraforming/template/tf/opsworks_custom_layer.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<% stacks.each do |stack| -%>
<% stack_layers(stack.stack_id).each do |layer| -%>
resource "aws_opsworks_custom_layer" "<%= module_name_of(stack) + "_" + module_name_of(layer) %>" {
auto_assign_elastic_ips = "<%= layer.auto_assign_elastic_ips %>"
auto_assign_public_ips = "<%= layer.auto_assign_public_ips %>"
auto_healing = "<%= layer.enable_auto_healing %>"
custom_instance_profile_arn = "<%= layer.custom_instance_profile_arn %>"
custom_security_group_ids = <%= layer.custom_security_group_ids %>
drain_elb_on_shutdown = "<%= layer.lifecycle_event_configuration.shutdown.delay_until_elb_connections_drained %>"
install_updates_on_boot = "<%= layer.install_updates_on_boot ? true : false %>"
instance_shutdown_timeout = "<%= layer.lifecycle_event_configuration.shutdown.execution_timeout %>"
name = "<%= layer.name %>"
short_name = "<%= layer.shortname %>"
stack_id = "<%= layer.stack_id %>"
system_packages = <%= layer.packages %>
use_ebs_optimized_instances = "<%= layer.use_ebs_optimized_instances %>"

custom_setup_recipes = <%= layer.custom_recipes.setup %>
custom_configure_recipes = <%= layer.custom_recipes.configure %>
custom_deploy_recipes = <%= layer.custom_recipes.deploy %>
custom_undeploy_recipes = <%= layer.custom_recipes.undeploy %>
custom_shutdown_recipes = <%= layer.custom_recipes.shutdown %>

<% layer.volume_configurations.each do |volume| -%>
ebs_volume = {
mount_point = "<%= volume.mount_point %>"
size = "<%= volume.size %>"
number_of_disks = "<%= volume.number_of_disks %>"
<% if volume.raid_level -%>
raid_level = "<%= volume.raid_level %>"
<% end -%>
type = "<%= volume.volume_type %>"
iops = "<%= (volume.iops || 0) %>"
}
<% end -%>
}

<% end -%>

<% end -%>
33 changes: 33 additions & 0 deletions lib/terraforming/template/tf/opsworks_stack.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<% stacks.each do |stack| -%>
resource "aws_opsworks_stack" "<%= module_name_of(stack) %>" {
color = "<%= stack.attributes['Color'] %>"
default_availability_zone = "<%= stack.default_availability_zone %>"
default_instance_profile_arn = "<%= stack.default_instance_profile_arn %>"
default_os = "<%= stack.default_os %>"
default_root_device_type = "<%= stack.default_root_device_type %>"
default_ssh_key_name = "<%= stack.default_ssh_key_name %>"
default_subnet_id = "<%= stack.default_subnet_id %>"
hostname_theme = "<%= stack.hostname_theme %>"
name = "<%= stack.name %>"
region = "<%= stack.region %>"
service_role_arn = "<%= stack.service_role_arn %>"
use_custom_cookbooks = "<%= stack.use_custom_cookbooks %>"
use_opsworks_security_groups = "<%= stack.use_opsworks_security_groups %>"
vpc_id = "<%= stack.vpc_id %>"

configuration_manager_name = "<%= stack.configuration_manager.name %>"
configuration_manager_version = "<%= stack.configuration_manager.version %>"

berkshelf_version = "<%= stack.chef_configuration.berkshelf_version %>"
manage_berkshelf = "<%= stack.chef_configuration.manage_berkshelf %>"

custom_cookbooks_source = {
password = "<%= stack.custom_cookbooks_source.password %>"
revision = "<%= stack.custom_cookbooks_source.revision %>"
ssh_key = "<%= stack.custom_cookbooks_source.ssh_key %>"
type = "<%= stack.custom_cookbooks_source.type %>"
url = "<%= stack.custom_cookbooks_source.url %>"
username = "<%= stack.custom_cookbooks_source.username %>"
}
}
<% end -%>

0 comments on commit e049e00

Please sign in to comment.