Class: Chef::Knife::TerremarkServerCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/terremark_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



47
48
49
# File 'lib/chef/knife/terremark_server_create.rb', line 47

def h
  @highline ||= HighLine.new
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'lib/chef/knife/terremark_server_create.rb', line 51

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

  server_name = @name_args[0]

  terremark = Fog::Terremark.new(
    :terremark_username => Chef::Config[:knife][:terremark_username],
    :terremark_password => Chef::Config[:knife][:terremark_password],
    :terremark_service  => Chef::Config[:knife][:terremark_service] || :vcloud
  )

  $stdout.sync = true

  puts "Instantiating vApp #{h.color(server_name, :bold)}"
  vapp_id = terremark.instantiate_vapp_template(server_name).body['href'].split('/').last

  deploy_task_id = terremark.deploy_vapp(vapp_id).body['href'].split('/').last
  print "Waiting for deploy task [#{h.color(deploy_task_id, :bold)}]"
  terremark.tasks.get(deploy_task_id).wait_for { print "."; ready? }
  print "\n"

  power_on_task_id = terremark.power_on(vapp_id).body['href'].split('/').last
  print "Waiting for power on task [#{h.color(power_on_task_id, :bold)}]"
  terremark.tasks.get(power_on_task_id).wait_for { print "."; ready? }
  print "\n"

  private_ip = terremark.get_vapp(vapp_id).body['IpAddress']
  ssh_internet_service = terremark.create_internet_service(terremark.default_vdc_id, 'SSH', 'TCP', 22).body
  ssh_internet_service_id = ssh_internet_service['Id']
  public_ip = ssh_internet_service['PublicIpAddress']['Name']
  public_ip_id = ssh_internet_service['PublicIpAddress']['Id']
  ssh_node_service_id = terremark.add_node_service(ssh_internet_service_id, private_ip, 'SSH', 22).body['Id']

  puts "\nBootstrapping #{h.color(server_name, :bold)}..."
  password = terremark.get_vapp_template(12).body['Description'].scan(/\npassword: (.*)\n/).first.first

  command =  "bash -c '\necho nameserver 208.67.222.222 > /etc/resolv.conf\necho nameserver 208.67.220.220 >> /etc/resolv.conf\n\nif [ ! -f /usr/bin/chef-client ]; then\n  apt-get update\n  apt-get install -y ruby ruby1.8-dev build-essential wget libruby-extras libruby1.8-extras\n  cd /tmp\n  wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz\n  tar xvf rubygems-1.3.6.tgz\n  cd rubygems-1.3.6\n  ruby setup.rb\n  cp /usr/bin/gem1.8 /usr/bin/gem\n  gem install chef ohai --no-rdoc --no-ri --verbose\nfi\n\nmkdir -p /etc/chef\n\n(\ncat <<'EOP'\n\#{IO.read(Chef::Config[:validation_key])}\nEOP\n) > /etc/chef/validation.pem\n\n(\ncat <<'EOP'\nlog_level        :info\nlog_location     STDOUT\nchef_server_url  \"\#{Chef::Config[:chef_server_url]}\" \nvalidation_client_name \"\#{Chef::Config[:validation_client_name]}\"\nEOP\n) > /etc/chef/client.rb\n\n(\ncat <<'EOP'\n\#{{ \"run_list\" => @name_args[1..-1] }.to_json}\nEOP\n) > /etc/chef/first-boot.json\n\n/usr/bin/chef-client -j /etc/chef/first-boot.json'\n"

  begin
    ssh = Chef::Knife::Ssh.new
    ssh.name_args = [ public_ip, "sudo #{command}" ]
    ssh.config[:ssh_user] = "vcloud"
    ssh.config[:manual] = true
    ssh.config[:password] = password
    ssh.password = password
    ssh.run
  rescue Errno::ETIMEDOUT
    puts "Timed out on bootstrap, re-trying. Hit CTRL-C to abort."
    puts "You probably need to log in to Terremark and powercycle #{h.color(@name_args[0], :bold)}"
    retry
  end

end