heat_template_version: 2014-10-16

parameters:
  name: { type: string }
  wc_notify: { type: string }
  subnet: { type: string }
  network: { type: string }
  security_group: { type: string }
  key_name: { type: string }
  flavor: { type: string }
  image: { type: string }
  db_name: { type: string }
  db_username: { type: string }
  db_password: { type: string }
  db_root_password: { type: string }

resources:
  wordpress_instance:
    type: OS::Nova::Server
    properties:
      name: { get_param: name }
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks:
        - port: {get_resource: port}
      user_data_format: RAW
      user_data:
        str_replace:
          template: |
            #!/bin/bash -v
            sudo yum -y install mariadb mariadb-server httpd wordpress curl
            sudo touch /var/log/mariadb/mariadb.log
            sudo chown mysql.mysql /var/log/mariadb/mariadb.log
            sudo systemctl start mariadb.service
            # Setup MySQL root password and create a user
            sudo 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
            sudo sed -i "/Deny from All/d" /etc/httpd/conf.d/wordpress.conf
            sudo sed -i "s/Require local/Require all granted/" /etc/httpd/conf.d/wordpress.conf
            sudo sed -i s/database_name_here/db_name/ /etc/wordpress/wp-config.php
            sudo sed -i s/username_here/db_user/ /etc/wordpress/wp-config.php
            sudo sed -i s/password_here/db_password/ /etc/wordpress/wp-config.php
            sudo systemctl start httpd.service
            IP=$(ip r get 8.8.8.8 | grep src | awk '{print $7}')
            curl --data 'user_name=admin&password=123&password2=123&admin_email=asd@asd.com' http://$IP/wordpress/wp-admin/install.php?step=2
            mkfifo /tmp/data
            (for i in $(seq 1000); do
             echo -n "1,$i,$i,page,"
             head -c 100000 /dev/urandom | base64 -w 0
             echo
             done
            ) > /tmp/data &
            mysql -u root --password=db_rootpassword wordpress -e 'LOAD DATA LOCAL INFILE "/tmp/data" INTO TABLE wp_posts FIELDS TERMINATED BY "," (post_author,post_title,post_name,post_type,post_content);'
            sudo sh -c 'echo "172.16.0.6 mos80-ssl.fuel.local" >> /etc/hosts'
            wc_notify --insecure --data-binary '{"status": "SUCCESS"}'
          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 }
            wc_notify: { get_param: wc_notify }

  port:
    type: OS::Neutron::Port
    properties:
      fixed_ips:
        - subnet: {get_param: subnet}
      network: {get_param: network}
      replacement_policy: AUTO
      security_groups:
        - {get_param: security_group}

outputs:
  ip:
    value: { get_attr: ['wordpress_instance', 'networks'] }
