Class: Kitchen::Provisioner::CincTarget

Inherits:
CincInfra show all
Defined in:
lib/kitchen/provisioner/cinc_target.rb

Overview

Cinc Target Mode provisioner for remote execution.

Requires Cinc Client 19.0.0+ and Train-based transport.

Author:

  • Cinc Project

Defined Under Namespace

Classes: CincClientNotFound, CincVersionTooLow, RequireTrainTransport

Constant Summary collapse

MIN_VERSION_REQUIRED =
"19.0.0".freeze

Instance Method Summary collapse

Methods inherited from CincInfra

#run_command

Methods inherited from CincBase

#initialize

Constructor Details

This class inherits a constructor from Kitchen::Provisioner::CincBase

Instance Method Details

#call(state) ⇒ Object



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
# File 'lib/kitchen/provisioner/cinc_target.rb', line 100

def call(state)
  remote_connection = instance.transport.connection(state)

  config[:uploads].to_h.each do |locals, remote|
    debug("Uploading #{Array(locals).join(", ")} to #{remote}")
    remote_connection.upload(locals.to_s, remote)
  end

  # no installation
  create_sandbox
  # no prepare command

  # Stream output to logger
  require "open3"
  Open3.popen2e(run_command) do |_stdin, output, _thread|
    output.each { |line| logger << line }
  end

  info("Downloading files from #{instance.to_str}")
  config[:downloads].to_h.each do |remotes, local|
    debug("Downloading #{Array(remotes).join(", ")} to #{local}")
    remote_connection.download(remotes, local)
  end
  debug("Download complete")
rescue Kitchen::Transport::TransportFailed => ex
  raise ActionFailed, ex.message
ensure
  cleanup_sandbox
end

#check_local_cinc_clientObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kitchen/provisioner/cinc_target.rb', line 67

def check_local_cinc_client
  debug("Checking for cinc-client version")

  begin
    client_version = `cinc-client -v`.chop.split(":")[-1]
  rescue Errno::ENOENT => e
    error("Error determining Cinc Client version: #{e.exception.message}")
    raise CincClientNotFound.new("Need cinc-client installed locally")
  end

  minimum_version = Gem::Version.new(MIN_VERSION_REQUIRED)
  installed_version = Gem::Version.new(client_version)

  if installed_version < minimum_version
    error("Found Cinc Client version #{installed_version}, but require #{minimum_version} for Target Mode")
    raise CincVersionTooLow.new("Need version #{MIN_VERSION_REQUIRED} or higher")
  end

  debug("Cinc Client found and version constraints match")
end

#check_transport(connection) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/kitchen/provisioner/cinc_target.rb', line 56

def check_transport(connection)
  debug("Checking for active transport")

  unless connection.respond_to? "train_uri"
    error("Cinc Target Mode provisioner requires a Train-based transport like kitchen-transport-train")
    raise RequireTrainTransport.new("No Train transport")
  end

  debug("Kitchen transport responds to train_uri function call, as required")
end

#chef_args(client_rb_filename) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kitchen/provisioner/cinc_target.rb', line 39

def chef_args(client_rb_filename)
  # Dummy execution to initialize and test remote connection
  connection = instance.remote_exec("echo Connection established")

  check_transport(connection)
  check_local_cinc_client

  instance_name = instance.name
  credentials_file = File.join(kitchen_basepath, ".kitchen", instance_name + ".ini")
  File.write(credentials_file, connection.credentials_file)

  super.push(
    "--target #{instance_name}",
    "--credentials #{credentials_file}"
  )
end

#create_sandboxObject



92
93
94
95
96
97
98
# File 'lib/kitchen/provisioner/cinc_target.rb', line 92

def create_sandbox
  super

  # Change config.rb to point to the local sandbox path, not to /tmp/kitchen
  config[:root_path] = sandbox_path
  prepare_config_rb
end

#init_commandObject



36
# File 'lib/kitchen/provisioner/cinc_target.rb', line 36

def init_command; ""; end

#install_commandObject



35
# File 'lib/kitchen/provisioner/cinc_target.rb', line 35

def install_command; ""; end

#kitchen_basepathObject



88
89
90
# File 'lib/kitchen/provisioner/cinc_target.rb', line 88

def kitchen_basepath
  instance.driver.config[:kitchen_root]
end

#prepare_commandObject



37
# File 'lib/kitchen/provisioner/cinc_target.rb', line 37

def prepare_command; ""; end