Class: BBGAPI::Blocks

Inherits:
Object
  • Object
show all
Defined in:
lib/bbgapi/blocks.rb

Class Method Summary collapse

Class Method Details

.createObject



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
# File 'lib/bbgapi/blocks.rb', line 69

def self.create
  productid = self.productmenu
  templateid = self.templatemenu
  ssh_key_file = File.new("#{ENV['HOME']}/.ssh/id_rsa.pub", "r")
  keyfile = ssh_key_file.gets
  hostname = ask("Hostname:  ")

  options = { :body => {
    :product => productid,
    :template => templateid,
    :ssh_public_key => keyfile,
    :hostname => hostname
    } }

  partial = '/api/blocks'
  api_response = BBGAPI::Client.posturl(partial,options)
  if api_response.code == 200
    puts "Name: #{api_response["hostname"]}"
    puts "IP: #{api_response["ips"].first["address"]}"
    puts "Status: #{api_response["status"]}"
    puts "\n"
    puts "Sleeping 5 minutes while server boots up"
    sleep 300
    choose do |menu|
      menu.prompt = "Do you want to ssh to this new server?"
      menu.choices(:yes) {
        system("open", "ssh://deploy@#{api_response["ips"].first["address"]}")
      }
      menu.choices(:no)
    end
    choose do |menu|
      menu.prompt = "Do you want to bootstrap this new server?"
      menu.choices(:yes) {
        
        ssh_host = api_response["ips"].first["address"]
        ssh_user = 'deploy'

        begin
          Net::SSH.start( ssh_host, ssh_user ) do|ssh|
            output = ssh.exec('sudo chmod +x /root/bootstrap.sh && sudo /root/bootstrap.sh')
            puts output
          end
        rescue Net::SSH::HostKeyMismatch => e
          puts "remembering new key: #{e.fingerprint}"
          e.remember_host!
          retry
        end
      }
      menu.choices(:no)
    end
    choose do |menu|
      menu.prompt = "Do you want to add this machine to a load balanced pool?"
      menu.choices(:yes) {

        ssh_host = api_response["ips"].first["address"]
        ssh_user = 'deploy'

        begin
          Net::SSH.start( ssh_host, ssh_user ) do|ssh|
            output = ssh.exec('sudo chmod +x /root/tools/lb_add.sh && sudo /root/tools/lb_add.sh autoadd')
            puts output
          end
        rescue Net::SSH::HostKeyMismatch => e
          puts "remembering new key: #{e.fingerprint}"
          e.remember_host!
          retry
        end
      }
      menu.choices(:no)
    end
  else
    #puts "Recieved a #{api_response.code} to the request"
  end
end

.find_id_from_nameObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/bbgapi/blocks.rb', line 32

def self.find_id_from_name
  partial = '/api/blocks'
  api_response = BBGAPI::Client.geturl(partial,"")

  machines=api_response.first["lb_machines"]
  hostname = "#{id}.c45451"
  srv = machines.find_all{|item| item["hostname"] == hostname }
  srv_id = srv.first["id"]

end

.listObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bbgapi/blocks.rb', line 15

def self.list
  partial = '/api/blocks'
  api_response = BBGAPI::Client.geturl(partial,"")

  api_response.each {|x|
    puts "\n"
    puts "Name:        #{x["hostname"]}"
    puts "ID:          #{x["id"]}"
    puts "External IP: #{x["ips"].first["address"]}"
    if !x["lb_applications"].empty?
      puts "LB App:      #{x["lb_applications"].first["lb_application_name"]}"
    end
  }
  puts "\n"

end


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/bbgapi/blocks.rb', line 3

def self.menulist
  choose do |menu|
    puts "Blocks"
    puts "----------------------------------------"
    menu.prompt = "Which Action?"

    menu.choices(:list) {self.list}
    menu.choices(:create) {self.create}
    menu.choices(:delete) {self.tbi}
  end
end

.productmenuObject



49
50
51
52
53
54
55
56
57
# File 'lib/bbgapi/blocks.rb', line 49

def self.productmenu
  products = BBGAPI::Blocks_Products.raw
  choose do |menu|
    menu.prompt = "Which Product Would You Like?"
    products.each {|product|
      menu.choices(product["description"]) {return product["id"]}
    }
  end
end

.rawObject



43
44
45
46
47
# File 'lib/bbgapi/blocks.rb', line 43

def self.raw
  partial = '/api/blocks'
  api_response = BBGAPI::Client.geturl(partial,"")
  return api_response
end

.tbiObject



145
146
147
# File 'lib/bbgapi/blocks.rb', line 145

def self.tbi
  puts "This is not yet implemented\n"
end

.templatemenuObject



59
60
61
62
63
64
65
66
67
# File 'lib/bbgapi/blocks.rb', line 59

def self.templatemenu
  templates = BBGAPI::Blocks_Templates.raw
  choose do |menu|
    menu.prompt = "Which Template Would You Like?"
    templates.each {|template|
      menu.choices(template["description"]) {return template["id"]}
    }
  end
end