Class: Procsd::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/procsd/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
11
12
# File 'lib/procsd/generator.rb', line 7

def initialize(config, options)
  @config = config
  @options = options
  @app_name = @config[:app]
  @target_name = "#{app_name}.target"
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



5
6
7
# File 'lib/procsd/generator.rb', line 5

def app_name
  @app_name
end

#target_nameObject (readonly)

Returns the value of attribute target_name.



5
6
7
# File 'lib/procsd/generator.rb', line 5

def target_name
  @target_name
end

Instance Method Details

#generate_nginx_conf(save: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/procsd/generator.rb', line 70

def generate_nginx_conf(save: false)
  root_path = File.join(@options["dir"], "public")
  content = generate_template("nginx", {
    port: @config[:environment]["PORT"],
    server_name: @config[:nginx]["server_name"],
    root: root_path,
    error_500: File.exist?(File.join root_path, "500.html"),
    error_404: File.exist?(File.join root_path, "404.html"),
    error_422: File.exist?(File.join root_path, "422.html")
  })

  if save
    config_path = File.join(NGINX_DIR, "sites-available", app_name)
    puts "Creating Nginx config (#{config_path})..."
    write_file!(config_path, content)
    puts "Link Nginx config file to the sites-enabled folder..."
    system "sudo", "ln", "-nfs", config_path, File.join(NGINX_DIR, "sites-enabled")
  else
    content
  end
end

#generate_sudoers(user, has_reload:, save: false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/procsd/generator.rb', line 49

def generate_sudoers(user, has_reload:, save: false)
  systemctl_path = `which systemctl`.strip
  commands = []
  %w(start stop restart).each { |cmd| commands << "#{systemctl_path} #{cmd} #{target_name}" }
  commands << "#{systemctl_path} reload-or-restart #{app_name}-\\* --all" if has_reload
  content = "#{user} ALL=NOPASSWD: #{commands.join(', ')}"

  if save
    puts "Creating sudoers rule file in the sudoers.d directory (#{SUDOERS_DIR})..."
    temp_path = "/tmp/#{app_name}"
    dest_path = "#{SUDOERS_DIR}/#{app_name}"

    File.open(temp_path, "w") { |f| f.puts content }
    system "sudo", "chown", "root:root", temp_path
    system "sudo", "chmod", "0440", temp_path
    system "sudo", "mv", temp_path, dest_path
  else
    content
  end
end

#generate_units(save: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/procsd/generator.rb', line 14

def generate_units(save: false)
  services = {}
  @config[:processes].each do |name, values|
    commands = values["commands"]
    size = values["size"]
    content = generate_template("service", @options.merge(
      "target_name" => target_name,
      "commands" => commands,
      "environment" => @config[:environment]
    ))

    services[name] = { content: content, size: size }
  end

  if save
    puts "Creating app units files in the systemd directory (#{DEFAULT_SYSTEMD_DIR})..."
    wants = []
    services.each do |service_name, values|
      values[:size].times do |i|
        unit_name = "#{app_name}-#{service_name}.#{i + 1}.service"
        wants << unit_name
        write_file!(File.join(@config[:systemd_dir], unit_name), values[:content])
      end
    end

    target_content = generate_template("target", {
      "app" => app_name,
      "wants" => wants.join(" ")
    })
    write_file!(File.join(@config[:systemd_dir], target_name), target_content)
  else
    services
  end
end