瀏覽代碼

add: new scripts and config files

joe 1 年之前
父節點
當前提交
8214771c10

+ 300 - 0
content/tools/fabfile.md

@@ -0,0 +1,300 @@
+---
+title: "Fabfile"
+date: 2024-01-04T14:21:55+07:00
+draft: false
+---
+
+## 工程部署一般腳本
+
+### Install
+
+```shell
+apt install python3
+apt install python3-pip
+pip install fabric
+touch fabfile.py
+```
+
+### fabfile.py
+
+```python
+# BsGRaUtSYaX5
+# 206.238.113.33
+import os
+import re
+import abc
+import shutil
+import platform
+import requests
+import subprocess
+
+from datetime import datetime
+
+from fabric import task, Connection
+
+
+'''
+部署工具
+部署时,受限于PHP本身。自动生成 version 文件,记录版本信息。
+'''
+
+
+class Tar:
+    @classmethod
+    def compress(cls, args):
+        pass
+
+    @classmethod
+    def uncompress(cls, args):
+        pass
+
+
+class Zip:
+    @classmethod
+    def compress(cls, args):
+        pass
+
+    @classmethod
+    def uncompress(cls, args):
+        pass
+
+
+class MessengerNull:
+    '''
+    No need to do more complicated implements.
+    I simply use Duck-Type.
+    '''
+    @classmethod
+    def send(cls, msg=''):
+        pass
+
+
+class MessengerTg:
+    @classmethod
+    def send(cls, msg=''):
+        pass
+
+
+class Remote:
+    """
+    Remote
+
+    Server where my project deployed. An instance refers a remote server.
+
+    NOTE: I leave it's defination here for simplicity, so no need to maintain multi-files.
+    """
+    def __init__(self, host, *, name='服务器', branch='', port=22, user='root', password='', 
+        user_group='www:www', pem='', deploy_path='/home', domain='', backup=False, tgmsg=False) -> None:
+        '''
+        Most people cares nothing, so do i.
+        '''
+        self.host = host
+        self.name = name
+        self.branch = branch    # MUST run in specified git branch
+        self.port = port
+        self.user = user
+        self.password = password
+        self.user_group = user_group
+        self.pem = pem
+        self.deploy_path = deploy_path # install path
+        self.domain = domain    #
+        self.backup = backup    # if backup before substitution
+        self.tgmsg = tgmsg      # deliver message to telegram
+
+
+''' <System Configures> '''
+# 要部署的文件/目录列表
+ITEMS = ('app', )
+
+# 部署是压缩上传。这个是压缩上传时压缩包文件
+FILE_TAR = './admin_api.tar.gz'
+
+# 上传位置 (注意不是部署目录,远程服务器必须首先接收文件)
+UPLOAD_PATH = '~'
+
+# 服务器列表
+mapModeRemote = {
+    'dev': Remote(
+        '192.168.1.15', 
+        name='dev',
+        password='123456',
+        deploy_path='/www',
+    ),
+    'prod': Remote(
+        '192.168.3.2',
+        name='2server',
+        backup=True,
+        password='23423423',
+        deploy_path='/home/ddd',
+    ),
+}
+''' </System Confirgures> '''
+
+
+def check_quit_mode(mode):
+    '''
+    Exit process if mode is invalid
+    '''
+    if mode not in mapModeRemote.keys():
+        print(f"Invalid mode: {mode}")
+        exit(-1)
+
+
+def get_server(mode):
+    return mapModeRemote.get(mode)
+
+
+def git_branch_name():
+    '''
+    Get active(current) git branch
+
+    @return str
+    '''
+    cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
+    return subprocess.check_output(cmd).decode('ascii').strip()
+
+
+def git_commit_hash(short=True):
+    '''
+    Get git latest commit hash
+
+    @short: is short hash or long(full)
+    @return str
+    '''
+    cmd = ['git', 'rev-parse', '--short', 'HEAD']
+    if not short:
+        cmd = ['git', 'rev-parse', 'HEAD']
+    return subprocess.check_output(cmd).decode('ascii').strip()
+
+
+def git_last_n_log(n=3):
+    cmd = ['git', 'log', '--oneline', f"-{n}"]
+    return subprocess.check_output(cmd).decode('utf8').strip()
+
+
+def run(c, cmd):
+    ''' Fabric Connection.run() wrapper'''
+    return c.run(cmd, hide=True)
+
+
+def generate_version_file():
+    '''
+    '''
+    commit = git_commit_hash()
+    with open('./app/version', 'wt', encoding='utf8') as fh:
+        content = f"{commit}"
+        fh.write(f"export const version='{content}'\n")
+
+
+def backup(r, server):
+    '''
+    backup to homedir before substitution
+
+    @r: remote connection
+    '''
+    now = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
+    filename = os.path.basename(server.deploy_path)
+    backup_filepath = os.path.join('~', f"{filename}{now}.tar.gz")
+    run(r, f"tar czf {backup_filepath} {server.deploy_path}")
+
+
+@task
+def test(c):
+    backup(None)
+
+
+def tar_posix(c):
+    run(c, f"rm -f {FILE_TAR}")
+    run(c, f"tar czf {FILE_TAR} {' '.join(ITEMS)}")
+
+
+def tar_win(c):
+    print('Unsupport function')
+    exit(-2)
+    # try:
+    #     os.unlink(FILE_TAR)
+    # except Exception as e:
+    #     print('Remove file warn: {}'.format(e))
+    # name, _ = os.path.splitext(FILE_TAR)
+    # shutil.make_archive(name, 'zip', ITEMS)
+    # print('zip file generated: {}'.format(FILE_TAR))
+
+
+@task
+def tar(c):
+    plt = platform.platform()
+    pattern_win = re.compile(r'windows', re.IGNORECASE)
+    match = pattern_win.search(plt)
+    _ = tar_win(c) if match else tar_posix(c)
+
+
+@task(help={'mode': 'Upload destination: "prod" or "dev"'})
+def upload(c, mode='dev'):
+    '''
+    Upload tarball(generated by tar command) to a remote server
+
+    @mode: specify server by mode. @see mapModeRemote
+    '''
+    check_quit_mode(mode)
+
+    if not os.path.isfile(FILE_TAR):
+        print("Please run tar command first.")
+        return
+
+    server = get_server(mode)
+
+    with Connection(host=server.host, port=server.port, 
+        user=server.user, connect_kwargs={'password': server.password}) as r:
+        with r.cd(UPLOAD_PATH):
+            r.put(FILE_TAR)
+            if server.backup:
+                backup(r, server)
+
+            run(r, f"tar xzf {FILE_TAR} -C {server.deploy_path}")
+            run(r, f"rm -f {FILE_TAR}")
+            if server.tgmsg:
+                send(c, f"新版本 {VersionString} 已发布 {server.name}: *.*.*.{server.host.split('.')[3]}")
+
+
+@task
+def send(c, msg='hello'):
+    '''
+    Send a telegram message
+
+    @msg: what to be sent
+    '''
+    chat_id = '1234567'
+    token = 'x454565465:Adskjfskldfjdsklfjskldfjskld-ppdfVdfd3f'
+    url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={msg}"
+    ret = requests.get(url).json()
+    if not ret['ok']:
+        print('send Telegram msg failed:', ret)
+
+
+@task(help={'mode': 'Deploy mode: "prod" or "dev"'})
+def deploy(c, mode='dev'):
+    check_quit_mode(mode)
+    server = get_server(mode)
+    branch = git_branch_name()
+    generate_version_file()
+    
+    if server.branch and branch != branch:
+        print(f"Wrong branch. mode={mode}, branch={branch}. branch should be {server.branch}")
+        exit(-3)
+
+    # dependencies
+    tar(c)
+    upload(c, mode)
+    print('Done.')
+
+```
+
+### 更改配置
+
+修改爲與當前工程適應的配置
+
+### 運行
+
+```shell
+fab deploy -m=<remoteName>
+```

