Class: SSP::App::Node

Inherits:
Thor
  • Object
show all
Defined in:
lib/ssp/application/node.rb

Instance Method Summary collapse

Instance Method Details

#create(fqdn, *roles) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
# File 'lib/ssp/application/node.rb', line 29

def create(fqdn, *roles)
  server = connection.servers.new

  # Convert roles array to proper run list
  run_list = roles.map { |r| r =~ /recipe\[(.*)\]/ ? $1 : "role[#{r}]" }

  # Script for creating an /etc/hosts file that will result in correct
  # fqdn and will point to the temporary chef server if we're testing
  setup_node_script = <<-EOH
#!/bin/bash

IP=$(ifconfig eth0 | sed -ne 's/.*inet addr:\\([0-9\\.]*\\).*/\\1/p')

echo "127.0.0.1	localhost localhost.localdomain" > /etc/hosts
echo "$IP	#{fqdn} #{fqdn.split(".").first}" >> /etc/hosts
  EOH
  if chef_config[:chef_server_url] == "https://chef-server.rackspace"
    chef_server_ip = %x{dscl . read Hosts/chef-server.rackspace 2>/dev/null}[/[\d\.]+/]
    setup_node_script << %{echo "#{chef_server_ip}	chef.sspti.me" >> /etc/hosts\n}
    chef_config[:chef_server_url] = "https://chef.sspti.me"
  end
  setup_node_script << "exit 0\n"

  # Always include the chef-client role in the run list
  unless run_list.include? "role[chef-client]"
    run_list.unshift "role[chef-client]"
  end

  server.flavor_id = options[:flavor]
  server.image_id = options[:image]
  server.name = fqdn
  server.personality = [
    {
      'path' => '/etc/setup-node',
      'contents' => setup_node_script
    },
    {
      '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" => run_list }.to_json
    },
  ]

  server.save

  say_status "Name: ", server.name, :cyan
  say_status "Flavor: ", server.flavor_id, :cyan
  say_status "Image: ", server.image_id, :cyan
  say_status "Public IP: ", server.addresses["public"], :cyan
  say_status "Private IP: ", server.addresses["private"], :cyan
  say_status "Password: ", server.password, :cyan

  say "\nRequesting server", :magenta
  saved_password = server.password

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

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

  say "\nBootstrapping #{shell.set_color(server.name, :bold)}..."

  ssh = ::Chef::Knife::Ssh.new
  ssh.name_args = [ server.addresses["public"][0], "/bin/bash /etc/setup-node && /usr/local/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

#listObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ssp/application/node.rb', line 114

def list
  server_list = [ ["ID", "Name", "Public IP", "Private IP", "Flavor ID"].map {|t| shell.set_color(t, :bold)} ]
  server_list += connection.servers.all.map do |server|
    [
      server.id.to_s, server.name,
      server.addresses["public"][0], server.addresses["private"][0],
      server.flavor_id.to_s
    ]
  end
  print_table(server_list)
end