Class: Chef::Knife::RackspaceServerCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/rackspace_server_create.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, build_sub_class, #bulk_delete, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, find_command, #format_for_display, #format_list_for_display, list_commands, load_commands, #load_from_file, #output, #pretty_print, #rest, #stdin, #stdout

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Instance Method Details

#hObject



60
61
62
# File 'lib/chef/knife/rackspace_server_create.rb', line 60

def h
  @highline ||= HighLine.new
end

#runObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chef/knife/rackspace_server_create.rb', line 64

def run 
  require 'fog'
  require 'highline'
  require 'net/ssh/multi'
  require 'readline'

  connection = Fog::Rackspace::Servers.new(
    :rackspace_api_key => Chef::Config[:knife][:rackspace_api_key],
    :rackspace_username => Chef::Config[:knife][:rackspace_api_username] 
  )

  server = connection.servers.new
 
  server.flavor_id = config[:flavor]
  server.image_id = config[:image]
  server.name = config[:server_name]
  server.personality = [
    { 
      'path' => '/etc/install-chef',
      'contents' => <<-EOH
#!/bin/bash
# Customized rc.local for chef installation

if [ ! -f /usr/bin/chef-client ]; then
  apt-get update
  apt-get install -y ruby ruby1.8-dev build-essential wget libruby-extras libruby1.8-extras
  cd /tmp
  wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz
  tar xvf rubygems-1.3.6.tgz
  cd rubygems-1.3.6
  ruby setup.rb
  cp /usr/bin/gem1.8 /usr/bin/gem
  gem install chef ohai --no-rdoc --no-ri --verbose
fi

exit 0
EOH
    },
    { 
      'path' => "/etc/chef/validation.pem",
      'contents' => IO.read(Chef::Config[:validation_key])
    },
    { 
      'path' => "/etc/chef/client.rb",
      'contents' => <<-EOH
log_level        :info
log_location     STDOUT
chef_server_url  "#{Chef::Config[:chef_server_url]}" 
validation_client_name "#{Chef::Config[:validation_client_name]}"
EOH
    },
    {
      'path' => "/etc/chef/first-boot.json",
      'contents' => { "run_list" => @name_args }.to_json
    },
  ]

  server.save

  $stdout.sync = true

  puts "#{h.color("Name", :cyan)}: #{server.name}"
  puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{h.color("Image", :cyan)}: #{server.image_id}"
  puts "#{h.color("Public Address", :cyan)}: #{server.addresses["public"]}"
  puts "#{h.color("Private Address", :cyan)}: #{server.addresses["private"]}"
  puts "#{h.color("Password", :cyan)}: #{server.password}"
     
  print "\n#{h.color("Requesting server", :magenta)}"
  saved_password = server.password

  # wait for it to be ready to do stuff
  server.wait_for { print "."; ready? }

  puts "\nServer ready, waiting 15 seconds to bootstrap."
  sleep 15

  puts "\nBootstrapping #{h.color(server.name, :bold)}..."

  ssh = Chef::Knife::Ssh.new
  ssh.name_args = [ server.addresses["public"][0], "/bin/bash /etc/install-chef && /usr/bin/chef-client -j /etc/chef/first-boot.json" ]
  ssh.config[:ssh_user] = "root"
  ssh.config[:manual] = true
  ssh.config[:password] = saved_password
  ssh.password = saved_password
  ssh.run

end