plesk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile CHANGED
@@ -1,3 +1,2 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
-
data/Guardfile CHANGED
@@ -1,12 +1,3 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'bundler' do
5
- watch('Gemfile')
6
- # Uncomment next line if Gemfile contain `gemspec' command
7
- # watch(/^.+\.gemspec/)
8
- end
9
-
10
1
  guard 'rspec', :version => 2 do
11
2
  watch(%r{^spec/.+_spec\.rb$})
12
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
data/Rakefile CHANGED
@@ -1,3 +1,2 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
- require "pludoni-gem"
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+ # for 1.8.7 compat
3
+ unless File.respond_to? :realpath
4
+ class File #:nodoc:
5
+ def self.realpath path
6
+ return realpath(File.readlink(path)) if symlink?(path)
7
+ path
8
+ end
9
+ end
10
+ end
11
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
12
+
13
+ require 'plesk'
14
+ require 'gli'
15
+
16
+ include GLI
17
+
18
+ program_desc 'Use ./plesk to get your api key from your plesk instance.'
19
+
20
+ version Plesk::VERSION
21
+
22
+ #desc 'Describe some switch here'
23
+ #switch [:s,:switch]
24
+
25
+ #desc 'Describe some flag here'
26
+ #default_value 'the default'
27
+ #arg_name 'The name of the argument'
28
+ #flag [:f,:flagname]
29
+
30
+ desc 'Get your api key for the IP which will use the gem, so you dont have to use your login information for future use.'
31
+ arg_name 'You have to provide your hostname and your credentials.'
32
+ command :key do |c|
33
+ c.action do |global_options,options,args|
34
+ if args.length < 4
35
+ raise 'Usage: ./plesk key <hostname> <user> <password> <ip_address>'
36
+ end
37
+ client = Plesk::Client.new(*args[0..2])
38
+ puts client.get_secret_for_ip args[3]
39
+ end
40
+ end
41
+
42
+ pre do |global,command,options,args|
43
+ # Pre logic here
44
+ # Return true to proceed; false to abort and not call the
45
+ # chosen command
46
+ # Use skips_pre before a command to skip this block
47
+ # on that command only
48
+ true
49
+ end
50
+
51
+ post do |global,command,options,args|
52
+ # Post logic here
53
+ # Use skips_post before a command to skip this
54
+ # block on that command only
55
+ end
56
+
57
+ on_error do |exception|
58
+ # Error logic here
59
+ # return false to skip default error handling
60
+ true
61
+ end
62
+
63
+ exit GLI.run(ARGV)
64
+
@@ -1,132 +1,11 @@
1
- require "net/https"
2
- require "nokogiri"
3
1
  module Plesk
4
- class Client
5
- attr_accessor :response, :uri,:http
6
- def initialize path, user, password
7
- @response = 0
8
- @uri = URI(path)
9
- http = Net::HTTP.new(@uri.host, @uri.port)
10
- http.use_ssl = true
11
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
12
- @headers = {
13
- 'HTTP_AUTH_LOGIN' => user,
14
- 'HTTP_AUTH_PASSWD' => password,
15
- 'Accept' => '*/*',
16
- 'Content-Type' => 'text/xml',
17
- }
18
- @http = http
19
- end
20
- def get_domain_info
21
- packet = PleskPacket.new
22
- packet.domain_info
23
- start_request packet.to_xml
24
- end
25
- def get_domain_id_for domain
26
- packet = PleskPacket.new
27
- packet.domain_info_for_domain domain
28
- answer = start_request packet.to_xml
29
- Nokogiri::XML(answer.body).at('id').text
30
- end
31
- def get_mailgroup_info_for mail
32
- name,domain = mail.split("@")
33
- domain_id = get_domain_id_for domain
34
- packet = PleskPacket.new
35
- packet.mailgroup_info name,domain_id
36
- answer = start_request packet.to_xml
37
2
 
