smart_proxy_acd_core 0.0.1 → 0.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
  SHA256:
3
- metadata.gz: ab65b2e5c4dfc4a353ecfba363510d52c885442c372956d2f6b35c796753c58c
4
- data.tar.gz: f04b97ab4e74bb1ae449036f20bff594e45fe1c6708eb2dbaf058a229efecceb
3
+ metadata.gz: 215b06ad56d650035cdc4079ee6fcad40333f619366e9be8d4bc8c9451b9a207
4
+ data.tar.gz: 295fd6ae912f62f4dddc0e59fda3afb9b72c9e059df33dd7fc46878185b07996
5
5
  SHA512:
6
- metadata.gz: 2aed9d7ec66263cda317232f013f3c5bd3cf78f5197867a6c2cf8a874265c17798ee9b1fa1bc6aa8c89f61035e56e26441a5120d1be1fb703816d268b852c3d7
7
- data.tar.gz: 0bbf786b41d8de45b19c3083df9d8ddc057fb5239ac4f2c4c8623075e61c72f7f04346f991b17dd9f1950eebbfa44576ff3c144b05a9aef8e02883e33269f79e
6
+ metadata.gz: 6812cf3394f08d4d184841043ea653e7fe1bd4d59c39b7726c356667061975af4262383f9dc0d3d577093c65da44f849281d4f547f7b70003ba13465cdfdb6a5
7
+ data.tar.gz: e5bab31f198a74744bab097dac0426928855e7d38e77248b72dea2b81c1d9426593a344e9103e6e85308ae2a637bad3ccbcc9cdffa0bb153ab8e765a07affaad
@@ -3,13 +3,14 @@ require 'tempfile'
3
3
  require 'rest-client'
4
4
  require 'tmpdir'
5
5
 
6
+ # rubocop:disable ClassLength
7
+
6
8
  module SmartProxyAcdCore
7
9
  # Implements the AcdRunner to be used by foreman_remote_execution
8
10
  class AcdRunner < ForemanTasksCore::Runner::CommandRunner
9
11
  DEFAULT_REFRESH_INTERVAL = 1
10
12
 
11
13
  class << self
12
-
13
14
  def parse_dynflow_settings(path)
14
15
  return @dynflow_settings if defined? @dynflow_settings
15
16
  if File.exist?(path)
@@ -52,8 +53,8 @@ module SmartProxyAcdCore
52
53
 
53
54
  def get_playbook(playbook_id)
54
55
  logger.debug("Get playbook with id #{playbook_id}")
55
- response = playbook_resource(playbook_id).get()
56
- if response.code.to_s != "200"
56
+ response = playbook_resource(playbook_id).get
57
+ if response.code.to_s != '200'
57
58
  raise "Failed performing callback to Foreman server: #{response.code} #{response.body}"
58
59
  end
59
60
  tmp_file = Tempfile.new.path
@@ -68,17 +69,17 @@ module SmartProxyAcdCore
68
69
  end
69
70
 
70
71
  def store_playbook
71
- logger.debug("Unpack ansible playbook")
72
+ logger.debug('Unpack ansible playbook')
72
73
  dir = Dir.mktmpdir
73
- raise "Could not create temporary directory to run ansible playbook" if dir.nil? || !Dir.exists?(dir)
74
+ raise 'Could not create temporary directory to run ansible playbook' if dir.nil? || !Dir.exist?(dir)
74
75
  command = "base64 -d #{@playbook_tmp_base64_file} | tar xz -C #{dir}"
75
76
  system(command)
76
77
  @playbook_tmp_dir = dir
77
78
  end
78
79
 
79
80
  def cleanup
80
- File.unlink(@playbook_tmp_base64_file) if File.exists?(@playbook_tmp_base64_file)
81
- FileUtils.rm_rf(@playbook_tmp_dir) if Dir.exists?(@playbook_tmp_dir)
81
+ File.unlink(@playbook_tmp_base64_file) if File.exist?(@playbook_tmp_base64_file)
82
+ FileUtils.rm_rf(@playbook_tmp_dir) if Dir.exist?(@playbook_tmp_dir)
82
83
  end
