Class: Jamal::CLI

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

Class Method Summary collapse

Class Method Details

.deploy(config_path: '_jamal.yml') ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/jamal.rb', line 100

def self.deploy(config_path: '_jamal.yml')
  # Load configuration
  config = YAML.load_file(config_path)
  temp_password_file = nil  # Declare variable outside begin block
  
  begin
    Net::SSH.start(config['host'], config['user'],
      password: config['password']
    ) do |ssh|
      puts "Connected to #{config['host']}..."
      
      # Check rsync daemon status
      puts "Checking rsync daemon status..."
      status = ssh.exec!("sudo systemctl status rsync")
      unless status.include?("active (running)")
        puts "Restarting rsync daemon..."
        ssh.exec!("sudo systemctl restart rsync")
      end
      
      # Create nginx configuration with support for multiple domains
      nginx_config = <<~CONFIG
        server {
          listen 80;
          server_name #{Array(config['domains']).join(' ')};
          root /var/www/#{config['name']};
          index index.html index.htm;
          
          location / {
            try_files $uri $uri/ =404;
          }
          
          error_page 400 /400.html;
          error_page 401 /401.html;
          error_page 403 /403.html;
          error_page 404 /404.html;
          error_page 405 /405.html;
          error_page 408 /408.html;
          error_page 429 /429.html;
          error_page 500 /500.html;
          error_page 502 /502.html;
          error_page 503 /503.html;
          error_page 504 /504.html;
        }
      CONFIG
      
      # Check if nginx config already exists and is the same
      existing_config = ssh.exec!("sudo cat /etc/nginx/sites-available/#{config['name']} 2>/dev/null")
      if existing_config == nginx_config
        puts "Nginx configuration unchanged, skipping update..."
      else
        # Create directory and upload nginx config
        puts "Setting up nginx configuration..."
        ssh.exec!("sudo mkdir -p /var/www/#{config['name']}")
        ssh.exec!("sudo chown -R #{config['user']}:#{config['user']} /var/www/#{config['name']}")
        ssh.exec!("sudo chmod -R 755 /var/www/#{config['name']}")
        
        # Upload nginx config using SFTP
        File.write('/tmp/temp_nginx.conf', nginx_config)
        ssh.sftp.connect do |sftp|
          sftp.upload!('/tmp/temp_nginx.conf', "/tmp/#{config['name']}.conf")
          File.delete('/tmp/temp_nginx.conf')
        end
        
        # Move config to nginx sites and enable it
        ssh.exec!("sudo mv /tmp/#{config['name']}.conf /etc/nginx/sites-available/#{config['name']}")
        ssh.exec!("sudo ln -sf /etc/nginx/sites-available/#{config['name']} /etc/nginx/sites-enabled/")
        
        # Test and reload nginx
        puts "Testing nginx configuration..."
        result = ssh.exec!("sudo nginx -t")
        if result && !result.include?("error")
          ssh.exec!("sudo systemctl reload nginx")
          puts "Nginx configuration updated successfully!"
        else
          raise Error, "Invalid nginx configuration"
        end
      end

      # Replace SSH-based rsync with daemon-based rsync
      local_path = config['local_path']
      local_path = "#{local_path}/" unless local_path.end_with?('/')

      # Subdirectory of the site to sync into, e.g. 'en' for example.com/en
      remote_dir = config['remote_dir'].to_s.strip.gsub(%r{\A/+|/+\z}, '')
      remote_path = "/var/www/#{config['name']}#{"/" + remote_dir unless remote_dir.empty?}"

      unless remote_dir.empty?
        ssh.exec!("sudo mkdir -p #{remote_path}")
        ssh.exec!("sudo chown -R #{config['user']}:#{config['user']} #{remote_path}")
        ssh.exec!("sudo chmod -R 755 #{remote_path}")
      end

      # Directories left alone on the server: never uploaded, never deleted
      excludes = Array(config['remote_exclude']).map { |dir| "--exclude=#{dir}" }
      
      # Create temporary password file for rsync
      temp_password_file = Tempfile.new('rsync_password')
      temp_password_file.write(config['rsync_password'])
      temp_password_file.close
      FileUtils.chmod(0600, temp_password_file.path)

      # Build rsync command for daemon mode with simplified output
      rsync_cmd = [
        "rsync",
        "-ahi",                   # archive mode, human-readable, itemize changes
        "--delete",              # delete extraneous files
        "--out-format='%i | %n'", # simplified output format showing changes and filename
        "--password-file=#{temp_password_file.path}",
        *excludes,
        local_path,
        "rsync://#{config['name']}@#{config['host']}/#{config['name']}/#{remote_dir.empty? ? '' : remote_dir + '/'}"
      ].join(" ")
      
      # Execute rsync with better error handling
      puts "Syncing website files..."

      IO.popen(rsync_cmd) do |io|
        while line = io.gets
          info = line.partition('|').first.strip
          filename = line.partition('|').last.strip

          if info[0] == '<'
            puts "Uploading    #{filename}"
          elsif info[0] == '.'
            if info[1] == 'd'
              puts "Acessing     #{filename}"
            else
              puts "Unchanged    #{filename}"
            end
          elsif info == "*deleting"
            puts "Deleting    #{filename}"
          elsif info == "cd+++++++++"
            puts "Creating     #{filename}"
          else
            puts line
          end
        end
      end

      exit_status = $?.exitstatus
      
      # Clean up temporary password file
      temp_password_file.unlink
      
      if exit_status != 0
        puts "Rsync failed. Checking daemon accessibility..."
        system("nc -zv #{config['host']} 873")
        raise Error, "Rsync failed with status: #{exit_status}. Please check the rsync daemon is running and port 873 is accessible."
      end
      
      puts "Sync completed!"
    end
  rescue Net::SSH::AuthenticationFailed
    raise Error, "SSH authentication failed"
  rescue StandardError => e
    raise Error, "Deployment failed: #{e.message}"
  ensure
    # Now temp_password_file will be in scope
    temp_password_file&.unlink
  end
