Ecommerce + Grid http server Automatic Deployments for Workloads #104

Open
opened 2024-10-08 14:37:13 +00:00 by mik-tf · 4 comments
Owner

Situation

  • The grid allows users to deploy workloads such as Nextcloud (NC) and Wordpress
  • It has been asked by community members to add features so people can offer workloads to users, have them pay in fiat, and have the deployments done automatically

Specs

We provide an overview of the project. It should be done as a proof-of-concept to start. Then we should set high-availability and redundant backups.

  • Set wordpress with woocommerce, get a woocommerce API write/read key, set a VM on the grid using grid http server that uses the API key.
  • The VM checks every now and then if there's an update on the woo commerce database side, when there is a new confirmed purchase of nextcloud the VM deploys a NC instance with grid http server, when the instance is ready it sends the login credential to woocommerce database. Woocommerce database can send an email to the user with the credentials to log in.
  • With gateway domains, we could also have the possibility to set subdomain with gateway domains for users, e.g. username1.tfcloud.eu.
  • the woocommerce datase uses php and wordpress hook
  • the VM uses grid http server
  • we use grafana and prometheus for monitoring and health checks

Users POV

  • Go on the ecommerce
  • Order a nextcloud or wordpress instance
  • The API gets the new demand
  • The VM fetches the ecommerce API
  • The VM deploys the workload
  • The VM sends the info to the ecommerce API
  • The API sends an email to the user with the credentials
# Situation - The grid allows users to deploy workloads such as Nextcloud (NC) and Wordpress - It has been asked by community members to add features so people can offer workloads to users, have them pay in fiat, and have the deployments done automatically # Specs We provide an overview of the project. It should be done as a proof-of-concept to start. Then we should set high-availability and redundant backups. - Set wordpress with woocommerce, get a woocommerce API write/read key, set a VM on the grid using grid http server that uses the API key. - The VM checks every now and then if there's an update on the woo commerce database side, when there is a new confirmed purchase of nextcloud the VM deploys a NC instance with grid http server, when the instance is ready it sends the login credential to woocommerce database. Woocommerce database can send an email to the user with the credentials to log in. - With gateway domains, we could also have the possibility to set subdomain with gateway domains for users, e.g. username1.tfcloud.eu. - the woocommerce datase uses php and wordpress hook - the VM uses grid http server - we use grafana and prometheus for monitoring and health checks # Users POV - Go on the ecommerce - Order a nextcloud or wordpress instance - The API gets the new demand - The VM fetches the ecommerce API - The VM deploys the workload - The VM sends the info to the ecommerce API - The API sends an email to the user with the credentials
mik-tf added the
Story
label 2024-10-08 14:37:13 +00:00
Owner

Think this is a super worthwhile initiative for the community and any future audience looking to commercialize the grid. Would like to see it happen!

Think this is a super worthwhile initiative for the community and any future audience looking to commercialize the grid. Would like to see it happen!
Owner

Like Sam, I think this is a great initiative that will benefit the community and future users
With the OurVerse events and Hero coming up, our audience will be wider, e.g like freelancers or even digital nomads who would like to launch their products, this might give them another reason to deploy them on the grid

Like Sam, I think this is a great initiative that will benefit the community and future users With the OurVerse events and Hero coming up, our audience will be wider, e.g like freelancers or even digital nomads who would like to launch their products, this might give them another reason to deploy them on the grid
Author
Owner

I started some RnD with @scott. I will publish below what we have so far. It isn't fully tested yet, we are just working on the overall flow.

I started some RnD with @scott. I will publish below what we have so far. It isn't fully tested yet, we are just working on the overall flow.
Author
Owner

Note: The following is a WIP. It has not been completely tested yet.

Comprehensive Guide: Automated Nextcloud Deployment using WooCommerce and ThreeFold Grid

Table of Contents


Summary of Steps:

  1. Set up VM1 for WordPress and WooCommerce
  2. Set up VM2 for Grid HTTP Server
  3. Configure HTTPS with Caddy on both VMs
  4. Set up UFW firewall on both VMs
  5. Set up monitoring with Prometheus and Grafana on both VMs
  6. Create a Nextcloud product in WooCommerce
  7. Create a custom WordPress plugin for Nextcloud deployment
  8. Configure and test the deployment process

