Class: Cyclid::API::Plugins::Script

Inherits:
Action show all
Defined in:
app/cyclid/plugins/action/script.rb

Overview

Script plugin

Instance Method Summary collapse

Methods inherited from Action

human_name, #prepare

Methods inherited from Base

config?, config_schema, default_config, get_config, human_name, register_plugin, set_config, update_config

Constructor Details

#initialize(args = {}) ⇒ Script

Returns a new instance of Script.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/cyclid/plugins/action/script.rb', line 26

def initialize(args = {})
  args.symbolize_keys!

  # At a bare minimum there has to be a script to execute.
  raise 'a command action requires a script' unless args.include? :script

  # Scripts can either be a single string, or an array of strings
  # which we will join back together
  @script = if args[:script].is_a? String
              args[:script]
            elsif args[:script].is_a? Array
              args[:script].join("\n")
            end

  # If no explicit path was given, create a temporary filename.
  # XXX This assumes the remote system has a /tmp, that it's writable
  # and not mounted NOEXEC, but there's no easy way to do this?
  @path = if args.include? :path
            args[:path]
          else
            file = "cyclid_#{SecureRandom.hex(16)}"
            File.join('/', 'tmp', file)
          end

  @env = args[:env] if args.include? :env

  Cyclid.logger.debug "script: '#{@script}' path: #{@path}"
end

Instance Method Details

#perform(log) ⇒ Object

Run the script action



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/cyclid/plugins/action/script.rb', line 56

def perform(log)
  begin
    # Export the environment data to the build host, if necesary
    env = @env % @ctx if @env
    @transport.export_env(env)

    # Add context data
    script = @script ** @ctx
    path = @path ** @ctx

    # Create an IO object containing the script and upload it to the
    # build host
    log.write("Uploading script to #{path}\n")

    io = StringIO.new(script)
    @transport.upload(io, path)

    # Execute the script
    log.write("Running script from #{path}...\n")
    success = @transport.exec("chmod +x #{path} && #{path}")
  rescue KeyError => ex
    # Interpolation failed
    log.write "#{ex.message}\n"
    success = false
  end

  [success, @transport.exit_code]
end