38
- Nokogiri::XML(answer.body).search('address').map(&:text)
39
- end
40
-
41
- def set_mailgroup_for mail,mails
42
- name,domain = mail.split("@")
43
- domain_id = get_domain_id_for domain
44
- packet = PleskPacket.new
45
- packet.mailgroup_set name,domain_id,mails
46
- answer = start_request packet.to_xml
47
- end
48
-
49
- def start_request(body)
50
- response = http.request_post(@uri.path,body,@headers)
51
- @response = response
52
- end
53
- end
54
- class PleskPacket
55
- attr_accessor :content
56
- def initialize version="1.4.1.2"
57
- @content = Nokogiri::XML::Builder.new do |xml|
58
- xml.packet(version: version)
59
- end.doc
60
- end
61
- def domain_info
62
- doc = @content
63
- Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
64
- xml.domain {
65
- xml.get {
66
- xml.filter
67
- xml.dataset {
68
- xml.limits
69
- xml.prefs
70
- }
71
- }
72
- }
73
- end
74
- end
75
- def domain_info_for_domain domain
76
- doc = @content
77
- @content=Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
78
- xml.domain {
79
- xml.get {
80
- xml.filter {
81
- xml.domain_name domain
82
- }
83
- xml.dataset {
84
- xml.limits
85
- xml.prefs
86
- }
87
- }
88
- }
89
- end
90
- end
91
- def mailgroup_info name,id
92
- doc = @content
93
- @content =Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
94
- xml.mail {
95
- xml.get_info {
96
- xml.filter {
97
- xml.name name
98
- xml.domain_id id
99
- }
100
- xml.group
101
- }
102
- }
103
- end
104
- end
105
- def mailgroup_set name,id,mails
106
- doc = @content
107
- @content =Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
108
- xml.mail {
109
- xml.update {
110
- xml.set {
111
- xml.filter {
112
- xml.domain_id id
113
- xml.mailname {
114
- xml.name name
115
- xml.mailgroup {
116
- xml.enabled :true
117
- mails.each do |mail|
118
- xml.address mail
119
- end
120
- }
121
- }
122
- }
123
- }
124
- }
125
- }
126
- end
127
- end
128
- def to_xml
129
- @content.to_xml
130
- end
3
+ class PleskException < Exception
131
4
  end
132
5
  end