83
84
 
84
85
  def start
@@ -89,13 +90,13 @@ module SmartProxyAcdCore
89
90
  store_playbook
90
91
 
91
92
  @playbook_path = File.join(@playbook_tmp_dir, @playbook_file)
92
- raise "Could not run playbook: playbook file #{@playbook_file} not found in playbook dir #{@playbook_tmp_dir}" unless File.exists?(@playbook_path)
93
+ raise "Could not run playbook: playbook file #{@playbook_file} not found in playbook dir #{@playbook_tmp_dir}" unless File.exist?(@playbook_path)
93
94
 
94
- publish_data("Write temporary inventory", 'stdout')
95
+ publish_data('Write temporary inventory', 'stdout')
95
96
  write_inventory
96
97
 
97
98
  command = generate_command
98
- logger.debug("Running command '#{command.join(' ')}'")
99
+ logger.debug("Running command #{command.join(' ')}")
99
100
  initialize_command(*command)
100
101
  end
101
102
 
@@ -113,7 +114,7 @@ module SmartProxyAcdCore
113
114
  private
114
115
 
115
116
  def parse_acd_job
116
- @acd_job = YAML.load(@options['script'])
117
+ @acd_job = YAML.safe_load(@options['script'])
117
118
  @application_name = @acd_job['application_name']
118
119
  @playbook_id = @acd_job['playbook_id']
119
120
  @playbook_file = @acd_job['playbook_file']
@@ -131,25 +132,49 @@ module SmartProxyAcdCore
131
132
  end
132
133
 
133
134
  def environment
134
- env={}
135
+ env = {}
135
136
  env['ANSIBLE_CALLBACK_WHITELIST'] = ''
136
137
  env['ANSIBLE_LOAD_CALLBACK_PLUGINS'] = '0'
137
138
  env
138
139
  end
139
140
 
141
+ def setup_verbosity
142
+ verbosity = ''
143
+ if @acd_job['verbose']
144
+ verbosity_level = @acd_job['verbose'].split(' ').first.to_i
145
+ if verbosity_level.positive?
146
+ verbosity = '-'
147
+ verbosity_level.times do
148
+ verbosity += 'v'
149
+ end
150
+ end
151
+ end
152
+ verbosity
153
+ end
154
+
155
+ def valid_tags(tag)
156
+ re = /^\w+(\s*,\s*\w+)*$/
157
+ return true if @acd_job[tag] && @acd_job[tag].match?(re)
158
+ end
159
+
140
160
  def generate_command
141
161
  logger.debug("Generate command with #{@inventory_path} to run #{@playbook_id} with path #{@playbook_path}")
142
162
  command = [environment]
143
- command << "ansible-playbook"
144
- command << "-i"
163
+ command << 'ansible-playbook'
164
+ command << '-i'
145
165
  command << @inventory_path
146
- command << "-v" if @acd_job['verbose'] == true
147
- if @acd_job.has_key?('extra_vars') && !@acd_job['extra_vars'].nil? && !@acd_job['extra_vars'].empty?
148
- command << "--extra-vars"
166
+ verbose = setup_verbosity
167
+ command << verbose unless verbose.empty?
168
+ command << "--tags '#{@acd_job['tags']}'" if valid_tags('tags')
169
+ command << "--skip-tags '#{@acd_job['skip_tags']}'" if valid_tags('skip_tags')
170
+ if @acd_job.key?('extra_vars') && !@acd_job['extra_vars'].nil? && !@acd_job['extra_vars'].empty?
171
+ command << '--extra-vars'
149
172
  command << "'#{@acd_job['extra_vars']}'"
150
173
  end
151
- command << "#{@playbook_path}"
174
+ command << @playbook_path.to_s
152
175
  command
153
176
  end
154
177
  end
155
178
  end
179
+
180
+ # rubocop:enable ClassLength
@@ -1,3 +1,3 @@
1
1
  module SmartProxyAcdCore
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_acd_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernhard suttner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-19 00:00:00.000000000 Z
11
+ date: 2021-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreman-tasks-core