VM1 Setup (WordPress/WooCommerce):

1. Deploy and Set up WordPress and WooCommerce:

a. Deploy a micro VM on ThreeFold Grid for WordPress
b. Install necessary software:

apt update && apt upgrade -y
apt install -y php php-fpm nginx mysql-server php-mysql php-json php-curl php-dom php-xml php-xmlwriter php-tokenizer php-simplexml php-mbstring

c. Install WordPress and configure Nginx as the web server
d. Install and activate the WooCommerce plugin

2. Configure HTTPS with Caddy:

a. Install Caddy:

apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
apt update
apt install caddy

b. Configure Caddy:
Create /etc/caddy/Caddyfile with:

your-wordpress-domain.com {
    reverse_proxy localhost:80
}

c. Create Zinit configuration for Caddy:

mkdir -p /etc/zinit
cat > /etc/zinit/caddy.yaml << EOL
exec: caddy run --config /etc/caddy/Caddyfile
log: stdout
EOL

d. Start Caddy:

zinit monitor caddy

3. Set up UFW firewall:

apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 3000/tcp  # For Grafana
ufw allow 9090/tcp  # For Prometheus
ufw allow 9100/tcp  # For Node Exporter
ufw allow 9253/tcp  # For PHP-FPM Exporter
ufw enable

4. Set up monitoring with Prometheus and Grafana:

a. Install Prometheus:

wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
mv prometheus promtool /usr/local/bin/
mkdir /etc/prometheus
mv prometheus.yml /etc/prometheus/

b. Create a Zinit configuration for Prometheus:

cat > /etc/zinit/prometheus.yaml << EOL
exec: prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/
log: stdout
EOL

c. Install Node Exporter:

wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
mv node_exporter-*/node_exporter /usr/local/bin/

d. Create a Zinit configuration for Node Exporter:

cat > /etc/zinit/node_exporter.yaml << EOL
exec: node_exporter
log: stdout
EOL

e. Install PHP-FPM Exporter:

wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.0.0/php-fpm_exporter_2.0.0_linux_amd64.tar.gz
tar xvfz php-fpm_exporter_*.tar.gz
mv php-fpm_exporter /usr/local/bin/

f. Create a Zinit configuration for PHP-FPM Exporter:

cat > /etc/zinit/php-fpm-exporter.yaml << EOL
exec: php-fpm_exporter --phpfpm.scrape-uri tcp://127.0.0.1:9000/status
log: stdout
EOL

g. Update Prometheus configuration:
Edit /etc/prometheus/prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'php-fpm'
    static_configs:
      - targets: ['localhost:9253']

h. Install Grafana:

apt-get install -y software-properties-common
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
apt-get update
apt-get install -y grafana

i. Create a Zinit configuration for Grafana:

cat > /etc/zinit/grafana.yaml << EOL
exec: grafana-server --config /etc/grafana/grafana.ini
log: stdout
EOL

j. Start the monitoring services:

zinit monitor prometheus
zinit monitor node_exporter
zinit monitor php-fpm-exporter
zinit monitor grafana

k. Configure Grafana:

  • Access Grafana web interface at http://your_vm_ip:3000
  • Log in with default credentials (admin/admin)
  • Change the password when prompted
  • Add Prometheus as a data source:
    • Click on Configuration (gear icon) > Data Sources
    • Click "Add data source"
    • Select Prometheus
    • Set URL to http://localhost:9090
    • Click "Save & Test"
  • Import dashboards:
    • Click on "+" > Import
    • Import dashboard ID 1860 (Node Exporter Full)
    • Import dashboard ID 2056 (PHP-FPM Full)
    • Select your Prometheus data source for both
    • Click "Import"

5. Create a Nextcloud product in WooCommerce:

a. Go to Products > Add New
b. Set the product name to "Nextcloud Instance"
c. Set the product type to "Simple product"
d. Set the price for the Nextcloud instance
e. Publish the product

6. Create a custom WordPress plugin for Nextcloud deployment:

Create wp-content/plugins/nextcloud-deployer/nextcloud-deployer.php:

