From 33b7061716a057f9788f6aa0cb33ff93c65ebb2f Mon Sep 17 00:00:00 2001 From: vincent Date: Fri, 26 Apr 2019 14:04:48 +0200 Subject: [PATCH] COMPETED CHAINETV ROLE --- defaults/main.yml | 12 +++- handlers/main.yml | 21 ++++++- meta/main.yml | 3 +- tasks/main.yml | 79 +++++++++++++++++++++++--- templates/nginx/chainetv.default.j2 | 19 ++++--- templates/systemd/gunicorn.service.j2 | 21 +++---- templates/systemd/gunicorn.socket.j2 | 10 ++++ templates/tmpfiles.d/gunicornn.conf.j2 | 1 - 8 files changed, 132 insertions(+), 34 deletions(-) delete mode 100644 templates/tmpfiles.d/gunicornn.conf.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 82faab1..c644d95 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,12 @@ --- -# defaults file for chainetv \ No newline at end of file +# defaults file for chainetv + + +name: chainetv +source_location: /srv/chainetv/ +venv_location: /opt/venv/ +venv_name: "{{name}}" +backendfolder: backend +exec_user: root +exec_group: root +repo: gitea@pi2:vincent/chainetv_web.git diff --git a/handlers/main.yml b/handlers/main.yml index 573c502..b6ce90a 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,21 @@ --- -# handlers file for chainetv \ No newline at end of file +# handlers file for chainetv + + +- name: Reload systemd + systemd: + daemon_reload: yes + become: yes + +- name: ensure socket is start + systemd: + name: "gunicorn-{{name}}.socket" # not required. Name of the service. When using in a chroot environment you always need to specify the full name i.e. (crond.service). + enabled: true # not required. Whether the service should start on boot. B(At least one of state and enabled are required.) + state: restarted # not required. choices: reloaded;restarted;started;stopped. C(started)/C(stopped) are idempotent actions that will not run commands unless necessary. C(restarted) will always bounce the service. C(reloaded) will always reload. + become: yes + +- name: restart service + systemd: + name: "gunicorn-{{name}}.service" # not required. Name of the service. When using in a chroot environment you always need to specify the full name i.e. (crond.service). + state: restarted # not required. choices: reloaded;restarted;started;stopped. C(started)/C(stopped) are idempotent actions that will not run commands unless necessary. C(restarted) will always bounce the service. C(reloaded) will always reload. + become: yes \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml index 5d50bf4..2f26134 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -55,6 +55,7 @@ galaxy_info: # NOTE: A tag is limited to a single word comprised of alphanumeric characters. # Maximum 20 tags per role. -dependencies: [] +dependencies: + - nginx # List your role dependencies here, one per line. Be sure to remove the '[]' above, # if you add dependencies to this list. \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index 0f49587..055016d 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,12 +1,75 @@ --- # tasks file for chainetv -- name: ensure that gunicorn is install - pacman: - state: present # not required. choices: absent;latest;present. Desired state of the package. - upgrade: false # not required. Whether or not to upgrade whole system. - force: false # not required. When removing package - force remove package, without any checks. When update_cache - force redownload repo databases. - name: gunicorn # not required. Name or list of names of the packages to install, upgrade, or remove. - update_cache: false # not required. Whether or not to refresh the master package lists. This can be run as part of a package installation or as a separate step. - recurse: false # not required. When removing a package, also remove its dependencies, provided that they are not required by other packages and were not explicitly installed by a user. +- name: stat location folder + stat: + path: "{{source_location}}/dist" # required. The full path of the file/object to get the facts of. + register: location_stat + +- name: install source file if not exist + git: + dest: "/tmp/{{name}}" # required. The path of where the repository should be checked out. This parameter is required, unless C(clone) is set to C(no). + repo: "{{repo}}" # required. git, SSH, or HTTP(S) protocol address of the git repository. + clone: yes # not required. If C(no), do not clone the repository if it does not exist locally + when: location_stat.stat.exists == false + +- name: compile client source + shell: "/tmp/{{name}}/deploy_client.sh" + when: location_stat.stat.exists == false + +- name: copy source + synchronize: + dest: "{{source_location}}" # required. Remote absolute path where the file should be copied to. If I(src) is a directory, this must be a directory too. If I(dest) is a nonexistent path and if either I(dest) ends with "/" or I(src) is a directory, I(dest) is created. If I(src) and I(dest) are files, the parent directory of I(dest) isn't created: the task fails if it doesn't already exist. + src: "/tmp/{{name}}/{{backendfolder}}/" # not required. Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied. This behavior is similar to Rsync. + when: location_stat.stat.exists == false + delegate_to: "{{ inventory_hostname }}" become: yes +- name: + file: + path: "/tmp/{{name}}" + state: absent + when: location_stat.stat.exists == false + +- name: ensure venv folder exist + file: + name: "{{venv_location}}" + state: directory + mode: 0777 + become: yes + +- name: create venv + pip: + requirements: "{{source_location}}/requirements.txt" + virtualenv: "{{venv_location}}{{venv_name}}" + virtualenv_command: /usr/bin/python -m venv + + +- name: install gunicorn + pip: + virtualenv: "{{venv_location}}{{venv_name}}" # not required. An optional path to a I(virtualenv) directory to install into. It cannot be specified together with the 'executable' parameter (added in 2.1). If the virtualenv does not exist, it will be created before installing packages. The optional virtualenv_site_packages, virtualenv_command, and virtualenv_python options affect the creation of the virtualenv. + name: gunicorn # not required. The name of a Python library to install or the url(bzr+,hg+,git+,svn+) of the remote package.,This can be a list (since 2.2) and contain version specifiers (since 2.7). + + +- name: apply nginx template + template: + dest: "/etc/nginx/conf.d/{{name}}.default" # required. Location to render the template to on the remote machine. + src: "nginx/{{name}}.default.j2" # required. Path of a Jinja2 formatted template on the Ansible controller. This can be a relative or absolute path. + become: yes + notify: restart nginx + +- name: apply systemd socket template + template: + dest: "/etc/systemd/system/gunicorn-{{name}}.socket" # required. Location to render the template to on the remote machine. + src: "systemd/gunicorn.socket.j2" # required. Path of a Jinja2 formatted template on the Ansible controller. This can be a relative or absolute path. + become: yes + notify: + - Reload systemd + - ensure socket is start + +- name: apply systemd service template + template: + dest: "/etc/systemd/system/gunicorn-{{name}}.service" # required. Location to render the template to on the remote machine. + src: "systemd/gunicorn.service.j2" # required. Path of a Jinja2 formatted template on the Ansible controller. This can be a relative or absolute path. + become: yes + notify: + - Reload systemd diff --git a/templates/nginx/chainetv.default.j2 b/templates/nginx/chainetv.default.j2 index 8fde4d2..016eaa0 100644 --- a/templates/nginx/chainetv.default.j2 +++ b/templates/nginx/chainetv.default.j2 @@ -1,10 +1,11 @@ - location /chainetv { - alias /srv/chainetv_web/dist; + location /{{name}}/static { + alias {{source_location}}dist/static; } - location /chainetv/api { - proxy_pass http://unix:/run/gunicorn/socket; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Scheme $scheme; - proxy_set_header X-Script-Name /chainetv; - } \ No newline at end of file + location /{{name}} { + proxy_pass http://unix:/run/gunicorn-{{name}}/socket:/; + #proxy_set_header Host $host; + #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + #proxy_set_header X-Scheme $scheme; + #proxy_set_header X-Script-Name /{{name}} + } + diff --git a/templates/systemd/gunicorn.service.j2 b/templates/systemd/gunicorn.service.j2 index cceda93..a5e7e27 100644 --- a/templates/systemd/gunicorn.service.j2 +++ b/templates/systemd/gunicorn.service.j2 @@ -1,22 +1,17 @@ [Unit] -Description=gunicorn daemon -Requires=gunicorn.socket +Description=gunicorn daemon for {{name}} +Requires=gunicorn-{{name}}.socket After=network.target [Service] -PIDFile=/run/gunicorn/pid -User=root -Group=root +PIDFile=/run/gunicorn-{{name}}/pid +User={{exec_user}} +Group={{exec_group}} RuntimeDirectory=gunicorn -WorkingDirectory=/srv/chainetv_web/backend -ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/pid \ - --bind unix:/run/gunicorn/socket run:app +WorkingDirectory={{source_location}} +ExecStart={{venv_location}}{{venv_name}}/bin/gunicorn --pid /run/gunicorn-{{name}}/pid \ + --bind unix:/run/gunicorn-{{name}}/socket run:app ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true -[Install] -WantedBy=multi-user.target -[Unit] -Description=gunicorn socket - diff --git a/templates/systemd/gunicorn.socket.j2 b/templates/systemd/gunicorn.socket.j2 index e69de29..9222754 100644 --- a/templates/systemd/gunicorn.socket.j2 +++ b/templates/systemd/gunicorn.socket.j2 @@ -0,0 +1,10 @@ +[Install] +WantedBy=multi-user.target +[Unit] +Description=gunicorn socket + +[Socket] +ListenStream=/run/gunicorn-{{name}}/socket + +[Install] +WantedBy=sockets.target diff --git a/templates/tmpfiles.d/gunicornn.conf.j2 b/templates/tmpfiles.d/gunicornn.conf.j2 deleted file mode 100644 index 9cdfd59..0000000 --- a/templates/tmpfiles.d/gunicornn.conf.j2 +++ /dev/null @@ -1 +0,0 @@ -d /run/gunicorn 0755 root roor - \ No newline at end of file