From a10af3279995e6564158502f65feef7ce7ecf771 Mon Sep 17 00:00:00 2001 From: Weiting Zhang <weitingzhangbb@gmail.com> Date: Wed, 15 May 2019 05:16:05 +1000 Subject: [PATCH] updated playbook --- Ansible/nectar/APIpassword.txt | 2 +- Ansible/nectar/host_vars/nectar.yaml | 22 +++----- Ansible/nectar/nectar.yaml | 14 ++++- Ansible/nectar/pt-39435-openrc.sh | 38 +++++++++++++ .../nectar/roles/openstack-common/main.yaml | 25 +++++---- .../roles/openstack-instance/tasks/main.yaml | 5 +- .../roles/openstack-setup/tasks/main.yaml | 23 -------- .../roles/openstack-volume/tasks/main.yaml | 2 +- Ansible/setup/APIpassword.txt | 9 +-- Ansible/setup/host_vars/nectar.yaml | 6 +- Ansible/setup/hosts | 5 +- Ansible/setup/nectar.retry | 2 +- Ansible/setup/nectar.yaml | 5 +- Ansible/setup/roles/common/tasks/main.yaml | 55 +++++++++++++++---- Ansible/setup/roles/docker/tasks/main.yaml | 17 +++++- Ansible/setup/roles/gitpull/tasks/main.yaml | 14 +++++ Ansible/setup/roles/volumes/tasks/main.yaml | 2 +- Ansible/setup/roles/webserver/tasks/main.yaml | 37 +++++++++++++ .../webserver/templates/docker-compose.yml.j2 | 23 ++++++++ Ansible/setup/{run-web.sh => run-setup.sh} | 0 20 files changed, 222 insertions(+), 84 deletions(-) create mode 100644 Ansible/nectar/pt-39435-openrc.sh delete mode 100644 Ansible/nectar/roles/openstack-setup/tasks/main.yaml create mode 100644 Ansible/setup/roles/gitpull/tasks/main.yaml create mode 100644 Ansible/setup/roles/webserver/tasks/main.yaml create mode 100644 Ansible/setup/roles/webserver/templates/docker-compose.yml.j2 rename Ansible/setup/{run-web.sh => run-setup.sh} (100%) diff --git a/Ansible/nectar/APIpassword.txt b/Ansible/nectar/APIpassword.txt index 93ac0ac..e47dd31 100644 --- a/Ansible/nectar/APIpassword.txt +++ b/Ansible/nectar/APIpassword.txt @@ -1,4 +1,4 @@ -MDlmNjRmODBkYmUxOTRh +NWM1YjdiZTRkNTlhZDY5 diff --git a/Ansible/nectar/host_vars/nectar.yaml b/Ansible/nectar/host_vars/nectar.yaml index b59d957..118a029 100644 --- a/Ansible/nectar/host_vars/nectar.yaml +++ b/Ansible/nectar/host_vars/nectar.yaml @@ -2,15 +2,14 @@ # availability zone availability_zone: melbourne-qh2-uom +vol_availability_zone: melbourne-qh2 # volume volumes: - - vol_name: master volume + - vol_name: vol_1 + vol_size: 10 + - vol_name: vol_2 vol_size: 10 - - vol_name: automated_vol_1 - vol_size: 30 - - vol_name: automated_vol_2 - vol_size: 30 # security groups security_groups: @@ -43,16 +42,11 @@ security_groups: # instance instances: - instance_name: master - instance_image: 4c5b48a0-fb86-4f4f-a98b-81e3af15e2eb - instance_key_name: xinjie - instance_flavor: uom.mse.1c4g - - instance_name: automated1 - instance_image: 4c5b48a0-fb86-4f4f-a98b-81e3af15e2eb + instance_image: 80601a5b-3bb8-40d5-8439-aa7f2070aed0 instance_key_name: xinjie instance_flavor: uom.mse.2c9g - - instance_name: automated2 - instance_image: 4c5b48a0-fb86-4f4f-a98b-81e3af15e2eb + - instance_name: worker + instance_image: 703d4362-dea8-425b-8654-313d50d40e03 instance_key_name: xinjie instance_flavor: uom.mse.2c9g - - \ No newline at end of file + \ No newline at end of file diff --git a/Ansible/nectar/nectar.yaml b/Ansible/nectar/nectar.yaml index 1b3c1b6..ec10430 100644 --- a/Ansible/nectar/nectar.yaml +++ b/Ansible/nectar/nectar.yaml @@ -7,10 +7,20 @@ roles: - role: openstack-common - - role: openstack-images - role: openstack-volume - role: openstack-security-group - role: openstack-instance # instance depends on the aboves - role: openstack-volume-snapshot - - role: openstack-setup +- name: attach a volume + hosts: localhost + tasks: + - name: attach volume to host + os_server_volume: + state: present + server: "{{ item.0 }}" + volume: "{{ item.1 }}" + device: /dev/vdb + with_together: + - '{{ os_instance_ids}}' + - '{{ os_vol_ids }}' diff --git a/Ansible/nectar/pt-39435-openrc.sh b/Ansible/nectar/pt-39435-openrc.sh new file mode 100644 index 0000000..eee465f --- /dev/null +++ b/Ansible/nectar/pt-39435-openrc.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# To use an OpenStack cloud you need to authenticate against the Identity +# service named keystone, which returns a **Token** and **Service Catalog**. +# The catalog contains the endpoints for all services the user/tenant has +# access to - such as Compute, Image Service, Identity, Object Storage, Block +# Storage, and Networking (code-named nova, glance, keystone, swift, +# cinder, and neutron). +# +# *NOTE*: Using the 3 *Identity API* does not necessarily mean any other +# OpenStack API is version 3. For example, your cloud provider may implement +# Image API v1.1, Block Storage API v2, and Compute API v2.0. OS_AUTH_URL is +# only for the Identity API served through keystone. +export OS_AUTH_URL=https://keystone.rc.nectar.org.au:5000/v3/ +# With the addition of Keystone we have standardized on the term **project** +# as the entity that owns the resources. +export OS_PROJECT_ID=eddaba81b47f41348b3c6ef3869bb354 +export OS_PROJECT_NAME="pt-39435" +export OS_USER_DOMAIN_NAME="Default" +if [ -z "$OS_USER_DOMAIN_NAME" ]; then unset OS_USER_DOMAIN_NAME; fi +export OS_PROJECT_DOMAIN_ID="default" +if [ -z "$OS_PROJECT_DOMAIN_ID" ]; then unset OS_PROJECT_DOMAIN_ID; fi +# unset v2.0 items in case set +unset OS_TENANT_ID +unset OS_TENANT_NAME +# In addition to the owning entity (tenant), OpenStack stores the entity +# performing the action as the **user**. +export OS_USERNAME="weiting.zhang1@student.unimelb.edu.au" +# With Keystone you pass the keystone password. +echo "Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: " +read -sr OS_PASSWORD_INPUT +export OS_PASSWORD=$OS_PASSWORD_INPUT +# If your configuration has multiple regions, we set that information here. +# OS_REGION_NAME is optional and only valid in certain environments. +export OS_REGION_NAME="Melbourne" +# Don't leave a blank variable, unset it if it was empty +if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi +export OS_INTERFACE=public +export OS_IDENTITY_API_VERSION=3 \ No newline at end of file diff --git a/Ansible/nectar/roles/openstack-common/main.yaml b/Ansible/nectar/roles/openstack-common/main.yaml index 6df16f7..599f433 100644 --- a/Ansible/nectar/roles/openstack-common/main.yaml +++ b/Ansible/nectar/roles/openstack-common/main.yaml @@ -22,20 +22,21 @@ pip: name: ['openstacksdk'] state: latest + +- name: Install git + become: yes + pip: + name: ['git'] + state: latest -#pip install docker -- name: Install docker +- name: Install ansible become: yes pip: - name: ['docker'] + name: ['ansible'] state: latest -# sudo apt-get install git -- name: Install git - become: yes - apt: - name: ['git'] - state: latest - update_cache: yes - when: ansible_distribution == "Ubuntu" - +- name: Install docker + become: yes + pip: + name: ['docker'] + state: latest \ No newline at end of file diff --git a/Ansible/nectar/roles/openstack-instance/tasks/main.yaml b/Ansible/nectar/roles/openstack-instance/tasks/main.yaml index a4be761..fe30e35 100644 --- a/Ansible/nectar/roles/openstack-instance/tasks/main.yaml +++ b/Ansible/nectar/roles/openstack-instance/tasks/main.yaml @@ -19,11 +19,12 @@ when: os_instance.openstack is defined #get a list of instance Ids from the return value of os_instance -- name: Create a list of instance Ids +- name: Create a list of instance Ids set_fact: os_instance_ids: '{{ os_instance_ids|default([]) + [ item.id ] }}' - loop: '{{ os_instance.results }}' + loop: '{{ os_instance.results }}' - debug: msg: "Instance {{ os_instance_ids }} has been created." when: os_instance.name is defined + diff --git a/Ansible/nectar/roles/openstack-setup/tasks/main.yaml b/Ansible/nectar/roles/openstack-setup/tasks/main.yaml deleted file mode 100644 index b72c688..0000000 --- a/Ansible/nectar/roles/openstack-setup/tasks/main.yaml +++ /dev/null @@ -1,23 +0,0 @@ -Attach a volume -- name: attach master volume to master - os_server_volume: - state: present - server: '{{ os_instance_ids|first }}' - volume: '{{ os_vol_ids|first }}' - device: /dev/vdb -- name: attach volume1 to instance1 - os_server_volume: - state: present - server: '{{ os_instance_ids|second }}' - volume: '{{ os_vol_ids|second }}' - device: /dev/vdb -- name: attach volume2 to instance2 - os_server_volume: - state: present - server: '{{ os_instance_ids|third }}' - volume: '{{ os_vol_ids|third }}' - device: /dev/vdb - - - - diff --git a/Ansible/nectar/roles/openstack-volume/tasks/main.yaml b/Ansible/nectar/roles/openstack-volume/tasks/main.yaml index c780edb..9bae49e 100644 --- a/Ansible/nectar/roles/openstack-volume/tasks/main.yaml +++ b/Ansible/nectar/roles/openstack-volume/tasks/main.yaml @@ -3,7 +3,7 @@ os_volume: display_name: '{{ item.vol_name }}' size: '{{ item.vol_size }}' - availability_zone: '{{ availability_zone }}' + availability_zone: '{{ vol_availability_zone }}' wait: yes timeout: 600 state: present diff --git a/Ansible/setup/APIpassword.txt b/Ansible/setup/APIpassword.txt index 6d11e9c..1e49779 100644 --- a/Ansible/setup/APIpassword.txt +++ b/Ansible/setup/APIpassword.txt @@ -1,8 +1,9 @@ -NGExZGEyYTg5MDRiYWI0 +NWM1YjdiZTRkNTlhZDY5 -#Note: first run the command "chmod a+x run-web.sh" -# and then run the command "./run-web.sh" +#Note: first run the command "chmod a+x run-setup.sh" +# and then run the command "./run-setup.sh" # ssh to master -# ssh -i deployment_key.txt ubuntu@172.26.37.182 +# ssh -i deployment_key.txt ubuntu@172.26.38.44 + diff --git a/Ansible/setup/host_vars/nectar.yaml b/Ansible/setup/host_vars/nectar.yaml index 3b37528..e2c9f4a 100644 --- a/Ansible/setup/host_vars/nectar.yaml +++ b/Ansible/setup/host_vars/nectar.yaml @@ -5,8 +5,4 @@ volumes: mountpoint: /data # define the working directory -working_dir: myWorkingDirectory - - - - +web_directory: /home/ubuntu/ccc-assignment2-team69/web_app_django/ diff --git a/Ansible/setup/hosts b/Ansible/setup/hosts index c82b33a..d1fb93d 100644 --- a/Ansible/setup/hosts +++ b/Ansible/setup/hosts @@ -1,4 +1,3 @@ [server] -172.26.37.182 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=deployment_key.txt -172.26.38.4 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=deployment_key.txt -172.26.38.157 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=deployment_key.txt \ No newline at end of file +172.26.38.140 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=deployment_key.txt +172.26.38.44 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=deployment_key.txt \ No newline at end of file diff --git a/Ansible/setup/nectar.retry b/Ansible/setup/nectar.retry index 771e890..37b5e19 100644 --- a/Ansible/setup/nectar.retry +++ b/Ansible/setup/nectar.retry @@ -1 +1 @@ -172.26.37.182 +172.26.38.140 diff --git a/Ansible/setup/nectar.yaml b/Ansible/setup/nectar.yaml index e16de28..9a27624 100644 --- a/Ansible/setup/nectar.yaml +++ b/Ansible/setup/nectar.yaml @@ -5,9 +5,12 @@ vars_files: - host_vars/nectar.yaml gather_facts: true - + roles: - role: common - role: volumes - role: docker + - role: gitpull + - role: webserver + diff --git a/Ansible/setup/roles/common/tasks/main.yaml b/Ansible/setup/roles/common/tasks/main.yaml index 1f5d828..ddceae9 100644 --- a/Ansible/setup/roles/common/tasks/main.yaml +++ b/Ansible/setup/roles/common/tasks/main.yaml @@ -1,5 +1,17 @@ # this file is for installing dependencies on the host +- name: add internet proxy + lineinfile: + dest: /etc/environment + regexp: '^\n' + insertafter: '^\n' + line: "{{ item.line }}" + with_items: + - { line: 'http_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'https_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'ftp_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'no_proxy=localhost,127.0.0.1,127.0.1.1,ubuntu' } + - name: Gather facts of remote host setup: gather_subset: all @@ -21,17 +33,38 @@ name: ['pip'] state: latest -- name: add internet proxy - lineinfile: - dest: /etc/environment + +- name: add git ssh key + lineinfile: + dest: /home/ubuntu/gitkey regexp: '^\n' insertafter: '^\n' - line: "{{ item.line }}" - with_items: - - { line: 'http_proxy="http://wwwproxy.unimelb.edu.au:8000"' } - - { line: 'https_proxy="http://wwwproxy.unimelb.edu.au:8000"' } - - { line: 'ftp_proxy="http://wwwproxy.unimelb.edu.au:8000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RY9/EodXcuaVIzv/tinXe/OHAfeA55VV5bHfS0X/9m5pH4qPyYn6Oy +TxDSEZj891S/yxhEoHiyUqlAFdikRuNS7e/iB/8CgYAmZ/TjQw1vssrXj2mnCF3Q +zINjcaD6jySo1uQROEr54hQisx8xthvIHpl+yFyY+MD9MWcqkgckDyUpK/eCh/29 +yb7uyCJ5mF+9N++WeaUMYmf3YM6uF7IfbjH4lyLoWoTvf4VCBWm+CK35LrW/5HP+ +pOEo2tJfTsuKRQeItzE+AwKBgDIVmlwuc9K5zQt0FEYmwEq2ynvqdnuc49pTNZE/ +2dYmL0u/J7VIBHnWhB/Hb54BSh0E+xRZ/9A2FUgCrQIoHWYdHq5kfhtCITzxf4Fa +4qMjg4Im9lh7m7D15R2aNzc/BCQi1T7lYkDDXfH0WRiqKpCuvFX6VrwkuMdYlGrn +V0PNAoGBALgDUpcepUi+zetaORy2NPpzPVpGSMLMfjm8aQa07ZoKD31XBvkJjIn5 +02E1hJiTZMc2KUua3fbYCaoTcAgq49m9uJBV5FQqA/mfCnzOAB6TJbOnDouSDHmy +vhkxD2LPDGboLnr34uRBIT5y0AWfqqSv+hOkmwOR2lBAQsOJ/5Mx +-----END RSA PRIVATE KEY-----' + create: yes \ No newline at end of file diff --git a/Ansible/setup/roles/docker/tasks/main.yaml b/Ansible/setup/roles/docker/tasks/main.yaml index 36c8c77..823056f 100644 --- a/Ansible/setup/roles/docker/tasks/main.yaml +++ b/Ansible/setup/roles/docker/tasks/main.yaml @@ -11,7 +11,7 @@ tags: 'docker' become: yes apt: - name: ['apt-transport-https', 'ca-certificates', 'curl', 'python-setuptools', 'software-properties-common'] + name: ['apt-transport-https', 'ca-certificates', 'curl', 'python-setuptools', 'software-properties-common', 'cron', 'gnupg2', 'aptitude'] state: latest install_recommends: no update_cache: yes @@ -27,7 +27,7 @@ tags: 'docker' become: yes apt_repository: - repo: "deb https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }}" + repo: "deb https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} {{ ansible_distribution_version }} " mode: '644' update_cache: yes state: present @@ -36,7 +36,7 @@ tags: 'docker' become: yes apt: - name: ['docker'] + name: ['docker.io'] state: latest install_recommends: no update_cache: yes @@ -49,3 +49,14 @@ version: 1.22.0 state: present +- name: add proxy to docker environment + lineinfile: + dest: /etc/default/docker + regexp: '^\n' + insertafter: '^\n' + line: "{{ item.line }}" + with_items: + - { line: 'http_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'https_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'ftp_proxy="http://wwwproxy.unimelb.edu.au:8000"' } + - { line: 'no_proxy=localhost,127.0.0.1,127.0.1.1,ubuntu' } \ No newline at end of file diff --git a/Ansible/setup/roles/gitpull/tasks/main.yaml b/Ansible/setup/roles/gitpull/tasks/main.yaml new file mode 100644 index 0000000..44250a8 --- /dev/null +++ b/Ansible/setup/roles/gitpull/tasks/main.yaml @@ -0,0 +1,14 @@ +# - name: git clone repo using ssh +# git: +# repo: git@gitlab.eng.unimelb.edu.au:christianLan/ccc-assignment2-team69.git +# dest: /home/ubuntu/CCC/ +# clone: yes +# accept_hostkey: yes +# key_file: /home/ubuntu/gitkey + + +- name: git clone repo using http + git: + repo: 'https://gitlab.eng.unimelb.edu.au/christianLan/ccc-assignment2-team69.git' + dest: /home/ubuntu/ccc-assignment2-team69 + clone: yes diff --git a/Ansible/setup/roles/volumes/tasks/main.yaml b/Ansible/setup/roles/volumes/tasks/main.yaml index d265299..d6c203d 100644 --- a/Ansible/setup/roles/volumes/tasks/main.yaml +++ b/Ansible/setup/roles/volumes/tasks/main.yaml @@ -37,7 +37,7 @@ state: directory when: item.stat.exists == false with_items: - - "{{ directory_stat.results }}" + - "{{ directory_stats.results }}" # mount the volume to the instance - name: Mount device diff --git a/Ansible/setup/roles/webserver/tasks/main.yaml b/Ansible/setup/roles/webserver/tasks/main.yaml new file mode 100644 index 0000000..97bb7e7 --- /dev/null +++ b/Ansible/setup/roles/webserver/tasks/main.yaml @@ -0,0 +1,37 @@ +- name: Create webserver directory + tags: 'webserver' + become: yes + file: + path: "{{ web_directory }}" + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + recurse: yes + state: directory + +- name: configure compose + tags: 'webserver' + become: yes + template: + src: docker-compose.yml.j2 + dest: "{{ web_directory }}/docker-compose.yml" + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + +- name: build the image + docker_image: + name: built-by-ansible + push: yes + path: "{{ web_directory }}" + state: present + +- name: Run docker compose + + tags: 'webserver' + become: yes + docker_compose: + project_src: "{{ web_directory }}" + pull: yes + state: present + remove_orphans: yes + recreate: always + diff --git a/Ansible/setup/roles/webserver/templates/docker-compose.yml.j2 b/Ansible/setup/roles/webserver/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..9f99978 --- /dev/null +++ b/Ansible/setup/roles/webserver/templates/docker-compose.yml.j2 @@ -0,0 +1,23 @@ +version: '3.6' + +services: + couchdb: + image: "couchdb:2.3.0" + hostname: couchdb.server + environment: + COUCHDB_USER: admin + COUCHDB_PASSWORD: password + ports: + - "5984:5984" + web: + build: . + command: python /ccc-assignment2-team69/web_app_django/manage.py runserver 0.0.0.0:5555 + volumes: + - .:/ccc-assignment2-team69/web_app_django/ + ports: + - 5555:5555 + depends_on: + - couchdb + +volumes: + couchdb: \ No newline at end of file diff --git a/Ansible/setup/run-web.sh b/Ansible/setup/run-setup.sh similarity index 100% rename from Ansible/setup/run-web.sh rename to Ansible/setup/run-setup.sh -- GitLab