lumberg 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -7,3 +7,4 @@ test.rb
7
7
  *~
8
8
  coverage/
9
9
  doc/
10
+ Gemfile.lock
data/.rvmrc CHANGED
@@ -1 +1,49 @@
1
- rvm 1.9.2@lumberg
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p180@lumberg"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficicent compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
17
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
18
+
19
+ [[ -s ".rvm/hooks/after_use" ]] && . ".rvm/hooks/after_use"
20
+ else
21
+ # If the environment file has not yet been created, use the RVM CLI to select.
22
+ rvm --create use "$environment_id"
23
+ fi
24
+
25
+ #
26
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
27
+ # it be automatically loaded. Uncomment the following and adjust the filename if
28
+ # necessary.
29
+ #
30
+ # filename=".gems"
31
+ # if [[ -s "$filename" ]] ; then
32
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
33
+ # fi
34
+
35
+ #
36
+ # If you use bundler and would like to run bundle each time you enter the
37
+ # directory, you can uncomment the following code.
38
+ #
39
+ # # Ensure that Bundler is installed. Install it if it is not.
40
+ # if ! command -v bundle >/dev/null; then
41
+ # printf "The rubygem 'bundler' is not installed. Installing it now.\n"
42
+ # gem install bundler
43
+ # fi
44
+ #
45
+ # # Bundle while reducing excess noise.
46
+ # printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
47
+ # bundle | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
48
+ #
49
+
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ script: "bundle exec rake spec"
2
+ notifications:
3
+ disabled: true
4
+ rvm:
5
+ - 1.8.7
6
+ - 1.9.2
7
+ - 1.9.3
8
+ - ree
9
+ - rbx
10
+ - rbx-head
11
+ - jruby
data/README.md CHANGED
@@ -1,81 +1,111 @@
1
- Lumberg
2
- =======
1
+ Lumberg [![Lumberg Build Status][Build Icon]][Build Status]
2
+ ===========================================================
3
3
 
4
4
  ![Lumberg logo](http://i.imgur.com/xC4Sw.jpg)
5
5
 
6
- Ruby library for the WHM & cPanel API. It is currently what we consider to be beta-ish. Please report any issues you find. :)
6
+ Ruby library for the WHM & cPanel API. It is currently what we consider to be
7
+ beta-ish. Please report any issues you find. :)
7
8
 
