kitchen-yansible-pusher 0.1.0

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
+ SHA256:
3
+ metadata.gz: cce6eccb4c8e13802da1576823bd80d4462d651eb383e0e8fedd0c2d9e9dc6e8
4
+ data.tar.gz: 96617f42c8b05043a26ded3e6e27b4301e74c10583ba0e7d410c6210f7343d22
5
+ SHA512:
6
+ metadata.gz: b2840c16a19b82f02de62d911875d3dd5f76b210cff316b558b6b165ca6ff4b2a683bea92c0ce3d6cf8d4ff69191144c5a6dc1133a86f133060a136ac21ed250
7
+ data.tar.gz: f3f7e6afee0f76826fc77a1f2704855267c6aef89348f470748e1ec76a8d534ab6d4bb62d15ff2065007f2956d104372e57063ac34a0d71c3f9c9a34baa44c93
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-29
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jose M. Tobar
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,52 @@
1
+ # kitchen-yansible-pusher
2
+
3
+ ![main](https://github.com/jmtx1020/kitchen-yansible-pusher/actions/workflows/main.yml/badge.svg)
4
+ ![main](https://github.com/jmtx1020/kitchen-yansible-pusher/actions/workflows/release.yml/badge.svg)
5
+ ![main](https://github.com/jmtx1020/kitchen-yansible-pusher/actions/workflows/integration.yml/badge.svg)
6
+
7
+ The goal of this project was to make a modern and minimalistic test-kitchen provisioner for Ansible, that works in push mode instead of pull mode.
8
+
9
+ From using Ansible for a while, I believe Gems like [kitchen-ansible](https://github.com/neillturner/kitchen-ansible) and [kitchen-ansiblepush](https://github.com/ahelal/kitchen-ansiblepush) both do too much, as well as seem to have been abandoned by their respective creators.
10
+
11
+ By doing less, and expecting the user to install their own Ansible, provide their configuration in the form of environment variables, an `ansible.cfg` file or tags and running only in `push` mode(normal mode) we free ourselves from having to support all kinds of installation methods across platforms and in a way future proof ourselves.
12
+
13
+ ## Installation
14
+
15
+ Edit your gem file to look like this:
16
+
17
+ ```ruby
18
+ # Install from Github
19
+ gem 'kitchen-yansible-pusher',
20
+ git: 'https://github.com/jmtx1020/kitchen-yansible-pusher.git',
21
+ branch: 'main'
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Keeping simplicity in mind, this kitchen-provisioner has minimal options to get going.
27
+ ```yaml
28
+ provisioner:
29
+ playbook: "/path/to/playbook.yaml"
30
+ config: "/path/to/ansible.cfg"
31
+ extra_vars:
32
+ MARIO: "MUSHROOM_KINGDOM"
33
+ LINK: "HYRULE_KINGDOM"
34
+ tags:
35
+ - tag1
36
+ - tag2
37
+ skip_tags:
38
+ - tag3
39
+ - tag4
40
+ verbosity: 1
41
+ vault_password_file: "/path/to/vault.password"
42
+ username: username
43
+ private_key: "/path/to/private.key"
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jmtx1020/kitchen-yansible-pusher.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kitchen/provisioner/base'
4
+ require_relative '../yansible/pusher/version'
5
+ require 'yaml'
6
+
7
+ module Kitchen
8
+ module Provisioner
9
+ # YansiblePusher is a Kitchen provisioner plugin for Ansible.
10
+ # It allows you to use Ansible playbooks to provision test instances in Test Kitchen.
11
+ #
12
+ # @example Using YansiblePusher in your kitchen.yml
13
+ # provisioner:
14
+ # name: yansible_pusher
15
+ # playbook: playbooks/playbook.yml
16
+ # extra_vars:
17
+ # MARIO: "MUSHROOM_KINGDOM"
18
+ # LINK: "HYRULE_KINGDOM"
19
+ #
20
+ # @author <MY_NAME>
21
+ # @version 0.1.0
22
+ # @see https://github.com/jmtx1020/kitchen-yansible-pusher Documentation/Homepage
23
+ # @see https://github.com/jmtx1020/kitchen-yansible-pusher/issues For bug reports and feature requests
24
+ class YansiblePusher < Kitchen::Provisioner::Base # rubocop:disable Metrics/ClassLength
25
+ kitchen_provisioner_api_version 2
26
+ plugin_version Kitchen::Yansible::Pusher::VERSION
27
+
28
+ default_config :playbook, nil
29
+ default_config :config, nil
30
+ default_config :extra_vars, {}
31
+ default_config :tags, []
32
+ default_config :skip_tags, []
33
+ default_config :verbosity, 1
34
+ default_config :vault_password_file, nil
35
+ default_config :username, nil
36
+ default_config :private_key, nil
37
+
38
+ attr_reader :sandbox_path
39
+
40
+ def install_command
41
+ # No initialization command needed on the remote instance
42
+ nil
43
+ end
44
+
45
+ def init_command
46
+ # No initialization command needed on the remote instance
47
+ nil
48
+ end
49
+
50
+ def prepare_command
51
+ # No preparation command needed on the remote instance
52
+ nil
53
+ end
54
+
55
+ def run_command
56
+ info("Running Ansible Playbook: #{config[:playbook]}")
57
+ begin
58
+ create_sandbox
59
+ run_ansible
60
+ info('Ansible Playbook Complete!')
61
+ ensure
62
+ cleanup_sandbox
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def run_ansible
69
+ command = build_ansible_command
70
+ info("Running Ansible Command: #{command}")
71
+ system(command)
72
+ end
73
+
74
+ def create_inventory
75
+ inventory = build_inventory
76
+ write_inventory_file(inventory)
77
+ end
78
+
79
+ def build_inventory
80
+ state = instance.transport.instance_variable_get(:@connection_options)
81
+ {
82
+ 'all' => {
83
+ 'hosts' => {
84
+ instance.name => build_host_config(state)
85
+ }
86
+ }
87
+ }
88
+ end
89
+
90
+ def build_host_config(state)
91
+ {
92
+ 'ansible_host' => state[:hostname],
93
+ 'ansible_port' => state[:port],
94
+ 'ansible_user' => config[:username] || state[:username]
95
+ }
96
+ end
97
+
98
+ def write_inventory_file(inventory)
99
+ inventory_file = File.join(sandbox_path, 'inventory.yml')
100
+ File.write(inventory_file, inventory.to_yaml)
101
+ inventory_file
102
+ end
103
+
104
+ def build_ansible_command
105
+ cmd = ['ansible-playbook']
106
+ ansible_options.each { |option| cmd = send(option, cmd) }
107
+ cmd << "--inventory #{create_inventory}"
108
+ cmd << config[:playbook]
109
+ cmd.join(' ')
110
+ end
111
+
112
+ def ansible_options
113
+ %i[
114
+ ansible_config
115
+ ansible_extra_vars
116
+ ansible_use_private_key
117
+ ansible_use_vault_password_file
118
+ ansible_tags
119
+ ansible_skip_tags
120
+ ansible_verbosity
121
+ ]
122
+ end
123
+
124
+ def ansible_config(cmd)
125
+ cmd.prepend("ANSIBLE_CONFIG=#{config[:config]}") if config[:config]
126
+ cmd
127
+ end
128
+
129
+ def ansible_extra_vars(cmd)
130
+ config[:extra_vars]&.each { |k, v| cmd.prepend("#{k}=\"#{v}\"") }
131
+ cmd
132
+ end
133
+
134
+ def ansible_tags(cmd)
135
+ cmd << "--tags \"#{config[:tags].join}\"" unless config[:tags].empty?
136
+ cmd
137
+ end
138
+
139
+ def ansible_skip_tags(cmd)
140
+ cmd << "--skip-tags \"#{config[:skip_tags].join}\"" unless config[:skip_tags].empty?
141
+ cmd
142
+ end
143
+
144
+ def ansible_use_private_key(cmd)
145
+ if config[:private_key]
146
+ cmd << "--private-key #{config[:private_key]}"
147
+ else
148
+ state = instance.transport.instance_variable_get(:@connection_options)
149
+ cmd << "--private-key #{state[:keys][0]}"
150
+ end
151
+ end
152
+
153
+ def ansible_use_vault_password_file(cmd)
154
+ cmd << "--vault-password-file #{config[:vault_password_file]}" if config[:vault_password_file]
155
+ end
156
+
157
+ def ansible_verbosity(cmd)
158
+ cmd << "-#{'v' * config[:verbosity]}" if config[:verbosity] >= 1
159
+ end
160
+
161
+ def create_sandbox
162
+ @sandbox_path = Dir.mktmpdir('kitchen-yansible-pusher')
163
+ info("Sandbox created at: #{@sandbox_path}")
164
+ rescue StandardError => e
165
+ error("Failed to create sandbox: #{e.message}")
166
+ raise
167
+ end
168
+
169
+ def cleanup_sandbox
170
+ if sandbox_path && Dir.exist?(sandbox_path)
171
+ FileUtils.remove_entry(sandbox_path)
172
+ info("Sandbox cleaned up: #{sandbox_path}")
173
+ else
174
+ info('No sandbox to clean up or sandbox already removed')
175
+ end
176
+ rescue StandardError => e
177
+ error("Failed to clean up sandbox: #{e.message}")
178
+ raise
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Yansible
5
+ module Pusher
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'kitchen/provisioner/yansible_pusher'
@@ -0,0 +1,53 @@
1
+ module Kitchen
2
+ module Provisioner
3
+ class YansiblePusher < Kitchen::Provisioner::Base
4
+ extend Kitchen::Configurable::ClassMethods
5
+
6
+ @sandbox_path: String
7
+
8
+ def self.kitchen_provisioner_api_version: () -> Integer
9
+ def self.plugin_version: () -> String
10
+
11
+ def install_command: () -> nil
12
+ def init_command: () -> nil
13
+ def prepare_command: () -> nil
14
+ def run_command: () -> void
15
+
16
+ private
17
+
18
+ def run_ansible: () -> void
19
+ def create_inventory: () -> String
20
+ def build_inventory: () -> Hash[String, Hash[String, Hash[String, Hash[String, String | Integer]]]]
21
+ def build_host_config: (Hash[Symbol, String | Integer]) -> Hash[String, String | Integer]
22
+ def write_inventory_file: (Hash[String, Hash[String, Hash[String, Hash[String, String | Integer]]]]) -> String
23
+ def build_ansible_command: () -> String
24
+ def ansible_options: () -> Array[Symbol]
25
+ def ansible_config: (Array[String]) -> Array[String]
26
+ def ansible_extra_vars: (Array[String]) -> Array[String]
27
+ def ansible_tags: (Array[String]) -> Array[String]
28
+ def ansible_skip_tags: (Array[String]) -> Array[String]
29
+ def ansible_use_private_key: (Array[String]) -> Array[String]
30
+ def ansible_use_vault_password_file: (Array[String]) -> Array[String]
31
+ def ansible_verbosity: (Array[String]) -> Array[String]
32
+ def create_sandbox: () -> void
33
+ def cleanup_sandbox: () -> void
34
+ end
35
+ end
36
+
37
+ module Yansible
38
+ module Pusher
39
+ VERSION: String
40
+ end
41
+ end
42
+
43
+ class Instance
44
+ def name: () -> String
45
+ def transport: () -> untyped
46
+ end
47
+
48
+ module Configurable
49
+ module ClassMethods
50
+ def default_config: (Symbol, untyped) -> void
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitchen-yansible-pusher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jose M. Tobar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A modern & minimalistic Ansible provisioner for Test Kitchen.
14
+ email:
15
+ - ''
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/kitchen-yansible-pusher.rb
26
+ - lib/kitchen/provisioner/yansible_pusher.rb
27
+ - lib/kitchen/yansible/pusher/version.rb
28
+ - sig/kitchen/yansible/pusher.rbs
29
+ homepage: https://github.com/jmtx1020/kitchen-yansible-pusher
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ allowed_push_host: https://rubygems.org
34
+ homepage_uri: https://github.com/jmtx1020/kitchen-yansible-pusher
35
+ source_code_uri: https://github.com/jmtx1020/kitchen-yansible-pusher/tree/main
36
+ changelog_uri: https://github.com/jmtx1020/kitchen-yansible-pusher/blob/main/CHANGELOG.md
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.5.11
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A modern & minimalistic Ansible provisioner for Test Kitchen.
56
+ test_files: []