Skip to content

Instantly share code, notes, and snippets.

@Ladas
Last active June 13, 2016 14:45
Show Gist options
  • Save Ladas/f7808133008cb5161b65365c607217eb to your computer and use it in GitHub Desktop.
Save Ladas/f7808133008cb5161b65365c607217eb to your computer and use it in GitHub Desktop.
ManageIQ automate code for creating and deploying CFN template
def template_interface(name, subnet_id, security_group)
eip = "#{name}Address"
eip_association = "Associate#{name}"
network_interface = "#{name}"
{
eip => {
"Type"=>"AWS::EC2::EIP",
"Properties"=>{"Domain"=>"vpc"}},
eip_association => {
"Type"=>"AWS::EC2::EIPAssociation",
"Properties"=>
{"AllocationId"=>{"Fn::GetAtt"=>[eip, "AllocationId"]},
"NetworkInterfaceId"=>{"Ref"=>network_interface}}},
network_interface => {
"Type"=>"AWS::EC2::NetworkInterface",
"Properties" => {
"SubnetId"=> subnet_id,
"Description"=>"",
"GroupSet"=>[{"Ref"=>security_group}],
"SourceDestCheck"=>"true",
"Tags"=>[{"Key"=>"Network", "Value"=>"Control"}]}},
}
end
def security_group(security_group, vpc_id)
{
security_group =>
{"Type"=>"AWS::EC2::SecurityGroup",
"Properties"=>
{"VpcId"=> vpc_id,
"GroupDescription"=>"Enable SSH access via port 22",
"SecurityGroupIngress"=>
[{"IpProtocol"=>"tcp",
"FromPort"=>"22",
"ToPort"=>"22",
"CidrIp"=>"0.0.0.0/0"}]}},
}
end
def security_group_1(security_group, vpc_id)
{
security_group =>
{"Type"=>"AWS::EC2::SecurityGroup",
"Properties"=>
{"VpcId" => vpc_id,
"GroupDescription"=>"Enable HTTP access via user defined port",
"SecurityGroupIngress"=>
[{"IpProtocol"=>"tcp",
"FromPort"=>80,
"ToPort"=>80,
"CidrIp"=>"0.0.0.0/0"}]}},
}
end
def instance(name, network_interfaces, keyname, image_id)
nics = network_interfaces.each_with_index.map do |x, i|
{"NetworkInterfaceId" => {"Ref" => x}, "DeviceIndex" => i.to_s}
end
{
name =>
{"Type"=>"AWS::EC2::Instance",
"Properties"=> {
"InstanceType" => "t1.micro",
"ImageId"=> image_id,
"KeyName"=> keyname,
"NetworkInterfaces"=> nics,
"Tags"=>[{"Key"=>"Role", "Value"=>"Test Instance"}],
"UserData"=>
{"Fn::Base64"=>
{"Fn::Join"=>
["",
["#!/bin/bash -ex",
"\n",
"\n",
"yum install ec2-net-utils -y",
"\n",
"ec2ifup eth1",
"\n",
"service httpd start"]]}}}}
}
end
def base_template
{"Resources"=>
{
}
}
end
def create_template
vpc_id = 'vpc-a06de3c5'
security_group_1 = "ControlSecurityGroup"
security_group_2 = "WebSecurityGroup"
nic1 = "ControlXface"
nic2 = "WebXface"
template_content = base_template
template_content["Resources"].merge!(security_group(security_group_1, vpc_id))
template_content["Resources"].merge!(security_group(security_group_2, vpc_id))
template_content["Resources"].merge!(template_interface(nic1, 'subnet-ac904787', security_group_1))
template_content["Resources"].merge!(template_interface(nic2, 'subnet-1852bb33', security_group_2))
template_content["Resources"].merge!(instance("Ec2Instance", [nic1, nic2], "EmsRefreshSpec-KeyPair", 'ami-5769193e'))
$evm.vmdb('orchestration_template_cfn').create(
:name => 'amazon_test',
:orderable => true,
:content => JSON.pretty_generate(template_content))
end
def deploy_amazon_stack(orchestration_manager, parent_service, template)
orchestration_service = $evm.vmdb('ServiceOrchestration').create(
:name => "#{parent_service.name}")
orchestration_service.stack_name = "#{parent_service.name}"
orchestration_service.orchestration_template = template
orchestration_service.orchestration_manager = orchestration_manager
orchestration_service.stack_options = {}
orchestration_service.display = true
orchestration_service.parent_service = parent_service
orchestration_service.deploy_orchestration_stack
end
begin
nsd = $evm.get_state_var(:nsd)
$evm.log("info", "Listing nsd #{nsd}")
$evm.log("info", "Listing Root Object Attributes:")
$evm.root.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") }
$evm.log("info", "===========================================")
parent_service = $evm.root['service_template_provision_task'].destination
parent_service.name = $evm.root.attributes['dialog_service_name']
orchestration_manager = $evm.vmdb('ManageIQ_Providers_Amazon_CloudManager').all.first
template = create_template
deploy_amazon_stack(orchestration_manager, parent_service, template)
rescue => err
$evm.log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
$evm.root['ae_result'] = 'error'
$evm.root['ae_reason'] = "Error: #{err.message}"
exit MIQ_ERROR
end
{
"Resources": {
"ControlSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": "vpc-a06de3c5",
"GroupDescription": "Enable SSH access via port 22",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": "0.0.0.0/0"
}
]
}
},
"WebSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": "vpc-a06de3c5",
"GroupDescription": "Enable SSH access via port 22",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": "0.0.0.0/0"
}
]
}
},
"ControlXfaceAddress": {
"Type": "AWS::EC2::EIP",
"Properties": {
"Domain": "vpc"
}
},
"AssociateControlXface": {
"Type": "AWS::EC2::EIPAssociation",
"Properties": {
"AllocationId": {
"Fn::GetAtt": [
"ControlXfaceAddress",
"AllocationId"
]
},
"NetworkInterfaceId": {
"Ref": "ControlXface"
}
}
},
"ControlXface": {
"Type": "AWS::EC2::NetworkInterface",
"Properties": {
"SubnetId": "subnet-ac904787",
"Description": "",
"GroupSet": [
{
"Ref": "ControlSecurityGroup"
}
],
"SourceDestCheck": "true",
"Tags": [
{
"Key": "Network",
"Value": "Control"
}
]
}
},
"WebXfaceAddress": {
"Type": "AWS::EC2::EIP",
"Properties": {
"Domain": "vpc"
}
},
"AssociateWebXface": {
"Type": "AWS::EC2::EIPAssociation",
"Properties": {
"AllocationId": {
"Fn::GetAtt": [
"WebXfaceAddress",
"AllocationId"
]
},
"NetworkInterfaceId": {
"Ref": "WebXface"
}
}
},
"WebXface": {
"Type": "AWS::EC2::NetworkInterface",
"Properties": {
"SubnetId": "subnet-1852bb33",
"Description": "",
"GroupSet": [
{
"Ref": "WebSecurityGroup"
}
],
"SourceDestCheck": "true",
"Tags": [
{
"Key": "Network",
"Value": "Control"
}
]
}
},
"Ec2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t1.micro",
"ImageId": "ami-5769193e",
"KeyName": "EmsRefreshSpec-KeyPair",
"NetworkInterfaces": [
{
"NetworkInterfaceId": {
"Ref": "ControlXface"
},
"DeviceIndex": "0"
},
{
"NetworkInterfaceId": {
"Ref": "WebXface"
},
"DeviceIndex": "1"
}
],
"Tags": [
{
"Key": "Role",
"Value": "Test Instance"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -ex",
"\n",
"\n",
"yum install ec2-net-utils -y",
"\n",
"ec2ifup eth1",
"\n",
"service httpd start"
]
]
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment