Module: NginxStage::GeneratorHelpers

Included in:
Generator
Defined in:
lib/nginx_stage/generator_helpers.rb

Overview

Module that adds common options to generators.

Instance Method Summary collapse

Instance Method Details

#add_skip_nginx_supportvoid

This method returns an undefined value.

Add support for accepting SKIP_NGINX as an option



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nginx_stage/generator_helpers.rb', line 40

def add_skip_nginx_support
  # @!method skip_nginx
  #   Whether we skip calling the NGINX process
  #   @return [Boolean] if true, skip calling the nginx binary
  add_option :skip_nginx do
    {
      opt_args: ["-N", "--[no-]skip-nginx", "# Skip execution of the per-user nginx process", "# Default: false"],
      default: false
    }
  end
end

#add_sub_uri_supportvoid

This method returns an undefined value.

Add support for accepting SUB_URI as an option



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nginx_stage/generator_helpers.rb', line 54

def add_sub_uri_support
  # @!method sub_uri
  #   The sub-uri that distinguishes the per-user NGINX process
  #   @example An app is requested through '/pun/usr/user/appname/...'
  #     sub_uri #=> "/pun"
  #   @return [String] the sub-uri for nginx
  add_option :sub_uri do
    {
      opt_args: ["-i", "--sub-uri=SUB_URI", "# The SUB_URI that requests the per-user nginx", "# Default: ''"],
      default: '',
      before_init: -> (sub_uri) do
        raise InvalidSubUri, "invalid sub-uri syntax: #{sub_uri}" if sub_uri =~ /[^-\w\/]/
        sub_uri
      end
    }
  end
end

#add_user_supportvoid

This method returns an undefined value.

Add support for accepting USER as an option



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nginx_stage/generator_helpers.rb', line 6

def add_user_support
  # @!method user
  #   The user that the per-user NGINX will run as
  #   @return [User] the user of the nginx process
  #   @raise [MissingOption] if user isn't supplied
  self.add_option :user do
    {
      opt_args: ["-u", "--user=USER", "# The USER of the per-user nginx process"],
      required: true,
      before_init: -> (user) do
        raise InvalidUser, "invalid user name syntax: #{user}" unless user =~ NginxStage.user_regex
        user ? User.new(user) : nil
      end
    }
  end

  # Validate that the user isn't a special user (i.e., `root`)
  self.add_hook :validate_user_not_special do
    min_uid = NginxStage.min_uid
    if user.uid < min_uid
      raise InvalidUser, "user is special: #{user} (#{user.uid} < #{min_uid})"
    end
  end

  # Validate that the user's home directory exists
  self.add_hook :validate_user_has_home_dir do
    unless Dir.exist?(user.dir)
      raise InvalidUser, "home directory '#{user.dir}' does not exist for user: #{user}"
    end
  end
end