8
9
  [RDoc](http://rdoc.info/github/site5/lumberg/master/frames)
9
10
 
10
- Confirmed to work with ruby 1.8.7, 1.9.2 and JRuby 1.6.0 on cPanel 11.28+
11
+ Confirmed to work with ruby 1.8.7, 1.9.2, Rubinius, REE and JRuby 1.6.0 on
12
+ cPanel 11.28+
13
+
14
+ [Build Status]: http://travis-ci.org/site5/lumberg
15
+ [Build Icon]: https://secure.travis-ci.org/site5/lumberg.png?branch=master
16
+
11
17
 
12
18
  Install
13
19
  -------
14
20
 
15
21
  gem install lumberg
16
22
 
23
+
17
24
  Usage
18
25
  -----
19
26
 
20
27
  Create a server object and connect to WHM using your host and hash:
21
28
 
22
- require 'lumberg'
23
- server = Lumberg::Whm::Server.new(:host => WHM_HOST, :hash => WHM_HASH)
29
+ ```ruby
30
+ require 'lumberg'
31
+ server = Lumberg::Whm::Server.new(
32
+ :host => WHM_HOST,
33
+ :hash => WHM_HASH
34
+ )
35
+ ```
36
+
37
+ If you are using HTTP Basic Authentication with a username/password:
38
+
39
+ ```ruby
40
+ server = Lumberg::Whm::Server.new(
41
+ :host => WHM_HOST,
42
+ :user => USERNAME,
43
+ :hash => 'my_password'
44
+ )
45
+ ```
24
46
 
25
- You can access the modules of WHM by calling server.<module>. For example, server.account or server.dns. Here are the currently supported modules:
47
+ You can access the modules of WHM by calling `server.<module>`. For example,
48
+ `server.account` or `server.dns`. Here are the currently supported modules:
26
49
 
27
50
  * Account
28
51
  * DNS
29
52
  * Reseller
30
53
 
31
-
32
54
  Here's how to get a list of accounts:
33
55
 
34
- account = server.account
35
- result = account.list
56
+ ```ruby
57
+ account = server.account
58
+ result = account.list
59
+ ```
36
60
 
37
61
  Most responses will return success and a message
38
62
 
39
- p "Returned #{result[:success]} with message: #{result[:message]}"
63
+ ```ruby
64
+ p "Returned #{result[:success]} with message: #{result[:message]}"
40
65
 
41
- Returned true with message: Ok
66
+ Returned true with message: Ok
67
+ ```
42
68
 
43
69
  You can grab the data you need from the results hash
44
70
 
45
- num_accounts = result[:params][:acct].size
46
- p "There are #{num_accounts} accounts"
47
-
48
- There are 6 accounts
49
-
50
-
51
- Here's an example of what the renurned data looks like for a single account
52
-
53
- pp result[:params][:acct].first
54
-
55
- {:startdate=>false,
56
- :plan=>"default",
57
- :suspended=>false,
58
- :theme=>"x3",
59
- :shell=>"/usr/local/cpanel/bin/jailshell",
60
- :maxpop=>"unlimited",
61
- :maxlst=>"unlimited",
62
- :maxaddons=>"*unknown*",
63
- :suspendtime=>nil,
64
- :ip=>false,
65
- :maxsub=>"unlimited",
66
- :domain=>"bob.com",
67
- :maxsql=>"unlimited",
68
- :partition=>"home",
69
- :maxftp=>"unlimited",
70
- :user=>"bob",
71
- :suspendreason=>"not suspended",
72
- :unix_startdate=>false,
73
- :diskused=>false,
74
- :maxparked=>"*unknown*",
75
- :email=>"*unknown*",
76
- :disklimit=>"unlimited",
77
- :owner=>"root"}
78
-
71
+ ```ruby
72
+ num_accounts = result[:params][:acct].size
73
+ p "There are #{num_accounts} accounts"
74
+
75
+ There are 6 accounts
76
+ ```
77
+
78
+ Here's an example of what the returned data looks like for a single account
79
+
80
+ ```ruby
81
+ pp result[:params][:acct].first
82
+
83
+ {
84
+ :startdate=>false,
85
+ :plan => "default",
86
+ :suspended => false,
87
+ :theme => "x3",
88
+ :shell => "/usr/local/cpanel/bin/jailshell",
89
+ :maxpop => "unlimited",
90
+ :maxlst => "unlimited",
91
+ :maxaddons => "*unknown*",
92
+ :suspendtime => nil,
93
+ :ip => false,
94
+ :maxsub => "unlimited",
95
+ :domain => "bob.com",
96
+ :maxsql => "unlimited",
97
+ :partition => "home",
98
+ :maxftp => "unlimited",
99
+ :user => "bob",
100
+ :suspendreason => "not suspended",
101
+ :unix_startdate => false,
102
+ :diskused => false,
103
+ :maxparked => "*unknown*",
104
+ :email => "*unknown*",
105
+ :disklimit => "unlimited",
106
+ :owner => "root"
107
+ }
108
+ ```
79
109
 
80
110
 
81
111
  Account Example
@@ -83,80 +113,110 @@ Account Example
83
113
 
84
114
  Creating a new account requires only a username, domain, and password.
85
115
 
86
- result = server.account.create(:username => 'newuser', :domain => 'newuser.com', :password => 'password')
87
- if result[:success]
88
- p result[:message]
89
- end
116
+ ```ruby
117
+ result = server.account.create(
118
+ :username => 'newuser',
119
+ :domain => 'newuser.com',
120
+ :password => 'password'
121
+ )
90
122
 
91
- Account Creation Ok
123
+ if result[:success]
124
+ p result[:message]
125
+ end
92
126
 
127
+ Account Creation Ok
128
+ ```
93
129
 
94
130
  You can list all accounts or search for a specific account.
95
131
 
96
- result = server.account.list(:search => 'new', :searchtype => 'user')
97
- acct = result[:params][:acct].first
98
- p "Found user '#{acct[:user]}' with domain '#{acct[:domain]}'"
132
+ ```ruby
133
+ result = server.account.list(:search => 'new', :searchtype => 'user')
134
+ acct = result[:params][:acct].first
135
+ p "Found user '#{acct[:user]}' with domain '#{acct[:domain]}'"
99
136
 
100
- Found user 'newuser' with domain 'newuser.com'
137
+ Found user 'newuser' with domain 'newuser.com'
138
+ ```
101
139
 
102
140
 
103
141
  Suspending an account is simple and the reason for suspension is optional.
104
142
 
105
- result = server.account.suspend(:username => 'newuser', :reason => 'bad user')
106
- p "user was suspended successfully" if result[:success]
143
+ ```ruby
144
+ result = server.account.suspend(:username => 'newuser', :reason => 'bad user')
145
+ p "user was suspended successfully" if result[:success]
107
146
 
108
- user was suspended successfully
147
+ user was suspended successfully
148
+ ```
109
149
 
110
- We can look at the account list again to see the reason for the user's suspension.
150
+ We can look at the account list again to see the reason for the user's
151
+ suspension.
111
152
 
112
- result = server.account.list(:search => 'new', :searchtype => 'user')
113
- acct = result[:params][:acct].first
114
- p "user '#{acct[:user]}' was suspended with reason '#{acct[:suspendreason]}'"
153
+ ```ruby
154
+ result = server.account.list(:search => 'new', :searchtype => 'user')
155
+ acct = result[:params][:acct].first
156
+ p "user '#{acct[:user]}' was suspended with reason '#{acct[:suspendreason]}'"
115
157
 
116
- user 'newuser' was suspended with reason 'bad user'
158
+ user 'newuser' was suspended with reason 'bad user'
159
+ ```
117
160
 
118
161
  Finally, we remove the user and delete their account.
119
162
 
120
- result = server.account.remove(:username => 'newuser')
121
- p result[:message]
122
-
123
- newuser account removed
163
+ ```ruby
164
+ result = server.account.remove(:username => 'newuser')
165
+ p result[:message]
124
166
 
167
+ newuser account removed
168
+ ```
125
169
 
126
170
 
127
171
  Reseller Example
128
172
  ----------------
129
173
 
130
- Creating a reseller works by changing the status of an existing user account, so first we create a user account and then we can create that user as a reseller.
174
+ Creating a reseller works by changing the status of an existing user account,
175
+ so first we create a user account and then we can create that user as a reseller.
131
176
 
132
- result = server.account.create(:username => 'rtest', :domain => 'resellerexample.com', :password => 'password')
133
- if result[:success]
134
- result = server.reseller.create(:username => 'rtest', :makeowner => true)
135
- p "created reseller rtest" if result[:success]
136
- end
177
+ ```ruby
178
+ result = server.account.create(
179
+ :username => 'rtest',
180
+ :domain => 'resellerexample.com',
181
+ :password => 'password'
182
+ )
137
183
 
138
- created reseller rtest
184
+ if result[:success]
185
+ result = server.reseller.create(:username => 'rtest', :makeowner => true)
186
+ p "created reseller rtest" if result[:success]
187
+ end
139
188
 
140
- You can get a list of all of the reseller acccounts easily.
189
+ created reseller rtest
190
+ ```
141
191
 
142
- result = server.reseller.list
143
- p result[:params][:resellers].inspect
192
+ You can get a list of all of the reseller accounts easily.
144
193
 
145
- ["samir", "rtest"]
194
+ ```
195
+ result = server.reseller.list
196
+ p result[:params][:resellers].inspect
146
197
 
147
- Suspending a reseller is pretty straightforward. It's optional to provide a reason for the suspension.
198
+ ["samir", "rtest"]
199
+ ```
148
200
 
149
- result = server.reseller.suspend(:username => 'rtest', :reason => 'bad user')
150
- p "reseller was suspended successfully" if result[:success]
201
+ Suspending a reseller is pretty straightforward. It's optional to provide a
202
+ reason for the suspension.
151
203
 
152
- user was suspended successfully
153
-
154
- Deleting the reseller removes the reseller status from the user account. To also delete the user account, set the :terminatereseller argument.
204
+ ```ruby
205
+ result = server.reseller.suspend(:username => 'rtest', :reason => 'bad user')
206
+ p "reseller was suspended successfully" if result[:success]
155
207
 
156
- result = server.reseller.terminate(:reseller => 'rtest', :terminatereseller => true)
157
- p result[:message]
208
+ user was suspended successfully
209
+ ```
158
210
 
159
- Account Terminations Complete
211
+ Deleting the reseller removes the reseller status from the user account. To
212
+ also delete the user account, set the `:terminatereseller` argument.
213
+
214
+ ```ruby
215
+ result = server.reseller.terminate(:reseller => 'rtest', :terminatereseller => true)
216
+ p result[:message]
217
+
218
+ Account Terminations Complete
219
+ ```
160
220
 
161
221
  Contributors
162
222
  ============
@@ -166,15 +226,18 @@ Contributors
166
226
 
167
227
  Note on Patches/Pull Requests
168
228
  =============================
169
- All HTTP interactions are recorded using VCR and FakeWeb. Please be sure to remove any sensitive information from your cassettes.
170
-
229
+
230
+ All HTTP interactions are recorded using VCR and FakeWeb. Please be sure to
231
+ remove any sensitive information from your cassettes.
232
+
171
233
  * Fork the project.
172
234
  * Add yourself to the Contributors list
173
235
  * Make your feature addition or bug fix.
174
236
  * Add tests for it. This is important so I don't break it in a
175
237
  future version unintentionally.
176
238
  * Commit, do not mess with rakefile, version, or history.
177
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
239
+ (if you want to have your own version, that is fine but bump version in a
240
+ commit by itself I can ignore when I pull)
178
241
  * Send me a pull request. Bonus points for topic branches.
179
242
 
180
243
  Copyright
data/Rakefile CHANGED
@@ -47,3 +47,5 @@ RSpec::Core::RakeTask.new(:rcov) do |t|
47
47
  t.rcov = true
48
48
  t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/,features\/}
