├── .gitignore ├── Gemfile ├── lib ├── knife-linode │ └── version.rb └── chef │ └── knife │ ├── linode_kernel_list.rb │ ├── linode_datacenter_list.rb │ ├── linode_stackscript_list.rb │ ├── linode_image_list.rb │ ├── linode_flavor_list.rb │ ├── linode_server_delete.rb │ ├── linode_server_reboot.rb │ ├── linode_server_list.rb │ ├── linode_base.rb │ └── linode_server_create.rb ├── knife-linode.gemspec ├── Rakefile ├── README.rdoc └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/* 2 | Gemfile.lock 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in knife-linode.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/knife-linode/version.rb: -------------------------------------------------------------------------------- 1 | module Knife 2 | module Linode 3 | VERSION = "0.1.0" 4 | MAJOR, MINOR, TINY = VERSION.split('.') 5 | end 6 | end 7 | 8 | -------------------------------------------------------------------------------- /knife-linode.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "knife-linode/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "knife-linode" 7 | s.version = Knife::Linode::VERSION 8 | s.has_rdoc = true 9 | s.authors = ["Adam Jacob","Seth Chisamore", "Lamont Granquist"] 10 | s.email = ["adam@opscode.com","schisamo@opscode.com", "lamont@opscode.com"] 11 | s.homepage = "http://wiki.opscode.com/display/chef" 12 | s.summary = "Linode Support for Chef's Knife Command" 13 | s.description = s.summary 14 | s.extra_rdoc_files = ["README.rdoc", "LICENSE" ] 15 | 16 | s.files = `git ls-files`.split("\n") 17 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 18 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 19 | s.add_dependency "fog", "~> 0.8.2" 20 | s.require_paths = ["lib"] 21 | 22 | end 23 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_kernel_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeKernelList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode kernel list (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | server_list = [ 36 | ui.color('ID', :bold), 37 | ui.color('Name', :bold), 38 | ] 39 | 40 | connection.kernels.each do |kernel| 41 | server_list << kernel.id.to_s 42 | server_list << kernel.name 43 | end 44 | 45 | puts ui.list(server_list, :columns_across, 2) 46 | 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_datacenter_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeDatacenterList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode datacenter list (options)" 30 | 31 | def run 32 | 33 | validate! 34 | server_list = [ 35 | ui.color('ID', :bold), 36 | ui.color('Location', :bold), 37 | ] 38 | 39 | connection.data_centers.each do |datacenter| 40 | server_list << datacenter.id.to_s 41 | server_list << datacenter.location 42 | end 43 | 44 | puts ui.list(server_list, :columns_across, 2) 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_stackscript_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeStackscriptList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode stackscript list (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | server_list = [ 36 | # ui.color('ID', :bold), 37 | # ui.color('Name', :bold), 38 | ] 39 | 40 | connection.stack_scripts.each do |stack_script| 41 | pp stack_script 42 | #server_list << kernel.id.to_s 43 | #server_list << kernel.name 44 | end 45 | 46 | # puts ui.list(server_list, :columns_across, 2) 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_image_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeImageList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode image list (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | server_list = [ 36 | ui.color('ID', :bold), 37 | ui.color('Name', :bold), 38 | ui.color('Bits', :bold), 39 | ui.color('Image Size', :bold), 40 | ] 41 | 42 | connection.images.each do |image| 43 | server_list << image.id.to_s 44 | server_list << image.name 45 | server_list << image.bits.to_s 46 | server_list << image.image_size.to_s 47 | end 48 | 49 | puts ui.list(server_list, :columns_across, 4) 50 | 51 | end 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_flavor_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeFlavorList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode flavor list (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | server_list = [ 36 | ui.color('ID', :bold), 37 | ui.color('Name', :bold), 38 | ui.color('RAM', :bold), 39 | ui.color('Disk', :bold), 40 | ui.color('Price', :bold), 41 | ] 42 | 43 | connection.flavors.each do |flavor| 44 | server_list << flavor.id.to_s 45 | server_list << flavor.name 46 | server_list << flavor.ram.to_s + " MB" 47 | server_list << flavor.disk.to_s + " GB" 48 | server_list << flavor.price.to_s 49 | end 50 | 51 | puts ui.list(server_list, :columns_across, 5) 52 | 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Author:: Daniel DeLeo () 4 | # Author:: Seth Chisamore () 5 | # Copyright:: Copyright (c) 2008, 2010 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'bundler' 22 | Bundler::GemHelper.install_tasks 23 | 24 | # require 'rubygems' 25 | # require 'rake/gempackagetask' 26 | require 'rake/rdoctask' 27 | 28 | begin 29 | require 'sdoc' 30 | 31 | Rake::RDocTask.new do |rdoc| 32 | rdoc.title = "Chef Ruby API Documentation" 33 | rdoc.main = "README.rdoc" 34 | rdoc.options << '--fmt' << 'shtml' # explictly set shtml generator 35 | rdoc.template = 'direct' # lighter template 36 | rdoc.rdoc_files.include("README.rdoc", "LICENSE", "spec/tiny_server.rb", "lib/**/*.rb") 37 | rdoc.rdoc_dir = "rdoc" 38 | end 39 | rescue LoadError 40 | puts "sdoc is not available. (sudo) gem install sdoc to generate rdoc documentation." 41 | end 42 | 43 | begin 44 | require 'rspec/core/rake_task' 45 | 46 | task :default => :spec 47 | 48 | desc "Run all specs in spec directory" 49 | RSpec::Core::RakeTask.new(:spec) do |t| 50 | t.pattern = 'spec/unit/**/*_spec.rb' 51 | end 52 | 53 | rescue LoadError 54 | STDERR.puts "\n*** RSpec not available. (sudo) gem install rspec to run unit tests. ***\n\n" 55 | end 56 | 57 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_server_delete.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeServerDelete < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode server delete (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | @name_args.each do |linode_id| 36 | 37 | begin 38 | server = connection.servers.get(linode_id) 39 | 40 | msg_pair("Linode ID", server.id.to_s) 41 | msg_pair("Name", server.name) 42 | msg_pair("IPs", server.ips.map { |x| x.ip }.join(",") ) 43 | msg_pair("Status", status_to_ui(server.status) ) 44 | 45 | puts "\n" 46 | confirm("Do you really want to delete this server") 47 | 48 | connection.servers.get(linode_id).destroy 49 | 50 | ui.warn("Deleted server #{linode_id}") 51 | rescue Fog::Compute::Linode::NotFound 52 | ui.error("Could not locate server '#{linode_id}'.") 53 | end 54 | 55 | end 56 | 57 | end 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_server_reboot.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeServerReboot < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode server reboot (options)" 30 | 31 | def run 32 | 33 | validate! 34 | 35 | @name_args.each do |linode_id| 36 | 37 | begin 38 | server = connection.servers.get(linode_id) 39 | 40 | msg_pair("Linode ID", server.id.to_s) 41 | msg_pair("Name", server.name) 42 | msg_pair("IPs", server.ips.map { |x| x.ip }.join(",") ) 43 | msg_pair("Status", status_to_ui(server.status) ) 44 | 45 | puts "\n" 46 | confirm("Do you really want to reboot this server") 47 | 48 | connection.servers.get(linode_id).reboot 49 | 50 | ui.warn("Rebooted server #{linode_id}") 51 | rescue Fog::Compute::Linode::NotFound 52 | ui.error("Could not locate server '#{linode_id}'.") 53 | end 54 | 55 | end 56 | 57 | end 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_server_list.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeServerList < Chef::Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | banner "knife linode server list (options)" 30 | 31 | def run 32 | 33 | $stdout.sync = true 34 | 35 | validate! 36 | 37 | server_list = [ 38 | ui.color('Linode ID', :bold), 39 | ui.color('Name', :bold), 40 | ui.color('IPs', :bold), 41 | ui.color('Status', :bold), 42 | ui.color('Backups', :bold), 43 | ui.color('Datacenter', :bold), 44 | ] 45 | 46 | dc_location = {} 47 | 48 | connection.data_centers.map { |dc| dc_location[dc.id] = dc.location } 49 | 50 | connection.servers.each do |server| 51 | server_list << server.id.to_s 52 | server_list << server.name 53 | server_list << server.ips.map { |x| x.ip }.join(",") 54 | server_list << status_to_ui(server.status) 55 | server_list << case connection.linode_list(server.id).body['DATA'][0]['BACKUPSENABLED'] 56 | when 0 57 | ui.color("No", :red) 58 | when 1 59 | ui.color("Yes", :green) 60 | else 61 | ui.color("UNKNOWN", :yellow) 62 | end 63 | server_list << dc_location[connection.linode_list(server.id).body['DATA'][0]['DATACENTERID']] 64 | end 65 | 66 | puts ui.list(server_list, :columns_across, 6) 67 | 68 | end 69 | end 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_base.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Seth Chisamore () 3 | # Author:: Lamont Granquist () 4 | # Copyright:: Copyright (c) 2011 Opscode, Inc. 5 | # License:: Apache License, Version 2.0 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | require 'chef/knife' 21 | 22 | class Chef 23 | class Knife 24 | module LinodeBase 25 | 26 | # :nodoc: 27 | # Would prefer to do this in a rational way, but can't be done b/c of 28 | # Mixlib::CLI's design :( 29 | def self.included(includer) 30 | includer.class_eval do 31 | 32 | deps do 33 | require 'fog' 34 | require 'readline' 35 | require 'chef/json_compat' 36 | end 37 | 38 | option :linode_api_key, 39 | :short => "-A KEY", 40 | :long => "--linode-api-key KEY", 41 | :description => "Your Linode API Key", 42 | :proc => Proc.new { |key| Chef::Config[:knife][:linode_api_key] = key } 43 | 44 | end 45 | end 46 | 47 | def connection 48 | @connection ||= begin 49 | connection = Fog::Compute.new( 50 | :provider => 'Linode', 51 | :linode_api_key => Chef::Config[:knife][:linode_api_key] 52 | ) 53 | end 54 | end 55 | 56 | def locate_config_value(key) 57 | key = key.to_sym 58 | Chef::Config[:knife][key] || config[key] 59 | end 60 | 61 | def msg_pair(label, value, color=:cyan) 62 | if value && !value.empty? 63 | puts "#{ui.color(label, color)}: #{value}" 64 | end 65 | end 66 | 67 | def validate!(keys=[:linode_api_key]) 68 | errors = [] 69 | 70 | keys.each do |k| 71 | pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(api)/i) ? w.upcase : w.capitalize } 72 | if Chef::Config[:knife][k].nil? 73 | errors << "You did not provided a valid '#{pretty_key}' value." 74 | end 75 | end 76 | 77 | if errors.each{|e| ui.error(e)}.any? 78 | exit 1 79 | end 80 | end 81 | 82 | def status_to_ui(status) 83 | case status 84 | when -2 85 | ui.color("Boot Failed", :red) 86 | when -1 87 | ui.color("Being Created", :yellow) 88 | when 0 89 | ui.color("Brand New", :yellow) 90 | when 1 91 | ui.color("Running", :green) 92 | when 2 93 | ui.color("Powered Off", :red) 94 | when 3 95 | ui.color("Shutting Down", :red) 96 | else 97 | ui.color("UNKNOWN: #{server.status}", :yellow) 98 | end 99 | end 100 | 101 | end 102 | end 103 | end 104 | 105 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = Knife Linode 2 | 3 | = DESCRIPTION: 4 | 5 | This is the official Opscode Knife plugin for Linode. This plugin gives knife the ability to create, bootstrap, and manage Linode instances. 6 | 7 | = INSTALLATION: 8 | 9 | Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins: 10 | 11 | gem install chef 12 | 13 | This plugin is distributed as a Ruby Gem. To install it, run: 14 | 15 | gem install knife-linode 16 | 17 | Depending on your system's configuration, you may need to run this command with root privileges. 18 | 19 | = CONFIGURATION: 20 | 21 | In order to communicate with the Linode API you will have to tell Knife about your Linode API Key. The easiest way to accomplish this is to create some entries in your knife.rb file: 22 | 23 | knife[:linode_api_key] = "Your Linode API Key" 24 | 25 | If your knife.rb file will be checked into a SCM system (ie readable by others) you may want to read the values from environment variables: 26 | 27 | knife[:linode_api_key] = "#{ENV['LINODE_API_KEY']}" 28 | 29 | You also have the option of passing your Linode API Key into the individual knife subcommands using the -A (or --linode-api-key) command option 30 | 31 | # provision a new m1.small Ubuntu 10.04 webserver # FIXME: change from EC2 to linode 32 | knife ec2 server create 'role[webserver]' -I ami-7000f019 -f m1.small -A 'Your AWS Access Key ID' -K "Your AWS Secret Access Key" 33 | 34 | Additionally the following options may be set in your `knife.rb`: 35 | 36 | * linode_flavor 37 | * linode_image 38 | * linode_kernel 39 | * ssh_password 40 | * bootstrap_version 41 | * distro 42 | * template_file 43 | 44 | = SUBCOMMANDS: 45 | 46 | This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a --help flag 47 | 48 | == knife linode server create 49 | 50 | Provisions a new server in Linode and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the {ubuntu10.04-gems}[https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb] template. This can be overridden using the -d or --template-file command options. 51 | 52 | == knife linode server delete 53 | 54 | Deletes an existing server in the currently configured Linode account. PLEASE NOTE - this does not delete the associated node and client objects from the Chef server. 55 | 56 | == knife linode server list 57 | 58 | Outputs a list of all servers in the currently configured Linode account. PLEASE NOTE - this shows all instances associated with the account, some of which may not be currently managed by the Chef server. 59 | 60 | == knife linode server reboot 61 | 62 | == knife linode datacenter list 63 | == knife linode flavor list 64 | == knife linode image list 65 | == knife linode kernel list 66 | == knife linode stackscript list 67 | 68 | = LICENSE: 69 | 70 | Author:: Adam Jacob () 71 | Copyright:: Copyright (c) 2009-2011 Opscode, Inc. 72 | License:: Apache License, Version 2.0 73 | 74 | Licensed under the Apache License, Version 2.0 (the "License"); 75 | you may not use this file except in compliance with the License. 76 | You may obtain a copy of the License at 77 | 78 | http://www.apache.org/licenses/LICENSE-2.0 79 | 80 | Unless required by applicable law or agreed to in writing, software 81 | distributed under the License is distributed on an "AS IS" BASIS, 82 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 83 | See the License for the specific language governing permissions and 84 | limitations under the License. 85 | 86 | -------------------------------------------------------------------------------- /lib/chef/knife/linode_server_create.rb: -------------------------------------------------------------------------------- 1 | 2 | # Author:: Adam Jacob () 3 | # Author:: Seth Chisamore () 4 | # Author:: Lamont Granquist () 5 | # Copyright:: Copyright (c) 2010-2011 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | # 20 | 21 | require 'chef/knife/linode_base' 22 | 23 | class Chef 24 | class Knife 25 | class LinodeServerCreate < Knife 26 | 27 | include Knife::LinodeBase 28 | 29 | deps do 30 | require 'fog' 31 | require 'readline' 32 | require 'chef/json_compat' 33 | require 'chef/knife/bootstrap' 34 | Chef::Knife::Bootstrap.load_deps 35 | end 36 | 37 | banner "knife linode server create (options)" 38 | 39 | attr_accessor :initial_sleep_delay 40 | 41 | option :linode_flavor, 42 | :short => "-f FLAVOR", 43 | :long => "--linode-flavor FLAVOR", 44 | :description => "The flavor of server", 45 | :proc => Proc.new { |f| Chef::Config[:knife][:linode_flavor] = f }, 46 | :default => 1 47 | 48 | option :linode_image, 49 | :short => "-I IMAGE", 50 | :long => "--linode-image IMAGE", 51 | :description => "The image for the server", 52 | :proc => Proc.new { |i| Chef::Config[:knife][:linode_image] = i }, 53 | :default => 93 54 | 55 | option :linode_kernel, 56 | :short => "-k KERNEL", 57 | :long => "--linode-kernel KERNEL", 58 | :description => "The kernel for the server", 59 | :proc => Proc.new { |i| Chef::Config[:knife][:linode_kernel] = i }, 60 | :default => 138 61 | 62 | option :linode_datacenter, 63 | :short => "-D DATACENTER", 64 | :long => "--linode-datacenter DATACENTER", 65 | :description => "The datacenter for the server", 66 | :proc => Proc.new { |i| Chef::Config[:knife][:linode_datacenter] = i }, 67 | :default => 3 68 | 69 | option :linode_node_name, 70 | :short => "-L NAME", 71 | :long => "--linode-node-name NAME", 72 | :description => "The Linode node name for your new node" 73 | 74 | option :chef_node_name, 75 | :short => "-N NAME", 76 | :long => "--node-name NAME", 77 | :description => "The Chef node name for your new node" 78 | 79 | option :ssh_user, 80 | :short => "-x USERNAME", 81 | :long => "--ssh-user USERNAME", 82 | :description => "The ssh username", 83 | :default => "root" 84 | 85 | chars = ("a".."z").to_a + ("1".."9").to_a + ("A".."Z").to_a 86 | @@defpass = Array.new(20, '').collect{chars[rand(chars.size)]}.push('A').push('a').join 87 | 88 | option :ssh_password, 89 | :short => "-P PASSWORD", 90 | :long => "--ssh-password PASSWORD", 91 | :proc => Proc.new { |p| Chef::Config[:knife][:ssh_password] = p }, 92 | :description => "The ssh password", 93 | :default => @@defpass 94 | 95 | option :identity_file, 96 | :short => "-i IDENTITY_FILE", 97 | :long => "--identity-file IDENTITY_FILE", 98 | :description => "The SSH identity file used for authentication" 99 | 100 | option :prerelease, 101 | :long => "--prerelease", 102 | :description => "Install the pre-release chef gems" 103 | 104 | option :bootstrap_version, 105 | :long => "--bootstrap-version VERSION", 106 | :description => "The version of Chef to install", 107 | :proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v } 108 | 109 | option :distro, 110 | :short => "-d DISTRO", 111 | :long => "--distro DISTRO", 112 | :description => "Bootstrap a distro using a template", 113 | :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d }, 114 | :default => "ubuntu10.04-gems" 115 | 116 | option :template_file, 117 | :long => "--template-file TEMPLATE", 118 | :description => "Full path to location of template to use", 119 | :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t }, 120 | :default => false 121 | 122 | option :run_list, 123 | :short => "-r RUN_LIST", 124 | :long => "--run-list RUN_LIST", 125 | :description => "Comma separated list of roles/recipes to apply", 126 | :proc => lambda { |o| o.split(/[\s,]+/) }, 127 | :default => [] 128 | 129 | option :no_host_key_verify, 130 | :long => "--no-host-key-verify", 131 | :description => "Disable host key verification", 132 | :boolean => true, 133 | :default => false 134 | 135 | def tcp_test_ssh(hostname) 136 | Chef::Log.debug("testing ssh connection to #{hostname}") 137 | tcp_socket = TCPSocket.new(hostname, 22) 138 | readable = IO.select([tcp_socket], nil, nil, 5) 139 | if readable 140 | Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}") 141 | yield 142 | true 143 | else 144 | false 145 | end 146 | rescue SocketError 147 | Chef::Log.debug("SocketError, retrying") 148 | sleep 2 149 | false 150 | rescue Errno::ETIMEDOUT 151 | Chef::Log.debug("ETIMEDOUT, retrying") 152 | false 153 | rescue Errno::EPERM 154 | Chef::Log.debug("EPERM, retrying") 155 | false 156 | rescue Errno::ECONNREFUSED 157 | Chef::Log.debug("ECONNREFUSED, retrying") 158 | sleep 2 159 | false 160 | rescue Errno::EHOSTUNREACH 161 | Chef::Log.debug("EHOSTUNREACH, retrying") 162 | sleep 2 163 | false 164 | ensure 165 | tcp_socket && tcp_socket.close 166 | end 167 | 168 | def run 169 | $stdout.sync = true 170 | 171 | validate! 172 | 173 | datacenter_id = locate_config_value(:linode_datacenter).to_i 174 | datacenter = connection.data_centers.select { |dc| dc.id == datacenter_id }.first 175 | 176 | flavor = connection.flavors.get(locate_config_value(:linode_flavor).to_i) 177 | 178 | image = connection.images.get(locate_config_value(:linode_image).to_i) 179 | 180 | kernel = connection.kernels.get(locate_config_value(:linode_kernel).to_i) 181 | 182 | # FIXME: tweakable stack_script 183 | # FIXME: tweakable payment terms 184 | # FIXME: tweakable disk type 185 | 186 | server = connection.servers.create( 187 | :data_center => datacenter, 188 | :flavor => flavor, 189 | :image => image, 190 | :kernel => kernel, 191 | :type => "ext3", 192 | :payment_terms => 1, 193 | :stack_script => nil, 194 | :name => locate_config_value(:linode_node_name), 195 | :password => locate_config_value(:ssh_password) 196 | ) 197 | 198 | fqdn = server.ips.select { |lip| !( lip.ip =~ /^192\.168\./ || lip.ip =~ /^10\./ || lip.ip =~ /^172\.(1[6-9]|2[0-9]|3[0-1])\./ ) }.first.ip 199 | 200 | msg_pair("Linode ID", server.id.to_s) 201 | msg_pair("Name", server.name) 202 | msg_pair("IPs", server.ips.map { |x| x.ip }.join(",") ) 203 | msg_pair("Status", status_to_ui(server.status) ) 204 | msg_pair("Public IP", fqdn) 205 | msg_pair("User", config[:ssh_user]) 206 | password = locate_config_value(:ssh_password) 207 | if password == @@defpass 208 | msg_pair("Password", password) 209 | end 210 | 211 | print "\n#{ui.color("Waiting for sshd", :magenta)}" 212 | 213 | print(".") until tcp_test_ssh(fqdn) { 214 | sleep @initial_sleep_delay ||= 10 215 | puts("done") 216 | } 217 | 218 | bootstrap_for_node(server,fqdn).run 219 | end 220 | 221 | def bootstrap_for_node(server,fqdn) 222 | bootstrap = Chef::Knife::Bootstrap.new 223 | bootstrap.name_args = [fqdn] 224 | bootstrap.config[:run_list] = config[:run_list] 225 | bootstrap.config[:ssh_user] = config[:ssh_user] 226 | bootstrap.config[:identity_file] = config[:identity_file] 227 | bootstrap.config[:ssh_password] = locate_config_value(:ssh_password) 228 | bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id 229 | bootstrap.config[:prerelease] = config[:prerelease] 230 | bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version) 231 | bootstrap.config[:distro] = locate_config_value(:distro) 232 | bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root' 233 | bootstrap.config[:template_file] = locate_config_value(:template_file) 234 | bootstrap.config[:environment] = config[:environment] 235 | bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify] 236 | bootstrap 237 | end 238 | 239 | end 240 | end 241 | end 242 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------