做為開發人員,您可以從只包含最少 WordPress 資源 (例如,僅包含一個應用程式伺服器) 的 Cloud Assembly 藍圖開始。

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

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

必要條件

自行熟悉您的基礎結構。此處顯示的範例使用使用案例第一部分中的基礎結構值,但您可以將其取代為自己的基礎結構值。

程序

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

    在與網路區塊接觸的行處按住滑鼠按鈕,拖曳到機器區塊,然後放開滑鼠。

    兩個機器和一個網路

    在編輯器中,您會發現網路代碼將新增到兩個機器:

    resources:
      DBTier:    
        type: Cloud.Machine    
        properties:      
            name: mysql      
            image: 'ubuntu-16'      
            flavor: 'small'      
            constraints:        
                -   tag: env:dev
            networks: - network: '${resource["WP-Network-Private"].id}'
      WebTier:
        type: Cloud.Machine    
        properties:     
            name: wordpress      
            image: 'ubuntu-16'      
            flavor: 'small'      
            constraints:        
                - tag: env:dev
            networks: - network: '${resource["WP-Network-Private"].id}'
      
  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:
        DBTier:    
          type: Cloud.Machine    
          properties:      
              name: mysql      
              image: 'ubuntu-16'      
              flavor: '${input.size}'      
              constraints:        
                  -   tag: '${input.env}'
              networks:
                  - network: '${resource["WP-Network-Private"].id}'
        WebTier:
          type: Cloud.Machine    
          properties:     
              name: wordpress      
              image: 'ubuntu-16'      
              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
              constraints:
                  - tag: 'type:isolated-net'
                  - tag: '${input.env}'
  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
      databaseDiskSize:
        type: number
        default: 4
        maximum: 10
        title: MySQL Data Disk Size
        description: Database Disk Size
    
    DBTier 資源
      DBTier:
        type: Cloud.Machine
        properties:
          name: mysql
          image: ubuntu-16
          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 "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysqlpassword';"
             - mysql -e "FLUSH PRIVILEGES;"
          attachedDisks: []
    WebTier 資源
      WebTier:
        type: Cloud.Machine
        properties:
          name: wordpress
          flavor: '${input.size}'
          image: ubuntu-16
          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
             - php-mcrypt
             - mysql-client
    
            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 5 ]; 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
             - 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
             - service apache2 reload
    

範例: 基本藍圖代碼已完成的範例

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
  databaseDiskSize:
    type: number
    default: 4
    maximum: 10
    title: MySQL Data Disk Size
    description: Database Disk Size
resources:
  DBTier:
    type: Cloud.Machine
    properties:
      name: mysql
      image: ubuntu-16
      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 "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysqlpassword';"
         - mysql -e "FLUSH PRIVILEGES;"
      attachedDisks: []
  WebTier:
    type: Cloud.Machine
    properties:
      name: wordpress
      flavor: '${input.size}'
      image: ubuntu-16
      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
         - php-mcrypt
         - mysql-client

        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 5 ]; 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
         - 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
         - service apache2 reload
  WP-Network-Private:
    type: Cloud.Network
    properties:
      name: WP-Network-Private
      networkType: existing
      constraints:
        - tag: 'type:isolated-net'
        - tag: '${input.env}'

後續步驟

透過檢查語法並進行部署來測試藍圖。