Class: Chef::Knife::TerremarkServerCreate

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/terremark_server_create.rb

Instance Method Summary collapse

Instance Method Details

#hObject



52
53
54
# File 'lib/chef/knife/terremark_server_create.rb', line 52

def h
  @highline ||= HighLine.new
end

#runObject



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
# File 'lib/chef/knife/terremark_server_create.rb', line 56

def run 
  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 =  <<EOH
bash -c '
echo nameserver 208.67.222.222 > /etc/resolv.conf
echo nameserver 208.67.220.220 >> /etc/resolv.conf

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

mkdir -p /etc/chef

(
cat <<'EOP'
#{IO.read(Chef::Config[:validation_key])}
EOP
) > /etc/chef/validation.pem

(
cat <<'EOP'
log_level        :info
log_location     STDOUT
chef_server_url  "#{Chef::Config[:chef_server_url]}" 
validation_client_name "#{Chef::Config[:validation_client_name]}"
EOP
) > /etc/chef/client.rb

(
cat <<'EOP'
#{{ "run_list" => @name_args[1..-1] }.to_json}
EOP
) > /etc/chef/first-boot.json

/usr/bin/chef-client -j /etc/chef/first-boot.json'
EOH

  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