From 70ef9793a9bb3e109d6a985cb76c351ba80cb9ec Mon Sep 17 00:00:00 2001 From: piyushawasthi Date: Mon, 13 Feb 2017 15:02:35 +0530 Subject: [PATCH 1/3] Added feature of tagging Root volumes Signed-off-by: piyushawasthi --- lib/chef/knife/ec2_server_create.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/chef/knife/ec2_server_create.rb b/lib/chef/knife/ec2_server_create.rb index e74cdb80..751d9b1a 100644 --- a/lib/chef/knife/ec2_server_create.rb +++ b/lib/chef/knife/ec2_server_create.rb @@ -442,6 +442,11 @@ class Ec2ServerCreate < Knife :boolean => true, :default => false + option :volume_tags, + :long => "--volume-tags Tag=Value[,Tag=Value...]", + :description => "Tag the Root volume", + :proc => Proc.new { |volume_tags| volume_tags.split(',') } + def run $stdout.sync = true validate! @@ -509,6 +514,10 @@ def run printed_tags = hashed_tags.map{ |tag, val| "#{tag}: #{val}" }.join(", ") + hashed_volume_tags={} + volume_tags.map{ |t| key,val=t.split('='); hashed_volume_tags[key]=val} unless volume_tags.nil? + printed_volume_tags = hashed_volume_tags.map{ |tag, val| "#{tag}: #{val}" }.join(", ") + msg_pair("Instance ID", @server.id) msg_pair("Flavor", @server.flavor_id) msg_pair("Image", @server.image_id) @@ -530,6 +539,7 @@ def run msg_pair("IAM Profile", locate_config_value(:iam_instance_profile)) msg_pair("Tags", printed_tags) + msg_pair("Volume Tags", printed_volume_tags) msg_pair("SSH Key", @server.key_name) print "\n#{ui.color("Waiting for EC2 to create the instance", :magenta)}" @@ -543,6 +553,7 @@ def run tries = 6 begin create_tags(hashed_tags) unless hashed_tags.empty? + create_volume_tags(hashed_volume_tags) unless hashed_volume_tags.empty? associate_eip(elastic_ip) if config[:associate_eip] enable_classic_link(config[:classic_link_vpc_id], config[:classic_link_vpc_security_group_ids]) if config[:classic_link_vpc_id] rescue Fog::Compute::AWS::NotFound, Fog::Errors::Error @@ -620,6 +631,7 @@ def run device_map = @server.block_device_mapping.first msg_pair("Root Volume ID", device_map['volumeId']) msg_pair("Root Device Name", device_map['deviceName']) + msg_pair("Root Volume Tags", printed_volume_tags) msg_pair("Root Device Delete on Terminate", device_map['deleteOnTermination']) msg_pair("Standard or Provisioned IOPS", device_map['volumeType']) msg_pair("IOPS rate", device_map['iops']) @@ -994,9 +1006,7 @@ def ssl_config_user_data $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH end @@ -1444,6 +1454,21 @@ def evaluate_node_name(node_name) return node_name%server.id end + def volume_tags + volume_tags = locate_config_value(:volume_tags) + if !volume_tags.nil? and volume_tags.length != volume_tags.to_s.count('=') + ui.error("Volume Tags should be entered in a key = value pair") + exit 1 + end + volume_tags + end + + def create_volume_tags(hashed_volume_tags) + hashed_volume_tags.each_pair do |key,val| + connection.tags.create :key => key, :value => val, :resource_id => @server.block_device_mapping.first['volumeId'] + end + end + end end end From 5e5de3bcda421f17f7cba3dfa365318dd56b85b3 Mon Sep 17 00:00:00 2001 From: Piyush Date: Mon, 13 Feb 2017 21:24:41 +0530 Subject: [PATCH 2/3] Fixed failing ec2 server create specs Signed-off-by: Piyush --- spec/unit/ec2_server_create_spec.rb | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/spec/unit/ec2_server_create_spec.rb b/spec/unit/ec2_server_create_spec.rb index b62dd0a8..277f00b4 100644 --- a/spec/unit/ec2_server_create_spec.rb +++ b/spec/unit/ec2_server_create_spec.rb @@ -1592,9 +1592,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH end @@ -1624,9 +1622,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH end @@ -1694,7 +1690,7 @@ end it 'returns false' do - expect(knife_ec2_create.ssl_config_data_already_exist?).to eq(true) + expect(knife_ec2_create.ssl_config_data_already_exist?).to eq(false) end end @@ -1751,9 +1747,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH knife_ec2_create.config[:aws_user_data] = @user_user_data @@ -1801,9 +1795,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH knife_ec2_create.config[:aws_user_data] = @user_user_data @@ -1844,9 +1836,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH end @@ -1869,9 +1859,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes - EOH knife_ec2_create.config[:aws_user_data] = @user_user_data @@ -1956,9 +1944,7 @@ $thumbprint = (Get-ChildItem -Path cert:\\localmachine\\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint; $create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'" iex $create_listener_cmd - netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=any enable=yes -