<?php
/*
Plugin Name: Nextcloud Deployer
Description: Handles Nextcloud deployment on ThreeFold Grid using Grid HTTP Server
Version: 1.0
Author: Your Name
*/

// Hook into new order creation
add_action('woocommerce_new_order', 'handle_nextcloud_deployment');

function handle_nextcloud_deployment($order_id) {
    $order = wc_get_order($order_id);
    
    // Check if it's a Nextcloud product
    $is_nextcloud = false;
    foreach ($order->get_items() as $item) {
        if ($item->get_product()->get_name() == 'Nextcloud Instance') {
            $is_nextcloud = true;
            break;
        }
    }
    
    if (!$is_nextcloud) {
        return;
    }
    
    // Trigger the deployment
    $result = deploy_nextcloud_instance($order_id);
    
    if ($result['success']) {
        update_post_meta($order_id, '_nextcloud_domain', $result['data']['domain']);
        update_post_meta($order_id, '_nextcloud_username', $result['data']['adminUsername']);
        update_post_meta($order_id, '_nextcloud_password', $result['data']['adminPassword']);
        
        // Send email to customer
        $to = $order->get_billing_email();
        $subject = 'Your Nextcloud Instance is Ready';
        $message = "Your Nextcloud instance has been deployed.\n\n";
        $message .= "Domain: " . $result['data']['domain'] . "\n";
        $message .= "Username: " . $result['data']['adminUsername'] . "\n";
        $message .= "Password: " . $result['data']['adminPassword'] . "\n";

        wp_mail($to, $subject, $message);
    } else {
        update_post_meta($order_id, '_nextcloud_deployment_failed', true);
        error_log('Nextcloud deployment failed: ' . $result['message']);
    }
}

function deploy_nextcloud_instance($order_id) {
    $grid_http_server_url = 'https://your-grid-http-server.com/machines/deploy';
    
    $response = wp_remote_post($grid_http_server_url, array(
        'body' => json_encode(array(
            'name' => 'nextcloud-' . $order_id,
            'network' => array(
                'name' => 'nextcloud-network-' . $order_id,
                'ip_range' => '10.20.0.0/16'
            ),
            'machines' => array(
                array(
                    'name' => 'nextcloud-' . $order_id,
                    'node_id' => 1, // Replace with an actual node ID
                    'public_ip' => true,
                    'planetary' => true,
                    'cpu' => 2,
                    'memory' => 4096,
                    'rootfs_size' => 10,
                    'disks' => array(
                        array(
                            'name' => 'data',
                            'size' => 100,
                            'mountpoint' => '/var/www/html'
                        )
                    ),
                    'flist' => 'https://hub.grid.tf/tf-official-apps/nextcloud-22.2.5.flist',
                    'entrypoint' => '/sbin/zinit init',
                    'env' => array(
                        'ADMIN_USER' => 'admin',
                        'ADMIN_PASSWORD' => wp_generate_password(),
                        'DB_PASSWORD' => wp_generate_password()
                    )
                )
            ),
            'metadata' => 'Nextcloud instance for order ' . $order_id,
            'description' => 'Nextcloud deployment for WooCommerce order'
        )),
        'headers' => array('Content-Type' => 'application/json'),
    ));

    if (is_wp_error($response)) {
        return array(
            'success' => false,
            'message' => $response->get_error_message()
        );
    }

    $body = wp_remote_retrieve_body($response);
    $result = json_decode($body, true);

    if (isset($result['data']['machines'][0]['public_ip'])) {
        return array(
            'success' => true,
            'data' => array(
                'domain' => $result['data']['machines'][0]['public_ip'],
                'adminUsername' => 'admin',
                'adminPassword' => $result['data']['machines'][0]['env']['ADMIN_PASSWORD']
            )
        );
    } else {
        return array(
            'success' => false,
            'message' => 'Deployment failed: ' . json_encode($result)
        );
    }
}

Activate the plugin in the WordPress admin panel.

VM2 Setup (Grid HTTP Server):

1. Set up the Grid HTTP Server:

a. Deploy a micro VM on ThreeFold Grid for the Grid HTTP Server

b. Install Node.js and npm:

curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
apt-get install -y nodejs

c. Install the Grid HTTP Server:

