Module: Kitchen::Yansible::Tools::Files

Included in:
Provisioner::Yansible
Defined in:
lib/kitchen-yansible/tools/files.rb

Constant Summary collapse

ANSIBLE_INVENTORY =
"inventory.yml"

Instance Method Summary collapse

Instance Method Details

#copy_dirs(src, dst, reject: '.git') ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/kitchen-yansible/tools/files.rb', line 152

def copy_dirs(src, dst, reject: '.git')
  expand_path=File.expand_path(src)
  if File.exist?(expand_path)
    debug("Copy '#{src}' to '#{dst}'.")
    debug("'#{src}' expanded to '#{expand_path}'")
    Dir.glob("#{expand_path}/**/{*,.*}").reject{|f| f[reject]}.each do |file|
      target = dst + file.sub(expand_path, '')
      if File.file?(file)
        FileUtils.copy_entry(file, target, remove_destination: true)
      else
        FileUtils.mkdir_p(target) unless File.exist?(target)
      end
    end
  else
    debug("Path '#{src}' doesn't exists. Omitting copy operation.")
  end
end

#copy_dirs_to_sandbox(src, dst: src, reject: '.git') ⇒ Object



170
171
172
173
174
175
# File 'lib/kitchen-yansible/tools/files.rb', line 170

def copy_dirs_to_sandbox(src, dst: src, reject: '.git')
  dest = generate_sandbox_path(dst)
  debug("'#{src}' => '#{dest}', reject => '#{reject}'.")

  copy_dirs(src, dest, reject: reject)
end

#copy_files(src, dst, overwrite: true) ⇒ Object



146
147
148
149
150
# File 'lib/kitchen-yansible/tools/files.rb', line 146

def copy_files(src, dst, overwrite: true)
  debug("Copy '#{src}' to '#{dst}'")

  FileUtils.copy_entry(src, dst, remove_destination=overwrite)
end

#dependencies_tmp_dirObject



79
80
81
82
83
84
85
# File 'lib/kitchen-yansible/tools/files.rb', line 79

def dependencies_tmp_dir
  if !@dependencies_tmp_dir && !instance.nil?
    @dependencies_tmp_dir = File.join(instance_tmp_dir, 'dependencies')
  end
  Dir.mkdir(@dependencies_tmp_dir) unless File.exist?(@dependencies_tmp_dir)
  @dependencies_tmp_dir
end

#executor_tmp_dirObject



63
64
65
66
67
68
69
# File 'lib/kitchen-yansible/tools/files.rb', line 63

def executor_tmp_dir
  if !@executor_tmp_dir && !instance.nil?
    @executor_tmp_dir = File.join(config[:kitchen_root], %w[ .kitchen yansible ])
  end
  Dir.mkdir(@executor_tmp_dir) unless File.exist?(@executor_tmp_dir)
  @executor_tmp_dir
end

#generate_inventory(inventory_file, remote: false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kitchen-yansible/tools/files.rb', line 94

