-
Notifications
You must be signed in to change notification settings - Fork 8
Add Ansible Script to Allow Nodes to Keep Their Own External Data Up-to-date #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| - name: Generate key pair if it does not exist | ||
| community.crypto.openssh_keypair: | ||
| force: no # Don't regenerate existing keys. | ||
| path: ~/.ssh/id_rsa | ||
|
|
||
| - name: Read public key into tmp to copy over. | ||
| fetch: | ||
| src: ~/.ssh/id_rsa.pub | ||
| dest: /tmp/{{ ansible_hostname }}-id_rsa.pub | ||
| flat: yes | ||
|
|
||
| - name: Add public key to ISIS mirror's authorized keys | ||
| ansible.posix.authorized_key: | ||
| user: "{{ ansible_user_id }}" | ||
| key: "{{ lookup('file','/tmp/{{ ansible_hostname }}-id_rsa.pub')}}" | ||
| remote_user: ubuntu | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running the ansbile script, for remote_user, I had to use my fedid with with |
||
| delegate_to: "{{ data_server_hostname }}" | ||
| delegate_facts: true | ||
|
|
||
| - name: Touch the known_hosts file if it's missing | ||
| file: | ||
| path: ~/.ssh/known_hosts | ||
| state: touch | ||
| mode: 0644 | ||
|
|
||
| - name: Check if known_hosts contains existing server fingerprint | ||
| command: ssh-keygen -F {{ data_server_hostname }} | ||
| register: key_exists | ||
| failed_when: key_exists.stderr != '' | ||
| changed_when: False | ||
|
|
||
| - name: Scan for existing remote ssh fingerprint | ||
| command: ssh-keyscan -T5 {{ data_server_hostname }} | ||
| register: keyscan | ||
| failed_when: keyscan.rc != 0 or keyscan.stdout == '' | ||
| changed_when: False | ||
| when: key_exists.rc == 1 | ||
|
|
||
| - name: Copy ssh-key to local known_hosts | ||
| lineinfile: | ||
| name: ~/.ssh/known_hosts | ||
| create: yes | ||
| line: "{{ item }}" | ||
| when: key_exists.rc == 1 | ||
| with_items: "{{ keyscan.stdout_lines|default([]) }}" | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||
| - name: Create a directory to hold the mirror of the external data. | ||||||||||||||||
| ansible.builtin.file: | ||||||||||||||||
| path: /{{ agent_name }}_external_data/MD5/ | ||||||||||||||||
| state: directory | ||||||||||||||||
| mode: '0755' | ||||||||||||||||
|
|
||||||||||||||||
| - name: Check if machine has SSH access to the ISIS data store. | ||||||||||||||||
| ansible.builtin.command: ssh -o BatchMode=True {{ ansible_user_id }}@{{ data_server_hostname }} 'echo success' | ||||||||||||||||
| register: connected | ||||||||||||||||
| ignore_errors: True | ||||||||||||||||
|
|
||||||||||||||||
| - name: Exchange SSH keys with linode so we can access the data. | ||||||||||||||||
| import_tasks: exchange-keys.yml | ||||||||||||||||
| when: connected.stdout != "success" | ||||||||||||||||
|
|
||||||||||||||||
| - name: Mirror the external data from the main server in a volume (this may take a while). | ||||||||||||||||
| ansible.builtin.command: "rsync -azvW --perms -o -g {{ ansible_user_id }}@{{ data_server_hostname }}:/external-data/MD5/ /{{ agent_name }}_external_data/MD5 -v" | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ssh connection was closed due to long running process. It would be better to run this asynchronously as follows.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition, after deleting some of the files from the mount path of the docker volume in the host machine, I cannot see the missing files are made available even after several cron runs. Shown below are all the current cron jobs, |
||||||||||||||||
|
|
||||||||||||||||
| - name: Copy the data update script onto the mirror machine. | ||||||||||||||||
| ansible.builtin.copy: | ||||||||||||||||
| src: ./update-external-data.sh | ||||||||||||||||
| dest: /{{ agent_name }}_external_data/update-external-data.sh | ||||||||||||||||
| mode: '0755' | ||||||||||||||||
|
|
||||||||||||||||
| - name: Create a crontab job that runs periodically to keep the data up to date. | ||||||||||||||||
| ansible.builtin.cron: | ||||||||||||||||
| name: Update external data | ||||||||||||||||
| minute: "*/5" | ||||||||||||||||
| job: /{{ agent_name }}_external_data/update-external-data.sh {{ data_server_hostname }} {{ agent_name }} {{ ansible_user_id }} >> /{{ agent_name }}_external_data/update-log.txt 2>&1 | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #! /bin/bash | ||
|
|
||
| SERVER_IP=${1} | ||
| HOST_NAME=${2} | ||
| USER_NAME=${3} | ||
|
|
||
| RSYNC_PROCESS_IDS=$(pidof rsync) | ||
|
|
||
| printf "%(%H:%M:%S)T " | ||
|
|
||
| if [ -z "${RSYNC_PROCESS_IDS}" ]; then | ||
| echo "running rsync..." | ||
| rsync -azvW --perms -o -g $USER_NAME@$SERVER_IP:/external-data/MD5/ /${HOST_NAME}_external_data/MD5/ | ||
| else | ||
| echo "rsync is already running. Skipping this time..." | ||
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including the
-Wflag seemed to reduce some of the flakiness that was happening when trying to rsync data though the load balancer.