Tuesday, June 12, 2018

Heat Template to install wordpress

We can automatically install wordpress during instance creation on openstack. We only need to create the heat template and call it from orchestration menu. Below is the template example to install wordpress.

Create file WordPress.yaml


heat_template_version: 2013-05-23

description: >
  Heat WordPress template.WordPress is web software you can use to create
  a beautiful website or blog.
  This template installs a single-instance WordPress deployment using a local
  MySQL database to store the data.

parameters:

  key_name:
    type: string
    description: Name of a KeyPair to enable SSH access to the instance
    default: demo
  instance_type:
    type: string
    description: Instance type for WordPress server
    default: c1.m1024.d10
    constraints:
      - allowed_values: [c1.m512.d1, c1.m1024.d1, c1.m1024.d10, m1.large]
        description: instance_type must be one of m1.small, m1.medium or m1.large
  volume_size:
    type: number
    description: Size of volume to attach to instance
    default: 10
    constraints:
      - range: { min: 1, max: 10 }
  num_volumes:
    type: number
    description: Number of volumes to attach to instance
    default: 1
    constraints:
      - range: { min: 1, max: 10 }
  image_id:
    type: string
    description: ID of the image to use for the WordPress server
    default: centos7
    constraints:
      - allowed_values: [ centos7 ]
        description: >
          Image ID must be centos7
  db_name:
    type: string
    description: WordPress database name
    default: wordpress
    constraints:
      - length: { min: 1, max: 64 }
        description: db_name must be between 1 and 64 characters
      - allowed_pattern: '[a-zA-Z][a-zA-Z0-9]*'
        description: >
          db_name must begin with a letter and contain only alphanumeric
          characters
  db_username:
    type: string
    description: The WordPress database admin account username
    default: admin
    hidden: true
    constraints:
      - length: { min: 1, max: 16 }
        description: db_username must be between 1 and 64 characters
      - allowed_pattern: '[a-zA-Z][a-zA-Z0-9]*'
        description: >
          db_username must begin with a letter and contain only alphanumeric
          characters
  db_password:
    type: string
    description: The WordPress database admin account password
    default: admin
    hidden: true
    constraints:
      - length: { min: 1, max: 41 }
        description: db_username must be between 1 and 64 characters
      - allowed_pattern: '[a-zA-Z0-9]*'
        description: db_password must contain only alphanumeric characters
  db_root_password:
    type: string
    description: Root password for MySQL
    default: admin
    hidden: true
    constraints:
      - length: { min: 1, max: 41 }
        description: db_username must be between 1 and 64 characters
      - allowed_pattern: '[a-zA-Z0-9]*'
        description: db_password must contain only alphanumeric characters

resources:
  wordpress_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: { get_param: instance_type }
      key_name: { get_param: key_name }
      user_data_format: SOFTWARE_CONFIG
      user_data:
       get_resource: cloud_init_userdata_app1

  group_of_volumes:
    type: OS::Heat::ResourceGroup
    properties:
      count: { get_param: num_volumes }
      resource_def:
        type: volume_with_attachment.yaml
        properties:
          instance_id: { get_resource: wordpress_instance }
          volume_size: { get_param: volume_size }

  cloud_init_userdata_app1:
    type: OS::Heat::MultipartMime
    properties:
      parts:
      - config: {get_resource: cloud_config_app1}

  cloud_config_app1:
    type: OS::Heat::CloudConfig
    properties:
      cloud_config:

       write_files:
        - path: /tmp/cloud_init.sh
          permissions: "0755"
          content:
             str_replace:
              params:
               db_rootpassword: { get_param: db_root_password }
               db_name: { get_param: db_name }
               db_user: { get_param: db_username }
               db_password: { get_param: db_password }
              template: |
                #!/bin/bash -v

                yum -y install mariadb-server httpd wget php-gd php php-mysql
                systemctl enable mariadb.service
                systemctl enable httpd.service
                systemctl start mariadb.service
                systemctl start httpd.service

                # firewall-cmd --add-service=http
                # firewall-cmd --permanent --add-service=http
                cd ~
                wget http://wordpress.org/latest.tar.gz
                tar xzvf latest.tar.gz
                sudo rsync -avP ~/wordpress/ /var/www/html/
                mkdir /var/www/html/wp-content/uploads
                cd /var/www/html
                cp wp-config-sample.php wp-config.php
                sudo chown -R apache:apache /var/www/html/*

                # Setup MySQL root password and create a user
                mysqladmin -u root password db_rootpassword
                cat << EOF | mysql -u root --password=db_rootpassword
                CREATE DATABASE db_name;
                GRANT ALL PRIVILEGES ON db_name.* TO "db_user"@"localhost"
                IDENTIFIED BY "db_password";
                FLUSH PRIVILEGES;
                EXIT
                EOF

                # sed -i "/Deny from All/d" /etc/httpd/conf.d/wordpress.conf
                # sed -i "s/Require local/Require all granted/" /etc/httpd/conf.d/wordpress.conf
                sed -i s/database_name_here/db_name/ /var/www/html/wp-config.php
                sed -i s/username_here/db_user/ /var/www/html/wp-config.php
                sed -i s/password_here/db_password/ /var/www/html/wp-config.php

                systemctl restart httpd.service
       runcmd:
        - [ sh, -c, "/tmp/cloud_init.sh" ]
       final_message: "cloud-init has been run, after $UPTIME seconds"

outputs:
  WebsiteURL:
    description: URL for Wordpress wiki
    value:
      str_replace:
        template: http://host/wordpress
        params:
          host: { get_attr: [wordpress_instance, first_address] }

No comments: