Class: Jamal::CLI
- Inherits:
-
Object
- Object
- Jamal::CLI
- Defined in:
- lib/jamal.rb
Class Method Summary collapse
- .deploy(config_path: '_jamal.yml') ⇒ Object
- .init(config_path: '_jamal.yml') ⇒ Object
- .remove(config_path: '_jamal.yml') ⇒ Object
- .setup(config_path: '_jamal.yml') ⇒ Object
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 |
# 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 = " server {\n listen 80;\n server_name \#{Array(config['domains']).join(' ')};\n root /var/www/\#{config['name']};\n index index.html index.htm;\n \n location / {\n try_files $uri $uri/ =404;\n }\n \n error_page 400 /400.html;\n error_page 401 /401.html;\n error_page 403 /403.html;\n error_page 404 /404.html;\n error_page 405 /405.html;\n error_page 408 /408.html;\n error_page 429 /429.html;\n error_page 500 /500.html;\n error_page 502 /502.html;\n error_page 503 /503.html;\n error_page 504 /504.html;\n }\n CONFIG\n \n # Check if nginx config already exists and is the same\n existing_config = ssh.exec!(\"sudo cat /etc/nginx/sites-available/\#{config['name']} 2>/dev/null\")\n if existing_config == nginx_config\n puts \"Nginx configuration unchanged, skipping update...\"\n else\n # Create directory and upload nginx config\n puts \"Setting up nginx configuration...\"\n ssh.exec!(\"sudo mkdir -p /var/www/\#{config['name']}\")\n ssh.exec!(\"sudo chown -R \#{config['user']}:\#{config['user']} /var/www/\#{config['name']}\")\n ssh.exec!(\"sudo chmod -R 755 /var/www/\#{config['name']}\")\n \n # Upload nginx config using SFTP\n File.write('/tmp/temp_nginx.conf', nginx_config)\n ssh.sftp.connect do |sftp|\n sftp.upload!('/tmp/temp_nginx.conf', \"/tmp/\#{config['name']}.conf\")\n File.delete('/tmp/temp_nginx.conf')\n end\n \n # Move config to nginx sites and enable it\n ssh.exec!(\"sudo mv /tmp/\#{config['name']}.conf /etc/nginx/sites-available/\#{config['name']}\")\n ssh.exec!(\"sudo ln -sf /etc/nginx/sites-available/\#{config['name']} /etc/nginx/sites-enabled/\")\n \n # Test and reload nginx\n puts \"Testing nginx configuration...\"\n result = ssh.exec!(\"sudo nginx -t\")\n if result && !result.include?(\"error\")\n ssh.exec!(\"sudo systemctl reload nginx\")\n puts \"Nginx configuration updated successfully!\"\n else\n raise Error, \"Invalid nginx configuration\"\n end\n end\n\n # Replace SSH-based rsync with daemon-based rsync\n local_path = config['local_path']\n local_path = \"\#{local_path}/\" unless local_path.end_with?('/')\n \n # Create temporary password file for rsync\n temp_password_file = Tempfile.new('rsync_password')\n temp_password_file.write(config['rsync_password'])\n temp_password_file.close\n FileUtils.chmod(0600, temp_password_file.path)\n\n # Build rsync command for daemon mode with simplified output\n rsync_cmd = [\n \"rsync\",\n \"-ahi\", # archive mode, human-readable, itemize changes\n \"--delete\", # delete extraneous files\n \"--out-format='%i | %n'\", # simplified output format showing changes and filename\n \"--password-file=\#{temp_password_file.path}\",\n local_path,\n \"rsync://\#{config['name']}@\#{config['host']}/\#{config['name']}/\"\n ].join(\" \")\n \n # Execute rsync with better error handling\n puts \"Syncing website files...\"\n\n IO.popen(rsync_cmd) do |io|\n while line = io.gets\n info = line.partition('|').first.strip\n filename = line.partition('|').last.strip\n\n if info[0] == '<'\n puts \"Uploading \#{filename}\"\n elsif info[0] == '.'\n if info[1] == 'd'\n puts \"Acessing \#{filename}\"\n else\n puts \"Unchanged \#{filename}\"\n end\n elsif info == \"*deleting\"\n puts \"Deleting \#{filename}\"\n elsif info == \"cd+++++++++\"\n puts \"Creating \#{filename}\"\n else\n puts line\n end\n end\n end\n\n exit_status = $?.exitstatus\n \n # Clean up temporary password file\n temp_password_file.unlink\n \n if exit_status != 0\n puts \"Rsync failed. Checking daemon accessibility...\"\n system(\"nc -zv \#{config['host']} 873\")\n raise Error, \"Rsync failed with status: \#{exit_status}. Please check the rsync daemon is running and port 873 is accessible.\"\n end\n \n puts \"Sync completed!\"\n end\n rescue Net::SSH::AuthenticationFailed\n raise Error, \"SSH authentication failed\"\n rescue StandardError => e\n raise Error, \"Deployment failed: \#{e.message}\"\n ensure\n # Now temp_password_file will be in scope\n temp_password_file&.unlink\n end\nend\n" |
.init(config_path: '_jamal.yml') ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/jamal.rb', line 292 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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 |
# File 'lib/jamal.rb', line 248 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 = " uid = \#{config['user']}\n gid = \#{config['user']}\n use chroot = no\n max connections = 4\n pid file = /var/run/rsyncd.pid\n exclude = .git/ .gitignore\n\n [\#{config['name']}]\n path = /var/www/\#{config['name']}\n comment = Website files for \#{config['name']}\n read only = no\n auth users = \#{config['name']}\n secrets file = /etc/rsyncd.secrets\n CONFIG\n\n # Generate a random password for rsync\n special_chars = '!@\#$%^&*()_+-=[]{}|;:,.<>?'.chars\n numbers = '0123456789'.chars\n letters = ('a'..'z').to_a + ('A'..'Z').to_a\n rsync_password = (letters + numbers + special_chars).shuffle[0,16].join\n \n # First, clean up any existing entries for both the site name and root user\n ssh.exec!(\"sudo sed -i '/^\#{config['name']}:/d' /etc/rsyncd.secrets\")\n\n # Add new entry with the website name\n ssh.exec!(\"echo '\#{config['name']}:\#{rsync_password}' | sudo tee -a /etc/rsyncd.secrets\")\n ssh.exec!(\"sudo chmod 600 /etc/rsyncd.secrets\")\n \n # Remove any existing module configuration and create new one\n ssh.exec!(\"sudo sed -i '/\\\\[\#{config['name']}\\\\]/,/^$/d' /etc/rsyncd.conf\")\n \n # Write the full rsyncd configuration\n ssh.exec!(\"echo '\#{rsyncd_conf}' | sudo tee -a /etc/rsyncd.conf\")\n \n # Update the config file with the rsync password\n config['rsync_password'] = rsync_password\n File.write(config_path, config.to_yaml)\n \n # Enable and start rsync daemon\n ssh.exec!(\"sudo systemctl enable rsync\")\n ssh.exec!(\"sudo systemctl start rsync\")\n \n puts \"Rsync daemon configured successfully!\"\n end\n rescue Net::SSH::AuthenticationFailed\n raise Error, \"SSH authentication failed\"\n rescue StandardError => e\n raise Error, \"Setup failed: \#{e.message}\"\n end\nend\n" |