Class: Nginxtra::Actions::Install

Inherits:
Object
  • Object
show all
Includes:
Nginxtra::Action
Defined in:
lib/nginxtra/actions/install.rb

Overview

The Nginxtra::Actions::Install class encapsulates installing nginxtra so that nginx can be started and stopped automatically when the server is started or stopped.

Instance Method Summary collapse

Methods included from Nginxtra::Action

#daemon, #initialize

Instance Method Details

#check_if_nginx_is_installedObject

Look for nginx installation and fail if it exists (unless –ignore-nginx-check is passed).



19
20
21
22
23
24
25
26
27
28
# File 'lib/nginxtra/actions/install.rb', line 19

def check_if_nginx_is_installed
  return unless File.exist?("/etc/init.d/nginx")

  if @thor.options["ignore-nginx-check"]
    @thor.say @thor.set_color("Detected nginx install, but ignoring!", :red, true)
    return
  end

  raise Nginxtra::Error::NginxDetected
end

#create_etc_scriptObject

Create a script in the base directory which be symlinked to /etc/init.d/nginxtra and then used to start and stop nginxtra via update-rc.d. rubocop:disable Metrics/AbcSize, Metrics/LineLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nginxtra/actions/install.rb', line 34

def create_etc_script
  filename = "etc.init.d.nginxtra"
  workingdir = File.expand_path "."

  @thor.inside Nginxtra::Config.base_dir do
    @thor.create_file filename, %(#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginxtra
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts nginxtra, which is a wrapper around the nginx web server
# Description:       starts nginxtra which starts nginx using start-stop-daemon
### END INIT INFO

export GEM_HOME="#{ENV["GEM_HOME"]}"
export GEM_PATH="#{ENV["GEM_PATH"]}"
#{Nginxtra::Config.ruby_path} "#{File.join Nginxtra::Config.gem_dir, "bin/nginxtra"}" "$1" --basedir="#{Nginxtra::Config.base_dir}" --config="#{Nginxtra::Config.loaded_config_path}" --workingdir="#{workingdir}" --non-interactive
), force: true
    @thor.chmod filename, 0755
  end

  run! %(#{sudo true}rm /etc/init.d/nginxtra) if File.exist? "/etc/init.d/nginxtra"
  run! %(#{sudo true}ln -s "#{File.join Nginxtra::Config.base_dir, filename}" /etc/init.d/nginxtra)
  run! %(#{sudo true}update-rc.d nginxtra defaults)
end

#installObject

Run the installation of nginxtra.



10
11
12
13
14
15
# File 'lib/nginxtra/actions/install.rb', line 10

def install
  return up_to_date unless should_install?
  check_if_nginx_is_installed
  create_etc_script
  update_last_install
end

#should_install?Boolean

Determine if the install should proceed. This will be true if the force option was used, or if this version of nginxtra differs from the last version installed.



80
81
82
83
# File 'lib/nginxtra/actions/install.rb', line 80

def should_install?
  return true if force?
  Nginxtra::Status[:last_install_version] != Nginxtra::Config.version
end

#up_to_dateObject

Notify the user that installation should be up to date.



65
66
67
# File 'lib/nginxtra/actions/install.rb', line 65

def up_to_date
  @thor.say "nginxtra installation is up to date"
end

#update_last_installObject

Mark the last installed version and last installed time (the former being used to determine if nginxtra has been installed yet).



72
73
74
75
# File 'lib/nginxtra/actions/install.rb', line 72

def update_last_install
  Nginxtra::Status[:last_install_version] = Nginxtra::Config.version
  Nginxtra::Status[:last_install_time] = Time.now
end