Skip to content

Commit

Permalink
Cleaning up some merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-ball committed Feb 13, 2015
1 parent e747ed1 commit 3799a43
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 120 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in kitchen-ec2.gemspec
gemspec

gem 'test-kitchen', git: 'https://github.com/test-kitchen/test-kitchen', branch: 'windows-guest-support'

group :test do
gem 'rake'
gem 'pry'
Expand Down
2 changes: 1 addition & 1 deletion kitchen-ec2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency 'test-kitchen', '~> 1.0'
gem.add_dependency 'test-kitchen'
gem.add_dependency 'fog'

gem.add_development_dependency 'rspec'
Expand Down
230 changes: 112 additions & 118 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Ec2 < Kitchen::Driver::Base
default_config :tags, { 'created-by' => 'test-kitchen' }
default_config :user_data, nil
default_config :iam_profile_name, nil
default_config :price, nil
default_config :price, nil
default_config :aws_access_key_id do |driver|
ENV['AWS_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID']
end
Expand Down Expand Up @@ -84,6 +84,10 @@ def create(state)
state[:password] = config[:password] if config[:password]
state[:username] = config[:username] if config[:username]

# TODO are these used by the transport yet?
state[:ssh_timeout] = config[:ssh_timeout]
state[:ssh_retries] = config[:ssh_retries]

info("Creating <#{state[:server_id]}>...")
info("If you are not using an account that qualifies under the AWS")
info("free-tier, you may be charged to run these suites. The charge")
Expand All @@ -95,7 +99,7 @@ def create(state)
server = submit_spot
else
# On-demand instance
server = create_server
server = submit_server
end

state[:server_id] = server.id
Expand All @@ -111,7 +115,6 @@ def create(state)
print '(Server Ready)'
state[:hostname] = hostname(server)

# TODO :ssh_timeout and :ssh_retries
#Windows preparartion
if transport.name.casecmp('winrm') == 0
debug("Waiting for Windows")
Expand Down Expand Up @@ -189,104 +192,134 @@ def connection
end

# Fog AWS helper for creating the instance
def create_server
def submit_server
debug_server_config

debug('Creating EC2 Instance..')
connection.servers.create(
:availability_zone => config[:availability_zone],
:security_group_ids => config[:security_group_ids],
:tags => config[:tags],
:flavor_id => config[:flavor_id],
:ebs_optimized => config[:ebs_optimized],
:image_id => config[:image_id],
:key_name => config[:aws_ssh_key_id],
:subnet_id => config[:subnet_id],
:iam_instance_profile_name => config[:iam_profile_name],
:associate_public_ip => config[:associate_public_ip],
:user_data => (config[:user_data].nil? ? nil :
(File.file?(config[:user_data]) ?
File.read(config[:user_data]) : config[:user_data]
)
),
:block_device_mapping => [{
'Ebs.VolumeSize' => config[:ebs_volume_size],
'Ebs.DeleteOnTermination' => config[:ebs_delete_on_termination],
'DeviceName' => config[:ebs_device_name]
}]
)
connection.servers.create(common_ec2_instance)
end

def request_spot
debug_server_config

debug('Creating EC2 Spot Instance..')
instance = common_ec2_instance
instance[:price] = config[:price]
instance[:instance_count] = config[:instance_count]
connection.spot_requests.create(instance)
end

def submit_spot
spot = request_spot
info("Spot instance <#{spot.id}> requested.")
info("Spot price is <#{spot.price}>.")
spot.wait_for { print '.'; spot.state == 'active' }
print '(spot active)'

# tag assignation on the instance.
if config[:tags]
connection.create_tags(
spot.instance_id,
spot.tags
)
end
connection.servers.get(spot.instance_id)
end

def common_ec2_instance
{
:availability_zone => config[:availability_zone],
:groups => config[:security_group_ids],
:tags => config[:tags],
:flavor_id => config[:flavor_id],
:ebs_optimized => config[:ebs_optimized],
:image_id => config[:image_id],
:key_name => config[:aws_ssh_key_id],
:subnet_id => config[:subnet_id],
:iam_instance_profile_name => config[:iam_profile_name],
:associate_public_ip => config[:associate_public_ip],
:user_data => prepared_user_data,
:block_device_mapping => [{
'Ebs.VolumeSize' => config[:ebs_volume_size],
'Ebs.DeleteOnTermination' => config[:ebs_delete_on_termination],
'DeviceName' => config[:ebs_device_name]
}]
}
end

# TODO add winrm_config to user_data
# Method for preparing user_data for enabling PS Remoting if the selected
# transport method is WinRM

def prepared_user_data
if transport.name.casecmp('winrm') == 0
debug("Injecting WinRM config to EC2 user_data")
# If user_data is a file reference, lets read it as such
unless config[:user_data].nil?
if File.file?(config[:user_data])
config[:user_data] = File.read(config[:user_data])
end

#Preparing custom static admin user if we defined something other than Administrator
customAdminScript = ''
if config[:username].casecmp('Administrator') != 0
debug('Injecting custom Local Administrator:')
debug("username '#{config[:username]}'")
debug("password '#{config[:password]}'")
if transport.name.casecmp('winrm') == 0
debug("Injecting WinRM config to EC2 user_data")

customAdminScript = <<-EOH.gsub(/^ {10}/, '')
"Disabling Complex Passwords" >> $logfile
$seccfg = [IO.Path]::GetTempFileName()
& secedit.exe /export /cfg $seccfg >> $logfile
(Get-Content $seccfg) | Foreach-Object {$_ -replace "PasswordComplexity\\s*=\\s*1", "PasswordComplexity = 0"} | Set-Content $seccfg
& secedit.exe /configure /db $env:windir\\security\\new.sdb /cfg $seccfg /areas SECURITYPOLICY >> $logfile
& cp $seccfg "c:\\"
& del $seccfg
#Preparing custom static admin user if we defined something other than Administrator
customAdminScript = ''
if config[:username].casecmp('Administrator') != 0
debug('Injecting custom Local Administrator:')
debug("username '#{config[:username]}'")
debug("password '#{config[:password]}'")