49
49
  end
50
+
51
+ task :default => :spec
@@ -1,3 +1,3 @@
1
1
  module Lumberg
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'cgi'
2
+ require 'base64'
2
3
 
3
4
  module Lumberg
4
5
  module Whm
@@ -12,6 +13,9 @@ module Lumberg
12
13
  # Base URL to the WHM API
13
14
  attr_accessor :base_url
14
15
 
16
+ # Enable Basic Authentication with API - default false
17
+ attr_accessor :basic_auth
18
+
15
19
  # API username - :default => root
16
20
  attr_accessor :user
17
21
 
@@ -47,10 +51,11 @@ module Lumberg
47
51
  # ==== Optional
48
52
  # * <tt>:user</tt> - PENDING
49
53
  # * <tt>:ssl</tt> - PENDING
54
+ # * <tt>:basic_auth</tt>
50
55
  def initialize(options)
51
56
  Args.new(options) do |c|
52
57
  c.requires :host, :hash
53
- c.optionals :user, :ssl
58
+ c.optionals :user, :ssl, :basic_auth
54
59
  end
55
60
 
56
61
  @ssl_verify ||= false
@@ -58,6 +63,7 @@ module Lumberg
58
63
  @host = options.delete(:host)
59
64
  @hash = format_hash(options.delete(:hash))
