Skip to content

Instantly share code, notes, and snippets.

@tbonza2
Last active November 4, 2016 21:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbonza2/20757551443a61254aa6 to your computer and use it in GitHub Desktop.
Save tbonza2/20757551443a61254aa6 to your computer and use it in GitHub Desktop.
Saltstack mode for emacs

Saltstack mode for emacs

Earlier today I was looking for a Saltstack mode for emacs. Couldn't find one, so I posted the question on Stack overflow. Somebody had a good suggestion about using yaml-mode because a Sublime Text implementation appeared to be taking this approach.Another person thought that Saltstack's use of jinja templates would make an emacs mode easy enough to configure. These were both helpful ideas so I wanted to implement parts of each.

Reasoning

After looking around, I found that someone had configured an emacs mode for a similar situation. They needed to combine HTML and Python to use Mako templates for a project not involving Saltstack. Since Edx uses Mako templates instead of jinja in their Django site, Mako seemed like a good alternative to Jinja templates.

Since someone previously combined HTML and Python modes it seemed reasonable to combine Mako and Yaml modes in Emacs.

Setup

Multiple Major (MMM) Mode allows Emacs major modes to be combined. MMM-Mako Mode provides support for Mako files using MMM. Configuring the setup can be a pain so I wanted to document it:

  1. Download & untar MMM mode from http://sourceforge.net/projects/mmm-mode/files

    mv ~/Downloads/mmm-mode-0.5.1.tar.gz ~/InstalledPrograms/
    tar -xvf mmm-mode-0.5.1.tar.gz && cd mmm-mode-0.5.1/
    whereis emacs
  2. Make & install MMM mode but first specify where your Emacs is installed. I'm using Emacs 24 installed in /usr/bin/emacs.

    EMACS="/usr/bin/emacs"
    ./configure
    make
    sudo make install

My elisp files were installed to /usr/local/share/emacs/site-lisp. You may need to add this directory to the Emacs load path. You will also need to tell Emacs to load the module. Here's what I added to my .emacs configuration.

````elisp
(add-to-list
 ;; Adding mmm-mode
 'load-path "/usr/local/share/emacs/site-lisp")
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
````

Make sure it's installed correctly by opening emacs M-x mmm-mode to enable MMM-mode.

  1. Next up, installing MMM-Mako. If I can install something with the package manager then I'll do it. Let's first make sure the package will be available by adding melpa to our .emacs

    $ emacs -nw ~/.emacs

Placed the following in my .emacs file

```elisp
(require 'package) ;; You might already have this line
(add-to-list 'package-archives
             '("melpa" . "http://melpa.org/packages/") t)
```

Back to emacs M-x package-install, Install package: mmm-mako. Once mmm-mako is installed then just finish configuring your .emacs

```elisp
(add-to-list
 ;; Saltstack config
 'auto-mode-alist '("\\.sls\\'" . yaml-mode))
(mmm-add-mode-ext-class 'html-mode "\\.sls\\'" 'mako)
```

My full .emacs file can be viewed here. Also, I want to say thank you to Zach for putting together the post about Mako/HTML.

This approach is a work in progress and I'll update as I find bugs/improvements.

@umeboshi2
Copy link

I thought it was kind of rude for them to close the question on you. I came back browsing around today and see you've done this. Cool! I have been using mako for far longer than jinja, but the saltstack developers focus on jinja and leave mako out in the cold. If you plan to use salt-minion on windows, mako is not in the install and you will have to update it through a messy esky update routine. Even more important I came across this serious bug here: saltstack/salt#18775

I still use mako when rendering templates, but I was forced to use jinja to render the sls files. The saltstack developers also put a lot of work into plugging jinja with some very useful features for salt,
so it may not be much of a drawback to look at using jinja for rendering states and using mako for the other templates. Take a look through my github stuff and you'll find a lot of salt files scattered around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment