diff --git a/roles/candles_api/defaults/main.yaml b/roles/candles_api/defaults/main.yaml new file mode 100644 index 0000000..5aa6683 --- /dev/null +++ b/roles/candles_api/defaults/main.yaml @@ -0,0 +1,8 @@ +--- +candles_api_binary_repository: nebula-dex/candles-api +candles_api_binary_version: v0.0.1 +candles_api_twelvedata_api_key: "" +candles_api_polygonio_api_key: "" +candles_api_with_caddy_config: false +candles_api_caddy_config_destination: /etc/caddy/sites/candles-api.caddy +candles_api_domains: [] diff --git a/roles/candles_api/handlers/main.yaml b/roles/candles_api/handlers/main.yaml new file mode 100644 index 0000000..ad41b6e --- /dev/null +++ b/roles/candles_api/handlers/main.yaml @@ -0,0 +1,9 @@ +--- +- name: Restart candles-api + ansible.builtin.service: + state: "restarted" + daemon_reload: true + enabled: true + name: candles-api + listen: "Restart candles-api" + when: not ansible_check_mode diff --git a/roles/candles_api/meta/main.yaml b/roles/candles_api/meta/main.yaml new file mode 100644 index 0000000..b55b873 --- /dev/null +++ b/roles/candles_api/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - vega_caddy_server diff --git a/roles/candles_api/tasks/main.yaml b/roles/candles_api/tasks/main.yaml new file mode 100644 index 0000000..3b3b595 --- /dev/null +++ b/roles/candles_api/tasks/main.yaml @@ -0,0 +1,56 @@ +--- +- name: Ensure group "candles-api" exists + ansible.builtin.group: + name: candles-api + state: present + +- name: Ensure a 'candles-api' user exists + ansible.builtin.user: + name: candles-api + create_home: true + password: '' + shell: /bin/bash + groups: sudo,candles-api + append: true + +- name: Download candles-api binary + ansible.builtin.get_url: + force: true + url: "https://github.com/{{- candles_api_binary_repository -}}/releases/download/{{- candles_api_binary_version -}}/candles-api-linux-amd64.zip" + dest: &bin_dest /tmp/candles_api.zip + mode: '0600' + +- name: Unpack candles-api + ansible.builtin.unarchive: + src: *bin_dest + remote_src: true + dest: /tmp + when: not ansible_check_mode + +- name: Copy candles-api binary + ansible.builtin.copy: + remote_src: true + src: "/tmp/candles-api-linux-amd64" + dest: /usr/local/bin/candles-api + owner: root + group: root + mode: '0755' + when: not ansible_check_mode + +- name: Install candles-api systemd files + ansible.builtin.template: + src: "lib/systemd/system/candles-api.service.j2" + dest: "/lib/systemd/system/candles-api.service" + owner: "root" + group: "root" + mode: "0644" + notify: "Restart candles-api" + +- name: Install Caddyfile + ansible.builtin.template: + src: "etc/caddy/sites/candles-api.caddy.j2" + dest: "{{- candles_api_caddy_config_destination -}}" + owner: "caddy" + group: "caddy" + mode: "0644" + notify: "Restart caddy" diff --git a/roles/candles_api/templates/etc/caddy/sites/candles-api.caddy.j2 b/roles/candles_api/templates/etc/caddy/sites/candles-api.caddy.j2 new file mode 100644 index 0000000..761a8e0 --- /dev/null +++ b/roles/candles_api/templates/etc/caddy/sites/candles-api.caddy.j2 @@ -0,0 +1,4 @@ +{{- candles_api_domains | map('ansible.builtin.regex_replace', '$', ':443') | join(', ') }} { + reverse_proxy http://localhost:8889 +} + diff --git a/roles/candles_api/templates/lib/systemd/system/candles-api.service.j2 b/roles/candles_api/templates/lib/systemd/system/candles-api.service.j2 new file mode 100644 index 0000000..fedf787 --- /dev/null +++ b/roles/candles_api/templates/lib/systemd/system/candles-api.service.j2 @@ -0,0 +1,29 @@ +[Unit] +Description=vegavisor +Documentation=https://github.com/nebula-dex/candles-api +After=network.target network-online.target +Requires=network-online.target +# Configure unit start rate limiting. Units which are started more than burst times within an interval time span are not permitted to start any more. +# Use StartLimitIntervalSec= to configure the checking interval and StartLimitBurst= to configure how many starts per interval are allowed. +StartLimitIntervalSec=30s +StartLimitBurst=3 + +[Service] +Environment="TWELVE_DATA_API_KEY={{- candles_api_twelvedata_api_key -}}" +Environment="POLYGON_API_KEY={{- candles_api_polygonio_api_key -}}" +User=candles-api +Group=candles-api +ExecStart=/usr/local/bin/candles-api +TimeoutStopSec=10s +LimitNOFILE=1048576 +LimitNPROC=512 +PrivateTmp=false +ProtectSystem=full +AmbientCapabilities=CAP_NET_BIND_SERVICE +# Configures the time to sleep before restarting a service +RestartSec=30s +# Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached +Restart=always + +[Install] +WantedBy=multi-user.target