Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
doziestar committed Jun 24, 2024
1 parent 51887ee commit 47529a9
Show file tree
Hide file tree
Showing 13 changed files with 872 additions and 12 deletions.
62 changes: 55 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1

jobs:
check:
Expand Down Expand Up @@ -39,6 +40,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --test main -- --nocapture

fmt:
name: Rustfmt
Expand Down Expand Up @@ -82,18 +84,47 @@ jobs:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
- name: Install mdBook
run: cargo install mdbook
- name: Build API docs
uses: actions-rs/cargo@v1
with:
command: doc
args: --no-deps
- uses: peaceiris/actions-gh-pages@v3
args: --no-deps --all-features
- name: Build mdBook docs
run: mdbook build docs
- name: Combine docs
run: |
mkdir -p ./public
cp -r ./target/doc ./public/api
cp -r ./docs/book ./public/guide
echo '<meta http-equiv="refresh" content="0; url=serverforge/index.html">' > ./public/index.html
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.TOKEN }}
publish_dir: ./target/doc
publish_dir: ./public
force_orphan: true

publish-dry-run:
name: Publish Dry Run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: publish
args: --dry-run

release:
name: Release
needs: [check, test, fmt, clippy, docs]
needs: [check, fmt, docs, publish-dry-run]
if: github.event_name == 'release' && github.event.action == 'created'
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -132,9 +163,26 @@ jobs:
tag: ${{ github.ref }}
overwrite: true

publish-crates-io:
name: Publish to crates.io
needs: [release]
if: github.event_name == 'release' && github.event.action == 'created'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: publish
args: --token ${{ secrets.CRATES_IO_TOKEN }}

homebrew:
name: Update Homebrew Formula
needs: release
needs: [release]
if: github.event_name == 'release' && github.event.action == 'created'
runs-on: ubuntu-latest
steps:
Expand All @@ -146,7 +194,7 @@ jobs:

linux-packages:
name: Create Linux Packages
needs: release
needs: [release]
if: github.event_name == 'release' && github.event.action == 'created'
runs-on: ubuntu-latest
steps:
Expand Down
59 changes: 59 additions & 0 deletions src/backup.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
//! # Backup Module
//!
//! This module provides functionality for setting up and configuring an automated backup system
//! for a Linux server. It uses `restic` as the backup tool, which provides efficient, encrypted,
//! and incremental backups.
//!
//! The module includes functions for installing backup tools, configuring backup schedules,
//! and setting up backup locations based on the server's role.

use crate::config::Config;
use crate::distro::{get_package_manager, PackageManager};
use crate::rollback::RollbackManager;
use crate::utils::run_command;
use log::info;
use std::error::Error;

/// Sets up the backup system based on the provided configuration.
///
/// This function orchestrates the entire backup setup process, including:
/// - Installing necessary backup tools
/// - Configuring the backup schedule
/// - Setting up backup locations
///
/// It creates a snapshot before starting the setup process for potential rollback.
///
/// # Arguments
///
/// * `config` - A reference to the `Config` struct containing backup configuration
/// * `rollback` - A reference to the `RollbackManager` for creating snapshots
///
/// # Returns
///
/// Returns `Ok(())` if the backup system is set up successfully, or an error if setup fails.
pub fn setup_backup_system(
config: &Config,
rollback: &RollbackManager,
Expand All @@ -23,6 +49,14 @@ pub fn setup_backup_system(
Ok(())
}

/// Installs the necessary backup tools (restic) on the system.
///
/// This function uses the appropriate package manager for the current Linux distribution
/// to install restic.
///
/// # Returns
///
/// Returns `Ok(())` if restic is installed successfully, or an error if installation fails.
pub fn install_backup_tools() -> Result<(), Box<dyn Error>> {
let package_manager = get_package_manager()?;
match package_manager {
Expand All @@ -33,6 +67,18 @@ pub fn install_backup_tools() -> Result<(), Box<dyn Error>> {
Ok(())
}

/// Configures the backup schedule based on the provided configuration.
///
/// This function creates a cron job for running backups at the specified frequency
/// (hourly, daily, or weekly).
///
/// # Arguments
///
/// * `config` - A reference to the `Config` struct containing the backup frequency
///
/// # Returns
///
/// Returns `Ok(())` if the backup schedule is configured successfully, or an error if configuration fails.
pub fn configure_backup_schedule(config: &Config) -> Result<(), Box<dyn Error>> {
let cron_job = match config.backup_frequency.as_str() {
"hourly" => {
Expand All @@ -51,6 +97,19 @@ pub fn configure_backup_schedule(config: &Config) -> Result<(), Box<dyn Error>>
Ok(())
}

/// Sets up backup locations based on the server's role.
///
/// This function determines which directories to back up based on the server's role
/// (web, database, or application server). It then initializes a restic repository
/// and creates a backup script that includes these locations.
///
/// # Arguments
///
/// * `config` - A reference to the `Config` struct containing the server role
///
/// # Returns
///
/// Returns `Ok(())` if backup locations are set up successfully, or an error if setup fails.
pub fn setup_backup_locations(config: &Config) -> Result<(), Box<dyn Error>> {
// Define backup locations based on server role
let backup_dirs = match config.server_role.as_str() {
Expand Down
34 changes: 34 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
//! # Configuration Module
//!
//! This module defines the `Config` struct, which represents the configuration
//! for the server setup and maintenance tool. It includes various settings
//! such as the Linux distribution, server role, security level, and deployment options.
//!
//! The `Config` struct implements `Serialize` and `Deserialize` traits from serde,
//! allowing for easy serialization and deserialization of the configuration.

use serde::{Deserialize, Serialize};

/// Represents the configuration for the server setup and maintenance tool.
///
/// This struct contains all the necessary settings and options for configuring
/// a server, including the operating system, security settings, and deployment options.
#[derive(Serialize, Deserialize, Clone)]
pub struct Config {
/// The Linux distribution being used (e.g., "ubuntu", "centos", "fedora")
pub linux_distro: String,

/// The role of the server (e.g., "web", "database", "application")
pub server_role: String,

/// The desired security level (e.g., "basic", "intermediate", "advanced")
pub security_level: String,

/// Whether to enable monitoring on the server
pub monitoring: bool,

/// The frequency of backups (e.g., "hourly", "daily", "weekly")
pub backup_frequency: String,

/// A list of applications to be deployed on the server
pub deployed_apps: Vec<String>,

/// A list of custom firewall rules to be applied
pub custom_firewall_rules: Vec<String>,

/// The schedule for automatic updates (e.g., "daily", "weekly", "monthly")
pub update_schedule: String,

/// Whether to use containerization for deployments
pub use_containers: bool,

/// Whether to use Kubernetes for container orchestration
pub use_kubernetes: bool,
}

/// Provides default values for the `Config` struct.
impl Default for Config {
/// Returns a new `Config` instance with default values.
fn default() -> Self {
Config {
linux_distro: String::from("ubuntu"),
Expand Down
Loading

0 comments on commit 47529a9

Please sign in to comment.