在此 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

下一步做什么

通过检查语法并部署云模板来测试云模板。