Module: Kitchen::Util

Defined in:
lib/kitchen/util.rb

Overview

Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.

Author:

Class Method Summary collapse

Class Method Details

.duration(total) ⇒ String

Returns a formatted string representing a duration in seconds.

Parameters:

  • total (Integer)

    the total number of seconds

Returns:

  • (String)

    a formatted string of the form (XmYY.00s)



95
96
97
98
99
100
# File 'lib/kitchen/util.rb', line 95

def self.duration(total)
  total = 0 if total.nil?
  minutes = (total / 60).to_i
  seconds = (total - (minutes * 60))
  "(%dm%.2fs)" % [minutes, seconds]
end

.from_logger_level(const) ⇒ Symbol

Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.

Parameters:

  • const (Integer)

    Logger::Severity constant value for a logging level (Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL)

Returns:

  • (Symbol)

    symbol representation of the logging level



47
48
49
50
51
52
53
54
55
# File 'lib/kitchen/util.rb', line 47

def self.from_logger_level(const)
  case const
  when Logger::DEBUG then :debug
  when Logger::INFO then :info
  when Logger::WARN then :warn
  when Logger::ERROR then :error
  else :fatal
  end
end

.shell_helpersString

Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions. This function is usually called inline in a string that will be executed remotely on a test instance.

Returns:

  • (String)

    a string representation of useful helper functions



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/kitchen/util.rb', line 107

def self.shell_helpers
  # use Bourne (/bin/sh) as Bash does not exist on all Unix flavors
  <<-HELPERS
    # Check whether a command exists - returns 0 if it does, 1 if it does not
    exists() {
      if command -v $1 >/dev/null 2>&1
      then
        return 0
      else
        return 1
      fi
    }

    # do_wget URL FILENAME
    do_wget() {
      echo "trying wget..."
      wget -O "$2" "$1" 2>/tmp/stderr
      # check for bad return status
      test $? -ne 0 && return 1
      # check for 404 or empty file
      grep "ERROR 404" /tmp/stderr 2>&1 >/dev/null
      if test $? -eq 0 || test ! -s "$2"; then
        return 1
      fi
      return 0
    }

    # do_curl URL FILENAME
    do_curl() {
      echo "trying curl..."
      curl -L "$1" > "$2"
      # check for bad return status
      [ $? -ne 0 ] && return 1
      # check for bad output or empty file
      grep "The specified key does not exist." "$2" 2>&1 >/dev/null
      if test $? -eq 0 || test ! -s "$2"; then
        return 1
      fi
      return 0
    }

    # do_fetch URL FILENAME
    do_fetch() {
      echo "trying fetch..."
      fetch -o "$2" "$1" 2>/tmp/stderr
      # check for bad return status
      test $? -ne 0 && return 1
      return 0
    }

    # do_perl URL FILENAME
    do_perl() {
      echo "trying perl..."
      perl -e "use LWP::Simple; getprint($ARGV[0]);" "$1" > "$2"
      # check for bad return status
      test $? -ne 0 && return 1
      # check for bad output or empty file
      # grep "The specified key does not exist." "$2" 2>&1 >/dev/null
      # if test $? -eq 0 || test ! -s "$2"; then
      #   unable_to_retrieve_package
      # fi
      return 0
    }

    # do_python URL FILENAME
    do_python() {
      echo "trying python..."
      python -c "import sys,urllib2 ; sys.stdout.write(urllib2.urlopen(sys.argv[1]).read())" "$1" > "$2"
      # check for bad return status
      test $? -ne 0 && return 1
      # check for bad output or empty file
      #grep "The specified key does not exist." "$2" 2>&1 >/dev/null
      #if test $? -eq 0 || test ! -s "$2"; then
      #  unable_to_retrieve_package
      #fi
      return 0
    }

    # do_download URL FILENAME
    do_download() {
      PATH=/opt/local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
      export PATH

      echo "downloading $1"
      echo "  to file $2"

      # we try all of these until we get success.
      # perl, in particular may be present but LWP::Simple may not be installed

      if exists wget; then
        do_wget $1 $2 && return 0
      fi

      if exists curl; then
        do_curl $1 $2 && return 0
      fi

      if exists fetch; then
        do_fetch $1 $2 && return 0
      fi

      if exists perl; then
        do_perl $1 $2 && return 0
      fi

      if exists python; then
        do_python $1 $2 && return 0
      fi

      echo ">>>>>> wget, curl, fetch, perl or python not found on this instance."
      return 16
    }
  HELPERS
end

.stringified_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to strings. All keys within a Hash are coerced by calling #to_s and hashes with arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as strings



81
82
83
84
85
86
87
88
89
# File 'lib/kitchen/util.rb', line 81

def self.stringified_hash(obj)
  if obj.is_a?(Hash)
    obj.inject({}) { |h, (k, v)| h[k.to_s] = stringified_hash(v) ; h }
  elsif obj.is_a?(Array)
    obj.inject([]) { |a, v| a << stringified_hash(v) ; a }
  else
    obj
  end
end

.symbolized_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to symbols. All keys within a Hash are coerced by calling #to_sym and hashes within arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as symbols



64
65
66
67
68
69
70
71
72
# File 'lib/kitchen/util.rb', line 64

def self.symbolized_hash(obj)
  if obj.is_a?(Hash)
    obj.inject({}) { |h, (k, v)| h[k.to_sym] = symbolized_hash(v) ; h }
  elsif obj.is_a?(Array)
    obj.inject([]) { |a, v| a << symbolized_hash(v) ; a }
  else
    obj
  end
end

.to_logger_level(symbol) ⇒ Integer

Returns the standard library Logger level constants for a given symbol representation.

Parameters:

  • symbol (Symbol)

    symbol representation of a logger level (:debug, :info, :warn, :error, :fatal)

Returns:

  • (Integer)

    Logger::Severity constant value or nil if input is not valid



34
35
36
37
38
# File 'lib/kitchen/util.rb', line 34

def self.to_logger_level(symbol)
  return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol)

  Logger.const_get(symbol.to_s.upcase)
end