Class: Smailr::Setup

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/smailr/setup.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_file(file, opts) ⇒ Object



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
# File 'lib/smailr/setup.rb', line 122

def self.create_file(file, opts)
    opts ||= {}

    if File.exists?(file)
        if File.writable?(File.dirname(file))
            backstamp = Time.now.strftime("pre_smailr-%F-%R")
            say "Creating backup of existing configuration: #{file}.#{backstamp}"
            FileUtils.mv file, "#{file}.#{backstamp}"
        else
            say_error "Cannot write to directory #{File.dirname(file)}."
            exit 1
        end
    end

    if File.directory?(File.dirname(file))
        if File.writable?(File.dirname(file))
            say "Installing configuration file: #{file}"

            # COPY
            if opts[:source]
                FileUtils.cp opts[:source], file
            end

            # CREATE
            if opts[:content]
                File.open(file, 'w') {|f| f.write(opts[:content]) }
            end

            # MODE
            FileUtils.chmod opts[:mode], file  if opts[:mode]

            # USER
            FileUtils.chown opts[:owner], nil, file if opts[:owner]

            # GROUP
            FileUtils.chown nil, opts[:group], file if opts[:group]
        else
            say_error "Cannot create configuration file in #{File.dirname(file)}, permission denied."
            say_error "Please repair the permissions, run with sudo or as root. Run setup again."
            exit 1
        end
    else
        say_error "Directory does not exist: #{File.dirname(file)}."
        say_error "Please check your configuration and run setup again."
        exit 1
    end
end

.setup_config(defaults_file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smailr/setup.rb', line 60

def self.setup_config(defaults_file)
    puts defaults_file
    defaults_fh = File.open(defaults_file, "r")
    defaults    = defaults_fh.read
    content     = ask_editor(defaults)

    create_file "/etc/smailr.yml",
        :content => content,
        :mode    => "0644",
        :owner   => "root",
        :group   => "root"

    Smailr.load_config
end

.setup_dovecotObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/smailr/setup.rb', line 93

def self.setup_dovecot
    source = File.expand_path("dovecot.conf", Smailr.contrib_directory)
    file   = File.expand_path("dovecot.conf", Smailr.config["dovecot_path"])

    create_file file,
        :source => source,
        :mode   => "0660",
        :owner  => "root",
        :group  => "root"

    source = File.expand_path("dovecot-sql.conf", Smailr.contrib_directory)
    file   = File.expand_path("dovecot-sql.conf", Smailr.config["dovecot_path"])

    create_file file,
        :source => source,
        :mode   => "0660",
        :owner  => "root",
        :group  => "root"
end

.setup_eximObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/smailr/setup.rb', line 75

def self.setup_exim
    source = File.expand_path("exim4.conf", Smailr.contrib_directory)

    # Debian fucks up exim's name (exim4), and i really don't want to maintain a list
    # of possible filenames nor do i want the user to configure it ATM. :-/
    if Smailr.config["exim_path"].include?("exim4")
        file   = File.expand_path("exim4.conf", Smailr.config["exim_path"])
    else
        file   = File.expand_path("exim.conf", Smailr.config["exim_path"])
    end

    create_file file,
        :source => source,
        :mode => "0660",
        :owner => Smailr.config[:exim_user],
        :group => "root"
end

.setup_mail_spoolObject



113
114
115
116
117
118
119
120
# File 'lib/smailr/setup.rb', line 113

def self.setup_mail_spool
    unless Etc.getpwnam("vmail")
        exec "useradd -r -d #{Smailr.config["mail_spool_path"]} vmail"
    end

    FileUtils.mkdir_p "#{Smailr.config["mail_spool_path"]}/users"
    FileUtils.chown "vmail", "vmail", Smailr.config["mail_spool_path"]
end

Instance Method Details

#runObject



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/smailr/setup.rb', line 7

def run

    if Process.euid != 0
        say "ERROR: YOU ARE NOT RUNNING THIS SCRIPT WITH ROOT PRIVILEGES, EXITING."
        exit
    end

    # This is still hardcoded, required too much brainfuck to deal
    # with mulitple possible configuration files locations ATM.
    if File.exists?("/etc/smailr.yml")
        say "SYSTEM UPGRADE"
        say "---------------------------------------------------------------------------"
        say "You appear to already have a copy of smailr installed. Are you sure you want"
        say "proceed with the setup routine?"
        say ""
        say "This script is going to replace exim and dovecot configuration files"
        say "from /etc ; Backups of the existing config files will be created!"
        say ""
        say "FILES TO BE WRITTEN:"
        say ""
        say " - %s" % File.expand_path("exim4.conf", Smailr.config["exim_path"])
        say " - %s" % File.expand_path("dovecot.conf", Smailr.config["dovecot_path"])
        say " - %s" % File.expand_path("dovecot-sql.conf", Smailr.config["dovecot_path"])
        say ""
        if agree "Continue? [yes/no]"
            defaults_file = "/etc/smailr.yml"
        else
            exit
        end
    else
        defaults_file = File.expand_path("../smailr.yml", Smailr.contrib_directory)
    end

    setup_config(defaults_file)

    if Smailr.config["exim_path"]
        say "Setting up exim configuration in: #{Smailr.config['exim_path']}"
        setup_exim
    end

    if Smailr.config["dovecot_path"]
        say "Setting up dovecot configuration in: #{Smailr.config['dovecot_path']}"
        setup_dovecot
    end

    if Smailr.config["mail_spool_path"]
        say "Setting up mailspool user: vmail"
        say "Setting up mailspool directory structure in: #{Smailr.config['mail_spool_path']}"
        setup_mail_spool
    end

end