Class: Cyclid::API::Plugins::Command

Inherits:
Action
  • Object
show all
Defined in:
app/cyclid/plugins/action/command.rb

Overview

Command plugin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Action

human_name, #prepare

Methods inherited from Base

author, config?, config_schema, default_config, get_config, homepage, human_name, license, register_plugin, set_config, update_config, version

Constructor Details

#initialize(args = {}) ⇒ Command

Returns a new instance of Command.



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

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

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

  # The command & arguments can either be passed seperately, with the
  # args as an array, or as a single string which we then split into
  # a command & array of args.
  if args.include? :args
    @cmd = args[:cmd]
    @args = args[:args]
  else
    cmd_args = args[:cmd].split
    @cmd = cmd_args.shift
    @args = cmd_args
  end

  Cyclid.logger.debug "cmd: '#{@cmd}' args: #{@args}"

  @env = args[:env]
  @path = args[:path]
  @sudo = args[:sudo]
end

Class Method Details

.metadataObject

Plugin metadata



86
87
88
89
90
91
# File 'app/cyclid/plugins/action/command.rb', line 86

def self.
  super.merge!(version: Cyclid::Api::VERSION,
               license: 'Apache-2.0',
               author: 'Liqwyd Ltd.',
               homepage: 'http://docs.cyclid.io')
end

Instance Method Details

#perform(log) ⇒ Object

Note that we don’t need to explicitly use the log for transport related tasks as the transport will take of writing any data from the commands into the log. The log is still passed in to perform() so that plugins can write their own data to it, as we do here by writing out the (optional) path & command that is being run.



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/command.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)

    # Log the command being run (and the working directory, if one is
    # set)
    cmd_args = "#{@cmd} #{@args.join(' ')}"
    log.write(@path.nil? ? "$ #{cmd_args}\n" : "$ #{@path} : #{cmd_args}\n")

    # Interpolate any data from the job context
    cmd_args = cmd_args ** @ctx

    # Interpolate the path if one is set
    path = @path
    path = path ** @ctx unless path.nil?

    # Run the command
    success = @transport.exec(cmd_args, path: path, sudo: @sudo)
  rescue KeyError => ex
    # Interpolation failed
    log.write "#{ex.message}\n"
    success = false
  end

  [success, @transport.exit_code]
end