Tuesday, May 28, 2019

WCM Week 15 實作

Ubuntu Server Setup

利用 Virtualbox 建立虛擬 Ubuntu 18.04 網際內容管理系統伺服主機:

1. 安裝最新版的 Virtualbox
2. 建立 Ubuntu 18.04 虛擬主機
3. 以網路卡橋接啟動 Ubuntu 伺服器
4. 以 sudo apt install nginx 安裝 nginx WWW 伺服器
5. 以 ifconfig 查 Ubuntu 伺服器的 IP, 測試 nginx 伺服器是否可以連線
6. 以 Windows 10  下的 Filezilla client, 將編輯好的 nginx 設定檔案送到 Ubuntu
7. 利用 Ubuntu 指令將設定檔案搬至設定位置後, 以 /etc/init.d/nginx restart 重新啟動 nginx
8. 在 Ubuntu 中以 sudo apt install uwsgi 安裝 uwsgi
9. 在 Windows 10 編輯 uwsgi 與 nginx 所需的設定檔案, 完成後以 filezilla 送至 Ubuntu.
10. 將 CMSimfly 程式碼送到 Ubuntu 伺服器中對應目錄後.
11. 在 Ubuntu 建立 cmsimfly 服務, 並設定隨機啟動.
12. 完成後, 在 Windows 10 以瀏覽器連線至 CMSimfly, 確認動態網頁與靜態網頁可以在虛擬主機中正常啟動.
13. 以上相關網際內容管理在機械工程相關應用可參考: https://www.driveworks.co.uk 或 https://robotbenchmark.net/

其他參考資料:

http://help.solidworks.com/2016/english/api/sldworksapiprogguide/Welcome.htm
https://github.com/OmniaGit/odooplm
https://github.com/vskrachkov/sw2014
https://github.com/cmuphyscomp/physcomp-solidworks
https://github.com/FuntechBlins/SwApiExamplesCs
http://joshuaredstone.blogspot.com/2015/02/solidworks-macros-via-python.html

Ubuntu 18.04 /etc/netplan/50-cloud-init.yaml 檔案參考內容:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            dhcp4: false 
            dhcp6: false 
            addresses: 
               #- 192.168.1.13/24
            - 2001:288:6004::1/64
            gateway4: 192.168.1.1
            gateway6: 2001:288:6004::254
            nameservers:
                addresses:
                - 140.130.1.2
                - 168.95.1.1
                - 2001:288:6004:1::2
                - 2001:b000:168::1
    version: 2

/etc/nginx/sites-available/default 參考檔案:

server {
    listen 80;
    listen [::]:80;
    root /home/kmol2019/newcms;
    index index.html;

    location /static {
        alias /home/kmol2019/newcms/cmsimde/static/;
    }

    location /downloads {
        alias /home/kmol2019/newcms/downloads/;
    }

    location /images {
        alias /home/kmol2019/newcms/images/;
    }

    location / {
            try_files $uri $uri/ =404;
    } 
    
    location /blog {
        alias /home/kmol2019/cmsimfly/blog/;
    }

    location /reveal {
        alias /home/kmol2019/cmsimfly/reveal/;
    }
}

server {
    listen 443 ssl;
    #listen [::]:443 ssl ipv6only=on;
 
    location /static {
        alias /home/kmol2019/cmsimfly/static/;
    }
 
    location / {
        include uwsgi_params;
        uwsgi_pass  127.0.0.1:8080;
    }
 
    #server_name ipv4_ip; 
    #ssl on;
    ssl_certificate /etc/stunnel/localhost.crt;
    ssl_certificate_key /etc/stunnel/localhost.key;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;
    try_files $uri $uri/ =404;
}
 
server {
    #listen 89 default_server;
    #listen [::]:89 default_server ipv6only=on;
 
    listen 8943 ssl;
    #listen [::]:8943 ssl ipv6only=on;
 
    location /static {
        alias /home/kmol2019/cmsimfly2/static/;
    }
 
    location / {
        include uwsgi_params;
        uwsgi_pass  127.0.0.1:8082;
    }
 
    #server_name ipv4_ip;
    #ssl on;
    ssl_certificate /etc/stunnel/localhost.crt;
    ssl_certificate_key /etc/stunnel/localhost.key;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;
    try_files $uri $uri/ =404;
}

