knife-baremetalcloud 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.project ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>knife-baremetalcloud</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ <nature>com.aptana.projects.webnature</nature>
17
+ </natures>
18
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-baremetalcloud.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Diego Desani
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Knife::Baremetalcloud
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'knife-baremetalcloud'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install knife-baremetalcloud
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife-baremetalcloud/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = ["knife-baremetalcloud"]
6
+ gem.version = '0.0.1'
7
+ gem.authors = ["Diego Desani"]
8
+ gem.email = ["diego@newservers.com"]
9
+ gem.summary = %q{baremetalcloud Compute Support for Chef's Knife Command}
10
+ gem.description = gem.summary
11
+ gem.homepage = "https://github.com/baremetalcloud/knife-baremetalcloud"
12
+ gem.rubyforge_project = 'rake'
13
+
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.name = "knife-baremetalcloud"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = Knife::Baremetalcloud::VERSION
20
+ gem.add_dependency "fog", ">= 1.5.0"
21
+ end
@@ -0,0 +1,56 @@
1
+ require 'fog'
2
+
3
+ class Chef
4
+ class Knife
5
+ class BaremetalcloudListAvailableServers < Knife
6
+
7
+ banner "knife baremetalcloud list available servers (options)"
8
+
9
+ option :baremetalcloud_username,
10
+ :short => '-x USERNAME',
11
+ :long => '--username USERNAME',
12
+ :description => 'Customer username'
13
+
14
+ option :baremetalcloud_password,
15
+ :short => '-P PASSWORD',
16
+ :long => '--password PASSWORD',
17
+ :description => 'Customer password'
18
+
19
+ def locate_config_value(key)
20
+ key = key.to_sym
21
+ config[key] || Chef::Config[:knife][key]
22
+ end
23
+
24
+ def run
25
+
26
+ # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
27
+ unless config[:baremetalcloud_username]
28
+ ui.error("--username is a mandatory parameter")
29
+ exit 1
30
+ end
31
+
32
+ unless config[:baremetalcloud_password]
33
+ ui.error("--password is a mandatory parameter")
34
+ exit 1
35
+ end
36
+
37
+ # Configure the API abstraction @bmc
38
+ @bmc = Fog::Compute.new({
39
+ :bare_metal_cloud_username => config[:baremetalcloud_username],
40
+ :bare_metal_cloud_password => config[:baremetalcloud_password],
41
+ :provider => 'BareMetalCloud'
42
+ })
43
+
44
+ response = @bmc.list_available_servers.body[:"available-server"]
45
+
46
+ if response.length > 1
47
+ #puts "#{ui.color("String", :cyan)}\t#{ui.color("Quantity", :cyan)}"
48
+ response.each do |r|
49
+ puts "#{r[:configuration]}\t#{r[:quantity]}"
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ require 'fog'
2
+
3
+ class Chef
4
+ class Knife
5
+ class BaremetalcloudListImages < Knife
6
+
7
+ banner "knife baremetalcloud list images (options)"
8
+
9
+ option :baremetalcloud_username,
10
+ :short => '-x USERNAME',
11
+ :long => '--username USERNAME',
12
+ :description => 'Customer username'
13
+
14
+ option :baremetalcloud_password,
15
+ :short => '-P PASSWORD',
16
+ :long => '--password PASSWORD',
17
+ :description => 'Customer password'
18
+
19
+ def locate_config_value(key)
20
+ key = key.to_sym
21
+ config[key] || Chef::Config[:knife][key]
22
+ end
23
+
24
+ def run
25
+
26
+ # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
27
+ unless config[:baremetalcloud_username]
28
+ ui.error("--username is a mandatory parameter")
29
+ exit 1
30
+ end
31
+
32
+ unless config[:baremetalcloud_password]
33
+ ui.error("--password is a mandatory parameter")
34
+ exit 1
35
+ end
36
+
37
+ # Configure the API abstraction @bmc
38
+ @bmc = Fog::Compute.new({
39
+ :bare_metal_cloud_username => locate_config_value(:baremetalcloud_username),
40
+ :bare_metal_cloud_password => locate_config_value(:baremetalcloud_password),
41
+ :provider => 'BareMetalCloud'
42
+ })
43
+
44
+ response = @bmc.list_images.body[:image]
45
+ if response.length > 1
46
+ #puts "#{ui.color("Id", :cyan)}\t#{ui.color("Name", :cyan)}\t#{ui.color("Size", :cyan)}"
47
+ response.each do |r|
48
+ puts "#{r[:id]}\t#{r[:name]}\t#{r[:size]}"
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,56 @@
1
+ require 'fog'
2
+
3
+ class Chef
4
+ class Knife
5
+ class BaremetalcloudListServers < Knife
6
+
7
+ banner "knife baremetalcloud list servers (options)"
8
+
9
+ option :baremetalcloud_username,
10
+ :short => '-x USERNAME',
11
+ :long => '--username USERNAME',
12
+ :description => 'Customer username'
13
+
14
+ option :baremetalcloud_password,
15
+ :short => '-P PASSWORD',
16
+ :long => '--password PASSWORD',
17
+ :description => 'Customer password'
18
+
19
+ def locate_config_value(key)
20
+ key = key.to_sym
21
+ config[key] || Chef::Config[:knife][key]
22
+ end
23
+
24
+ def run
25
+
26
+ # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
27
+ unless config[:baremetalcloud_username]
28
+ ui.error("--username is a mandatory parameter")
29
+ exit 1
30
+ end
31
+
32
+ unless config[:baremetalcloud_password]
33
+ ui.error("--password is a mandatory parameter")
34
+ exit 1
35
+ end
36
+
37
+ # Configure the API abstraction @bmc
38
+ @bmc = Fog::Compute.new({
39
+ :bare_metal_cloud_username => locate_config_value(:baremetalcloud_username),
40
+ :bare_metal_cloud_password => locate_config_value(:baremetalcloud_password),
41
+ :provider => 'BareMetalCloud'
42
+ })
43
+
44
+
45
+ response = @bmc.list_servers.body[:server]
46
+
47
+ if response.length > 1
48
+ response.each do |r|
49
+ puts "#{r[:id]}\t#{r[:state]}\t#{r[:name]}\t#{r[:location]}\t#{r[:ip][:address]}\t#{r[:login][:username]}\t#{r[:login][:password]}"
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,178 @@
1
+ require 'fog'
2
+
3
+ class Chef
4
+ class Knife
5
+ class BaremetalcloudServerCreate < Knife
6
+
7
+ deps do
8
+ require 'fog'
9
+ require 'readline'
10
+ require 'chef/json_compat'
11
+ require 'chef/knife/bootstrap'
12
+ Chef::Knife::Bootstrap.load_deps
13
+ end
14
+
15
+ banner "knife baremetalcloud server create NAME [RUN LIST...] (options)"
16
+
17
+ attr_accessor :initial_sleep_delay, :bmc
18
+
19
+ option :baremetalcloud_username,
20
+ :short => '-x USERNAME',
21
+ :long => '--username USERNAME',
22
+ :description => 'Customer username'
23
+
24
+ option :baremetalcloud_password,
25
+ :short => '-P PASSWORD',
26
+ :long => '--password PASSWORD',
27
+ :description => 'Customer password'
28
+
29
+ option :name,
30
+ :short => '-n NAME',
31
+ :long => '--name NAME',
32
+ :description => 'Label for the new servers'
33
+
34
+ option :run_list,
35
+ :short => "-r RUN_LIST",
36
+ :long => "--run-list RUN_LIST",
37
+ :description => "Comma separated list of roles/recipes to apply",
38
+ :proc => lambda { |o| o.split(/[\s,]+/) },
39
+ :default => []
40
+
41
+ option :location,
42
+ :short => '-l LOCATION',
43
+ :long => '--location LOCATION',
44
+ :description => 'Location of the server (possible values: miami-fl-usa, santaclara-ca-usa)',
45
+ :default => "miami-fl-usa"
46
+
47
+ option :imageName,
48
+ :short => '-i IMAGE_NAME',
49
+ :long => '--image IMAGE_NAME',
50
+ :description => 'Image installed of the server (CentOS5.5x64 or Win2003x64)'
51
+
52
+ def tcp_test_ssh(hostname)
53
+ tcp_socket = TCPSocket.new(hostname, "22")
54
+ readable = IO.select([tcp_socket], nil, nil, 5)
55
+ if readable
56
+ Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
57
+ yield
58
+ true
59
+ else
60
+ false
61
+ end
62
+ rescue SocketError
63
+ sleep 2
64
+ false
65
+ # Connection timed out
66
+ rescue Errno::ETIMEDOUT
67
+ false
68
+ # Operation not permitted
69
+ rescue Errno::EPERM
70
+ false
71
+ # Connection refused
72
+ rescue Errno::ECONNREFUSED
73
+ sleep 2
74
+ false
75
+ # No route to host
76
+ rescue Errno::EHOSTUNREACH
77
+ sleep 2
78
+ false
79
+ # Network is unreachable
80
+ rescue Errno::ENETUNREACH
81
+ sleep 2
82
+ false
83
+ ensure
84
+ tcp_socket && tcp_socket.close
85
+ end
86
+
87
+
88
+ def locate_config_value(key)
89
+ key = key.to_sym
90
+ config[key] || Chef::Config[:knife][key]
91
+ end
92
+
93
+ def bootstrap_for_node(public_ip)
94
+ puts "node bootstrap - #{public_ip}"
95
+ bootstrap = Chef::Knife::Bootstrap.new
96
+ bootstrap.name_args = [public_ip]
97
+ bootstrap.config[:run_list] = locate_config_value(:run_list)
98
+ bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
99
+ bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
100
+ bootstrap
101
+ end
102
+
103
+
104
+
105
+ def addServer(planId, options = {})
106
+ puts "Connecting to baremetalcloud"
107
+
108
+ # make the request and get the body
109
+ response = @bmc.add_server(planId,options).body
110
+
111
+ # Loop while SSH is not available and sleep @initial_sleep_delay seconds
112
+ print(".") until tcp_test_ssh(response[:server][:ip][:address]) {
113
+ sleep @initial_sleep_delay
114
+ print("done")
115
+ }
116
+
117
+ # Get Server's user and password
118
+ config[:ssh_user] = response[:server][:login][:username]
119
+ config[:ssh_password] = response[:server][:login][:password]
120
+
121
+ # Configure bootstrap and trigger "run" to start up
122
+ bootstrap_for_node(response[:server][:ip][:address]).run
123
+ end
124
+
125
+ def run
126
+
127
+ # sleeptime for testing ssh connectivity
128
+ @initial_sleep_delay = 3
129
+
130
+ # Parameters :baremetalcloud_username and :baremetalcloud_password are mandatory
131
+ unless config[:baremetalcloud_username]
132
+ ui.error("--username is a mandatory parameter")
133
+ exit 1
134
+ end
135
+
136
+ unless config[:baremetalcloud_password]
137
+ ui.error("--password is a mandatory parameter")
138
+ exit 1
139
+ end
140
+
141
+ # Configure the API abstraction @bmc
142
+ @bmc = Fog::Compute.new({
143
+ :bare_metal_cloud_username => locate_config_value(:baremetalcloud_username),
144
+ :bare_metal_cloud_password => locate_config_value(:baremetalcloud_password),
145
+ :provider => 'BareMetalCloud'
146
+ })
147
+
148
+
149
+ # Waiting for API rectoring
150
+ # PlanId = 7
151
+ ## CentOS small
152
+ config[:planId] = 7
153
+
154
+ # Options
155
+ options = {
156
+ :location => locate_config_value(:location),
157
+ :imageName => locate_config_value(:imageName),
158
+ :name => locate_config_value(:name)
159
+ }
160
+
161
+ if (config[:planId])
162
+ addServer(locate_config_value(:planId), options)
163
+
164
+ elsif (config[:config]) # BLOCK WILL BE REMOVED
165
+ @bmc.list_plans.body[:plan].each do | plan |
166
+ if plan[:config] == config[:config]
167
+ addServer(plan[:id], options) # small plan == 7
168
+ end
169
+ end
170
+
171
+ else
172
+ ui.error("PlanId or Configuration is mandatory")
173
+ exit 1
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Baremetalcloud
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "knife-baremetalcloud/version"
2
+
3
+ module Knife
4
+ module Baremetalcloud
5
+ # Your code goes here...
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-baremetalcloud
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Diego Desani
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: fog
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 1
31
+ - 5
32
+ - 0
33
+ version: 1.5.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: baremetalcloud Compute Support for Chef's Knife Command
37
+ email:
38
+ - diego@newservers.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - .project
48
+ - Gemfile
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - knife-baremetalcloud.gemspec
53
+ - lib/chef/baremetalcloud_list_available_servers.rb
54
+ - lib/chef/baremetalcloud_list_images.rb
55
+ - lib/chef/baremetalcloud_list_servers.rb
56
+ - lib/chef/baremetalcloud_server_create.rb
57
+ - lib/knife-baremetalcloud.rb
58
+ - lib/knife-baremetalcloud/version.rb
59
+ homepage: https://github.com/baremetalcloud/knife-baremetalcloud
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: rake
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: baremetalcloud Compute Support for Chef's Knife Command
92
+ test_files: []
93
+