60
65
  @user = (options.has_key?(:user) ? options.delete(:user) : 'root')
66
+ @basic_auth = options.has_key?(:basic_auth) && options.delete(:basic_auth)
61
67
 
62
68
  @base_url = format_url(options)
63
69
  end
@@ -110,6 +116,75 @@ module Lumberg
110
116
  perform_request('getlanglist', {:key => 'lang'})
111
117
  end
112
118
 
119
+ def list_ips
120
+ perform_request('listips', {:key => 'result'})
121
+ end
122
+
123
+ def add_ip(options = {})
124
+ Args.new(options) do |c|
125
+ c.requires :ip, :netmask
126
+ end
127
+
128
+ perform_request('addip', options.merge(:key => 'addip'))
129
+ end
130
+
131
+ def delete_ip(options = {})
132
+ Args.new(options) do |c|
133
+ c.requires :ip
134
+ c.optionals :ethernetdev
135
+ c.booleans :skipifshutdown
136
+ end
137
+
138
+ perform_request('delip', options.merge(:key => 'delip'))
139
+ end
140
+
141
+ def set_hostname(options = {})
142
+ Args.new(options) do |c|
143
+ c.requires :hostname
144
+ end
145
+
146
+ perform_request('sethostname', options.merge(:key => 'sethostname'))
147
+ end
148
+
149
+ def set_resolvers(options = {})
150
+ Args.new(options) do |c|
151
+ c.requires :nameserver1
152
+ c.optionals :nameserver2, :nameserver3
153
+ end
154
+
155
+ perform_request('setresolvers', options.merge(:key => 'setresolvers'))
156
+ end
157
+
158
+ def show_bandwidth(options = {})
159
+ Args.new(options) do |c|
160
+ c.optionals :month, :year, :showres, :search, :searchtype
161
+ end
162
+
163
+ perform_request('showbw', options.merge(:key => 'bandwidth'))
164
+ end
165
+
166
+ def set_nv_var(options = {})
167
+ Args.new(options) do |c|
168
+ c.requires :key
169
+ c.optionals :value
170
+ end
171
+
172
+ perform_request('nvset', options.merge(:key => 'nvset'))
173
+ end
174
+
175
+ def get_nv_var(options = {})
176
+ Args.new(options) do |c|
177
+ c.requires :key
178
+ c.optionals :value
179
+ end
180
+
181
+ perform_request('nvget', options.merge(:key => 'nvget'))
182
+ end
183
+
184
+ def reboot
185
+ perform_request('reboot', {:key => "reboot"})
186
+ end
187
+
113
188
  protected