end

.init(config_path: '_jamal.yml') ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/jamal.rb', line 306

def self.init(config_path: '_jamal.yml')
  # Create the config file
  File.write(config_path, {
    'name' => 'example',
    'host' => '1.2.3.4',
    'user' => 'root',
    'password' => 'password',
    'domains' => ['example.com', 'www.example.com'],
    'local_path' => './_site'
  }.to_yaml)
  puts "Created #{config_path} with default configuration."
end

.remove(config_path: '_jamal.yml') ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/jamal.rb', line 262

def self.remove(config_path: '_jamal.yml')
  # Load configuration
  config = YAML.load_file(config_path)
  
  begin
    Net::SSH.start(config['host'], config['user'],
      password: config['password']
    ) do |ssh|
      puts "Connected to #{config['host']}..."
      
      # Remove nginx configuration
      puts "Removing nginx configuration..."
      ssh.exec!("sudo rm -f /etc/nginx/sites-enabled/#{config['name']}")
      ssh.exec!("sudo rm -f /etc/nginx/sites-available/#{config['name']}")
      ssh.exec!("sudo systemctl reload nginx")
      
      # Remove website files
      puts "Removing website files..."
      ssh.exec!("sudo rm -rf /var/www/#{config['name']}")
      
      # Remove rsync secrets
      puts "Removing rsync secrets..."
      ssh.exec!("sudo sed -i '/#{config['name']}:.*$/d' /etc/rsyncd.secrets")
      
      # Remove rsync configuration
      puts "Removing rsync configuration..."
      ssh.exec!("sudo sed -i '/\\[#{config['name']}\\]/,/^$/d' /etc/rsyncd.conf")
      
      # Clean up empty lines in rsync config
      ssh.exec!("sudo sed -i '/^$/N;/^\\n$/D' /etc/rsyncd.conf")
      
      # Remove rsync_password from local config file
      config.delete('rsync_password')
      File.write(config_path, config.to_yaml)
      
      puts "Successfully removed all configurations for #{config['name']}!"
    end
  rescue Net::SSH::AuthenticationFailed
    raise Error, "SSH authentication failed"
  rescue StandardError => e
    raise Error, "Removal failed: #{e.message}"
  end
end

.setup(config_path: '_jamal.yml') ⇒ 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jamal.rb', line 14

def self.setup(config_path: '_jamal.yml')
  # Load configuration
  config = YAML.load_file(config_path)
  
  begin
    # Establish SSH connection using config values
    Net::SSH.start(config['host'], config['user'],
      password: config['password']
    ) do |ssh|
      puts "Connected to #{config['host']}..."
      
      # Check if nginx is installed
      result = ssh.exec!("nginx -v")
      if result && !result.include?("not found")
        puts "nginx is already installed"
      else
        puts "Installing nginx..."
        # Update package list and install nginx
        ssh.exec!("sudo apt-get update")
        ssh.exec!("sudo apt-get install -y nginx")
        
        # Verify installation
        result = ssh.exec!("nginx -v")
        if result && !result.include?("not found")
          puts "nginx installed successfully"
        else
          raise Error, "Failed to install nginx"
        end
      end

      # Set up rsync daemon configuration
      puts "Setting up rsync daemon..."
      
      # Create rsyncd configuration with consistent user permissions
      rsyncd_conf = <<~CONFIG
        uid = #{config['user']}
        gid = #{config['user']}
        use chroot = no
        max connections = 4
        pid file = /var/run/rsyncd.pid
        exclude = .git/ .gitignore

        [#{config['name']}]
            path = /var/www/#{config['name']}
            comment = Website files for #{config['name']}
            read only = no
            auth users = #{config['name']}
            secrets file = /etc/rsyncd.secrets
      CONFIG

      # Generate a random password for rsync
      special_chars = '!@#$%^&*()_+-=[]{}|;:,.<>?'.chars
      numbers = '0123456789'.chars
      letters = ('a'..'z').to_a + ('A'..'Z').to_a
      rsync_password = (letters + numbers + special_chars).shuffle[0,16].join
      
      # First, clean up any existing entries for both the site name and root user
      ssh.exec!("sudo sed -i '/^#{config['name']}:/d' /etc/rsyncd.secrets")

      # Add new entry with the website name
      ssh.exec!("echo '#{config['name']}:#{rsync_password}' | sudo tee -a /etc/rsyncd.secrets")
      ssh.exec!("sudo chmod 600 /etc/rsyncd.secrets")
      
      # Remove any existing module configuration and create new one
      ssh.exec!("sudo sed -i '/\\[#{config['name']}\\]/,/^$/d' /etc/rsyncd.conf")
      
      # Write the full rsyncd configuration
      ssh.exec!("echo '#{rsyncd_conf}' | sudo tee -a /etc/rsyncd.conf")
      
      # Update the config file with the rsync password
      config['rsync_password'] = rsync_password
      File.write(config_path, config.to_yaml)
      
      # Enable and start rsync daemon
      ssh.exec!("sudo systemctl enable rsync")
      ssh.exec!("sudo systemctl start rsync")
      
      puts "Rsync daemon configured successfully!"
    end
  rescue Net::SSH::AuthenticationFailed
    raise Error, "SSH authentication failed"
  rescue StandardError => e
    raise Error, "Setup failed: #{e.message}"
  end
end