|
| 1 | +# |
| 2 | +# Author:: Adam Jacob (<[email protected]>) |
| 3 | +# Copyright:: Copyright (c) 2009 Opscode, Inc. |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require'chef/knife' |
| 20 | +require'json' |
| 21 | + |
| 22 | +classChef |
| 23 | +classKnife |
| 24 | +classRackspaceServerCreate < Knife |
| 25 | + |
| 26 | +banner"Sub-Command: rackspace server create [RUN LIST...] (options)" |
| 27 | + |
| 28 | +option:flavor, |
| 29 | +:short=>"-f FLAVOR", |
| 30 | +:long=>"--flavor FLAVOR", |
| 31 | +:description=>"The flavor of server", |
| 32 | +:proc=>Proc.new{ |f| f.to_i}, |
| 33 | +:default=>1 |
| 34 | + |
| 35 | +option:image, |
| 36 | +:short=>"-i IMAGE", |
| 37 | +:long=>"--image IMAGE", |
| 38 | +:description=>"The image of the server", |
| 39 | +:proc=>Proc.new{ |i| i.to_i}, |
| 40 | +:default=>14362 |
| 41 | + |
| 42 | +option:server_name, |
| 43 | +:short=>"-N NAME", |
| 44 | +:long=>"--server-name NAME", |
| 45 | +:description=>"The server name", |
| 46 | +:default=>"wtf" |
| 47 | + |
| 48 | +option:api_key, |
| 49 | +:short=>"-K KEY", |
| 50 | +:long=>"--rackspace-api-key KEY", |
| 51 | +:description=>"Your rackspace API key", |
| 52 | +:proc=>Proc.new{ |key| Chef::Config[:knife][:rackspace_api_key]=key} |
| 53 | + |
| 54 | +option:api_username, |
| 55 | +:short=>"-A USERNAME", |
| 56 | +:long=>"--rackspace-api-username USERNAME", |
| 57 | +:description=>"Your rackspace API username", |
| 58 | +:proc=>Proc.new{ |username| Chef::Config[:knife][:rackspace_api_username]=username} |
| 59 | + |
| 60 | +defh |
| 61 | +@highline ||= HighLine.new |
| 62 | +end |
| 63 | + |
| 64 | +defrun |
| 65 | +require'fog' |
| 66 | +require'highline' |
| 67 | +require'net/ssh/multi' |
| 68 | +require'readline' |
| 69 | + |
| 70 | +connection=Fog::Rackspace::Servers.new( |
| 71 | +:rackspace_api_key=>Chef::Config[:knife][:rackspace_api_key], |
| 72 | +:rackspace_username=>Chef::Config[:knife][:rackspace_api_username] |
| 73 | +) |
| 74 | + |
| 75 | +server=connection.servers.new |
| 76 | + |
| 77 | +server.flavor_id=config[:flavor] |
| 78 | +server.image_id=config[:image] |
| 79 | +server.name=config[:server_name] |
| 80 | +server.personality=[ |
| 81 | +{ |
| 82 | +'path'=>'/etc/install-chef', |
| 83 | +'contents'=><<-EOH |
| 84 | +#!/bin/bash |
| 85 | +# Customized rc.local for chef installation |
| 86 | +
|
| 87 | +if [ ! -f /usr/bin/chef-client ]; then |
| 88 | + apt-get update |
| 89 | + apt-get install -y ruby ruby1.8-dev build-essential wget libruby-extras libruby1.8-extras |
| 90 | + cd /tmp |
| 91 | + wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz |
| 92 | + tar xvf rubygems-1.3.6.tgz |
| 93 | + cd rubygems-1.3.6 |
| 94 | + ruby setup.rb |
| 95 | + cp /usr/bin/gem1.8 /usr/bin/gem |
| 96 | + gem install chef ohai --no-rdoc --no-ri --verbose |
| 97 | +fi |
| 98 | +
|
| 99 | +exit 0 |
| 100 | +EOH |
| 101 | +}, |
| 102 | +{ |
| 103 | +'path'=>"/etc/chef/validation.pem", |
| 104 | +'contents'=>IO.read(Chef::Config[:validation_key]) |
| 105 | +}, |
| 106 | +{ |
| 107 | +'path'=>"/etc/chef/client.rb", |
| 108 | +'contents'=><<-EOH |
| 109 | +log_level :info |
| 110 | +log_location STDOUT |
| 111 | +chef_server_url "#{Chef::Config[:chef_server_url]}" |
| 112 | +validation_client_name "#{Chef::Config[:validation_client_name]}" |
| 113 | +EOH |
| 114 | +}, |
| 115 | +{ |
| 116 | +'path'=>"/etc/chef/first-boot.json", |
| 117 | +'contents'=>{"run_list"=>@name_args}.to_json |
| 118 | +}, |
| 119 | +] |
| 120 | + |
| 121 | +server.save |
| 122 | + |
| 123 | + $stdout.sync=true |
| 124 | + |
| 125 | +puts"#{h.color("Name",:cyan)}: #{server.name}" |
| 126 | +puts"#{h.color("Flavor",:cyan)}: #{server.flavor_id}" |
| 127 | +puts"#{h.color("Image",:cyan)}: #{server.image_id}" |
| 128 | +puts"#{h.color("Public Address",:cyan)}: #{server.addresses["public"]}" |
| 129 | +puts"#{h.color("Private Address",:cyan)}: #{server.addresses["private"]}" |
| 130 | +puts"#{h.color("Password",:cyan)}: #{server.password}" |
| 131 | + |
| 132 | +print"\n#{h.color("Requesting server",:magenta)}" |
| 133 | +saved_password=server.password |
| 134 | + |
| 135 | +# wait for it to be ready to do stuff |
| 136 | +server.wait_for{print".";ready?} |
| 137 | + |
| 138 | +puts"\nServer ready, waiting 15 seconds to bootstrap." |
| 139 | +sleep15 |
| 140 | + |
| 141 | +puts"\nBootstrapping #{h.color(server.name,:bold)}..." |
| 142 | + |
| 143 | +ssh=Chef::Knife::Ssh.new |
| 144 | +ssh.name_args=[server.addresses["public"][0],"/bin/bash /etc/install-chef && /usr/bin/chef-client -j /etc/chef/first-boot.json"] |
| 145 | +ssh.config[:ssh_user]="root" |
| 146 | +ssh.config[:manual]=true |
| 147 | +ssh.config[:password]=saved_password |
| 148 | +ssh.password=saved_password |
| 149 | +ssh.run |
| 150 | + |
| 151 | +end |
| 152 | +end |
| 153 | +end |
| 154 | +end |
| 155 | + |
| 156 | + |
0 commit comments