Skip to content

Instantly share code, notes, and snippets.

@tmatilai
Last active October 13, 2015 12:18
Show Gist options
  • Save tmatilai/4194666 to your computer and use it in GitHub Desktop.
Save tmatilai/4194666 to your computer and use it in GitHub Desktop.
POC for integrating knife-ec2 and knife-solo plugins

knife-ec2-solo

Description

Proof of concept implementation to integrate knife-ec2 and knife-solo Chef Knife plugins. Adds --solo option to knife ec2 server create that uses knife solo bootstrap for bootstrapping the instance. Also other solo bootstrap options are available.

Requirements

  • "knife-ec2" gem
  • "knife-solo" gem

Installation

Copy the knife_ec2_solo.rb to ~/.chef/plugins/knife/ directory.

#
# Author:: Teemu Matilainen <[email protected]>
# Copyright 2012, Reaktor Innovations Oy
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/knife/ec2_server_create'
require 'chef/knife/solo_bootstrap'
require 'knife-solo/kitchen_command'
class Chef
class Knife
class Ec2ServerCreate
include KnifeSolo::KitchenCommand
def self.load_deps
super
SoloBootstrap.load_deps
end
option :solo,
:long => "--solo",
:description => "Bootstrap Chef using knife-solo"
self.options.merge! SoloBootstrap.options
# Rename and override knife-ec2 plugin's validate! method
alias_method :orig_validate!, :validate!
def validate!(*args)
orig_validate!(*args)
validate_kitchen! if config[:solo]
end
# Rename and override knife-ec2 plugin's bootstrap_for_node method
alias_method :chef_server_bootstrap_for_node, :bootstrap_for_node
def bootstrap_for_node(server, ssh_host)
if config[:solo]
chef_solo_bootstrap_for_node(server, ssh_host)
else
chef_server_bootstrap_for_node(server, ssh_host)
end
end
def chef_solo_bootstrap_for_node(server, ssh_host)
bootstrap = SoloBootstrap.new
bootstrap.ui = ui
bootstrap.name_args = ["#{config[:ssh_user]}@#{@ssh_host}"]
bootstrap.config = config
# Modify global configuration state to ensure hint gets set by
# knife-bootstrap
Chef::Config[:knife][:hints] ||= {}
Chef::Config[:knife][:hints]["ec2"] ||= {}
bootstrap
end
end
end
end
@tmatilai
Copy link
Author

It seems like a better idea to do this one layer lower, on knife bootstrap. See gist 4559333.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment