---
# Set up directory and files for specifying software needs rather than using hosts file.

- name: General | Account Management | Provisioning Configuration | Variables 1
  set_fact:
    prov_dir: "/usr/local/etc/hyperling-scm"
    provision_variables: []

- name: General | Account Management | Provisioning Configuration | Variables 2
  set_fact:
    gen_file: "{{ prov_dir }}/general.ini"
    wrk_file: "{{ prov_dir }}/workstation.ini"
    mnr_file: "{{ prov_dir }}/miner.ini"
    srv_file: "{{ prov_dir }}/server.ini"

- name: General | Account Management | Provisioning Configuration | Create Directory
  file: 
    path: "{{ prov_dir }}"
    state: directory
    mode: '0755'


## General ##

- name: General | Account Management | Provisioning Configuration | General | Create
  blockinfile:
    path: "{{ gen_file }}"
    block: |
      ;;;;;;; Available options - all require lowercase values ;;;;;;
      ;
      ;     enable : Provide true for the provision to occur.
      ;
      ;       user : Provide the name of the user account.
      ;                Example: tom, jerry, kim, sarah
      ;
      ;  user_desc : Provide the description of the user account.
      ;                Example: Thomas, Jerry, Kimberly, Sarah
      ;
      ;     branch : Provide the Github branch that the machine should poll.
      ;                Example: main, dev
      ;
      ; pentesting : Set to true to install tools such as metasploit (nmap is already provided for reporting)
      ;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Generic Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | General | Load
  set_fact:
    provision:  "{{ lookup('ini', 'enable     file={{gen_file}} default=false') }}"
    user:       "{{ lookup('ini', 'user       file={{gen_file}} default=ling') }}"
    user_desc:  "{{ lookup('ini', 'user_desc  file={{gen_file}} default=Hyperling') }}"
    branch:     "{{ lookup('ini', 'branch     file={{gen_file}} default=main') }}"
    pentesting: "{{ lookup('ini', 'pentesting file={{gen_file}} default=false') }}"

- name: General | Account Management | Provisioning Configuration | General | List
  set_fact: 
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'provision': "{{ provision }}" }
    - { 'user': "{{ user }}" }
    - { 'user_desc': "{{ user_desc }}" }
    - { 'branch': "{{ branch }}" }
    - { 'pentesting': "{{ pentesting }}" }


## Workstation ##

- name: General | Account Management | Provisioning Configuration | Workstation | Create
  blockinfile:
    path: "{{ wrk_file }}"
    block: |
      ;;;;;;; Available options - all require lowercase values ;;;;;;
      ;
      ;  enable : Set to true for system to be considered a workstation.
      ;
      ;  coding : Set to true for installation of code editors (VSCode, PyCharm, Android Studio)
      ;
      ; editing : Set to true for installation of Audio/Video editors (Shotcut, Audacity, OBS Stdio, GIMP)
      ;
      ;  gaming : Set to true for installation of gaming software (Steam, Lutris)
      ;
      ;     rdp : Set to true for installation of RDP protocol
      ;             CURRENTLY FREEBSD-ONLY
      ;
      ;     vnc : Set to true for installation of VNC protocol
      ;             UNFINISHED/UNTESTED
      ;
      ; bsd_gpu : Set to [] to install GPU driver
      ;             Example: amdgpu
      ;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Workstation Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | Workstation | Load
  set_fact:
    workstation: "{{ lookup('ini', 'enable  file={{wrk_file}} default=false') }}"
    coding:      "{{ lookup('ini', 'coding  file={{wrk_file}} default=false') }}"
    editing:     "{{ lookup('ini', 'editing file={{wrk_file}} default=false') }}"
    gaming:      "{{ lookup('ini', 'gaming  file={{wrk_file}} default=false') }}"
    rdp:         "{{ lookup('ini', 'rdp     file={{wrk_file}} default=false') }}"
    vnc:         "{{ lookup('ini', 'vnc     file={{wrk_file}} default=false') }}"
    bsd_gpu:     "{{ lookup('ini', 'bsd_gpu file={{wrk_file}} default=false') }}"

- name: General | Account Management | Provisioning Configuration | Workstation | List
  set_fact: 
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'workstation': "{{ workstation }}" }
    - { 'coding': "{{ coding }}" }
    - { 'editing': "{{ editing }}" }
    - { 'gaming': "{{ gaming }}" }
    - { 'rdp': "{{ rdp }}" }
    - { 'vnc': "{{ vnc }}" }
    - { 'bsd_gpu': "{{ bsd_gpu }}" }

