diff --git a/data/ansible/generate_package/go.yaml b/data/ansible/generate_package/go.yaml new file mode 100644 index 000000000..c08fae40b --- /dev/null +++ b/data/ansible/generate_package/go.yaml @@ -0,0 +1,122 @@ +--- +- name: create gopath directory + file: + state: directory + mode: 'u=rwx,go=rx' + dest: '{{ golang_gopath }}' + +- name: create download directory + file: + state: directory + mode: 'u=rwx,go=rx' + dest: '{{ golang_download_dir }}' + +- name: Register the current Go version (if any) + command: /usr/local/go/bin/go version + ignore_errors: yes + register: go_version + changed_when: false + +- name: Remove old installation of Go + become: yes + file: + path: /usr/local/go + state: absent + when: go_version is failed or go_version.stdout != go_version_target + +- name: download Go language SDK + get_url: + url: '{{ golang_mirror }}/{{ golang_redis_filename }}' + dest: '{{ golang_download_dir }}/{{ golang_redis_filename }}' + mode: 'u=rw,go=r' + +- name: create Go language SDK installation directory + become: yes + file: + state: directory + owner: root + group: root + mode: 'u=rwx,go=rx' + dest: '{{ golang_install_dir }}' + +- name: install Go language SDK + become: yes + unarchive: + src: '{{ golang_download_dir }}/{{ golang_redis_filename }}' + remote_src: yes + extra_opts: '--strip-components=1' + dest: '{{ golang_install_dir }}' + owner: root + group: root + creates: '{{ golang_install_dir }}/bin' + +# Set Go language SDK environment variables +- name: make sure /etc/profile.d exists + become: yes + file: + path: /etc/profile.d + state: directory + owner: root + group: root + mode: 'u=rwx,go=rx' + +- name: export Go language SDK environment variables + become: yes + template: + src: golang.sh.j2 + dest: /etc/profile.d/golang.sh + owner: root + group: root + mode: 'u=rw,go=r' + +- name: Export GOROOT for root + become: yes + lineinfile: + path: ~/.bashrc + line: export GOROOT='{{ golang_install_dir }}' + insertafter: last + +- name: Add GOROOT to PATH for root + become: yes + lineinfile: + dest: ~/.bashrc + line: export PATH=$PATH:$GOROOT/bin + insertafter: last + +- name: Export GOPATH for root + become: yes + lineinfile: + dest: ~/.bashrc + line: export GOPATH='{{ golang_gopath }}' + insertafter: last + +- name: Add GOPATH to PATH for root + become: yes + lineinfile: + dest: ~/.bashrc + line: export PATH=$PATH:$GOPATH/bin + insertafter: last + +- name: Export GOROOT + lineinfile: + dest: ~/.bashrc + line: export GOROOT='{{ golang_install_dir }}' + insertafter: last + +- name: Add GOROOT to PATH + lineinfile: + dest: ~/.bashrc + line: export PATH=$PATH:$GOROOT/bin + insertafter: last + +- name: Export GOPATH + lineinfile: + dest: ~/.bashrc + line: export GOPATH='{{ golang_gopath }}' + insertafter: last + +- name: Add GOPATH to PATH + lineinfile: + dest: ~/.bashrc + line: export PATH=$PATH:$GOPATH/bin + insertafter: last diff --git a/data/ansible/generate_package/golang.sh.j2 b/data/ansible/generate_package/golang.sh.j2 new file mode 100644 index 000000000..7bfc41c2e --- /dev/null +++ b/data/ansible/generate_package/golang.sh.j2 @@ -0,0 +1,12 @@ + +#!/bin/sh + +{{ ansible_managed | comment('plain') }} + +export GOROOT='{{ golang_install_dir }}' +export PATH=$PATH:$GOROOT/bin + +{% if golang_gopath not in (None, '') %} +export GOPATH="{{ golang_gopath }}" +export PATH=$PATH:$GOPATH/bin +{% endif %} \ No newline at end of file diff --git a/data/ansible/generate_package/main.yaml b/data/ansible/generate_package/main.yaml new file mode 100644 index 000000000..c247e8ec4 --- /dev/null +++ b/data/ansible/generate_package/main.yaml @@ -0,0 +1,183 @@ +--- +- name: Check and set python version on APT server + hosts: apt + remote_user: '{{ gouser }}' + gather_facts: false + tasks: + - name: symlink /usr/bin/python -> /usr/bin/python3 + raw: | + if [ -f /usr/bin/python3 ] && [ ! -f /usr/bin/python ]; then + ln --symbolic /usr/bin/python3 /usr/bin/python; + fi + become: true + +- name: Check and set python version on PKG server + hosts: pkg + remote_user: '{{ gouser }}' + gather_facts: false + tasks: + - name: symlink /usr/bin/python -> /usr/bin/python3 + raw: | + if [ -f /usr/bin/python3 ] && [ ! -f /usr/bin/python ]; then + ln --symbolic /usr/bin/python3 /usr/bin/python; + fi + become: true + +- hosts: apt + vars: + ############################################################### + ##################### Golang Vars ############################# + ############################################################### + # Go language SDK version number + golang_version: '1.13' + go_version_target: "go version go{{ golang_version }} linux/amd64" + # Mirror to download the Go language SDK redistributable package from + golang_mirror: 'https://storage.googleapis.com/golang' + # Base installation directory the Go language SDK distribution + golang_install_dir: '/usr/local/go' + # Directory to store files downloaded for Go language SDK installation + golang_download_dir: "{{ x_ansible_download_dir | default(ansible_env.HOME + '/.ansible/tmp/downloads') }}" + # Location for GOPATH environment variable + golang_gopath: "/home/{{ gouser }}/go" + # Filename of Go language SDK redistributable package + golang_redis_filename: 'go{{ golang_version }}.linux-amd64.tar.gz' + + ############################################################### + # CGRateS location + cgrates_dir: "{{ golang_gopath }}/src/github.com/cgrates/cgrates" + + rootUser : root + + customPath: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + + dependencies: + - build-essential + - git + - devscripts + + remote_user: '{{ gouser }}' + tasks: +########################################################################################################################### +########################################################################################################################### +# install dependencies + - name: Install dependencies + become: yes + apt: name={{ dependencies }} state=present +########################################################################################################################### +########################################################################################################################### +# Install Golang + - name: install unarchive dependencies (zypper) + become: yes + zypper: + name: + - gzip + - tar + state: present + when: ansible_pkg_mgr == 'zypper' + + - name: Install golang + include: go.yaml + +########################################################################################################################### +########################################################################################################################### +# Install CGRateS + - name: create cgrates directory + become: yes + file: + state: directory + mode: 'u=rwx,go=rx' + owner: "{{ gouser }}" + group: "{{ gouser }}" + dest: '{{ cgrates_dir }}' + become_user: "{{ gouser }}" + + - name: git clone cgrates + git: + repo: https://github.com/cgrates/cgrates.git + dest: '{{ cgrates_dir }}' + update: yes + force: yes + version: v0.10 + become: yes + become_user: "{{ gouser }}" + + - name: build cgrates + shell: "sh {{ cgrates_dir }}/build.sh" + environment: + PATH: "{{ lookup('env','PATH') }}:{{ golang_gopath }}/bin:/usr/local/go/bin:{{ ansible_env.PATH }}" + args: + chdir: '{{ cgrates_dir }}' + + - name: symbol link + become: yes + file: + src: "{{ cgrates_dir }}/data" + dest: "/usr/share/cgrates" + state: link +########################################################################################################################### +########################################################################################################################### +# Generate package + - name: Generate package + become: yes + command: 'sudo env "PATH={{ customPath }}" make deb_hash' + args: + chdir: '{{ cgrates_dir }}/packages' + + - name: Check if the package was generated + become: yes + shell: "sudo ls {{ golang_gopath }}/src/github.com/cgrates | grep 'cgrates_'" + ignore_errors: true + register: packageVar + + - name: Move the files to /var/packages/debian/incoming + become: yes + command: sudo mv {{item}} /var/packages/debian/incoming/ + args: + chdir: '{{ golang_gopath }}/src/github.com/cgrates' + when : packageVar.stdout_lines|length > 0 + with_items: '{{ packageVar.stdout_lines }}' + + - name : Get the name of the deb file + become: yes + shell : "sudo ls /var/packages/debian/incoming/ | grep '.deb'" + register: debFileName + + # Move the file to PKG server + - name: Copy the file to PKG server + become: yes + shell: 'sudo sshpass -p "{{ pkgPass }}" scp /var/packages/debian/incoming/{{ item }} {{ gouser }}@{{ internalPkgAddr }}:/tmp/' + args: + chdir: /var/packages/debian/incoming/ + with_items: '{{ debFileName.stdout_lines }}' + + # Clean the incoming folder + - name: Clean the incoming folder + become: yes + shell: "sudo rm /var/packages/debian/incoming/*" + args: + chdir: /var/packages/debian + +- hosts: pkg + remote_user: '{{ gouser }}' + tasks: + - name: Creates directory + become: yes + file: + path: /var/packages/debian/v0.10/ + state: directory + + - name: Remove symlink from current deb package + become: yes + file: + path: /var/packages/debian/v0.10/cgrates_current_amd64.deb + state: absent + + - name: Move the new package to /var/packages/debian + become: yes + shell: "sudo mv /tmp/{{ item}} /var/packages/debian/v0.10/" + with_items: "{{ hostvars['apt']['debFileName']['stdout_lines'] }}" + + - name: Create the new symlink cgrates_current_amd64.deb + become: yes + shell: "sudo ln -s /var/packages/debian/v0.10/{{ item }} /var/packages/debian/v0.10/cgrates_current_amd64.deb" + with_items: "{{ hostvars['apt']['debFileName']['stdout_lines'] }}" \ No newline at end of file