114
189
  def response_type
115
190
  if !@force_response_type.nil?
@@ -268,10 +343,16 @@ module Lumberg
268
343
  query = uri.query
269
344
  url << "?" + query unless query.nil? || query.empty?
270
345
 
271
- # Add Auth Header
272
346
  req = Net::HTTP::Get.new(url)
273
- req.add_field("Authorization", "WHM #{@user}:#{@hash}")
274
347
 
348
+ # Add Auth Header
349
+ if basic_auth
350
+ encoded = Base64.encode64("#{@user}:#{@hash}")
351
+ auth = "Basic #{encoded}"
352
+ else
353
+ auth = "WHM #{@user}:#{@hash}"
354
+ end
355
+ req.add_field("Authorization", auth)
275
356
  req
276
357
  end
277
358
 
data/lumberg.gemspec CHANGED
@@ -16,11 +16,10 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency 'json'
18
18
  s.add_runtime_dependency('jruby-openssl', '~> 0.7.3') if RUBY_PLATFORM == 'java'
19
- s.add_development_dependency 'rspec'
20
- s.add_development_dependency 'fakeweb'
21
- s.add_development_dependency 'vcr'
22
- s.add_development_dependency 'rcov'
23
- s.add_development_dependency 'metric_fu'
19
+ s.add_development_dependency 'rspec', '~> 2.8.0'
20
+ s.add_development_dependency 'fakeweb', '~> 1.3.0'
21
+ s.add_development_dependency 'vcr', '~> 1.11.3'
22
+ s.add_development_dependency 'rake', '~> 0.9.2.2'
24
23
 
25
24
  s.files = `git ls-files`.split("\n")
26
25
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,22 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: https://myhost.com:2087/json-api/addip?ip=208.77.188.166&netmask=255.255.255.0
6
+ body:
7
+ headers:
8
+ authorization:
9
+ - WHM root:iscool
10
+ response: !ruby/struct:VCR::Response
11
+ status: !ruby/struct:VCR::ResponseStatus
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ server:
16
+ - cpsrvd/11.28.87
17
+ transfer-encoding:
18
+ - chunked
19
+ content-type:
20
+ - text/plain
21
+ body: "{\"addip\":[{\"errors\":[],\"status\":1,\"statusmsg\":\"Success\",\"msgs\":\"eth0:cp242 is now up. 208.77.188.166/255.255.255.0 broadcast 208.77.188.255 has been added\\nSystem has 240 free ips.\"}]}"
22
+ http_version: "1.1"
@@ -0,0 +1,22 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: https://myhost.com:2087/json-api/delip?ip=208.77.188.166
6
+ body:
7
+ headers:
8
+ authorization:
9
+ - WHM root:iscool
10
+ response: !ruby/struct:VCR::Response
11
+ status: !ruby/struct:VCR::ResponseStatus
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ server:
16
+ - cpsrvd/11.28.87
17
+ transfer-encoding:
18
+ - chunked
19
+ content-type:
20
+ - text/plain
21
+ body: "{\"delip\":[{\"status\":1,\"statusmsg\":\"208.77.188.166 has been removed.\",\"warnings\":[\"Skipping shutdown of ethernet device 208.77.188.166 is bound to.\"]}]}"
22
+ http_version: "1.1"