def generate_inventory(inventory_file, remote: false)
  connection = @instance.transport.instance_variable_get(:@connection_options)
  transport_conf = @instance.transport.diagnose
  host_conn_vars = {}

  debug("===> Connection options")
  debug(connection.to_s)
  debug("===> Transport options")
  debug(transport_conf.to_s)
  if remote
    debug("Generating inventory stub for execution on remote target")
    host_conn_vars['ansible_connection'] = 'local'
    host_conn_vars['ansible_host'] = 'localhost'
  else
    debug("Generating inventory for execution on local host with remote targets")
    host_conn_vars['ansible_connection'] = transport_conf[:name] if transport_conf[:name]
    host_conn_vars['ansible_password'] = connection[:password] if connection[:password]

    case transport_conf[:name]
    when 'winrm'
      host_conn_vars['ansible_host'] = URI.parse(connection[:endpoint]).hostname
      host_conn_vars['ansible_user'] = connection[:user] if connection[:user]
      host_conn_vars['ansible_winrm_transport'] = @config[:ansible_winrm_auth_transport] if @config[:ansible_winrm_auth_transport]
      host_conn_vars['ansible_winrm_scheme'] = transport_conf[:winrm_transport] == :ssl ? 'https' : 'http'
      host_conn_vars['ansible_winrm_server_cert_validation'] = @config[:ansible_winrm_cert_validation] if @config[:ansible_winrm_cert_validation]
    when 'ssh'
      host_conn_vars['ansible_host'] = connection[:hostname]
      host_conn_vars['ansible_user'] = connection[:username] if connection[:username]
      host_conn_vars['ansible_port'] = connection[:port] if connection[:port]
      host_conn_vars['ansible_ssh_retries'] = connection[:connection_retries] if connection[:connection_retries]
      host_conn_vars['ansible_private_key_file'] = connection[:keys].first if connection[:keys]
      host_conn_vars['ansible_host_key_checking'] = @config[:ansible_host_key_checking] if @config[:ansible_host_key_checking]
    else
      message = unindent(<<-MSG)
  
        ===============================================================================
         Unsupported transport - #{transport_conf[:name]}
         SSH and WinRM transports are allowed.
        ===============================================================================
      MSG
      raise UserError, message
    end
  end

  # noinspection RubyStringKeysInHashInspection
  inv = { 'all' => { 'hosts' => { @instance.name => host_conn_vars } } }

  File.open(inventory_file, 'w') do |file|
    file.write inv.to_yaml
  end
end

#generate_sandbox_path(directory) ⇒ Object



57
58
59
60
61
# File 'lib/kitchen-yansible/tools/files.rb', line 57

def generate_sandbox_path(directory)
  path = File.join(sandbox_path, directory)
  Dir.mkdir(path) unless File.exist?(path)
  path
end

#instance_tmp_dirObject



71
72
73
74
75
76
77
# File 'lib/kitchen-yansible/tools/files.rb', line 71

def instance_tmp_dir
  if !@instance_tmp_dir && !instance.nil?
    @instance_tmp_dir = File.join(executor_tmp_dir, @instance.name)
  end
  Dir.mkdir(@instance_tmp_dir) unless File.exist?(@instance_tmp_dir)
  @instance_tmp_dir
end

#inventory_fileObject



27
28
29
# File 'lib/kitchen-yansible/tools/files.rb', line 27

def inventory_file
  File.join(instance_tmp_dir, ANSIBLE_INVENTORY)
end

#prepare_ansible_configObject



39
40
41
42
43
# File 'lib/kitchen-yansible/tools/files.rb', line 39

def prepare_ansible_config
  if @config[:ansible_config]
    copy_files(@config[:ansible_config], File.join(sandbox_path, @config[:ansible_config]))
  end
end

#prepare_inventory_fileObject



51
52
53
54
55
# File 'lib/kitchen-yansible/tools/files.rb', line 51

def prepare_inventory_file
  if @config[:remote_executor]
    copy_files(inventory_file, File.join(sandbox_path, ANSIBLE_INVENTORY))
  end
end

#prepare_playbook_fileObject



45
46
47
48
49
# File 'lib/kitchen-yansible/tools/files.rb', line 45

def prepare_playbook_file
  if @config[:remote_executor]
    copy_files(@config[:playbook], File.join(sandbox_path, @config[:playbook]))
  end
end

#remote_file_path(file_path, fallback: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/kitchen-yansible/tools/files.rb', line 31

def remote_file_path(file_path, fallback: nil)
  if @config[:remote_executor]
    File.join(@config[:root_path], file_path)
  else
    fallback.nil? ? file_path : fallback
  end
end

#venv_rootObject



87
88
89
90
91
92
# File 'lib/kitchen-yansible/tools/files.rb', line 87

def venv_root
  if !@venv_root && !instance.nil?
    @venv_root = File.join(instance_tmp_dir, 'venv')
  end
  @venv_root
end