+ 59 - 0
content/tools/nginx_laravel_conf.md

@@ -0,0 +1,59 @@
+---
+title: "Nginx_laravel_conf"
+date: 2024-01-04T14:27:11+07:00
+draft: false
+---
+
+## PHP8.x laravel nginx 配置
+
+根據實際環境做調整
+
+```shell
+server {
+    listen 80;
+    server_name _ ;
+    index index.php index.html default.html default.htm default.php;
+    root  /web/lararoot/public/;
+
+    location / {
+        index index.php index.html index.htm;
+        try_files $uri $uri/ /index.php$is_args$query_string;
+    }
+
+    #error_page   404   /404.html;
+
+    # Deny access to PHP files in specific directory
+    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
+
+    location ~ [^/]\.php(/|$){
+        try_files $uri =404;
+        fastcgi_pass  unix:/tmp/php-cgi8.1.sock;
+        fastcgi_index index.php;
+        include fastcgi.conf;
+    }
+
+    add_header 'Access-Control-Allow-Origin' '*.qieqie.cc';
+    add_header 'Access-Control-Allow-Credentials' 'true';
+    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
+
+    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
+        expires      30d;
+    }
+
+    location ~ .*\.(js|css)?$ {
+        expires      12h;
+    }
+
+    location ~ /.well-known {
+        allow all;
+    }
+
+    location ~ /\. {
+        deny all;
+    }
+
+    access_log  /var/log/a.log main1;
+    error_log  /var/log/a_err.log;
+    }
+
+```

+ 38 - 0
content/tools/nginx_tp5_conf.md

@@ -0,0 +1,38 @@
+---
+title: "Nginx_tp5_conf"
+date: 2024-01-04T14:26:56+07:00
+draft: true
+---
+
+# nginx config with thinkPHP5.x
+
+```shell
+server {
+    listen 80;
+    server_name _.com;
+    root  /home/web/tp5root/public;
+    access_log /var/log/access.log;
+    error_log  /var/log/error.log;
+    
+    location / {
+        index index.php index.html index.htm;
+        if (!-e $request_filename) {
+            rewrite ^(.*)$ /index.php?s=/$1 last;
+            break;
+        }
+    }
+
+    location ~ .*\.(html|txt|sql|log)$ {
+        return 403;
+    }
+
+    location ~ \.php$ {
+        index index.php;
+        fastcgi_pass  unix:/tmp/php-cgi.sock;
+        fastcgi_index  index.php;
+        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+        include        fastcgi_params;
+    }
+}
+
+```

+ 41 - 0
content/tools/nginx_tp6_conf.md

@@ -0,0 +1,41 @@
+---
+title: "Nginx_tp6_conf"
+date: 2024-01-04T14:27:01+07:00
+draft: false
+---
+
+# nginx config with thinkPHP6.x
+
+根據自己的系統做相應調整
+
+```shell
+
+server {
+    listen 80;
+    server_name _.com;
+    root  /home/web/tproot/public;
+    access_log /var/log/access.log;
+    error_log  /var/log/error.log;
+    
+    location / {
+        index index.php index.html index.htm;
+        if (!-e $request_filename) {
+            rewrite ^(.*)$ /index.php?s=/$1 last;
+            break;
+        }
+    }
+
+    location ~ .*\.(html|txt|sql|log)$ {
+        return 403;
+    }
+
+    location ~ \.php$ {
+        index index.php;
+        fastcgi_pass  unix:/tmp/php-cgi.sock;
+        fastcgi_index  index.php;
+        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+        include        fastcgi_params;
+    }
+}
+
+```

+ 6 - 0
content/tools/nginx_webman_conf.md

@@ -0,0 +1,6 @@
+---
+title: "Nginx_webman_conf"
+date: 2024-01-04T14:27:27+07:00
+draft: true
+---
+

+ 41 - 0
content/tools/phpgo.md

@@ -0,0 +1,41 @@
+---
+title: "Phpgo switch between multiple PHP versions"
+date: 2024-01-04T15:01:57+07:00
+draft: false
+---
+
+# PHPGO
+
+```shell
+#!/bin/env bash
+
+BIN=/usr/bin
+ver=$1
+
+# check is has parameter
+if [ $# -eq 0 ]; then
+    echo usage: phpgo version. eg. phpgo 82
+    exit 1
+fi
+
+# check if specified version exists
+if ! [ -e $BIN/php$ver ]; then
+    echo Bad PHP version
+    exit 2
+fi
+
+# delete
+rm $BIN/php
+rm $BIN/phpize
+rm $BIN/php-config
+rm $BIN/php-fpm
+
+# reconnect
+ln -sf $BIN/php$ver $BIN/php
+ln -sf $BIN/phpize$ver $BIN/phpize
+ln -sf $BIN/php-config$ver $BIN/php-config
+ln -sf $BIN/php-fpm$ver $BIN/php-fpm
+
+# output current version
+php -v
+```