foreman_ansible_core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03c7a14d3363e5eb597ea3139968ad39879d55f3
4
+ data.tar.gz: 510720a3943124f046353c066761ec7ed5cd2435
5
+ SHA512:
6
+ metadata.gz: 99f8eb8f59d77301dc0fcd0ecc3f28b278665ec9356d4fe4e0fac7358bea4d18d3c55768b642396c42a7d4d2d2eb34b154dfaeb1d8dd169777397604be4e8b73
7
+ data.tar.gz: 2bd393fd15b149748917d3fe6b498a786278d8059d8843dd7ee1d9592048c97d1005a8e019e781b7bebf3741ee45528abb437bf756983946639c410790121808
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+ # An inventory script to load the inventory from JSON file.
3
+ #
4
+
5
+ if [ -z "$JSON_INVENTORY_FILE" ]; then
6
+ echo "JSON_INVENTORY_FILE not specified"
7
+ exit 1
8
+ fi
9
+
10
+ cat $JSON_INVENTORY_FILE
@@ -0,0 +1,14 @@
1
+ require 'foreman_tasks_core/shareable_action'
2
+
3
+ module ForemanAnsibleCore
4
+ module Actions
5
+ # Action that can be run both on Foreman or Foreman proxy side
6
+ # to execute the playbook run
7
+ class RunPlaybook < ForemanTasksCore::Runner::Action
8
+ def initiate_runner
9
+ ForemanAnsibleCore::PlaybookRunner.new(input[:inventory],
10
+ input[:playbook])
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,90 @@
1
+ require 'foreman_tasks_core/runner/command_runner'
2
+ require 'tmpdir'
3
+ require 'securerandom'
4
+
5
+ module ForemanAnsibleCore
6
+ # Implements ForemanTasksCore::Runner::Base interface for running
7
+ # Ansible playbooks
8
+ class PlaybookRunner < ForemanTasksCore::Runner::CommandRunner
9
+ def initialize(inventory, playbook)
10
+ super
11
+ @inventory = inventory
12
+ @playbook = playbook
13
+ initialize_dirs
14
+ end
15
+
16
+ def start
17
+ write_inventory
18
+ write_playbook
19
+ logger.debug('initalizing runner')
20
+ Dir.chdir(@ansible_dir) do
21
+ initialize_command(*command)
22
+ end
23
+ end
24
+
25
+ def command
26
+ command = [{ 'JSON_INVENTORY_FILE' => inventory_file }]
27
+ command << 'ansible-playbook'
28
+ command.concat(['-i', json_inventory_script])
29
+ command << playbook_file
30
+ command
31
+ end
32
+
33
+ def close
34
+ super
35
+ FileUtils.remove_entry(@working_dir) if @tmp_working_dir
36
+ end
37
+
38
+ def json_inventory_script
39
+ File.expand_path('../../bin/json_inventory.sh', File.dirname(__FILE__))
40
+ end
41
+
42
+ private
43
+
44
+ def write_inventory
45
+ ensure_directory(File.dirname(inventory_file))
46
+ File.write(inventory_file, @inventory)
47
+ end
48
+
49
+ def write_playbook
50
+ ensure_directory(File.dirname(playbook_file))
51
+ File.write(playbook_file, @playbook)
52
+ end
53
+
54
+ def inventory_file
55
+ File.join(@working_dir, 'foreman-inventories', id)
56
+ end
57
+
58
+ def playbook_file
59
+ File.join(@working_dir, "foreman-playbook-#{id}.yml")
60
+ end
61
+
62
+ def events_dir
63
+ File.join(@working_dir, 'events', id.to_s)
64
+ end
65
+
66
+ def ensure_directory(path)
67
+ if File.exist?(path)
68
+ raise "#{path} expected to be a directory" unless File.directory?(path)
69
+ else
70
+ FileUtils.mkdir_p(path)
71
+ end
72
+ path
73
+ end
74
+
75
+ # rubocop:disable Metrics/MethodLength
76
+ def initialize_dirs
77
+ settings = ForemanAnsibleCore.settings
78
+ @ansible_dir = settings[:ansible_dir]
79
+ unless File.exist?(@ansible_dir)
80
+ raise "Ansible dir #{@ansible_dir} does not exist"
81
+ end
82
+ if ForemanAnsibleCore.settings[:working_dir]
83
+ @working_dir = File.expand_path(settings[:working_dir])
84
+ else
85
+ @working_dir = Dir.mktmpdir
86
+ @tmp_working_dir = true
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module ForemanAnsibleCore
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'foreman_tasks_core'
2
+
3
+ # Core parts for the Foreman Ansinble, usable by both Foreman and
4
+ # Foreman proxy
5
+ module ForemanAnsibleCore
6
+ extend ForemanTasksCore::SettingsLoader
7
+ register_settings(:ansible, :ansible_dir => '/etc/ansible',
8
+ :working_dir => nil)
9
+
10
+ if ForemanTasksCore.dynflow_present?
11
+ require 'foreman_tasks_core/runner'
12
+ require 'foreman_ansible_core/playbook_runner'
13
+ require 'foreman_ansible_core/actions'
14
+ end
15
+
16
+ require 'foreman_ansible_core/version'
17
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreman_ansible_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Lobato Garcia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.42'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.42'
27
+ - !ruby/object:Gem::Dependency
28
+ name: foreman-tasks-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ description: |
42
+ Ansible integration with Foreman - core parts for dealing with Ansible concepts,
43
+ usable by foreman_ansible or smart_proxy_ansible to delegate the execution.
44
+ email:
45
+ - elobatocs@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - bin/json_inventory.sh
51
+ - lib/foreman_ansible_core.rb
52
+ - lib/foreman_ansible_core/actions.rb
53
+ - lib/foreman_ansible_core/playbook_runner.rb
54
+ - lib/foreman_ansible_core/version.rb
55
+ homepage: https://github.com/theforeman/foreman_ansible
56
+ licenses:
57
+ - GPL-3
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.0
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: 'Ansible integration with Foreman (theforeman.org): core bits'
79
+ test_files: []