npm install -g @threefold/grid_http_server

d. Create a configuration file config.json:

{
  "network": "dev",
  "mnemonic": "your account mnemonics",
  "storeSecret": "your_secret_for_tfkvStore",
  "keypairType": "sr25519"
}

e. Create a Zinit configuration for the Grid HTTP Server:

mkdir -p /etc/zinit
cat > /etc/zinit/grid-http-server.yaml << EOL
exec: grid_http_server --config /path/to/config.json
log: stdout
EOL

f. Start the Grid HTTP Server:

zinit monitor grid-http-server

2. Configure HTTPS with Caddy:

a. Install Caddy:

apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
apt update
apt install caddy

b. Configure Caddy:
Create /etc/caddy/Caddyfile with:

your-grid-http-server.com {
    reverse_proxy localhost:3000
}

c. Create Zinit configuration for Caddy:

cat > /etc/zinit/caddy.yaml << EOL
exec: caddy run --config /etc/caddy/Caddyfile
log: stdout
EOL

d. Start Caddy:

zinit monitor caddy

3. Set up UFW firewall:

apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 3000/tcp  # For Grid HTTP Server
ufw allow 9090/tcp  # For Prometheus
ufw allow 9100/tcp  # For Node Exporter
ufw enable

4. Set up monitoring with Prometheus and Grafana:

a. Install Prometheus:

wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
mv prometheus promtool /usr/local/bin/
mkdir /etc/prometheus
mv prometheus.yml /etc/prometheus/

b. Create a Zinit configuration for Prometheus:

cat > /etc/zinit/prometheus.yaml << EOL
exec: prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/
log: stdout
EOL

c. Install Node Exporter:

wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
mv node_exporter-*/node_exporter /usr/local/bin/

d. Create a Zinit configuration for Node Exporter:

cat > /etc/zinit/node_exporter.yaml << EOL
exec: node_exporter
log: stdout
EOL

e. Update Prometheus configuration:
Edit /etc/prometheus/prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'grid-http-server'
    static_configs:
      - targets: ['localhost:3000']

f. Install Grafana:

apt-get install -y software-properties-common
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
apt-get update
apt-get install -y grafana

g. Create a Zinit configuration for Grafana:

cat > /etc/zinit/grafana.yaml << EOL
exec: grafana-server --config /etc/grafana/grafana.ini
log: stdout
EOL

h. Start the monitoring services:

zinit monitor prometheus
zinit monitor node_exporter
zinit monitor grafana

i. Configure Grafana:

  • Access Grafana web interface at http://your_vm_ip:3000
  • Log in with default credentials (admin/admin)
  • Change the password when prompted
  • Add Prometheus as a data source:
    • Click on Configuration (gear icon) > Data Sources
    • Click "Add data source"
    • Select Prometheus
    • Set URL to http://localhost:9090
    • Click "Save & Test"
  • Import dashboard:
    • Click on "+" > Import
    • Import dashboard ID 1860 (Node Exporter Full)
    • Select your Prometheus data source
    • Click "Import"

Final Configuration and Testing:

  1. Update the $grid_http_server_url in the WordPress plugin on VM1 to match the HTTPS URL of your Grid HTTP Server on VM2.

  2. Test the deployment process:

    • Place a test order for a Nextcloud instance in your WooCommerce store on VM1
    • Check the WordPress error log on VM1 for any issues
    • Verify that the order is updated with Nextcloud deployment information on VM1
    • Check your email for the deployment notification
    • Monitor the Grid HTTP Server logs on VM2 to ensure it's receiving and processing requests correctly
  3. Create custom Grafana dashboards:

    • On VM1, create a dashboard for WordPress and WooCommerce metrics
    • On VM2, create a dashboard for Grid HTTP Server metrics and deployment statistics
  4. Set up Grafana alerts:

    • Configure alerts for high CPU/memory usage, disk space running low, and unusual number of failed deployments
    • Set up notification channels (email, Slack, etc.) for these alerts
  5. Implement regular backups for both VMs

Zinit Management Commands:

  • To check the status of services:

    zinit list
    
  • To stop a service:

    zinit stop service_name
    
  • To start a service:

    zinit start service_name
    
  • To view logs:

    zinit log service_name
    

