在此 Automation Assembler 設計範例中,您可以從只包含最少 WordPress 資源 (例如,僅包含一個應用程式伺服器) 的雲端範本開始。

Automation Assembler 是基礎結構即代碼工具。您可以將資源拖曳至設計畫布以開始。然後,使用畫布右側的代碼編輯器填寫詳細資料。

透過代碼編輯器,您可以直接輸入、剪下和貼上代碼。如果不願意編輯代碼,您可以在畫布中選取資源,按一下代碼編輯器的內容索引標籤,然後在此輸入值。您輸入的值會出現在代碼中,如同您直接將其輸入一樣。

程序

  1. 移至設計 > 雲端範本,然後按一下新增自 > 空白畫布
  2. 將雲端範本命名為 Wordpress-BP
  3. 選取 WordPress 專案,然後按一下建立
  4. 從雲端範本設計頁面左側的資源中,將兩個非雲端機器拖曳至畫布。
    這兩個機器分別用作 WordPress 應用程式伺服器 (WebTier) 和 MySQL 資料庫伺服器 (DBTier)。
  5. 在右側,編輯機器 YAML 代碼以新增名稱、映像、類型模板和限制標籤:
    resources:
      WebTier:
        type: Cloud.Machine    
        properties:     
            name: wordpress      
            image: ubuntu      
            flavor: small      
            constraints:        
                - tag: env:dev
      DBTier:    
        type: Cloud.Machine    
        properties:      
            name: mysql      
            image: ubuntu      
            flavor: small      
            constraints:        
                - tag: env:dev
    
  6. 將非雲端網路拖曳至畫布,並編輯其代碼:
     WP-Network-Private:
        type: Cloud.Network
        properties:
            name: WP-Network-Private
            networkType: existing
    
  7. 將兩個機器連線至該網路:

    在畫布中,將游標暫留在網路區塊上,按一下並按住線與區塊接觸處的泡泡,將其拖曳至機器區塊,然後放開滑鼠。

    建立連接線時,請注意網路代碼會自動新增至編輯器中的機器。

    兩個機器和一個網路
  8. 新增使用者輸入提示。

    在某些位置,範例基礎結構已設定用於多個選項。例如:

    • 用於開發、測試和生產的雲端區域環境
    • 用於小型、中型和大型機器的類型模板對應

    您可以直接在雲端範本中設定特定選項,但更好的方法是讓使用者在部署範本時選取選項。透過提示使用者輸入,您可以建立具有多種部署方式的單一範本,而不需要建立多個硬式編碼範本。

    1. 在代碼中建立 inputs 區段,以便使用者可以在部署時選取機器大小和目標環境。定義可選取的值:
      inputs:
        env:
          type: string
          enum:
            - env:dev
            - env:prod
            - env:test
          default: env:dev
          title: Environment
          description: Target Environment
        size:
          type: string
          enum:
            - small
            - medium
            - large
          description: Size of Nodes
          title: Tier Machine Size
      
    2. 在代碼的 resources 區段中,新增 ${input.input-name} 代碼以提示使用者選取:
      resources:
        WebTier:
          type: Cloud.Machine    
          properties:     
              name: wordpress      
              image: ubuntu      
              flavor: '${input.size}'      
              constraints:        
                  - tag: '${input.env}'
              networks:
                  - network: '${resource["WP-Network-Private"].id}'
        DBTier:    
          type: Cloud.Machine    
          properties:      
              name: mysql      
              image: ubuntu      
              flavor: '${input.size}'      
              constraints:        
                  - tag: '${input.env}'
              networks:
                  - network: '${resource["WP-Network-Private"].id}'
        WP-Network-Private:
          type: Cloud.Network
          properties:
              name: WP-Network-Private
              networkType: existing
      
  9. 最後,使用下列範例增強 WebTierDBTier 代碼。WP-Network-Private 代碼不需要其他變更。
    請注意,增強功能包括對資料庫伺服器和部署時 cloudConfig 初始化指令碼的登入存取。
    元件 範例
    其他 DBTier 輸入
      username:
        type: string
        minLength: 4
        maxLength: 20
        pattern: '[a-z]+'
        title: Database Username
        description: Database Username
      userpassword:
        type: string
        pattern: '[a-z0-9A-Z@#$]+'
        encrypted: true
        title: Database Password
        description: Database Password
    
    DBTier 資源
      DBTier:
        type: Cloud.Machine
        properties:
          name: mysql
          image: ubuntu
          flavor: '${input.size}'
          constraints:
            - tag: '${input.env}'
          networks:
            - network: '${resource["WP-Network-Private"].id}'
              assignPublicIpAddress: true
          remoteAccess:
            authentication: usernamePassword
            username: '${input.username}'
            password: '${input.userpassword}'
          cloudConfig: |
            #cloud-config
            repo_update: true
            repo_upgrade: all
            packages:
            - mysql-server
            runcmd:
            - sed -e '/bind-address/ s/^#*/#/' -i /etc/mysql/mysql.conf.d/mysqld.cnf
            - service mysql restart
            - mysql -e "CREATE USER 'root'@'%' IDENTIFIED BY 'mysqlpassword';" 
            - mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';"
            - mysql -e "FLUSH PRIVILEGES;"
          attachedDisks: []
    
    WebTier 資源
      WebTier:
        type: Cloud.Machine
        properties:
          name: wordpress
          image: ubuntu
          flavor: '${input.size}'
          constraints:
            - tag: '${input.env}'
          networks:
            - network: '${resource["WP-Network-Private"].id}'
              assignPublicIpAddress: true
          cloudConfig: |
            #cloud-config
            repo_update: true
            repo_upgrade: all
            packages:
            - apache2
            - php
            - php-mysql
            - libapache2-mod-php
            - mysql-client
            - gcc
            - make
            - autoconf
            - libc-dev
            - pkg-config
            - libmcrypt-dev
            - php-pear
            - php-dev
            runcmd:
            - mkdir -p /var/www/html/mywordpresssite && cd /var/www/html && wget https://wordpress.org/latest.tar.gz && tar -xzf /var/www/html/latest.tar.gz -C /var/www/html/mywordpresssite --strip-components 1
            - i=0; while [ $i -le 10 ]; do mysql --connect-timeout=3 -h ${DBTier.networks[0].address} -u root -pmysqlpassword -e "SHOW STATUS;" && break || sleep 15; i=$((i+1)); done
            - mysql -u root -pmysqlpassword -h ${DBTier.networks[0].address} -e "create database wordpress_blog;"
            - mv /var/www/html/mywordpresssite/wp-config-sample.php /var/www/html/mywordpresssite/wp-config.php
            - pecl channel-update pecl.php.net
            - pecl update-channels
            - pecl install mcrypt
            - sed -i -e s/"define( 'DB_NAME', 'database_name_here' );"/"define( 'DB_NAME', 'wordpress_blog' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_USER', 'username_here' );"/"define( 'DB_USER', 'root' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_PASSWORD', 'password_here' );"/"define( 'DB_PASSWORD', 'mysqlpassword' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_HOST', 'localhost' );"/"define( 'DB_HOST', '${DBTier.networks[0].address}' );"/ /var/www/html/mywordpresssite/wp-config.php
            - sed -i '950i extension=mcrypt.so' /etc/php/7.4/apache2/php.ini
            - service apache2 reload
    