server {
    #listen 88 default_server;
    #listen [::]:88 default_server ipv6only=on;
 
    listen 8843 ssl;
    #listen [::]:8843 ssl ipv6only=on;
 
    location /static {
        alias /home/kmol2019/newcms/cmsimde/static/;
    }
 
    location / {
        include uwsgi_params;
        uwsgi_pass  127.0.0.1:8081;
    }
 
    #server_name ipv4_ip;
    #ssl on;
    ssl_certificate /etc/stunnel/localhost.crt;
    ssl_certificate_key /etc/stunnel/localhost.key;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;
    try_files $uri $uri/ =404;
}

uwsgi.ini 參考檔案:

[uwsgi]
socket = 127.0.0.1:8080
uid = kmol2019
gid = kmol2019
plugins-dir = /usr/lib/uwsgi/plugins/
plugin = python3
master = true
logto = /var/log/uwsgi/emperor.log
logfile-chown = kmol2019:kmol2019
processes = 4
threads = 2
chdir = /home/kmol2019/cmsimfly
wsgi-file = /home/kmol2019/cmsimfly/wsgi.py
網際介面與資料庫伺服器採用 Ubuntu Server, Web Based Solid Modeling Server 採用 Windows 10, 以下列 Python 程式透過 COM 建立 SolidWorks 主機伺服器上的零件:
# 在 windows 10 以 python -m pip install pywin32
import win32com.client
import pythoncom


# 透過 win32com.client 建立 swApp
swApp = win32com.client.DispatchEx("SldWorks.Application")
swApp.Visible = 1

# 使用已經開啟的檔案或開啟新檔案
if swApp.ActiveDoc:
    swDoc = swApp.ActiveDoc
else:
    swApp.NewDocument(
        TemplateName="C:\\ProgramData\\SolidWorks\\SolidWorks 2016\\templates\\gost-part.prtdot",
        PaperSize=0,
        Width=0,
        Height=0,
    )
    swApp.ActivateDoc2(
        Name="name1", 
        Silent=False, 
        Errors=Exception
    )
    swDoc = swApp.ActiveDoc

swDoc.Extension.SelectByID2(
    "name2",  
    "PLANE",
    0,  # X
    0,  # Y
    0,  # Z
    False, 
    0, 
    win32com.client.VARIANT(pythoncom.VT_DISPATCH, None), 
    0, 
)

swDoc.SketchManager.InsertSketch(True)

swDoc.SketchManager.CreateCornerRectangle(
    0,
    0,
    0,
    0.067899689762150967,
    -0.035683453981385727,
    0
)

swDoc.FeatureManager.FeatureExtrusion2(
    True,
    False,
    False,
    0,
    0,
    0.0115,
    0.01,
    False,
    False,
    False,
    False,
    0.017453292519943334,
    0.017453292519943334,
    False,
    False,
    False,
    False,
    True,
    True,
    True,
    0,
    0,
    False
)

python3 環境準備:

安裝 pip3:

    sudo apt install python3-pip

安裝 CMSimfly 所需模組:

    sudo pip3 install flask bs4 lxml

uwsgi 環境準備:

    sudo apt install uwsgi uwsgi-plugin-python3

    sudo pip3 install uwsgi

編輯的設定檔案為 uwsgi_ini 目錄中的各個 .ini 設定檔案.

nginx 環境準備:

    sudo apt install nginx

編輯的設定檔案為 /etc/nginx/sites-available/default 檔案.

CMSimfly 的設定:

    init.py 中必須將 uwsgi = False 改為 True, 因為準備採用 uwsgi 模式啟動 CMSimfly 

apt proxy 設定檔案:

    /etc/apt/apt.conf.d/90curtin-aptproxy

啟動 uwsgi 指令, 將會逐一啟動 wsgi_ini 目錄中個別 .ini 檔案:

    sudo /usr/bin/uwsgi --emperor /home/kmol2019/wsgi_ini

最後則設定 Ubuntu 系統服務, 用來啟動 uwsgi:

/etc/systemd/system 的 cmsimfly.service 服務啟動檔案內容:

[Unit]
Description=uWSGI to serve CMSimfly
After=network.target

[Service]
User=kmol2019
Group=kmol2019
WorkingDirectory=/home/kmol2019/uwsgi_ini
ExecStart=/usr/local/bin/uwsgi --emperor /home/kmol2019/uwsgi_ini

