Skip to content

Commit

Permalink
Merge pull request #750 from h0tw1r3/lxd-backend
Browse files Browse the repository at this point in the history
add lxd backend
  • Loading branch information
mizzy authored Feb 7, 2024
2 parents 8e89b44 + c3d195b commit 0118f6d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/specinfra/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
65 changes: 65 additions & 0 deletions lib/specinfra/backend/lxd.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/specinfra/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class << self
:docker_image,
:docker_url,
:lxc,
:lxd_remote,
:lxd_instance,
:request_pty,
:ssh_options,
:ssh_without_env,
Expand Down

0 comments on commit 0118f6d

Please sign in to comment.