smart_proxy_salt 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77c8f6c3ad50715f41fb5606a65ddd3cb6f06bd1
4
- data.tar.gz: 61153e01b2c2043c19b22f33a651932c6c8fb8b9
3
+ metadata.gz: 3351c2de7272f0dbb7b148909a51ad602199427d
4
+ data.tar.gz: 64aa21a514991c9b241451210c1972df71a61ee5
5
5
  SHA512:
6
- metadata.gz: de0c1674f1e3742a185e3f1f4bb9574e8c2c498cc3fa1278914c1e0617517cfe19a63774f4c53d3b567650bbc959816ee312a7a7e6dee3cd42785d071de1c13f
7
- data.tar.gz: f85fb6810cbb2c47846f167875880825b788f9cf7cd430358c1d58becdaae63b4b2b9f8884428104a5626288d15e761d415019a2bb24b2f0acedbffca49a5fb6
6
+ metadata.gz: 70501217d2996bb1a95dc6845396a0cf8df049c3642626eb219d3d84f1cc058b15558aeac6acacdd3b722a89d7ae2e99b3db06e5baf71537061959a049ea1f2b
7
+ data.tar.gz: 4ce92e47305d9ea121449067c6945fcbeb92a94378b9ba259d8709d4e6fe9d7f7563289c318fa0363fa20d5b1ceeefed7577d35a33d18507d0d329794289191c
@@ -0,0 +1,45 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+
6
+ module Proxy::Salt
7
+ class ApiError < RuntimeError; end
8
+
9
+ class ApiRequest
10
+ attr_reader :url, :username, :password, :auth
11
+
12
+ def initialize
13
+ @url = Proxy::Salt::Plugin.settings.api_url
14
+ @auth = Proxy::Salt::Plugin.settings.api_auth
15
+ @username = Proxy::Salt::Plugin.settings.api_username
16
+ @password = Proxy::Salt::Plugin.settings.api_password
17
+
18
+ begin
19
+ URI.parse(url)
20
+ rescue URI::InvalidURIError => e
21
+ raise ConfigurationError.new("Invalid Salt api_url setting: #{e}")
22
+ end
23
+ end
24
+
25
+ def post(path, options = {})
26
+ uri = URI.parse(url)
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.use_ssl = uri.scheme == 'https'
29
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
30
+ path = [uri.path, path].join('/') unless uri.path.empty?
31
+
32
+ request = Net::HTTP::Post.new(URI.join(uri.to_s, path).path)
33
+ request.add_field('Accept', 'application/json')
34
+ request.set_form_data(options.merge(:username => username, :password => password, :eauth => auth))
35
+
36
+ response = http.request(request)
37
+
38
+ if response.is_a? Net::HTTPOK
39
+ JSON.load(response.body)
40
+ else
41
+ raise ApiError.new("Failed to query Salt API (#{response.code}): #{response.body}")
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,34 +1,8 @@
1
- module Proxy::Salt
1
+ module Proxy::Salt::CLI
2
2
  extend ::Proxy::Log
3
3
  extend ::Proxy::Util
4
4
 
5
5
  class << self
6
-
7
- # Move these to a util thingy later
8
- def shell_command(cmd, wait = true)
9
- begin
10
- c = popen(cmd)
11
- unless wait
12
- Process.detach(c.pid)
13
- return 0
14
- end
15
- Process.wait(c.pid)
16
- logger.info("Result: #{c.read}")
17
- rescue Exception => e
18
- logger.error("Exception '#{e}' when executing '#{cmd}'")
19
- return false
20
- end
21
- logger.warn("Non-null exit code when executing '#{cmd}'") if $?.exitstatus != 0
22
- $?.exitstatus == 0
23
- end
24
-
25
- def popen(cmd)
26
- # 1.8.7 note: this assumes that cli options are space-separated
27
- cmd = cmd.join(' ') unless RUBY_VERSION > '1.8.7'
28
- logger.debug("about to execute: #{cmd}")
29
- IO.popen(cmd)
30
- end
31
-
32
6
  def autosign_file
33
7
  Proxy::Salt::Plugin.settings.autosign_file
34
8
  end
@@ -136,6 +110,32 @@ module Proxy::Salt
136
110
 
137
111
  end
138
112
 
113
+ private
114
+
115
+ def shell_command(cmd, wait = true)
116
+ begin
117
+ c = popen(cmd)
118
+ unless wait
119
+ Process.detach(c.pid)
120
+ return 0
121
+ end
122
+ Process.wait(c.pid)
123
+ logger.info("Result: #{c.read}")
124
+ rescue Exception => e
125
+ logger.error("Exception '#{e}' when executing '#{cmd}'")
126
+ return false
127
+ end
128
+ logger.warn("Non-null exit code when executing '#{cmd}'") if $?.exitstatus != 0
129
+ $?.exitstatus == 0
130
+ end
131
+
132
+ def popen(cmd)
133
+ # 1.8.7 note: this assumes that cli options are space-separated
134
+ cmd = cmd.join(' ') unless RUBY_VERSION > '1.8.7'
135
+ logger.debug("about to execute: #{cmd}")
136
+ IO.popen(cmd)
137
+ end
138
+
139
139
  def find_salt_binaries
140
140
  @salt_key = which('salt-key')
141
141
  unless File.exists?("#{@salt_key}")
@@ -159,6 +159,5 @@ module Proxy::Salt
159
159
  logger.debug "Found sudo at #{@sudo}"
160
160
  @sudo = "#{@sudo}"
161
161
  end
162
-
163
162
  end
164
163
  end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+ require 'smart_proxy_salt/api_request'
