diff --git a/lib/specinfra/backend.rb b/lib/specinfra/backend.rb index bbe976c6..752062b0 100644 --- a/lib/specinfra/backend.rb +++ b/lib/specinfra/backend.rb @@ -6,6 +6,7 @@ require 'specinfra/backend/cmd' require 'specinfra/backend/docker' require 'specinfra/backend/lxc' +require 'specinfra/backend/lxd' require 'specinfra/backend/winrm' require 'specinfra/backend/shell_script' require 'specinfra/backend/dockerfile' diff --git a/lib/specinfra/backend/lxd.rb b/lib/specinfra/backend/lxd.rb new file mode 100644 index 00000000..a89d31d0 --- /dev/null +++ b/lib/specinfra/backend/lxd.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'singleton' +require 'fileutils' +require 'shellwords' +require 'sfl' if Specinfra.ruby_is_older_than?(1, 9, 0) + +module Specinfra + module Backend + # LXD transport + class Lxd < Exec + def initialize(config = {}) + super + + raise 'Please specify lxd_instance' unless (@instance = get_config(:lxd_instance)) + raise 'Please specify lxd_remote' unless (@remote = get_config(:lxd_remote)) + + @remote_instance = [@remote, @instance].compact.join(':') + end + + class << self + protected + + def run_command(cmd, opts = {}) + cmd = build_command(cmd) + run_pre_command(opts) + stdout, stderr, exit_status = with_env do + spawn_command(cmd) + end + + if @example + @example.metadata[:command] = cmd + @example.metadata[:stdout] = stdout + end + + CommandResult.new :stdout => stdout, :stderr => stderr, :exit_status => exit_status + end + + def build_command(cmd) + cmd = super(cmd) + "lxc exec #{@remote_instance} -- #{cmd}" + end + + def send_file(source, destination) + flags = %w[--create-dirs] + if File.directory?(source) + flags << '--recursive' + destination = Pathname.new(destination).dirname.to_s + end + cmd = %W[lxc file push #{source} #{@remote_instance}#{destination}] + flags + spawn_command(cmd.join(' ')) + end + + private + + def run_pre_command(_opts) + return unless get_config(:pre_command) + + cmd = build_command(get_config(:pre_command)) + spawn_command(cmd) + end + end + end + end +end diff --git a/lib/specinfra/configuration.rb b/lib/specinfra/configuration.rb index 3e61eb70..0dfc09ef 100644 --- a/lib/specinfra/configuration.rb +++ b/lib/specinfra/configuration.rb @@ -20,6 +20,8 @@ class << self :docker_image, :docker_url, :lxc, + :lxd_remote, + :lxd_instance, :request_pty, :ssh_options, :ssh_without_env,