Skip to content

One Context Refresh

Context Variables are only updated in a VM when contextualization is run, but there's no way to trigger this from inside the VM.

one-context-refresh uses onegate to check for updated context variables and updates the files in /run/one-context.

It can also be supplied with a configuration file to define additional files to update, and a command to be run when a variable is updated.

Usage

Run with no config if you only need to update the files in /run/one-context:

python3 one-context-refresh.py

Specify a config file with -c :

python3 one-context-refresh.py -c example_config.yaml

Create a cron job to refresh automatically:

echo "* * * * * python3 one-context-refresh.py -c example_config.yaml " > /etc/cron.d/one-context-refresh

Configuration

This example will :

  • Iterate over the GRAFANA_ADMIN_USERNAME and GRAFANA_ADMIN_PASSWORD variables, in the HyperCloud VM's Custom Attributes.

  • Find lines in the specified file_name that match both start_regex and end_regex.

  • Compare the text between start_regex and end_regex with the variable's value from onegate

  • If the variable has been changed, a string replacement will be performed in the file, and the command is added to a list to be executed later.

  • When all variables have been check and updated, the command list will be executed. If multiple variables have the same command, it will only be executed once.

example_config.yml

GRAFANA_ADMIN_USERNAME:
replacements:
    - start_regex: 'admin_user = '
    end_regex: ' #MANAGED-BY-HC\n'
    file_name: '/etc/grafana/grafana.ini'
command: 'systemctl restart grafana-server'

GRAFANA_ADMIN_PASSWORD:
replacements:
    - start_regex: 'admin_user = '
    end_regex: ' #MANAGED-BY-HC\n'
    file_name: '/etc/grafana/grafana.ini'
command: 'systemctl restart grafana-server'

Integrating one-context-refresh into HyperCloud App Image

If you're building an image with libguestfs as shown in this guide, here is how to include one-context-refresh in your image:

  • Clone the repo to your build directory.
git clone ssh://git@git.softiron.com:30022/pe/one-context-refresh.git
  • Write your config to local-config/one-context-refresh.yaml.

  • Add to build.sh :

--upload one-context-refresh/one-context-refresh.py:/usr/local/bin/one-context-refresh.py \
--upload local-config/one-context-refresh.yaml:/etc/one-context-refresh/one-context-refresh.yaml \
--run-command 'echo "* * * * * python3 /usr/local/bin/one-context-refresh.py -c /etc/one-context-refresh/one-context-refresh.yaml " > /etc/cron.d/one-context-refresh'

This will include the script and your configuration in the image, and create a cron job to run the script every minute.

Next, You'll need to Create a VM Template with this image and prompts the user to input these variables.