This comprehensive guide provides a complete setup for automating Nextcloud deployments using WooCommerce and the ThreeFold Grid, including monitoring with Prometheus and Grafana. It's specifically tailored for ThreeFold Grid micro VMs using Zinit and the official Grid HTTP Server, now updated for Ubuntu.

Remember to replace placeholder values (like your account mnemonics, your_secret_for_tfkvStore, node IDs, etc.) with your actual data. Also, ensure that you're using the correct network (dev, qa, or test) based on your deployment environment.

Always thoroughly test the system and implement proper error handling and security measures before using it in a production environment.

> Note: The following is a WIP. It has not been completely tested yet. <h1> Comprehensive Guide: Automated Nextcloud Deployment using WooCommerce and ThreeFold Grid </h1> <h2>Table of Contents</h2> - [Summary of Steps:](#summary-of-steps) - [VM1 Setup (WordPress/WooCommerce):](#vm1-setup-wordpresswoocommerce) - [1. Deploy and Set up WordPress and WooCommerce:](#1-deploy-and-set-up-wordpress-and-woocommerce) - [2. Configure HTTPS with Caddy:](#2-configure-https-with-caddy) - [3. Set up UFW firewall:](#3-set-up-ufw-firewall) - [4. Set up monitoring with Prometheus and Grafana:](#4-set-up-monitoring-with-prometheus-and-grafana) - [5. Create a Nextcloud product in WooCommerce:](#5-create-a-nextcloud-product-in-woocommerce) - [6. Create a custom WordPress plugin for Nextcloud deployment:](#6-create-a-custom-wordpress-plugin-for-nextcloud-deployment) - [VM2 Setup (Grid HTTP Server):](#vm2-setup-grid-http-server) - [1. Set up the Grid HTTP Server:](#1-set-up-the-grid-http-server) - [2. Configure HTTPS with Caddy:](#2-configure-https-with-caddy-1) - [3. Set up UFW firewall:](#3-set-up-ufw-firewall-1) - [4. Set up monitoring with Prometheus and Grafana:](#4-set-up-monitoring-with-prometheus-and-grafana-1) - [Final Configuration and Testing:](#final-configuration-and-testing) - [Zinit Management Commands:](#zinit-management-commands) --- ## Summary of Steps: 1. Set up VM1 for WordPress and WooCommerce 2. Set up VM2 for Grid HTTP Server 3. Configure HTTPS with Caddy on both VMs 4. Set up UFW firewall on both VMs 5. Set up monitoring with Prometheus and Grafana on both VMs 6. Create a Nextcloud product in WooCommerce 7. Create a custom WordPress plugin for Nextcloud deployment 8. Configure and test the deployment process ## VM1 Setup (WordPress/WooCommerce): ### 1. Deploy and Set up WordPress and WooCommerce: a. Deploy a micro VM on ThreeFold Grid for WordPress b. Install necessary software: ```bash apt update && apt upgrade -y apt install -y php php-fpm nginx mysql-server php-mysql php-json php-curl php-dom php-xml php-xmlwriter php-tokenizer php-simplexml php-mbstring ``` c. Install WordPress and configure Nginx as the web server d. Install and activate the WooCommerce plugin ### 2. Configure HTTPS with Caddy: a. Install Caddy: ```bash apt install -y debian-keyring debian-archive-keyring apt-transport-https curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list apt update apt install caddy ``` b. Configure Caddy: Create `/etc/caddy/Caddyfile` with: ``` your-wordpress-domain.com { reverse_proxy localhost:80 } ``` c. Create Zinit configuration for Caddy: ```bash mkdir -p /etc/zinit cat > /etc/zinit/caddy.yaml << EOL exec: caddy run --config /etc/caddy/Caddyfile log: stdout EOL ``` d. Start Caddy: ```bash zinit monitor caddy ``` ### 3. Set up UFW firewall: ```bash apt install -y ufw ufw default deny incoming ufw default allow outgoing ufw allow 22/tcp ufw allow 80/tcp ufw allow 443/tcp ufw allow 3000/tcp # For Grafana ufw allow 9090/tcp # For Prometheus ufw allow 9100/tcp # For Node Exporter ufw allow 9253/tcp # For PHP-FPM Exporter ufw enable ``` ### 4. Set up monitoring with Prometheus and Grafana: a. Install Prometheus: ```bash wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz tar xvfz prometheus-*.tar.gz cd prometheus-* mv prometheus promtool /usr/local/bin/ mkdir /etc/prometheus mv prometheus.yml /etc/prometheus/ ``` b. Create a Zinit configuration for Prometheus: ```bash cat > /etc/zinit/prometheus.yaml << EOL exec: prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ log: stdout EOL ``` c. Install Node Exporter: ```bash wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz tar xvfz node_exporter-*.tar.gz mv node_exporter-*/node_exporter /usr/local/bin/ ``` d. Create a Zinit configuration for Node Exporter: ```bash cat > /etc/zinit/node_exporter.yaml << EOL exec: node_exporter log: stdout EOL ``` e. Install PHP-FPM Exporter: ```bash wget https://github.com/hipages/php-fpm_exporter/releases/download/v2.0.0/php-fpm_exporter_2.0.0_linux_amd64.tar.gz tar xvfz php-fpm_exporter_*.tar.gz mv php-fpm_exporter /usr/local/bin/ ``` f. Create a Zinit configuration for PHP-FPM Exporter: ```bash cat > /etc/zinit/php-fpm-exporter.yaml << EOL exec: php-fpm_exporter --phpfpm.scrape-uri tcp://127.0.0.1:9000/status log: stdout EOL ``` g. Update Prometheus configuration: Edit `/etc/prometheus/prometheus.yml`: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] - job_name: 'php-fpm' static_configs: - targets: ['localhost:9253'] ``` h. Install Grafana: ```bash apt-get install -y software-properties-common add-apt-repository "deb https://packages.grafana.com/oss/deb stable main" wget -q -O - https://packages.grafana.com/gpg.key | apt-key add - apt-get update apt-get install -y grafana ``` i. Create a Zinit configuration for Grafana: ```bash cat > /etc/zinit/grafana.yaml << EOL exec: grafana-server --config /etc/grafana/grafana.ini log: stdout EOL ``` j. Start the monitoring services: ```bash zinit monitor prometheus zinit monitor node_exporter zinit monitor php-fpm-exporter zinit monitor grafana ``` k. Configure Grafana: - Access Grafana web interface at `http://your_vm_ip:3000` - Log in with default credentials (admin/admin) - Change the password when prompted - Add Prometheus as a data source: - Click on Configuration (gear icon) > Data Sources - Click "Add data source" - Select Prometheus - Set URL to `http://localhost:9090` - Click "Save & Test" - Import dashboards: - Click on "+" > Import - Import dashboard ID 1860 (Node Exporter Full) - Import dashboard ID 2056 (PHP-FPM Full) - Select your Prometheus data source for both - Click "Import" ### 5. Create a Nextcloud product in WooCommerce: a. Go to Products > Add New b. Set the product name to "Nextcloud Instance" c. Set the product type to "Simple product" d. Set the price for the Nextcloud instance e. Publish the product ### 6. Create a custom WordPress plugin for Nextcloud deployment: Create `wp-content/plugins/nextcloud-deployer/nextcloud-deployer.php`: ```php <?php /* Plugin Name: Nextcloud Deployer Description: Handles Nextcloud deployment on ThreeFold Grid using Grid HTTP Server Version: 1.0 Author: Your Name */ // Hook into new order creation add_action('woocommerce_new_order', 'handle_nextcloud_deployment'); function handle_nextcloud_deployment($order_id) { $order = wc_get_order($order_id); // Check if it's a Nextcloud product $is_nextcloud = false; foreach ($order->get_items() as $item) { if ($item->get_product()->get_name() == 'Nextcloud Instance') { $is_nextcloud = true; break; } } if (!$is_nextcloud) { return; } // Trigger the deployment $result = deploy_nextcloud_instance($order_id); if ($result['success']) { update_post_meta($order_id, '_nextcloud_domain', $result['data']['domain']); update_post_meta($order_id, '_nextcloud_username', $result['data']['adminUsername']); update_post_meta($order_id, '_nextcloud_password', $result['data']['adminPassword']); // Send email to customer $to = $order->get_billing_email(); $subject = 'Your Nextcloud Instance is Ready'; $message = "Your Nextcloud instance has been deployed.\n\n"; $message .= "Domain: " . $result['data']['domain'] . "\n"; $message .= "Username: " . $result['data']['adminUsername'] . "\n"; $message .= "Password: " . $result['data']['adminPassword'] . "\n"; wp_mail($to, $subject, $message); } else { update_post_meta($order_id, '_nextcloud_deployment_failed', true); error_log('Nextcloud deployment failed: ' . $result['message']); } } function deploy_nextcloud_instance($order_id) { $grid_http_server_url = 'https://your-grid-http-server.com/machines/deploy'; $response = wp_remote_post($grid_http_server_url, array( 'body' => json_encode(array( 'name' => 'nextcloud-' . $order_id, 'network' => array( 'name' => 'nextcloud-network-' . $order_id, 'ip_range' => '10.20.0.0/16' ), 'machines' => array( array( 'name' => 'nextcloud-' . $order_id, 'node_id' => 1, // Replace with an actual node ID 'public_ip' => true, 'planetary' => true, 'cpu' => 2, 'memory' => 4096, 'rootfs_size' => 10, 'disks' => array( array( 'name' => 'data', 'size' => 100, 'mountpoint' => '/var/www/html' ) ), 'flist' => 'https://hub.grid.tf/tf-official-apps/nextcloud-22.2.5.flist', 'entrypoint' => '/sbin/zinit init', 'env' => array( 'ADMIN_USER' => 'admin', 'ADMIN_PASSWORD' => wp_generate_password(), 'DB_PASSWORD' => wp_generate_password() ) ) ), 'metadata' => 'Nextcloud instance for order ' . $order_id, 'description' => 'Nextcloud deployment for WooCommerce order' )), 'headers' => array('Content-Type' => 'application/json'), )); if (is_wp_error($response)) { return array( 'success' => false, 'message' => $response->get_error_message() ); } $body = wp_remote_retrieve_body($response); $result = json_decode($body, true); if (isset($result['data']['machines'][0]['public_ip'])) { return array( 'success' => true, 'data' => array( 'domain' => $result['data']['machines'][0]['public_ip'], 'adminUsername' => 'admin', 'adminPassword' => $result['data']['machines'][0]['env']['ADMIN_PASSWORD'] ) ); } else { return array( 'success' => false, 'message' => 'Deployment failed: ' . json_encode($result) ); } } ``` Activate the plugin in the WordPress admin panel. ## VM2 Setup (Grid HTTP Server): ### 1. Set up the Grid HTTP Server: a. Deploy a micro VM on ThreeFold Grid for the Grid HTTP Server b. Install Node.js and npm: ```bash curl -fsSL https://deb.nodesource.com/setup_14.x | bash - apt-get install -y nodejs ``` c. Install the Grid HTTP Server: ```bash npm install -g @threefold/grid_http_server ``` d. Create a configuration file `config.json`: ```json { "network": "dev", "mnemonic": "your account mnemonics", "storeSecret": "your_secret_for_tfkvStore", "keypairType": "sr25519" } ``` e. Create a Zinit configuration for the Grid HTTP Server: ```bash mkdir -p /etc/zinit cat > /etc/zinit/grid-http-server.yaml << EOL exec: grid_http_server --config /path/to/config.json log: stdout EOL ``` f. Start the Grid HTTP Server: ```bash zinit monitor grid-http-server ``` ### 2. Configure HTTPS with Caddy: a. Install Caddy: ```bash apt install -y debian-keyring debian-archive-keyring apt-transport-https curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list apt update apt install caddy ``` b. Configure Caddy: Create `/etc/caddy/Caddyfile` with: ``` your-grid-http-server.com { reverse_proxy localhost:3000 } ``` c. Create Zinit configuration for Caddy: ```bash cat > /etc/zinit/caddy.yaml << EOL exec: caddy run --config /etc/caddy/Caddyfile log: stdout EOL ``` d. Start Caddy: ```bash zinit monitor caddy ``` ### 3. Set up UFW firewall: ```bash apt install -y ufw ufw default deny incoming ufw default allow outgoing ufw allow 22/tcp ufw allow 80/tcp ufw allow 443/tcp ufw allow 3000/tcp # For Grid HTTP Server ufw allow 9090/tcp # For Prometheus ufw allow 9100/tcp # For Node Exporter ufw enable ``` ### 4. Set up monitoring with Prometheus and Grafana: a. Install Prometheus: ```bash wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz tar xvfz prometheus-*.tar.gz cd prometheus-* mv prometheus promtool /usr/local/bin/ mkdir /etc/prometheus mv prometheus.yml /etc/prometheus/ ``` b. Create a Zinit configuration for Prometheus: ```bash cat > /etc/zinit/prometheus.yaml << EOL exec: prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ log: stdout EOL ``` c. Install Node Exporter: ```bash wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz tar xvfz node_exporter-*.tar.gz mv node_exporter-*/node_exporter /usr/local/bin/ ``` d. Create a Zinit configuration for Node Exporter: ```bash cat > /etc/zinit/node_exporter.yaml << EOL exec: node_exporter log: stdout EOL ``` e. Update Prometheus configuration: Edit `/etc/prometheus/prometheus.yml`: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] - job_name: 'grid-http-server' static_configs: - targets: ['localhost:3000'] ``` f. Install Grafana: ```bash apt-get install -y software-properties-common add-apt-repository "deb https://packages.grafana.com/oss/deb stable main" wget -q -O - https://packages.grafana.com/gpg.key | apt-key add - apt-get update apt-get install -y grafana ``` g. Create a Zinit configuration for Grafana: ```bash cat > /etc/zinit/grafana.yaml << EOL exec: grafana-server --config /etc/grafana/grafana.ini log: stdout EOL ``` h. Start the monitoring services: ```bash zinit monitor prometheus zinit monitor node_exporter zinit monitor grafana ``` i. Configure Grafana: - Access Grafana web interface at `http://your_vm_ip:3000` - Log in with default credentials (admin/admin) - Change the password when prompted - Add Prometheus as a data source: - Click on Configuration (gear icon) > Data Sources - Click "Add data source" - Select Prometheus - Set URL to `http://localhost:9090` - Click "Save & Test" - Import dashboard: - Click on "+" > Import - Import dashboard ID 1860 (Node Exporter Full) - Select your Prometheus data source - Click "Import" ## Final Configuration and Testing: 1. Update the `$grid_http_server_url` in the WordPress plugin on VM1 to match the HTTPS URL of your Grid HTTP Server on VM2. 2. Test the deployment process: - Place a test order for a Nextcloud instance in your WooCommerce store on VM1 - Check the WordPress error log on VM1 for any issues - Verify that the order is updated with Nextcloud deployment information on VM1 - Check your email for the deployment notification - Monitor the Grid HTTP Server logs on VM2 to ensure it's receiving and processing requests correctly 3. Create custom Grafana dashboards: - On VM1, create a dashboard for WordPress and WooCommerce metrics - On VM2, create a dashboard for Grid HTTP Server metrics and deployment statistics 4. Set up Grafana alerts: - Configure alerts for high CPU/memory usage, disk space running low, and unusual number of failed deployments - Set up notification channels (email, Slack, etc.) for these alerts 5. Implement regular backups for both VMs ### Zinit Management Commands: - To check the status of services: ```bash zinit list ``` - To stop a service: ```bash zinit stop service_name ``` - To start a service: ```bash zinit start service_name ``` - To view logs: ```bash zinit log service_name ``` This comprehensive guide provides a complete setup for automating Nextcloud deployments using WooCommerce and the ThreeFold Grid, including monitoring with Prometheus and Grafana. It's specifically tailored for ThreeFold Grid micro VMs using Zinit and the official Grid HTTP Server, now updated for Ubuntu. Remember to replace placeholder values (like `your account mnemonics`, `your_secret_for_tfkvStore`, node IDs, etc.) with your actual data. Also, ensure that you're using the correct network (`dev`, `qa`, or `test`) based on your deployment environment. Always thoroughly test the system and implement proper error handling and security measures before using it in a production environment.
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: tfgrid/circle_engineering#104
No description provided.