### Automate Application Deployment with Deno SSH Client Source: https://context7.com/denosaurs/ssh/llms.txt Demonstrates a complete application deployment workflow using the Deno SSH client. It covers connecting to a server, backing up the current application, uploading new files, installing dependencies, running migrations, restarting the service, verifying the deployment, and downloading logs. Includes error handling and rollback functionality. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; async function deployApplication() { const client = new Client({ hostname: Deno.env.get("DEPLOY_HOST") || "production.example.com", port: parseInt(Deno.env.get("DEPLOY_PORT") || "22"), username: "deployer", privateKey: await Deno.readTextFile("./keys/deploy_key"), passphrase: Deno.env.get("KEY_PASSPHRASE") }); try { // Establish connection console.log("Connecting to deployment server..."); await client.connect(); if (!client.isConnected) { throw new Error("Failed to establish connection"); } console.log("✓ Connected successfully"); // Create backup of current version console.log("Creating backup..."); await client.run("tar -czf /backups/app-$(date +%Y%m%d-%H%M%S).tar.gz /var/www/app", { cwd: "/home/deployer" }); // Upload new application files console.log("Uploading new version..."); await client.putDirectory("/var/www/app"); // Install dependencies console.log("Installing dependencies..."); await client.run("npm ci --production", { cwd: "/var/www/app" }); // Run database migrations console.log("Running migrations..."); await client.run("npm run migrate", { cwd: "/var/www/app" }); // Restart application service console.log("Restarting service..."); await client.run("systemctl restart myapp"); // Verify deployment await client.run("systemctl status myapp"); // Download deployment logs console.log("Downloading logs..."); await client.getFile("/var/log/myapp/deployment.log"); console.log("✓ Deployment completed successfully"); } catch (error) { console.error("Deployment failed:", error.message); // Attempt rollback try { console.log("Attempting rollback..."); await client.run("./rollback.sh", { cwd: "/home/deployer/scripts" }); } catch (rollbackError) { console.error("Rollback failed:", rollbackError.message); } throw error; } finally { // Always cleanup connection client.disconnect(); console.log("Connection closed"); } } // Run with proper permissions // deno run -A --unstable deploy.ts if (import.meta.main) { await deployApplication(); } ``` -------------------------------- ### Get Directory Method - Download Remote Directory via SSH Source: https://context7.com/denosaurs/ssh/llms.txt Recursively downloads an entire directory and its contents from the remote server to the local system. Preserves directory structure and handles nested files. Requires active SSH connection with appropriate credentials and permissions. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "backupserver.internal", username: "backup", password: "backuppass123" }); await client.connect(); try { // Download entire directory structure await client.getDirectory("/var/www/html"); // Download user home directory await client.getDirectory("/home/user/projects"); // Download logs directory await client.getDirectory("/var/log/myapp"); // Download from default/current directory await client.getDirectory(); } catch (error) { console.error("Directory download failed:", error); } finally { client.disconnect(); } ``` -------------------------------- ### Get File Method - Download Remote File via SSH Source: https://context7.com/denosaurs/ssh/llms.txt Downloads a file from a remote server to the local system using SSH. Requires an established SSH client connection with authentication credentials. Supports downloading from custom remote paths or using default directory path. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "fileserver.local", username: "backup", privateKey: await Deno.readTextFile("./backup_key") }); await client.connect(); try { // Download a specific file await client.getFile("/var/log/application.log"); // Download from custom path await client.getFile("/home/user/documents/report.pdf"); // Download configuration file await client.getFile("/etc/nginx/nginx.conf"); // Download with default path (current directory) await client.getFile(); } catch (error) { console.error("File download failed:", error); } finally { client.disconnect(); } ``` -------------------------------- ### Initialize SSH Client with TypeScript - Deno Source: https://context7.com/denosaurs/ssh/llms.txt Create new SSH client instances with various authentication methods including default settings, custom hostname/port, password authentication, and private key authentication with passphrases. The Client class manages SSH connections and supports configuration of hostname, port, username, password, and privateKey parameters. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; // Basic connection with defaults (localhost:22, root user) const client1 = new Client(); // Connection with custom hostname and port const client2 = new Client({ hostname: "192.168.1.100", port: 2222, username: "admin" }); // Connection with password authentication const client3 = new Client({ hostname: "example.com", port: 22, username: "deployuser", password: "securepassword123" }); // Connection with private key authentication const privateKey = await Deno.readTextFile("./id_rsa"); const client4 = new Client({ hostname: "production.server.com", username: "deploy", privateKey: privateKey, passphrase: "keypassphrase" }); console.log(client1.isConnected); // false (not yet connected) ``` -------------------------------- ### ClientOptions Interface - SSH Client Configuration Source: https://context7.com/denosaurs/ssh/llms.txt Defines configuration interface for SSH client initialization with options for hostname, port, authentication methods (password or private key), and connection parameters. Supports both complete and minimal configurations with sensible defaults (port 22, username root). ```typescript import { Client, type ClientOptions } from "https://deno.land/x/ssh/mod.ts"; // Complete configuration object const options: ClientOptions = { hostname: "production.example.com", port: 2222, username: "deploybot", password: "secretpass", privateKey: await Deno.readTextFile("./keys/deploy_rsa"), passphrase: "keypassphrase123" }; const client = new Client(options); // Partial configuration with defaults const minimalOptions: ClientOptions = { hostname: "dev.local" // Uses defaults: port=22, username="root", no auth }; const devClient = new Client(minimalOptions); // Password-based auth const passwordAuth: ClientOptions = { hostname: "legacy.server.com", username: "admin", password: "admin123" }; // Key-based auth const keyAuth: ClientOptions = { hostname: "secure.server.com", username: "secure_user", privateKey: "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----" }; await client.connect(); ``` -------------------------------- ### Establish SSH Connection with TypeScript - Deno Source: https://context7.com/denosaurs/ssh/llms.txt Connect to a configured SSH server using the connect() method, which establishes a TCP connection and updates the connection status. The method is asynchronous and handles connection errors including network issues and incorrect ports. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "192.168.1.50", port: 22, username: "devuser" }); try { console.log("Connecting to server..."); await client.connect(); if (client.isConnected) { console.log("Successfully connected to SSH server"); console.log(`Connection status: ${client.isConnected}`); // true } } catch (error) { console.error("Connection failed:", error.message); // Handle connection errors (network issues, wrong port, etc.) } // Usage with environment variable (from examples) const envClient = new Client({ hostname: Deno.env.get("DENO_SSH_HOST") || "localhost" }); await envClient.connect(); ``` -------------------------------- ### Put Directory Method - Upload Local Directory via SSH Source: https://context7.com/denosaurs/ssh/llms.txt Recursively uploads an entire directory and its contents to the remote server. Maintains directory structure and handles nested files and subdirectories. Requires active SSH connection with appropriate authentication and remote write permissions. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "webserver.prod", username: "deployer", privateKey: await Deno.readTextFile("./deploy_key"), passphrase: "keypass" }); await client.connect(); try { // Upload entire build directory await client.putDirectory("/var/www/myapp/build"); // Upload configuration directory await client.putDirectory("/etc/myapp/config"); // Upload static assets await client.putDirectory("/var/www/static"); // Upload to default location await client.putDirectory(); } catch (error) { console.error("Directory upload failed:", error); } finally { client.disconnect(); } ``` -------------------------------- ### Execute Remote Commands with TypeScript - Deno Source: https://context7.com/denosaurs/ssh/llms.txt Execute shell commands on remote SSH servers using the run() method with optional working directory specification via the cwd option. Supports simple commands, complex pipes, redirects, and sequential command execution with error handling. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "webserver.example.com", username: "deploy", password: "deploypass" }); await client.connect(); try { // Simple command execution await client.run("ls -la /var/www"); // Command with custom working directory await client.run("npm install", { cwd: "/var/www/myapp" }); // Multiple commands in sequence await client.run("git pull origin main", { cwd: "/home/deploy/project" }); await client.run("systemctl restart nginx"); // Complex command with pipes and redirects await client.run("ps aux | grep node | wc -l"); } catch (error) { console.error("Command execution failed:", error); } finally { client.disconnect(); } ``` -------------------------------- ### Put File Method - Upload Local File via SSH Source: https://context7.com/denosaurs/ssh/llms.txt Uploads a file from the local system to a remote server using SSH. Requires an established SSH connection and supports uploading to custom remote paths or default directory. Handles connection errors and ensures proper disconnection. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "deployserver.com", port: 22, username: "deployer" }); await client.connect(); try { // Upload a file to specific path await client.putFile("/var/www/html/index.html"); // Upload configuration file await client.putFile("/etc/myapp/config.json"); // Upload build artifacts await client.putFile("/home/deploy/releases/app-v1.2.3.tar.gz"); // Upload to default path await client.putFile(); } catch (error) { console.error("File upload failed:", error); } finally { client.disconnect(); } ``` -------------------------------- ### Close SSH Connection with TypeScript - Deno Source: https://context7.com/denosaurs/ssh/llms.txt Disconnect from the active SSH server using the disconnect() method to close connections and clean up resources. This method should be called in finally blocks to ensure proper cleanup even if errors occur during SSH operations. ```typescript import { Client } from "https://deno.land/x/ssh/mod.ts"; const client = new Client({ hostname: "example.com", username: "admin" }); await client.connect(); // Perform operations... console.log(`Connected: ${client.isConnected}`); // true // Disconnect when done client.disconnect(); // Best practice: Use try-finally for cleanup try { await client.connect(); // Perform SSH operations } finally { client.disconnect(); console.log("Connection closed"); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.