$username="#{config[:username]}"
$password="#{config[:password]}"
customAdminScript = <<-EOH.gsub(/^ {10}/, '')
"Disabling Complex Passwords" >> $logfile
$seccfg = [IO.Path]::GetTempFileName()
& secedit.exe /export /cfg $seccfg >> $logfile
(Get-Content $seccfg) | Foreach-Object {$_ -replace "PasswordComplexity\\s*=\\s*1", "PasswordComplexity = 0"} | Set-Content $seccfg
& secedit.exe /configure /db $env:windir\\security\\new.sdb /cfg $seccfg /areas SECURITYPOLICY >> $logfile
& cp $seccfg "c:\\"
& del $seccfg
"Creating static user: $username" >> $logfile
& net.exe user /y /add $username $password >> $logfile
$username="#{config[:username]}"
$password="#{config[:password]}"
"Adding $username to Administrators" >> $logfile
& net.exe localgroup Administrators /add $username >> $logfile
"Creating static user: $username" >> $logfile
& net.exe user /y /add $username $password >> $logfile
EOH
end
"Adding $username to Administrators" >> $logfile
& net.exe localgroup Administrators /add $username >> $logfile
# Returning the fully constructed PowerShell script to user_data
return <<-EOH.gsub(/^ {10}/, '')
<powershell>
$logfile="C:\\Program Files\\Amazon\\Ec2ConfigService\\Logs\\kitchen-ec2.log"
EOH
end

#PS Remoting and & winrm.cmd basic config
Enable-PSRemoting -Force -SkipNetworkProfileCheck
& winrm.cmd quickconfig -q >> $logfile
& winrm.cmd set winrm/config '@{MaxTimeoutms="1800000"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="512"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser="50"}' >> $logfile
# Returning the fully constructed PowerShell script to user_data
config[:user_data] = <<-EOH.gsub(/^ {12}/, '')
<powershell>
$logfile="C:\\Program Files\\Amazon\\Ec2ConfigService\\Logs\\kitchen-ec2.log"
#Client settings
& winrm.cmd set winrm/config/client/auth '@{Basic="true"}' >> $logfile
#PS Remoting and & winrm.cmd basic config
Enable-PSRemoting -Force -SkipNetworkProfileCheck
& winrm.cmd quickconfig -q >> $logfile
& winrm.cmd set winrm/config '@{MaxTimeoutms="1800000"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="512"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser="50"}' >> $logfile
#Server settings
& winrm.cmd set winrm/config/service/auth '@{Basic="true"}' >> $logfile
& winrm.cmd set winrm/config/service '@{AllowUnencrypted="true"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile
#Client settings
& winrm.cmd set winrm/config/client/auth '@{Basic="true"}' >> $logfile
#Firewall Config
& netsh.exe advfirewall set publicprofile state off >> $logfile
& netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any >> $logfile
#Server settings
& winrm.cmd set winrm/config/service/auth '@{Basic="true"}' >> $logfile
& winrm.cmd set winrm/config/service '@{AllowUnencrypted="true"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile
#{customAdminScript}
#Firewall Config
& netsh.exe advfirewall set publicprofile state off >> $logfile
& netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any >> $logfile
#{config[:user_data]}
#{customAdminScript}
</powershell>
EOH
else
return config[:user_data] if !config[:user_data].nil?
#{config[:user_data]}
</powershell>
EOH
end
end
return ''
config[:user_data]
end

# Helper method to check whether Amazon reported a server Ready
Expand All @@ -313,29 +346,6 @@ def windows_password state
return ''
end

def request_spot
debug_server_config

connection.spot_requests.create(
:availability_zone => config[:availability_zone],
:groups => config[:security_group_ids],
:tags => config[:tags],
:flavor_id => config[:flavor_id],
:ebs_optimized => config[:ebs_optimized],
:image_id => config[:image_id],
:key_name => config[:aws_ssh_key_id],
:subnet_id => config[:subnet_id],
:iam_instance_profile_name => config[:iam_profile_name],
:user_data => (config[:user_data].nil? ? nil :
(File.file?(config[:user_data]) ?
File.read(config[:user_data]) : config[:user_data]
)
),
:price => config[:price],
:instance_count => config[:instance_count]
)
end

# Debug helper to display applied configuration
def debug_server_config
debug('EC2 Server Configuration')
Expand Down Expand Up @@ -393,26 +403,10 @@ def aws_private_key
begin
ENV['AWS_PRIVATE_KEY'] || ENV['AWS_SSH_KEY'] || (File.read config[:ssh_key])
rescue
debug('SSH_KEY_RAW and SSH_KEY is not set.')
debug('AWS_PRIVATE_KEY and AWS_SSH_KEY are not set.')
end
end

def submit_spot
spot = request_spot
info("Spot instance <#{spot.id}> requested.")
info("Spot price is <#{spot.price}>.")
spot.wait_for { print '.'; spot.state == 'active' }
print '(spot active)'

# tag assignation on the instance.
if config[:tags]
connection.create_tags(
spot.instance_id,
spot.tags
)
end
connection.servers.get(spot.instance_id)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

before do
instance
allow(driver).to receive(:create_server).and_return(server)
allow(driver).to receive(:submit_server).and_return(server)
allow(driver).to receive(:wait_for_sshd)
end

Expand Down

0 comments on commit 3799a43

Please sign in to comment.