netbox-community / netbox-chart

A Helm chart for NetBox
https://netbox.readthedocs.io/
Apache License 2.0
247 stars 150 forks source link

Support Netbox custom scripts #10

Open eversC opened 4 years ago

eversC commented 4 years ago

I'd like to be able to use custom scripts in Netbox.

I believe this will need a volume to be mounted at /opt/netbox/netbox/scripts in the deployment, and a means by which Helm users can store their scripts in a configmap (which I'm not so clear on).

bootc commented 4 years ago

I've been pondering this myself as well. I'm not so sure about the ConfigMap for this, it feels kind of wrong for scripts, but I don't have an immediate better answer for you either.

The old gitRepo volumes would have been more interesting for this, and there is now https://github.com/kubernetes/git-sync, but that's all a bit implementation specific. You can achieve all that today with the extraVolumeMounts / extraVolumes / extraContainers / extraInitContainers support, but it's not particularly pretty. Maybe simply adding some documentation for this would be enough?

eversC commented 4 years ago

Ah I wasn't aware you could do that (re: extraVolumeMounts / extraVolumes / extraContainers / extraInitContainers). That'll work for me, having it documented would be great.

benjy44 commented 2 years ago

something good to know is that the scripts must be also on the worker pods, a working example for me:

worker:
  extraContainers:
    - name: git-custom-scripts
      image: k8s.gcr.io/git-sync/git-sync:v3.6.0
      env:
        - name: GIT_SYNC_REPO
          value: <some_repo>
        - name: GIT_SYNC_BRANCH
          value: main
        - name: GIT_SYNC_ROOT
          value: /data
        - name: GIT_SYNC_PERIOD
          value: 10m
      volumeMounts:
        - name: custom-scripts
          mountPath: /data
  extraVolumeMounts:
    - name: custom-scripts
      mountPath: /opt/netbox/netbox/scripts
  extraVolumes:
    - name: custom-scripts
      emptyDir: {}

extraContainers:
  - name: git-custom-scripts
    image: k8s.gcr.io/git-sync/git-sync:v3.6.0
    env:
      - name: GIT_SYNC_REPO
        value: <some_repo>
      - name: GIT_SYNC_BRANCH
        value: main
      - name: GIT_SYNC_ROOT
        value: /data
      - name: GIT_SYNC_PERIOD
        value: 10m
    volumeMounts:
      - name: custom-scripts
        mountPath: /data

extraVolumeMounts:
  - name: custom-scripts
    mountPath: /opt/netbox/netbox/scripts

extraVolumes:
  - name: custom-scripts
    emptyDir: {}

extraConfig:
  - values:
      SCRIPTS_ROOT: /opt/netbox/netbox/scripts/netbox-scripts.git/scripts

it took me a while to figure it out, as my scripts were failing with this exception when they were missing on the worker node:

Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.9/site-packages/rq/worker.py", line 1061, in perform_job
    rv = job.perform()
  File "/opt/netbox/venv/lib/python3.9/site-packages/rq/job.py", line 821, in perform
    self._result = self._execute()
  File "/opt/netbox/venv/lib/python3.9/site-packages/rq/job.py", line 844, in _execute
    result = self.func(*self.args, **self.kwargs)
  File "/opt/netbox/netbox/extras/scripts.py", line 438, in run_script
    script = get_script(module, script_name)()
TypeError: 'NoneType' object is not callable
LeoColomb commented 3 months ago

From #148 by @florianschendel

Support storage config for scripts as for reports

How it should look in the values file

## Storage configuration for Scripts
scriptsPersistence:
  enabled: true
  ##
  ## Existing claim to use
  existingClaim: ""
  ## Existing claim's subPath to use, e.g. "media" (optional)
  subPath: ""
  ##
.....

My plan is to use scripts in combination with your helm chart. I know i could also create an extra container but it make no sense for me because the Reports/Scripts are standard features of Netbox.

There is already an open issue #10. My opinion is that the extra container solution is for special use cases.