3
+
4
+ module Proxy::Salt::Rest
5
+ extend ::Proxy::Log
6
+ extend ::Proxy::Util
7
+
8
+ class << self
9
+ def environments_list
10
+ Proxy::Salt::ApiRequest.new.post('/run', :fun => 'fileserver.envs', :client => 'runner')['return'][0].to_s
11
+ end
12
+
13
+ def states_list(environment)
14
+ states = []
15
+ files = Proxy::Salt::ApiRequest.new.post('/run', :fun => 'fileserver.file_list',
16
+ :saltenv => environment,
17
+ :client => 'runner')['return'][0]
18
+
19
+ files.each do |file|
20
+ if file =~ /\.sls\Z/ && file != "top.sls"
21
+ states << file.gsub('.sls', '')
22
+ .gsub('/init', '')
23
+ .chomp('/')
24
+ .gsub('/', '.')
25
+ end
26
+ end
27
+
28
+ states.to_s
29
+ end
30
+ end
31
+ end
@@ -1,13 +1,32 @@
1
+ require 'smart_proxy_salt/cli'
2
+ require 'smart_proxy_salt/rest'
3
+
1
4
  module Proxy::Salt
2
5
  class NotFound < RuntimeError; end
3
6
 
4
7
  class Plugin < ::Proxy::Plugin
5
8
  plugin 'salt', Proxy::Salt::VERSION
6
9
 
7
- default_settings :autosign_file => '/etc/salt/autosign.conf'
8
- default_settings :salt_command_user => 'root'
10
+ default_settings :autosign_file => '/etc/salt/autosign.conf',
11
+ :salt_command_user => 'root',
12
+ :use_api => false
9
13
 
10
14
  http_rackup_path File.expand_path('salt_http_config.ru', File.expand_path('../', __FILE__))
11
15
  https_rackup_path File.expand_path('salt_http_config.ru', File.expand_path('../', __FILE__))
12
16
  end
17
+
18
+ class << self
19
+ def method_missing(m, *args, &block)
20
+ # Use API, if it supports it, otherwise fallback to the CLI
21
+ if Proxy::Salt::Plugin.settings.use_api && Proxy::Salt::Rest.respond_to?(m)
22
+ Proxy::Salt::Rest.send(m, *args, &block)
23
+ elsif Proxy::Salt::CLI.respond_to?(m)
24
+ Proxy::Salt::CLI.send(m, *args, &block)
25
+ elsif !Proxy::Salt::Plugin.settings.use_api && Proxy::Salt::Rest.respond_to?(m)
26
+ raise NotImplementedError, 'You must enable the Salt API to use this feature.'
27
+ else
28
+ super
29
+ end
30
+ end
31
+ end
13
32
  end
@@ -1,6 +1,5 @@
1
1
  require 'sinatra'
2
2
  require 'smart_proxy_salt/salt'
3
- require 'smart_proxy_salt/salt_main'
4
3
 
5
4
  module Proxy::Salt
6
5
  class Api < ::Sinatra::Base
@@ -37,6 +36,26 @@ module Proxy::Salt
37
36
  end
38
37
  end
39
38
 
39
+ get '/environments' do
40
+ content_type :json
41
+ begin
42
+ Proxy::Salt::environments_list
43
+ rescue => e
44
+ log_halt 406, "Failed to list environments: #{e}"
45
+ end
46
+ end
47
+
48
+ get '/environments/:environment' do
49
+ content_type :json
50
+ begin
51
+ Proxy::Salt::states_list params[:environment]
52
+ rescue Proxy::Salt::NotFound => e
53
+ log_halt 404, "#{e}"
54
+ rescue => e
55
+ log_halt 406, "Failed to list states for #{params[:host]}: #{e}"
56
+ end
57
+ end
58
+
40
59
  post '/highstate/:host' do
41
60
  content_type :json
42
61
  begin
@@ -82,6 +101,5 @@ module Proxy::Salt
82
101
  log_halt 406, "Failed to list keys: #{e}"
83
102
  end
84
103
  end
85
-
86
104
  end
87
105
  end
@@ -1,5 +1,5 @@
1
1
  module Proxy
2
2
  module Salt
3
- VERSION = '2.0.0'
3
+ VERSION = '2.1.0'
4
4
  end
5
5
  end
@@ -2,3 +2,10 @@
2
2
  :enabled: true
3
3
  :autosign_file: /etc/salt/autosign.conf
4
4
  :salt_command_user: root
5
+ # Some features require using the Salt API - such as listing
6
+ # environments and retrieving state info
7
+ :use_api: false
8
+ :api_url: https://localhost:8080
9
+ :api_auth: pam
10
+ :api_username: saltuser
11
+ :api_password: password
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_salt
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Moll
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-02 00:00:00.000000000 Z
12
+ date: 2015-05-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: SaltStack Plug-In for Foreman's Smart Proxy
15
15
  email: foreman-dev@googlegroups.com
@@ -27,10 +27,12 @@ files:
27
27
  - cron/smart_proxy_salt
28
28
  - etc/foreman.yaml.example
29
29
  - lib/smart_proxy_salt.rb
30
+ - lib/smart_proxy_salt/api_request.rb
31
+ - lib/smart_proxy_salt/cli.rb
32
+ - lib/smart_proxy_salt/rest.rb
30
33
  - lib/smart_proxy_salt/salt.rb
31
34
  - lib/smart_proxy_salt/salt_api.rb
32
35
  - lib/smart_proxy_salt/salt_http_config.ru
33
- - lib/smart_proxy_salt/salt_main.rb
34
36
  - lib/smart_proxy_salt/version.rb
35
37
  - sbin/upload-salt-reports
36
38
  - settings.d/salt.yml.example