[Install]
WantedBy=multi-user.target

接著將 cmsimfly 服務設為隨系統開機啟動:

    sudo systemctl enable cmsimfly

若要取消 cmsimfly 服務隨系統開機啟動:

    sudo systemctl disable cmsimfly

手動啟動 cmsimfly.service 服務

    sudo systemctl start cmsimfly

手動停止 cmsimfly.service 服務

    sudo systemctl stop cmsimfly

最後, 在 Ubuntu 虛擬主機中設定 Fossil SCM:

安裝 fossil:

    sudo apt install fossil

查 fossil 版本:

    fossil version

因為採用 sudo apt install fossil 的版本為 2.5 版, 希望採用目前 (2019.05.28) 的最新版本 2.8, 因此:

直接在 Windows 10 下載 https://fossil-scm.org/home/uv/fossil-linux-x64-2.8.tar.gz, 解開壓縮檔案後, 將 fossil 送到 /homt/kmol2019/ 目錄下, 再使用指令, 將檔案複製到 /usr/bin/ 目錄下, 在 Ubuntu 要查詢 fossil 可執行檔案的位置, 使用 whereis fossil.

    sudo cp fossil /usr/bin/

複製後, 再用 fossil version 查驗版本, 確認為最新的 2.8 版.

接著在 /home/kmol2019 目錄下建立 repository 目錄, 並且利用 fossil 起始一個 kmol2019.fossil 倉儲壓縮檔案.

    mkdir repository
    cd repository
    fossil init kmol2019.fossil

完成後, 紀錄 kmol2019.fossil 的管理密碼, 以便啟動 Fossil SCM 伺服器時, 利用 kmol2019 當作帳號, 而以記錄的管理者密碼登入進行設定.

接下來為了要在 https 的模式下使用 Fossil SCM 伺服器, 因此必須安裝配置 stunnel:

    sudo apt install stunnel

修改 /etc/environment, 加入:

    HTTPS=on

修改 /etc/default/stunnel4, 修改 ENABLED=1

假如此時 /etc/stunnel 目錄下尚無 localhost.crt 與 localhost.key, 可以利用下列指令建立:

建立 localhost.key 與 localhost.crt:

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt

接著設定 /etc/stunnel/stunnel.conf 如下:

[https]
accept = kmol2019:5443
# 只先啟用 ipv4
#accept = :::443
cert = /etc/stunnel/localhost.crt
key = /etc/stunnel/localhost.key
exec = /usr/bin/fossil
execargs = /usr/bin/fossil http /home/kmol2019/repository/ --https --nojail --notfound kmol2019

表示利用 stunnel 執行 fossil 指令, 並且透過 http 協定啟動 位於 /home/kmol2019/repository 目錄下的倉儲壓縮檔案, 且附加採用 https 與 nojail 模式啟動.

其中 --nojail 目的在 drop the root privilege but do not enter the chroot jail, 其後的 --notfound 表示若沒有特別在 URL 中列出所要擷取的 .fossil 檔案 (指位於 /home/kmol2019/repository/ 目錄下), 則採用 kmol2019.fossil

重新啟用 stunnel:

    sudo /etc/init.d/stunnel4 restart

啟動後的 Fossil SCM 位於 port 5443, 且將 kmol2019 管理者密碼設為 kmol2019.

W15 網際內容管理系統實作結果:

W15 實作所完成的虛擬主機 vmdk 壓縮檔案.

W15 實作所完成的 Solidworks 2016 虛擬主機壓縮檔案. (登入 @gm.nfu.edu.tw 後才能下載)


參考資料:

https://github.com/mhammond/pywin32
http://timgolden.me.uk/pywin32-docs/contents.html
https://sourceforge.net/projects/pywin32/files/
http://timgolden.me.uk/python/win32_how_do_i.html
http://timgolden.me.uk/python/winsys/index.html
https://win32com.goermezer.de/category/cad-systems/solidworks
https://blog.csdn.net/qq_41185868/article/details/80467713
https://win32com.goermezer.de/category/cad-systems
https://forum.freecadweb.org/viewtopic.php?t=21810

No comments:

Post a Comment

NX12

NX 12 Help https://docs.plm.automation.siemens.com/tdoc/nx/12/nx_help Python related https://docs.plm.automation.siemens.com/tdoc/nx/...