# No longer mining, this is now considered deprecated.
### Miner ##
#
#- name: General | Account Management | Provisioning Configuration | Miner | Create
#  blockinfile:
#    path: "{{ mnr_file }}"
#    block: |
#      ;;;;;;; Available options - all require lowercase values ;;;;;;
#      ;
#      ;             enable : Set to true for system to be considered a miner.
#      ;
#      ;             amdgpu : Set to true for installation of AMDGPU-Pro drivers on Ubuntu (20.04 preferred)
#      ;
#      ;          nanominer : Set to true for installation of nanominer
#      ;
#      ;      nanominer_cpu : Set to coin code that you'd like the CPU to work on 
#      ;                        Valid values: xmr
#      ;
#      ; nanominer_cpu_pool : Set to pool organization to use for CPU mining
#      ;                        Valid values: nanopool
#      ;
#      ;      nanominer_gpu : Set to coin code that you'd like the GPU to work on
#      ;                        Valid values: eth
#      ;
#      ;     nanominer_gpus : Set to comma list of GPU devices that should be used for nanominer
#      ;                        Example: 0,2,3
#      ;
#      ; nanominer_gpu_pool : Set to pool organization to use for GPU mining
#      ;                        Valid values: nanopool etherpool f2pool
#      ;
#      ;    eth_minhashrate : Set to minimum hashrate over 10 minutes before restarting. Can accept M for Million and K for thousand.
#      ;                        Example: 100K
#      ;
#      ;           ethminer : Set to true for installation of ethminer
#      ;                        UNFINISHED/UNTESTED
#      ;
#      ;             nvidia : Set to true for NVidia support in ethminer.
#      ;                        UNFINISHED/UNTESTED
#      ;
#      ;       xmr_stak_cpu : Set to true for istallation
#      ;                        UNFINISHED/UNTESTED
#      ;
#      [global]
#    marker: '; {mark} MANAGED BY ANSIBLE | Miner Config'
#    state: present
#    create: yes
#    backup: yes
#
#- name: General | Account Management | Provisioning Configuration | Miner | Load
#  set_fact:
#    miner:              "{{ lookup('ini', 'enable             file={{mnr_file}} default=false') }}"
#    amdgpu:             "{{ lookup('ini', 'amdgpu             file={{mnr_file}} default=false') }}"
#    nanominer:          "{{ lookup('ini', 'nanominer          file={{mnr_file}} default=false') }}"
#    nanominer_cpu:      "{{ lookup('ini', 'nanominer_cpu      file={{mnr_file}} default=false') }}"
#    nanominer_cpu_pool: "{{ lookup('ini', 'nanominer_cpu_pool file={{mnr_file}} default=false') }}"
#    nanominer_gpu:      "{{ lookup('ini', 'nanominer_gpu      file={{mnr_file}} default=false') }}"
#    nanominer_gpus:     "{{ lookup('ini', 'nanominer_gpus     file={{mnr_file}} default=false') }}"
#    nanominer_gpu_pool: "{{ lookup('ini', 'nanominer_gpu_pool file={{mnr_file}} default=false') }}"
#    eth_minhashrate:    "{{ lookup('ini', 'eth_minhashrate    file={{mnr_file}} default=false') }}"
#    ethminer:           "{{ lookup('ini', 'ethminer           file={{mnr_file}} default=false') }}"
#    nvidia:             "{{ lookup('ini', 'nvidia             file={{mnr_file}} default=false') }}"
#    xmr_stak_cpu:       "{{ lookup('ini', 'xmr_stak_cpu       file={{mnr_file}} default=false') }}"
#
#- name: General | Account Management | Provisioning Configuration | Miner | List
#  set_fact: 
#    provision_variables: "{{ provision_variables | combine(item) }}"
#  loop:
#    - { 'miner': "{{ miner }}" }
#    - { 'amdgpu': "{{ amdgpu }}" }
#    - { 'nanominer': "{{ nanominer }}" }
#    - { 'nanominer_cpu': "{{ nanominer_cpu }}" }
#    - { 'nanominer_cpu_pool': "{{ nanominer_cpu_pool }}" }
#    - { 'nanominer_gpu': "{{ nanominer_gpu }}" }
#    - { 'nanominer_gpus': "{{ nanominer_gpus }}" }
#    - { 'nanominer_gpu_pool': "{{ nanominer_gpu_pool }}" }
#    - { 'eth_minhashrate': "{{ eth_minhashrate }}" }
#    - { 'ethminer': "{{ ethminer }}" }
#    - { 'nvidia': "{{ nvidia }}" }
#    - { 'xmr_stak_cpu': "{{ xmr_stak_cpu }}" }


## Server ##

- name: General | Account Management | Provisioning Configuration | Server | Create
  blockinfile:
    path: "{{ srv_file }}"
    block: |
      ;;;;;;; Parameters ;;;;;;
      ;
      ;     enable : Set to true for system to be considered a server.
      ;
      ;     domain : Set to the domain which gives the server its FQDN.
      ;                Example: hyperling.com
      ;
      ; onlyoffice : Set to true for this server to be configured as an OnlyOffice server.
      ;
      ;    grafana : Set to true for this server to be configured as an Grafana web server.
      ;
      ;  influxdb1 : Set to true for this server to be configured as an Influx 1 DB server.
      ;
      ;  influxdb2 : Set to true for this server to be configured as an Influx 2 DB + web server.
      ;
      ;    certbot : Set to true to add cron job for `certbot renew`.
      ;
      ;       hugo : Set to true to install HUGO static website generator.
      ;
      ;     gitlab : ee - Installs Enterprise Edition Free Tier. Basically CE with an easier upgrade path for Paid Features.
      ;              ce - Installs Community Edition. Excludes paid features if you'll never use them. Supposedly difficult to move to EE.
      ;                Example: ce
      ;
      ; gitlab_url : The URL prefix for the server. Using 'http://mygitlabserver' would result in "http://mygitlabserver.((domain))".
      ;              Regardless of whether you choose http or https, the Certbot config will be skipped since the maintainer uses a reverse proxy.
      ;                Example: https://gitlab
      ;
      ;        git : Set to true for this server to be configured as an HTTP git server. 
      ;
      ;   git_name : Name(s) of project(s) that the Git server should host.
      ;                Example: ansible#ansible-dev
      ;
      ; git_branch : Branch(es) of project(s) corresponding with git_name.
      ;                Example: main#dev
      ;
      ;    git_url : URL(s) of project(s) corresponding with git_name.
      ;                Example: https://github.com/Hyperling/ansible#https://github.com/Hyperling/ansible
      ;
      ;    git_sep : Separator for git variables above.
      ;                Example: #
      ;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Server Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | Server | Load
  set_fact:
    server:     "{{ lookup('ini', 'enable     file={{srv_file}} default=false') }}"
    domain:     "{{ lookup('ini', 'domain     file={{srv_file}} default=hyperling.com') }}"
    onlyoffice: "{{ lookup('ini', 'onlyoffice file={{srv_file}} default=false') }}"
    grafana:    "{{ lookup('ini', 'grafana    file={{srv_file}} default=false') }}"
    influxdb1:  "{{ lookup('ini', 'influxdb1  file={{srv_file}} default=false') }}"
    influxdb2:  "{{ lookup('ini', 'influxdb2  file={{srv_file}} default=false') }}"
    certbot:    "{{ lookup('ini', 'certbot    file={{srv_file}} default=false') }}"
    hugo:       "{{ lookup('ini', 'hugo       file={{srv_file}} default=false') }}"
    gitlab:     "{{ lookup('ini', 'gitlab     file={{srv_file}} default=false') }}"
    gitlab_url: "{{ lookup('ini', 'gitlab_url file={{srv_file}} default=false') }}"
    git:        "{{ lookup('ini', 'git        file={{srv_file}} default=false') }}"
    git_name:   "{{ lookup('ini', 'git_name   file={{srv_file}} default=false') }}"
    git_branch: "{{ lookup('ini', 'git_branch file={{srv_file}} default=false') }}"
    git_url:    "{{ lookup('ini', 'git_url    file={{srv_file}} default=false') }}"
    git_sep:    "{{ lookup('ini', 'git_sep    file={{srv_file}} default=false') }}"

- name: General | Account Management | Provisioning Configuration | Server | List
  set_fact: 
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'server': "{{ server }}" }
    - { 'domain': "{{ domain }}" }
    - { 'onlyoffice': "{{ onlyoffice }}" }
    - { 'grafana': "{{ grafana }}" }
    - { 'influxdb1': "{{ influxdb1 }}" }
    - { 'influxdb2': "{{ influxdb2 }}" }
    - { 'certbot': "{{ certbot }}" }
    - { 'hugo': "{{ hugo }}" }
    - { 'gitlab': "{{ gitlab }}" }
    - { 'gitlab_url': "{{ gitlab_url }}" }
    - { 'git': "{{ git }}" }
    - { 'git_name': "{{ git_name }}" }
    - { 'git_branch': "{{ git_branch }}" }
    - { 'git_url': "{{ git_url }}" }
    - { 'git_sep': "{{ git_sep }}" }


## Exit if not turned on yet ##

- name: General | Account Management | Provisioning Configuration | Exit Check
  shell: echo "Please configure the config files in {{ prov_dir }} then try again." && exit 1
  when: provision != true


## Display values ##

- name: General | Account Management | Provisioning Configuration | Print
  debug: 
    var: provision_variables