summaryrefslogtreecommitdiff
path: root/chef/lib/chef/knife/slicehost_server_create.rb
blob: 086fd387bc09b26f38bbbd7eb62f9b26c73b88b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
#
# Author:: Ian Meyer (<ianmmeyer@gmail.com>)
# Copyright:: Copyright (c) 2009 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/knife'
require 'json'

class Chef
  class Knife
    class SlicehostServerCreate < Knife

      banner "knife slicehost server create [RUN LIST...] (options)"

      option :flavor,
        :short => "-f FLAVOR",
        :long => "--flavor FLAVOR",
        :description => "The flavor of server",
        :proc => Proc.new { |f| f.to_i },
        :default => 1

      option :image,
        :short => "-i IMAGE",
        :long => "--image IMAGE",
        :description => "The image of the server",
        :proc => Proc.new { |i| i.to_i },
        :default => 49

      option :server_name,
        :short => "-N NAME",
        :long => "--server-name NAME",
        :description => "The server name",
        :default => "wtf"

      option :slicehost_password,
        :short => "-K KEY",
        :long => "--slicehost-password password",
        :description => "Your slicehost API password",
        :proc => Proc.new { |password| Chef::Config[:knife][:slicehost_password] = password } 


      def h
        @highline ||= HighLine.new
      end

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

        slicehost = Fog::Slicehost.new(
          :slicehost_password => Chef::Config[:knife][:slicehost_password]
        )

        flavors = slicehost.flavors.inject({}) { |h,f| h[f.id] = f.name; h }
        images  = slicehost.images.inject({}) { |h,i| h[i.id] = i.name; h }

        response = slicehost.create_slice(config[:flavor],
                               config[:image],
                               config[:server_name])
        $stdout.sync = true

        puts "#{h.color("Name", :cyan)}: #{response.body['name']}"
        puts "#{h.color("Flavor", :cyan)}: #{flavors[response.body['flavor-id']]}"
        puts "#{h.color("Image", :cyan)}: #{images[response.body['image-id']]}"
        puts "#{h.color("Public Address", :cyan)}: #{response.body['addresses'][1]}"
        puts "#{h.color("Private Address", :cyan)}: #{response.body['addresses'][0]}"
        puts "#{h.color("Password", :cyan)}: #{response.body['root-password']}"
     
        print "\n#{h.color("Requesting status of #{response.body['name']}", :magenta)}"
        saved_password = response.body['root-password']

        # wait for it to be ready to do stuff
        loop do
          sleep 15
          host = slicehost.get_slice(response.body['id'])
          if host.body['status'] == 'active'
            break
          end
        end

        puts "\nServer ready!!"
      
      end
    end
  end
end