範例: 基本雲端範本代碼已完成的範例

formatVersion: 1
inputs: 
  env:
    type: string
    enum:
      - env:dev
      - env:prod
      - env:test
    default: env:dev
    title: Environment
    description: Target Environment
  size:
    type: string
    enum:
      - small
      - medium
      - large
    description: Size of Nodes
    title: Tier Machine Size
  username:
    type: string
    minLength: 4
    maxLength: 20
    pattern: '[a-z]+'
    title: Database Username
    description: Database Username
  userpassword:
    type: string
    pattern: '[a-z0-9A-Z@#$]+'
    encrypted: true
    title: Database Password
    description: Database Password
resources:
  WebTier:
    type: Cloud.Machine
    properties:
      name: wordpress
      image: ubuntu
      flavor: '${input.size}'
      constraints:
        - tag: '${input.env}'
      networks:
        - network: '${resource["WP-Network-Private"].id}'
          assignPublicIpAddress: true
      cloudConfig: |
        #cloud-config
        repo_update: true
        repo_upgrade: all
        packages:
        - apache2
        - php
        - php-mysql
        - libapache2-mod-php
        - mysql-client
        - gcc
        - make
        - autoconf
        - libc-dev
        - pkg-config
        - libmcrypt-dev
        - php-pear
        - php-dev
        runcmd:
        - mkdir -p /var/www/html/mywordpresssite && cd /var/www/html && wget https://wordpress.org/latest.tar.gz && tar -xzf /var/www/html/latest.tar.gz -C /var/www/html/mywordpresssite --strip-components 1
        - i=0; while [ $i -le 10 ]; do mysql --connect-timeout=3 -h ${DBTier.networks[0].address} -u root -pmysqlpassword -e "SHOW STATUS;" && break || sleep 15; i=$((i+1)); done
        - mysql -u root -pmysqlpassword -h ${DBTier.networks[0].address} -e "create database wordpress_blog;"
        - mv /var/www/html/mywordpresssite/wp-config-sample.php /var/www/html/mywordpresssite/wp-config.php
        - pecl channel-update pecl.php.net
        - pecl update-channels
        - pecl install mcrypt
        - sed -i -e s/"define( 'DB_NAME', 'database_name_here' );"/"define( 'DB_NAME', 'wordpress_blog' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_USER', 'username_here' );"/"define( 'DB_USER', 'root' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_PASSWORD', 'password_here' );"/"define( 'DB_PASSWORD', 'mysqlpassword' );"/ /var/www/html/mywordpresssite/wp-config.php && sed -i -e s/"define( 'DB_HOST', 'localhost' );"/"define( 'DB_HOST', '${DBTier.networks[0].address}' );"/ /var/www/html/mywordpresssite/wp-config.php
        - sed -i '950i extension=mcrypt.so' /etc/php/7.4/apache2/php.ini
        - service apache2 reload
  DBTier:
    type: Cloud.Machine
    properties:
      name: mysql
      image: ubuntu
      flavor: '${input.size}'
      constraints:
        - tag: '${input.env}'
      networks:
        - network: '${resource["WP-Network-Private"].id}'
          assignPublicIpAddress: true
      remoteAccess:
        authentication: usernamePassword
        username: '${input.username}'
        password: '${input.userpassword}'
      cloudConfig: |
        #cloud-config
        repo_update: true
        repo_upgrade: all
        packages:
        - mysql-server
        runcmd:
        - sed -e '/bind-address/ s/^#*/#/' -i /etc/mysql/mysql.conf.d/mysqld.cnf
        - service mysql restart
        - mysql -e "CREATE USER 'root'@'%' IDENTIFIED BY 'mysqlpassword';" 
        - mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';"
        - mysql -e "FLUSH PRIVILEGES;"
      attachedDisks: []
  WP-Network-Private:
    type: Cloud.Network
    properties:
      name: WP-Network-Private
      networkType: existing

下一步

透過檢查語法並進行部署來測試雲端範本。