moxprox 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/moxprox.rb +16 -0
- data/lib/moxprox/cluster.rb +18 -0
- data/lib/moxprox/machine.rb +38 -0
- data/lib/moxprox/node.rb +29 -0
- data/lib/moxprox/null_logger.rb +18 -0
- data/lib/moxprox/request.rb +15 -0
- data/lib/moxprox/ticket.rb +14 -0
- data/lib/moxprox/version.rb +3 -0
- data/moxprox.gemspec +34 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2d687aa7b4ebeb9ad3580ae5586409086f9555d
|
4
|
+
data.tar.gz: d1e44ddd5ea2468b639eba07cb72565595a2ee46
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fa5b17b5de9cd74fb44697d19c6ecbfc8b0c8c5c29c758cfad7d2de748ff11b5f4a0fad66a33d66a67d8c45a0f573689f9dc04a2da6e4de9a365ff39ca17ce87
|
7
|
+
data.tar.gz: 05bf64c6542c3e58c62ded5106d8b4e323fbf8538190b8a908aa70894005032292fd2f9c8c6e92d3ef8d0b5b8607d3668996b4510716e9771cb5d4c212e91e69
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 José Tomás Albornoz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Moxprox
|
2
|
+
|
3
|
+
A very simple Ruby client to [Proxmox](https://www.proxmox.com/en/)'s api.
|
4
|
+
|
5
|
+
This gem is under active development. Suggestions [are always welcome](https://github.com/eljojo/moxprox/issues/new).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'moxprox'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'logger'
|
23
|
+
require 'moxprox'
|
24
|
+
|
25
|
+
cluster = Moxprox.new(
|
26
|
+
host: 'my-proxmox-cluster.com',
|
27
|
+
username: 'login@pve', # can also be 'root@pam'
|
28
|
+
password: 'peanut_butter_and_j3lly',
|
29
|
+
logger: Logger.new(STDOUT)
|
30
|
+
)
|
31
|
+
node = proxmox.node("homer")
|
32
|
+
machine = node.machine(101)
|
33
|
+
machine.status # :stopped
|
34
|
+
machine.start
|
35
|
+
machine.status # :running
|
36
|
+
sleep 10
|
37
|
+
machine.shutdown
|
38
|
+
```
|
39
|
+
|
40
|
+
## Development
|
41
|
+
|
42
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
43
|
+
|
44
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/eljojo/moxprox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
49
|
+
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "moxprox"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/moxprox.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "json"
|
2
|
+
require "logger"
|
3
|
+
require "excon"
|
4
|
+
require "moxprox/version"
|
5
|
+
require "moxprox/machine"
|
6
|
+
require "moxprox/node"
|
7
|
+
require "moxprox/request"
|
8
|
+
require "moxprox/ticket"
|
9
|
+
require "moxprox/null_logger"
|
10
|
+
require "moxprox/cluster"
|
11
|
+
|
12
|
+
module Moxprox
|
13
|
+
def self.new(*params)
|
14
|
+
Cluster.new(*params)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Moxprox
|
2
|
+
class Cluster
|
3
|
+
attr_reader :logger
|
4
|
+
|
5
|
+
def initialize(opts = {})
|
6
|
+
@ticket = Ticket.login(opts.fetch(:host), opts.fetch(:username), opts.fetch(:password))
|
7
|
+
@logger = opts[:logger] || NullLoger.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def node(name)
|
11
|
+
Node.new(self, name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def request(path, method = "get", query = {})
|
15
|
+
Request.new(path, method, query).perform(@ticket)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Moxprox
|
2
|
+
class Machine < Struct.new(:node, :id)
|
3
|
+
def start(&block)
|
4
|
+
logger.info "starting machine #{id} on #{node.name}"
|
5
|
+
upid = request("status/start", "post")
|
6
|
+
if block_given?
|
7
|
+
node.wait_for_task(upid, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def shutdown(&block)
|
12
|
+
logger.info "shutting down machine #{id} on #{node.name}"
|
13
|
+
upid = request("status/shutdown", "post")
|
14
|
+
if block_given?
|
15
|
+
node.wait_for_task(upid, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
request("config")
|
21
|
+
end
|
22
|
+
|
23
|
+
def status
|
24
|
+
res = request("status/current")
|
25
|
+
logger.debug "status of machine #{id} on #{node.name}: #{res["status"]}"
|
26
|
+
res["status"].to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
def request(path, method = "get", query = {})
|
30
|
+
node.request("qemu/#{id}/#{path}", method, query)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def logger
|
35
|
+
node.send(:logger)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/moxprox/node.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Moxprox
|
2
|
+
class Node < Struct.new(:cluster, :name)
|
3
|
+
def machine(id)
|
4
|
+
Machine.new(self, id)
|
5
|
+
end
|
6
|
+
|
7
|
+
def request(path, method = "get", query = {})
|
8
|
+
cluster.request("nodes/#{name}/#{path}", method, query)
|
9
|
+
end
|
10
|
+
|
11
|
+
def task_status(upid)
|
12
|
+
res = request("tasks/#{upid}/status")
|
13
|
+
status = (res["exitstatus"] || res["status"] || "error").downcase.to_sym
|
14
|
+
logger.debug "status of task #{upid}: #{status}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def wait_for_task(upid, &block)
|
18
|
+
while (status = task_status(upid)) == :running
|
19
|
+
sleep 1
|
20
|
+
end
|
21
|
+
yield status
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def logger
|
26
|
+
cluster.send(:logger)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Moxprox
|
4
|
+
attr_reader :logger
|
5
|
+
|
6
|
+
def initialize(opts = {})
|
7
|
+
@ticket = Ticket.login(opts.fetch(:host), opts.fetch(:username), opts.fetch(:password))
|
8
|
+
@logger = opts[:logger] || NullLoger.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def node(name)
|
12
|
+
Node.new(self, name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def request(path, method = "get", query = {})
|
16
|
+
Request.new(path, method, query).perform(@ticket)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Moxprox
|
2
|
+
class Request
|
3
|
+
def initialize(path, method = "post", query = {})
|
4
|
+
@path = path
|
5
|
+
@query = query
|
6
|
+
@method = method
|
7
|
+
end
|
8
|
+
|
9
|
+
def perform(ticket)
|
10
|
+
result = ticket.connection.request(path: "/api2/json/#{@path}", method: @method, query: @query, headers: ticket.headers)
|
11
|
+
data = JSON.parse(result.body)
|
12
|
+
data["data"]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Moxprox
|
2
|
+
class Ticket < Struct.new(:connection, :ticket, :csrf)
|
3
|
+
def self.login(host, username, password)
|
4
|
+
connection = Excon.new("https://#{host}:8006")
|
5
|
+
res = connection.post(path: "/api2/json/access/ticket", query: { username: username, password: password })
|
6
|
+
data = JSON.parse(res.body)
|
7
|
+
self.new(connection, data["data"]["ticket"], data["data"]["CSRFPreventionToken"])
|
8
|
+
end
|
9
|
+
|
10
|
+
def headers
|
11
|
+
{ 'Cookie' => "PVEAuthCookie=#{ticket}", "CSRFPreventionToken" => csrf }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/moxprox.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'moxprox/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "moxprox"
|
8
|
+
spec.version = Moxprox::VERSION
|
9
|
+
spec.authors = ["José Tomás Albornoz"]
|
10
|
+
spec.email = ["jojo@eljojo.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple client for Proxmox's API}
|
13
|
+
spec.description = %q{Control your Proxmox's virtual machines with Moxprox}
|
14
|
+
spec.homepage = "https://github.com/eljojo/moxprox"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "excon", ">= 0.30.0"
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moxprox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- José Tomás Albornoz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: excon
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.30.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.30.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Control your Proxmox's virtual machines with Moxprox
|
70
|
+
email:
|
71
|
+
- jojo@eljojo.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/moxprox.rb
|
87
|
+
- lib/moxprox/cluster.rb
|
88
|
+
- lib/moxprox/machine.rb
|
89
|
+
- lib/moxprox/node.rb
|
90
|
+
- lib/moxprox/null_logger.rb
|
91
|
+
- lib/moxprox/request.rb
|
92
|
+
- lib/moxprox/ticket.rb
|
93
|
+
- lib/moxprox/version.rb
|
94
|
+
- moxprox.gemspec
|
95
|
+
homepage: https://github.com/eljojo/moxprox
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.5
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Simple client for Proxmox's API
|
119
|
+
test_files: []
|