6
+
7
+ require "plesk/client"
8
+ require "plesk/packet"
9
+ require "plesk/version"
10
+
11
+
@@ -0,0 +1,83 @@
1
+ require "net/https"
2
+ require "nokogiri"
3
+ require "open-uri"
4
+ module Plesk
5
+ class Client
6
+ attr_accessor :response, :uri,:http
7
+ def initialize host, *credentials
8
+ if credentials.size == 2
9
+ @headers = {
10
+ 'HTTP_AUTH_LOGIN' => credentials[0],
11
+ 'HTTP_AUTH_PASSWD' => credentials[1],
12
+ 'Accept' => '*/*',
13
+ 'Content-Type' => 'text/xml',
14
+ }
15
+ elsif credentials.length == 1
16
+ @headers = {
17
+ 'KEY' => credentials[0],
18
+ 'Accept' => '*/*',
19
+ 'Content-Type' => 'text/xml',
20
+ }
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ @response = 0
25
+ @uri = URI.parse "https://example.com:8443/enterprise/control/agent.php"
26
+ @uri.host = host
27
+ http = Net::HTTP.new(@uri.host, @uri.port)
28
+ http.use_ssl = true
29
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
30
+ @http = http
31
+ end
32
+ def get_domain_id_for domain
33
+ packet = Packet.new
34
+ packet.domain_info_for_domain domain
35
+ answer = start_request packet.to_xml
36
+ answer.at('id').text
37
+ end
38
+ def get_mailgroup_info_for mail
39
+ name,domain = mail.split("@")
40
+ domain_id = get_domain_id_for domain
41
+ packet = Packet.new
42
+ packet.mailgroup_info name,domain_id
43
+ answer = start_request packet.to_xml
44
+
45
+ answer.search('address').map(&:text)
46
+ end
47
+ def set_mailgroup_for mail,mails
48
+ name,domain = mail.split("@")
49
+ domain_id = get_domain_id_for domain
50
+ packet = Packet.new
51
+ packet.mailgroup_set name,domain_id,mails
52
+ answer = start_request packet.to_xml
53
+ end
54
+ def get_secret_for_ip ip
55
+ packet = Packet.new
56
+ packet.secret_key_for_ip ip
57
+ answer = start_request packet.to_xml
58
+ if answer.at('status').text == "ok"
59
+ answer.at('key').text
60
+ else
61
+ $stderr.puts answer.body
62
+ raise PleskException
63
+ end
64
+ end
65
+
66
+ def start_request(body)
67
+ response = http.request_post(@uri.path,body,@headers)
68
+ if (400...600).include? response.code.to_i
69
+ raise PleskException
70
+ end
71
+ @response = response
72
+ Nokogiri::XML(response.body)
73
+ end
74
+
75
+ private
76
+
77
+ def get_domain_info
78
+ packet = Packet.new
79
+ packet.domain_info
80
+ start_request packet.to_xml
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,91 @@
1
+
2
+ module Plesk
3
+ class Packet
4
+ attr_accessor :content
5
+ def initialize version="1.4.1.2"
6
+ @content = Nokogiri::XML::Builder.new do |xml|
7
+ xml.packet(version: version)
8
+ end.doc
9
+ end
10
+ def domain_info
11
+ doc = @content
12
+ Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
13
+ xml.domain {
14
+ xml.get {
15
+ xml.filter
16
+ xml.dataset {
17
+ xml.limits
18
+ xml.prefs
19
+ }
20
+ }
21
+ }
22
+ end
23
+ end
24
+ def domain_info_for_domain domain
25
+ doc = @content
26
+ @content=Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
27
+ xml.domain {
28
+ xml.get {
29
+ xml.filter {
30
+ xml.domain_name domain
31
+ }
32
+ xml.dataset {
33
+ xml.limits
34
+ xml.prefs
35
+ }
36
+ }
37
+ }
38
+ end
39
+ end
40
+ def mailgroup_info name,id
41
+ doc = @content
42
+ @content =Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
43
+ xml.mail {
44
+ xml.get_info {
45
+ xml.filter {
46
+ xml.name name
47
+ xml.domain_id id
48
+ }
49
+ xml.group
50
+ }
51
+ }
52
+ end
53
+ end
54
+ def mailgroup_set name,id,mails
55
+ doc = @content
56
+ @content =Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
57
+ xml.mail {
58
+ xml.update {
59
+ xml.set {
60
+ xml.filter {
61
+ xml.domain_id id
62
+ xml.mailname {
63
+ xml.name name
64
+ xml.mailgroup {
65
+ xml.enabled :true
66
+ mails.each do |mail|
67
+ xml.address mail
68
+ end
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
+ end
76
+ end
77
+ def secret_key_for_ip ip
78
+ doc = @content
79
+ Nokogiri::XML::Builder.with(doc.at('packet')) do |xml|
80
+ xml.secret_key {
81
+ xml.create {
82
+ xml.ip_address ip
83
+ }
84
+ }
85
+ end
86
+ end
87
+ def to_xml
88
+ @content.to_xml
89
+ end
90
+ end
91
+ end
@@ -1,4 +1,3 @@
1
1
  module Plesk
2
- VERSION = File.open("VERSION").read.strip
3
-
2
+ VERSION = "0.0.2"
4
3
  end
@@ -14,5 +14,9 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "plesk"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.add_dependency 'nokogiri'
17
+ gem.add_dependency 'gli'
18
+ gem.add_development_dependency 'guard-rspec'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
17
21
  gem.version = Plesk::VERSION
18
22
  end
@@ -1,28 +1,38 @@
1
1
  require "spec_helper"
2
2
  require "yaml"
3
+ require "ostruct"
3
4
  include Plesk
4
5
  describe "Plesk" do
5
6
  let(:api) {
6
7
  config = YAML.load( File.open "spec/config.yml")
7
- path = config["path"]
8
+ host = config["host"]
8
9
  user = config["user"]
9
10
  pass = config["password"]
10
- Client.new(path,user,pass)
11
+ Client.new(host,user,pass)
12
+ }
13
+ let(:api_via_key) {
14
+ config = YAML.load( File.open "spec/config.yml")
15
+ host = config["host"]
16
+ key = config["key"]
17
+ Client.new(host,key)
11
18
  }
12
19
 
13
20
  it "should connect to the Plesk RPC" do
14
21
  api.start_request("bla")
15
22
  api.response.code.should == "200"
16
23
  end
17
-
18
- it "should be able to get basic domain list" do
19
- api.get_domain_info
20
- api.response.code.should == "200"
24
+ it "should connect to the Plesk RPC via secret key" do
25
+ api_via_key.start_request("bla")
26
+ api_via_key.response.code.should == "200"
21
27
  end
28
+
29
+ #it "should be able to get basic domain list" do
30
+ #api.get_domain_info
31
+ #api.response.code.should == "200"
32
+ #end
22
33
  it "should be able to find domain id by name" do
23
34
  id = api.get_domain_id_for "itsax.de"
24
35
  id.should be_kind_of String
25
-
26
36
  end
27
37
  it "should get a mailgroup list for a mail" do
28
38
  mails = api.get_mailgroup_info_for "developer@pludoni.de"
@@ -31,28 +41,57 @@ describe "Plesk" do
31
41
  it "should set mailgroup for a mail" do
32
42
  mails_to_set = ["stefan.wienert@pludoni.de","akos.toth@pludoni.de","martin.schneider@pludoni.de"]
33
43
  answer = api.set_mailgroup_for "developer@pludoni.de", mails_to_set
34
- Nokogiri::XML(answer.response.body).at('status').text.should == "ok"
44
+ answer.at('status').text.should == "ok"
45
+ end
46
+ it "should be able to retrieve the api secret" do
47
+ #ip = "46.4.99.113"
48
+ #answer = api.get_secret_for_ip ip
49
+ #Nokogiri::XML(answer.response.body).at('status').text.should == "ok"
50
+ #Nokogiri::XML(answer.response.body).at('key').text.to_s.length.should > 10
51
+ end
52
+ it "should have a convenient way to get the key" do
53
+ ip = "46.4.99.113"
54
+ answer = api.get_secret_for_ip ip
55
+ answer.should be_kind_of String
56
+ (10..50).should include answer.length
57
+ end
58
+
59
+ it "should raise an error when plesk" do
60
+ object = OpenStruct.new body: "ERROR", code: "500"
61
+
62
+ Net::HTTP.any_instance.should_receive(:request_post).and_return(object)
63
+ ip = "46.4.99.113"
64
+
65
+ lambda {
66
+ answer = api.get_secret_for_ip ip
67
+ }.should raise_error(PleskException)
35
68
  end
36
69
  end
37
70
 
38
- describe PleskPacket do
71
+ describe Packet do
39
72
  it "should generate xml" do
40
- p = PleskPacket.new
73
+ p = Packet.new
41
74
  Nokogiri::XML(p.to_xml).errors.should be_empty
42
75
  end
43
76
  it "should have a packet version matching the param" do
44
77
  ver = "1.0.0.0"
45
- p = PleskPacket.new(ver)
78
+ p = Packet.new(ver)
46
79
  Nokogiri::XML(p.to_xml).at('packet').attr('version').should == ver
47
80
  end
48
81
  it "should create a packet to get a domain list" do
49
- p = PleskPacket.new("1.4.1.2")
82
+ p = Packet.new("1.4.1.2")
50
83
  p.domain_info
51
84
  Nokogiri::XML(p.to_xml).at('get').children.count.should > 0
52
85
  end
53
86
  it "should create a packet to get the mailgroup of a mail adress" do
54
- p = PleskPacket.new("1.6.0.2")
87
+ p = Packet.new("1.6.0.2")
55
88
  p.mailgroup_info "developer","1"
56
89
  Nokogiri::XML(p.to_xml).at('get_info').children.count.should > 0
57
90
  end
91
+ it "should create a packet to retrieve the secret" do
92
+ ip = "192.0.0.1"
93
+ p = Packet.new
94
+ p.secret_key_for_ip ip
95
+ Nokogiri::XML(p.to_xml).at('ip_address').text.should == ip
96
+ end
58
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plesk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-07 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &20145220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,29 +21,71 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ version_requirements: *20145220
25
+ - !ruby/object:Gem::Dependency
26
+ name: gli
27
+ requirement: &19733340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19733340
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-rspec
38
+ requirement: &19732240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19732240
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &19731200 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *19731200
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &19729460 !ruby/object:Gem::Requirement
25
61
  none: false
26
62
  requirements:
27
63
  - - ! '>='
28
64
  - !ruby/object:Gem::Version
29
65
  version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *19729460
30
69
  description: ! 'Plesk RPC API wrapper written in Ruby '
31
70
  email:
32
71
  - info@outsmartin.de
33
72
  executables:
34
- - template
73
+ - plesk
35
74
  extensions: []
36
75
  extra_rdoc_files: []
37
76
  files:
38
77
  - .gitignore
78
+ - .rspec
39
79
  - Gemfile
40
80
  - Guardfile
41
81
  - LICENSE
42
82
  - README.md
43
83
  - Rakefile
44
84
  - VERSION
45
- - bin/template
85
+ - bin/plesk
46
86
  - lib/plesk.rb
87
+ - lib/plesk/client.rb
88
+ - lib/plesk/packet.rb
47
89
  - lib/plesk/version.rb
48
90
  - plesk.gemspec
49
91
  - spec/config.yml.example
@@ -61,15 +103,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
103
  - - ! '>='
62
104
  - !ruby/object:Gem::Version
63
105
  version: '0'
106
+ segments:
107
+ - 0
108
+ hash: 2607453395036010812
64
109
  required_rubygems_version: !ruby/object:Gem::Requirement
65
110
  none: false
66
111
  requirements:
67
112
  - - ! '>='
68
113
  - !ruby/object:Gem::Version
69
114
  version: '0'
115
+ segments:
116
+ - 0
117
+ hash: 2607453395036010812
70
118
  requirements: []
71
119
  rubyforge_project:
72
- rubygems_version: 1.8.24
120
+ rubygems_version: 1.8.10
73
121
  signing_key:
74
122
  specification_version: 3
75
123
  summary: You can communicate with your Plesk System through the RPC API. Sends XML
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # for 1.8.7 compat
3
- unless File.respond_to? :realpath
4
- class File #:nodoc:
5
- def self.realpath path
6
- return realpath(File.readlink(path)) if symlink?(path)
7
- path
8
- end
9
- end
10
- end
11
- $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
12
-
13
- # require 'module' here