Module: Puppet::Util::Webapp

Extended by:
Webapp
Included in:
Webapp
Defined in:
lib/puppet/util/webapp.rb

Defined Under Namespace

Classes: WebappError

Constant Summary collapse

NAME_PATTERN =
'(\S+)::(\/?\S*)'
PATH_PATTERN =
'\S*\/(\S+)\/htdocs(-secure)?(\/\S*)?'
APP_PATTERN =
'(\S+)\s(\S+)'
NAME_REGEX =
Regexp.new "^#{NAME_PATTERN}$"
PATH_REGEX =
Regexp.new "^#{PATH_PATTERN}$"
APP_REGEX =
Regexp.new "^#{APP_PATTERN}$"

Instance Method Summary collapse

Instance Method Details

#build_opts(hash, *args) ⇒ Array

Build webapp-config options from webapp properties

Parameters:

  • Properties (Hash)

    of a webapp

Returns:

  • (Array)

    Options to pass to webapp-config



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/util/webapp.rb', line 60

def build_opts(hash, *args)
  opts = args
  hash.each do |key, value|
    case key
    when :secure, :soft
      value == :yes && opts << '--%s' % key
    when :appname, :appversion
    when :name
    else
      opts << '--%s' % key << value
    end
  end
  opts << hash[:appname]
  opts << hash[:appversion]
  opts.compact
end

#fix_dir(dir) ⇒ String

Fix empty directory strings

Parameters:

  • dir (String)

    A directory string

Returns:

  • (String)

    Fixed directory string



51
52
53
# File 'lib/puppet/util/webapp.rb', line 51

def fix_dir(dir)
  (dir.nil? || dir.empty?) && '/' || dir
end

#format_webapp(hash) ⇒ String

Format webapp properties to a webapp name

Parameters:

  • Properties (Hash)

    of a webapp

Returns:

  • (String)

    The webapp name



147
148
149
# File 'lib/puppet/util/webapp.rb', line 147

def format_webapp(hash)
  '%s::%s' % [hash[:host], hash[:dir]]
end

#parse_app(app) ⇒ Hash

Parse an app into appname and appversion

Parameters:

  • An (String)

    app string

Returns:

  • (Hash)

    Parsed appname and appversion



117
118
119
120
121
122
123
124
125
126
# File 'lib/puppet/util/webapp.rb', line 117

def parse_app(app)
  if (match = app.match(APP_REGEX))
    {
      :appname    => match[1],
      :appversion => match[2],
    }
  else
    raise WebappError, "#{app} is not a valid app string"
  end
end

#parse_name(name) ⇒ Hash

Parse a webapp name into host and dir

Parameters:

  • A (String)

    webapp name

Returns:

  • (Hash)

    Parsed host and dir values



82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/util/webapp.rb', line 82

def parse_name(name)
  if (match = name.match(NAME_REGEX))
    {
      :host => match[1],
      :dir => fix_dir(match[2])
    }
  else
    raise WebappError, "#{name} is not a valid webapp name"
  end
end

#parse_path(path) ⇒ Hash

Parse a webapp path into webapp properties

Parameters:

  • A (String)

    webapp installed path

Returns:

  • (Hash)

    Parsed webapp properties



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/util/webapp.rb', line 98

def parse_path(path)
  if (match = path.match(PATH_REGEX))
    host, secure, dir = match[1], match[2], fix_dir(match[3])
    {
      :name   => '%s::%s' % [host, dir],
      :host   => host,
      :dir    => dir,
      :secure => secure.nil? && :no || :yes
    }
  else
    raise WebappError, "#{path} is not a valid webapp path"
  end
end

#parse_resource(resource) ⇒ Hash

Parse a webapp resource into webapp properties

Parameters:

  • A (Hash)

    webapp resource

Returns:

  • (Hash)

    Parsed webapp properties



133
134
135
136
137
138
139
140
# File 'lib/puppet/util/webapp.rb', line 133

def parse_resource(resource)
  webapp = parse_name(resource[:name])
  keys = [:appname, :appversion, :server, :user, :group, :soft, :secure]
  keys.each do |key|
    resource[key] && webapp[key] = resource[key]
  end
  webapp
end

#valid_app?(app) ⇒ TrueClass, FalseClass

Determine if a string is a valid app

Parameters:

  • name (String)

    The string to validate as an app

Returns:

  • (TrueClass)
  • (FalseClass)


42
43
44
# File 'lib/puppet/util/webapp.rb', line 42

def valid_app?(app)
  !!(app =~ APP_REGEX)
end

#valid_name?(name) ⇒ TrueClass, FalseClass

Determine if a string is a valid webapp name

Parameters:

  • name (String)

    The string to validate as a webapp name

Returns:

  • (TrueClass)
  • (FalseClass)


22
23
24
# File 'lib/puppet/util/webapp.rb', line 22

def valid_name?(name)
  !!(name =~ NAME_REGEX)
end

#valid_path?(path) ⇒ TrueClass, FalseClass

Determine if a string is a valid webapp path

Parameters:

  • name (String)

    The string to validate as a webapp path

Returns:

  • (TrueClass)
  • (FalseClass)


32
33
34
# File 'lib/puppet/util/webapp.rb', line 32

def valid_path?(path)
  !!(path =~ PATH_REGEX)
end