diff --git a/scripts/ansiblePlaybook/wordpress/group_vars/all b/scripts/ansiblePlaybook/wordpress/group_vars/all new file mode 100644 index 0000000..b699c3f --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/group_vars/all @@ -0,0 +1,52 @@ +# Below variables values are updated at runtime +wp_db_server_name: wordpress +wp_db_user: wordpress +wp_db_password: password +wp_db_name: wordpress +user_name: azusername +vm_password: password +vm_ip: IP +lb_dns_name: domain +site_domain_name: domain + +# Password will be auto generated +wp_admin_password: +wp_db_user_pass: + +# Static values +wordpress_dir: wordpress +wp_title: LAMP-WordPress +wp_admin_user: admin +wp_admin_email: test@microsoft.com +wp_db_user_id: wordpress + +# Disable All Updates +# By default automatic updates are enabled, set this value to true to disable all automatic updates +auto_up_disable: true + +#Define Core Update Level +#true = Development, minor, and major updates are all enabled +#false = Development, minor, and major updates are all disabled +#minor = Minor updates are enabled, development, and major updates are disabled +core_update_level: true + +# WordPress Latest Version +#if you want to update wordpress to latest version then update sha1sum value from https://wordpress.org/download/releases/ +wp_version: 5.4.1 +woocommerce_version: 4.0.1 +wp_sha1sum: 9800c231828eb5cd76ba0b8aa6c1a74dfca2daff + +#default_paths +default_permission: www-data +woocommerce_script_name: woocommerce.sh +replication_name: wordpress_replication.sh + +webroot: /var/www/html +wordpress_path: /tmp/wordpress + +wordpress_url: https://wordpress.org/wordpress +wp_salt_path: https://api.wordpress.org/secret-key/1.1/salt/ +wp_cli_url: https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar +woocommerce_plugin_url: https://downloads.wordpress.org/plugin/woocommerce. +woocommerce_url: https://raw.githubusercontent.com/Azure/LAMP/master/scripts/woocommerce.sh +replication_url: https://raw.githubusercontent.com/Azure/LAMP/master/scripts/wordpress_replication.sh \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/playbook.yml b/scripts/ansiblePlaybook/wordpress/playbook.yml new file mode 100644 index 0000000..4265730 --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/playbook.yml @@ -0,0 +1,10 @@ +- hosts: 127.0.0.1 + connection: local + roles: + - sshkeyconfig + +- hosts: webservers + roles: + - wordpress + - woocommerce + - replication \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/roles/replication/tasks/main.yml b/scripts/ansiblePlaybook/wordpress/roles/replication/tasks/main.yml new file mode 100644 index 0000000..417eadb --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/roles/replication/tasks/main.yml @@ -0,0 +1,25 @@ +--- +- + get_url: + dest: /home/{{user_name | quote }} + group: "{{ user_name | quote }}" + mode: 493 + owner: "{{ user_name | quote }}" + url: "{{replication_url}}" + name: "Download replica script using get_url" +- + name: "Run a replica script with arguments (using 'cmd' parameter)" + shell: /home/{{ user_name | quote }}/{{replication_name}} {{ site_domain_name | quote }} {{ webroot | quote }} + args: + executable: /bin/bash + +- name: Create wordpress.txt file for user + copy: + dest: "/home/{{ user_name | quote }}/wordpress.txt" + content: | + WordPress Site Details: + uid= {{ wp_admin_user | quote }} + pwd= {{ wp_admin_password | quote }} + WordPress DB User Details: + db_id= {{ wp_db_user | quote }} + db_pwd= {{ wp_db_user_pass | quote }} \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/roles/sshkeyconfig/tasks/main.yml b/scripts/ansiblePlaybook/wordpress/roles/sshkeyconfig/tasks/main.yml new file mode 100644 index 0000000..886e9f5 --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/roles/sshkeyconfig/tasks/main.yml @@ -0,0 +1,23 @@ +--- + - name: "upgrade" + apt: + upgrade: true + become: true + + - name: "install sshpass" + #command: apt-get install sshpass + apt: + name: sshpass + state: latest + become: yes + + - name: “generate sshkey” + openssh_keypair: + path: /home/{{ user_name | quote }}/.ssh/id_rsa + group: "{{ user_name | quote }}" + owner: "{{ user_name | quote }}" + size: 4096 + become: yes + + - name: "copy file to remote host" + command: sshpass -p {{ vm_password | quote }} ssh-copy-id -i /home/{{ user_name | quote }}/.ssh/id_rsa.pub -o StrictHostKeyChecking=no {{user_name | quote }}@{{ vm_ip | quote }} \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/roles/woocommerce/tasks/main.yml b/scripts/ansiblePlaybook/wordpress/roles/woocommerce/tasks/main.yml new file mode 100644 index 0000000..36ddfa8 --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/roles/woocommerce/tasks/main.yml @@ -0,0 +1,18 @@ +--- +- + get_url: + dest: /home/{{user_name | quote }} + group: "{{user_name | quote }}" + mode: 493 + owner: "{{user_name | quote }}" + url: "{{woocommerce_url}}" + name: "Download woocommerce script using get_url" +- + name: "Run a woocommerce script with arguments (using 'cmd' parameter)" + shell: /home/{{user_name | quote }}/{{woocommerce_script_name}} {{ woocommerce_plugin_url | quote }}{{ woocommerce_version | quote }}.zip {{ user_name | quote }} {{ woocommerce_version | quote }} {{ webroot | quote }} + args: + executable: /bin/bash + +- + name: "Activate woocommerce with wp cli command" + command: "wp plugin activate woocommerce --path='{{webroot}}/{{wordpress_dir}}'" \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/roles/wordpress/tasks/main.yml b/scripts/ansiblePlaybook/wordpress/roles/wordpress/tasks/main.yml new file mode 100644 index 0000000..bbd8b8b --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/roles/wordpress/tasks/main.yml @@ -0,0 +1,87 @@ +--- +- name: Create webroot + file: + state: directory + path: "{{webroot}}" + owner: "{{default_permission}}" + group: "{{default_permission}}" + mode: 0755 + become: yes + +- name: Check if WordPress directory exists in "{{webroot}}" + stat: path="{{webroot}}/{{wordpress_dir}}" + register: check_path + +- name: Download WordPress + get_url: + url: "{{wordpress_url}}-{{ wp_version }}.tar.gz" + dest: "{{wordpress_path}}-{{ wp_version }}.tar.gz" + checksum: "sha1:{{ wp_sha1sum }}" + become: yes + when: not check_path.stat.exists + +- name: Extract WordPress + unarchive: + src: "{{wordpress_path}}-{{ wp_version }}.tar.gz" + dest: /tmp + owner: "{{default_permission}}" + group: "{{default_permission}}" + copy: no + become: yes + when: not check_path.stat.exists + +- + name: create a database + command: mysql -h {{wp_db_server_name}} -u {{wp_db_user}} -p{{wp_db_password}} -e "CREATE DATABASE {{wp_db_name}} CHARACTER SET utf8;" + register: output + +- + name: grant user permission to database + command: mysql -h {{wp_db_server_name}} -u {{wp_db_user}} -p{{wp_db_password}} -e "GRANT ALL ON {{wp_db_name}}.* TO {{wp_db_user_id}} IDENTIFIED BY '{{wp_db_user_pass}}';" + register: outputpremission + +- name: Move WordPress install files + command: "mv {{wordpress_path}} {{webroot}}/{{wordpress_dir}}" + become: yes + when: not check_path.stat.exists + +- name: Fetch random salts for WordPress config + local_action: "command curl {{wp_salt_path}}" + register: "wp_salt" + become: no + +- name: Add wp-config + template: "src=wp-config.php dest={{webroot}}/{{wordpress_dir}}/wp-config.php" + become: yes + +- name: Update WordPress config file + + lineinfile: + dest: "{{webroot}}/{{wordpress_dir}}/wp-config.php" + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + with_items: + - {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{wp_db_name}}');"} + - {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{wp_db_user}}');"} + - {'regexp': "define\\('DB_PASSWORD', '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{wp_db_password}}');"} + become: yes + no_log: true + +- name: download wpcli + get_url: + dest: "/home/{{user_name}}" + group: "{{default_permission}}" + owner: "{{default_permission}}" + url: "{{wp_cli_url}}" + become: yes + +- name: install wp-cli + command: "chmod +x /home/{{user_name}}/wp-cli.phar" + become: yes + +- name: move wp-cli + command: "mv /home/{{user_name}}/wp-cli.phar /usr/local/bin/wp" + become: yes + +- name: install wordpress + command: "wp core install --url='http://{{lb_dns_name}}' --title='{{wp_title}}' --admin_user='{{wp_admin_user}}' --admin_password='{{wp_admin_password}}' --admin_email='{{wp_admin_email}}' --path='{{webroot}}/{{wordpress_dir}}'" \ No newline at end of file diff --git a/scripts/ansiblePlaybook/wordpress/roles/wordpress/templates/wp-config.php b/scripts/ansiblePlaybook/wordpress/roles/wordpress/templates/wp-config.php new file mode 100644 index 0000000..6538409 --- /dev/null +++ b/scripts/ansiblePlaybook/wordpress/roles/wordpress/templates/wp-config.php @@ -0,0 +1,76 @@ + /home/"${1}"/run.sh + #!/bin/bash + bash /home/${1}/wordpress_script.sh ${decotext} +EOF +sudo chown -R "${1}":"${1}" /home/"${1}"/run.sh +sudo -u ${1} bash /home/"${1}"/run.sh \ No newline at end of file diff --git a/scripts/wordpress_replication.sh b/scripts/wordpress_replication.sh new file mode 100644 index 0000000..145ffe2 --- /dev/null +++ b/scripts/wordpress_replication.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Generates OpenSSL certificates. +# WordPress replication script will be replicating the WordPress folder to virtual machine scaleset +# Updates the nginx configuration + +webroot=${2} +replica_path=/azlamp/html/${1} +replica_certs=/azlamp/certs/${1} +replica_data=/azlamp/data/${1} +replica_bin=/azlamp/bin +wp_content=wp-content/uploads + +change_location() { + sudo mkdir ${replica_path} + sudo cp -rf ${webroot}/wordpress/* ${replica_path} + sudo chown -R www-data:www-data ${replica_path} +} +configuring_certs() { + sudo mkdir ${replica_certs} + sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ${replica_certs}/nginx.key -out ${replica_certs}/nginx.crt -subj "/C=US/ST=WA/L=Redmond/O=IT/CN=${1}" + sudo chown www-data:www-data ${replica_certs}/nginx.* + sudo chmod 400 ${replica_certs}/nginx.* +} +linking_data_location() { + sudo mkdir -p ${replica_data}/${wp_content} + sudo ln -s ${replica_data}/${wp_content} ${replica_path}/${wp_content} + sudo chmod 0755 ${replica_data}/${wp_content} + sudo chown -R www-data:www-data ${replica_data}/wp-content +} +update_nginx_configuration() { + cd ${replica_bin}/ + sudo sed -i "s~#1)~1)~" ${replica_bin}/update-vmss-config + sudo sed -i "s~# . /azlamp/bin/utils.sh~ . /azlamp/bin/utils.sh~" ${replica_bin}/update-vmss-config + sudo sed -i "s~# reset_all_sites_on_vmss true VMSS~ reset_all_sites_on_vmss true VMSS~" ${replica_bin}/update-vmss-config + sudo sed -i "s~#;;~;;~" ${replica_bin}/update-vmss-config +} +replication() { + cd /usr/local/bin/ + sudo bash update_last_modified_time.azlamp.sh +} + +# ${1} value is a domain name which will update in runtime +change_location +configuring_certs ${1} +linking_data_location +update_nginx_configuration +replication \ No newline at end of file diff --git a/scripts/wordpress_script.sh b/scripts/wordpress_script.sh new file mode 100644 index 0000000..a1ff7e6 --- /dev/null +++ b/scripts/wordpress_script.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Install ansible server, SVN and configure the host VM IP (controller VM IP) +# It will update groups_var/all file in playbook with the user inputs dynamically +# It will execute ansible playbook for installing WordPress in host VM (controller VM) + +log_path=/home/${3}/var.txt +home_path=/home/${3} +vars_path=/home/${3}/wordpress/group_vars/all +# wp_admin_password is the password for wordpress site +wp_admin_password=$(> ${log_path} + sudo chmod 777 /etc/ansible/hosts + sudo echo -e "[webservers]\n${1}" >>/etc/ansible/hosts + sudo chmod 755 /etc/ansible/hosts +} +install_svn() { + sudo apt-get update -y + sudo apt-get install -y subversion +} +wordpress_install() { + cd /home/${1} + svn checkout https://github.com/Azure/LAMP/trunk/scripts/ansiblePlaybook/wordpress + sudo sed -i "s~domain_name: domain~domain_name: ${5}~" ${vars_path} + sudo sed -i "s~dns_name: domain~dns_name: ${5}~" ${vars_path} + sudo sed -i "s~user_name: azusername~user_name: ${1}~" ${vars_path} + sudo sed -i "s~wp_db_server_name: wordpress~wp_db_server_name: ${2}~" ${vars_path} + sudo sed -i "s~wp_db_user: wordpress~wp_db_user: ${3}~" ${vars_path} + sudo sed -i "s~wp_db_password: password~wp_db_password: ${4}~" ${vars_path} + sudo sed -i "s~vm_password: password~vm_password: ${6}~" ${vars_path} + sudo sed -i "s~vm_ip: IP~vm_ip: ${7}~" ${vars_path} + sudo sed -i "s~wp_db_name: wordpress~wp_db_name: ${8}~" ${vars_path} + sudo sed -i "s~wp_admin_password: ~wp_admin_password: ${wp_admin_password}~" ${vars_path} + sudo sed -i "s~wp_db_user_pass: ~wp_db_user_pass: ${wp_db_user_pass}~" ${vars_path} + ansible-playbook /home/${1}/wordpress/playbook.yml -i /etc/ansible/hosts -u ${1} +} + +# Disable strict host key checking to configure host VM IP (controller VM IP) +sudo sed -i "s~# StrictHostKeyChecking ask~ StrictHostKeyChecking no~" /etc/ssh/ssh_config +sudo systemctl restart ssh +install_ansible >> ${log_path} +configure_ansible ${1} ${3} >> ${log_path} +install_svn +wordpress_install ${3} ${4} ${5} ${6} ${7} ${2} ${1} ${8} >> ${log_path} +# Enable strict host key checking +sudo sed -i "s~ StrictHostKeyChecking no~# StrictHostKeyChecking ask~" /etc/ssh/ssh_config +sudo systemctl restart ssh \ No newline at end of file