Skip to content

SmartOS state module for managing vms and images#28765

Merged
cachedout merged 9 commits intosaltstack:developfrom
sjorge:state-smartos
Nov 11, 2015
Merged

SmartOS state module for managing vms and images#28765
cachedout merged 9 commits intosaltstack:developfrom
sjorge:state-smartos

Conversation

@sjorge
Copy link
Copy Markdown
Contributor

@sjorge sjorge commented Nov 11, 2015

Although this PR is separate from #28734, all testing was done with the changes from #28734 in place.

This PR implements the ideas discussed in #27658.

Included in the state module is:

  • /usrbkey/config management using:
    • smartos.config_present :: add/update configuration option
    • smartos.config_absent :: remove configuration option
  • image management using:
    • smartos.image_present :: install image if needed
    • smartos.image_absent :: delete image if needed and not in use
    • smartos.image_vacuum :: delete image if not in use or no smartos.image_present entry
  • virtual machine management:
    • smartos.vm_running :: make sure a vm is in the running state
    • smartos.vm_stopped :: make sure vm is not running
    • smartos.vm_present :: create vm if needed or update vm if config is different from state
    • smartos.vm_absent :: delete vm

For more information the docs for each of these states.

There is a esky packages available for testing here: http://sjorge.sinners.be/illumos/joyent/salt-2015.8.0-661-ga3abab1-esky-smartos.tar.gz

@sjorge
Copy link
Copy Markdown
Contributor Author

sjorge commented Nov 11, 2015

test data

smartos.config_present

incomplete state (should fail)

state_test:
  smartos.config_present
local:
----------
          ID: state_test
    Function: smartos.config_present
      Result: False
     Comment: Missing parameter value for state smartos.config_present
     Started:
    Duration:
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms

setting state_test config option to ''hello world''

state_test:
  smartos.config_present:
    - value: hello world

Initial run

local:
----------
          ID: state_test
    Function: smartos.config_present
      Result: True
     Comment: added property state_test with value "hello world"
     Started: 09:55:24.042407
    Duration: 1.828 ms
     Changes:
              ----------
              state_test:
                  hello world

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   1.828 ms

Sequential run

local:
----------
          ID: state_test
    Function: smartos.config_present
      Result: True
     Comment: property state_test already has value "hello world"
     Started: 09:55:53.263671
    Duration: 1.268 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:   1.268 ms

setting state_test config option to ''hello earth''

Sequential run (after changing value to ''hello earth'')

local:
----------
          ID: state_test
    Function: smartos.config_present
      Result: True
     Comment: updated property state_test with value "hello earth"
     Started: 09:57:08.431585
    Duration: 1.84 ms
     Changes:
              ----------
              state_test:
                  hello earth

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   1.840 ms

Sequential run

local:
----------
          ID: state_test
    Function: smartos.config_present
      Result: True
     Comment: property state_test already has value "hello earth"
     Started: 09:56:31.263671
    Duration: 1.242 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:   1.242 ms

Content of /usbkey/config

#
# This file was generated by salt
#
...
state_test=hello earth
...

smartos.config_absent

removing state_test config option

Initial run

state_test:
  smartos.config_absent
local:
----------
          ID: state_test
    Function: smartos.config_absent
      Result: True
     Comment: property state_test deleted
     Started: 10:03:37.199488
    Duration: 2.078 ms
     Changes:
              ----------
              state_test:
                  None

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   2.078 m

Sequential run

local:
----------
          ID: state_test
    Function: smartos.config_absent
      Result: True
     Comment: property state_test is absent
     Started: 10:04:05.442374
    Duration: 1.085 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:   1.085 ms

Content of /usbkey/config

#
# This file was generated by salt
#
...

smartos.image_present

install non-exising image (should fail)

xxxe6fa6-6e9b-11e5-8402-1b490459exxx:
  smartos.image_present
local:
----------
          ID: xxxe6fa6-6e9b-11e5-8402-1b490459exxx
    Function: smartos.image_present
      Result: False
     Comment: image xxxe6fa6-6e9b-11e5-8402-1b490459exxx does not exists
     Started: 10:24:14.517541
    Duration: 14616.011 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:  14.616 s

install image base-64 15.1.3

842e6fa6-6e9b-11e5-8402-1b490459e334:
  smartos.image_present
local:
----------
          ID: 842e6fa6-6e9b-11e5-8402-1b490459e334
    Function: smartos.image_present
      Result: True
     Comment: image 842e6fa6-6e9b-11e5-8402-1b490459e334 installed
     Started: 10:25:15.014353
    Duration: 42248.638 ms
     Changes:
              ----------
              842e6fa6-6e9b-11e5-8402-1b490459e334:
                  base-64@15.3.0 [2015-10-09T15:36:32Z]

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  42.249 s

smartos.image_absent

delete non-exising image

xxxe6fa6-6e9b-11e5-8402-1b490459exxx:
  smartos.image_absent
local:
----------
          ID: xxxe6fa6-6e9b-11e5-8402-1b490459exxx
    Function: smartos.image_absent
      Result: True
     Comment: image xxxe6fa6-6e9b-11e5-8402-1b490459exxx is absent
     Started: 10:29:44.078355
    Duration: 693.984 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 693.984 ms

delete image base-64 15.3.0

842e6fa6-6e9b-11e5-8402-1b490459e334:
  smartos.image_absent
local:
----------
          ID: 842e6fa6-6e9b-11e5-8402-1b490459e334
    Function: smartos.image_absent
      Result: True
     Comment: image 842e6fa6-6e9b-11e5-8402-1b490459e334 deleted
     Started: 10:30:13.600480
    Duration: 3234.592 ms
     Changes:
              ----------
              842e6fa6-6e9b-11e5-8402-1b490459e334:
                  None

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   3.235 s

smartos.image_vacuum

base-64 15.1.3 does not get vacuumed because in use by vm or installed via image_present

842e6fa6-6e9b-11e5-8402-1b490459e334:
  smartos.image_present

vacuum_images:
  smartos.image_vacuum
local:
----------
          ID: 842e6fa6-6e9b-11e5-8402-1b490459e334
    Function: smartos.image_present
      Result: True
     Comment: image 842e6fa6-6e9b-11e5-8402-1b490459e334 is present
     Started: 10:33:10.595022
    Duration: 679.373 ms
     Changes:
----------
          ID: vacuum_images
    Function: smartos.image_vacuum
      Result: True
     Comment: no images deleted
     Started: 10:33:11.275308
    Duration: 1205.413 ms
     Changes:

Summary for local
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:   1.885 s

base-64 15.1.3 gets vacuumed because not in use or installed via image_present

vacuum_images:
  smartos.image_vacuum
local:
----------
          ID: vacuum_images
    Function: smartos.image_vacuum
      Result: True
     Comment: images deleted
     Started: 10:34:07.889027
    Duration: 2839.453 ms
     Changes:
              ----------
              842e6fa6-6e9b-11e5-8402-1b490459e334:
                  None

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   2.839 s

smartos.vm_running

no existing vm

nina.acheron.be:
  smartos.vm_running
local:
----------
          ID: nina.acheron.be
    Function: smartos.vm_running
      Result: False
     Comment: failed to start nina.acheron.be
     Started: 10:46:12.631570
    Duration: 1045.48 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   1.045 s

stopped vm

test.acheron.be:
  smartos.vm_running
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_running
      Result: True
     Comment: vm test.acheron.be started
     Started: 10:47:18.634534
    Duration: 4046.712 ms
     Changes:
              ----------
              test.acheron.be:
                  running

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   4.047 s

running vm

test.acheron.be:
  smartos.vm_running
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_running
      Result: True
     Comment: vm test.acheron.be already running
     Started: 10:47:46.265922
    Duration: 489.107 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 489.107 ms

smartos.vm_stopped

no existing vm

nina.acheron.be:
  smartos.vm_stopped
local:
----------
          ID: nina.acheron.be
    Function: smartos.vm_stopped
      Result: False
     Comment: failed to stop nina.acheron.be
     Started: 10:49:35.211013
    Duration: 1017.519 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   1.018 s

stopped vm

test.acheron.be:
  smartos.vm_stopped
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_stopped
      Result: True
     Comment: vm test.acheron.be already stopped
     Started: 10:49:14.527360
    Duration: 502.72 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 502.720 ms

running vm

test.acheron.be:
  smartos.vm_stopped
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_stopped
      Result: True
     Comment: vm test.acheron.be stopped
     Started: 10:48:16.675644
    Duration: 9857.206 ms
     Changes:
              ----------
              test.acheron.be:
                  stopped

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   9.857 s

smartos.vm_absent

deleteing existing vm

test.acheron.be:
  smartos.vm_absent
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_absent
      Result: True
     Comment: vm test.acheron.be deleted
     Started: 10:53:54.674219
    Duration: 2760.56 ms
     Changes:
              ----------
              test.acheron.be:
                  None

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   2.761 s

deleting non-existing vm

test.acheron.be:
  smartos.vm_absent
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_absent
      Result: True
     Comment: vm test.acheron.be is absent
     Started: 10:54:07.486133
    Duration: 491.909 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 491.909 ms

smartos.vm_present

create vm with incomplete config (failure expected)

test.acheron.be:
  smartos.vm_present:
    - vmconfig:
      - image_uuid: e69a0918-055d-11e5-8912-e3ceb6df4cf8
      - alias: test
      - quota: 1
      - max_physical_memory: 512
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: False
     Comment: {u'bad_brand': u'undefined'}
     Started: 10:55:27.264857
    Duration: 1613.725 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   1.614 s

Comment is taking from vmadm.create failure reason

create vm with image that is not installed (failure expected)

test.acheron.be:
  smartos.vm_present:
    - vmconfig:
      - image_uuid: 842e6fa6-6e9b-11e5-8402-1b490459e334
      - brand: joyent
      - alias: test
      - quota: 1
      - max_physical_memory: 512
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: False
     Comment: image 842e6fa6-6e9b-11e5-8402-1b490459e334 not installed
     Started: 10:57:52.092539
    Duration: 1172.281 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   1.172 s

We can make vm_present install the image if it exists by adding 'auto_import': true to the config section.
Or smartos.image_present can be used to install the image first.

create vm

test.acheron.be:
  smartos.vm_present:
    - config:
      - auto_import: true
    - vmconfig:
      - image_uuid: e69a0918-055d-11e5-8912-e3ceb6df4cf8
      - brand: joyent
      - alias: test
      - quota: 1
      - max_physical_memory: 512
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: True
     Comment: vm test.acheron.be created
     Started: 11:00:15.114554
    Duration: 86048.114 ms
     Changes:
              ----------
              test.acheron.be:
                  ----------
                  alias:
                      test
                  brand:
                      joyent
                  hostname:
                      test.acheron.be
                  image_uuid:
                      842e6fa6-6e9b-11e5-8402-1b490459e334
                  max_physical_memory:
                      512
                  quota:
                      1

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  86.048 s

update vm (add 2 nics)

test.acheron.be:
  smartos.vm_present:
    - config:
      - auto_import: true
    - vmconfig:
      - image_uuid: 842e6fa6-6e9b-11e5-8402-1b490459e334
      - brand: joyent
      - alias: test
      - quota: 1
      - max_physical_memory: 512
      - nics:
        -
          - mac: "82:1b:8e:49:e9:12"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 10
        -
          - mac: "82:1b:8e:49:e9:13"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 30
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: True
     Comment: vm test.acheron.be updated
     Started: 11:03:18.947881
    Duration: 5971.735 ms
     Changes:
              ----------
              test.acheron.be:
                  ----------
                  add_nics:
                      |_
                        ----------
                        ips:
                            - dhcp
                        mac:
                            82:1b:8e:49:e9:12
                        mtu:
                            1500
                        nic_tag:
                            trunk
                        vlan_id:
                            10
                      |_
                        ----------
                        ips:
                            - dhcp
                        mac:
                            82:1b:8e:49:e9:13
                        mtu:
                            1500
                        nic_tag:
                            trunk
                        vlan_id:
                            30

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   5.972 s

update vm (actually, we only need 1 nic but more diskspace)

test.acheron.be:
  smartos.vm_present:
    - config:
      - auto_import: true
    - vmconfig:
      - image_uuid: 842e6fa6-6e9b-11e5-8402-1b490459e334
      - brand: joyent
      - alias: test
      - quota: 5
      - max_physical_memory: 512
      - nics:
        -
          - mac: "82:1b:8e:49:e9:12"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 10
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: True
     Comment: vm test.acheron.be updated
     Started: 11:04:39.366550
    Duration: 4964.617 ms
     Changes:
              ----------
              test.acheron.be:
                  ----------
                  quota:
                      5
                  remove_nics:
                      - 82:1b:8e:49:e9:13

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   4.965 s

reprovision vm (failure)

Oh boy! The jr. sysadmin used the latest and greatest base-64 image! Not the lts one. Let's fix that!

test.acheron.be:
  smartos.vm_present:
    - config:
      - auto_import: true
    - vmconfig:
      - image_uuid: c02a2044-c1bd-11e4-bd8c-dfc1db8b0182
      - brand: joyent
      - alias: test
      - quota: 5
      - max_physical_memory: 512
      - nics:
        -
          - mac: "82:1b:8e:49:e9:12"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 10
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: True
     Comment: vm test.acheron.be is up to date
     Started: 11:05:56.265247
    Duration: 1657.178 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:   1.657 s

:?: what's this? Actually this is correct, reprovision destroys the vm and creates it again!
With great power comes great responsibility... especially when it can apply to 100+ vms

[WARNING ] image_uuid in state does not match current, reprovision not allowed

reprovision vm (with config reprovision: true)

sr. sysadmin frowns... but I am responsible!

test.acheron.be:
  smartos.vm_present:
    - config:
      - auto_import: true
      - reprovision: true
    - vmconfig:
      - image_uuid: c02a2044-c1bd-11e4-bd8c-dfc1db8b0182
      - brand: joyent
      - alias: test
      - quota: 5
      - max_physical_memory: 512
      - nics:
        -
          - mac: "82:1b:8e:49:e9:12"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 10
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: True
     Comment: vm test.acheron.be updated and reprovisioned
     Started: 11:08:43.380696
    Duration: 63545.712 ms
     Changes:
              ----------
              test.acheron.be:
                  ----------
                  image_uuid:
                      c02a2044-c1bd-11e4-bd8c-dfc1db8b0182

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  63.546 s

reprovision vm (with config reprovision: true but not installed image)

But what if... you know... we don't have the image? Because it was vacuumed perhaps?

test.acheron.be:
  smartos.vm_present:
    - config:
      - reprovision: true
    - vmconfig:
      - image_uuid: 842e6fa6-6e9b-11e5-8402-1b490459e334
      - brand: joyent
      - alias: test
      - quota: 5
      - max_physical_memory: 512
      - nics:
        -
          - mac: "82:1b:8e:49:e9:12"
          - nic_tag: trunk
          - mtu: 1500
          - ips: [ "dhcp" ]
          - vlan_id: 10
local:
----------
          ID: test.acheron.be
    Function: smartos.vm_present
      Result: False
     Comment: vm test.acheron.be updated, reprovision failed because images 842e6fa6-6e9b-11e5-8402-1b490459e334 not installed
     Started: 11:12:12.248540
    Duration: 4249.024 ms
     Changes:

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   4.249 s

We can ofcourse fix this by installed the image first or adding 'auto_import': true to the config section.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

salt.utils.fopen instead, please.

@cachedout
Copy link
Copy Markdown
Contributor

Hi @sjorge. This looks great. There are a few minor concerns that I have that I left as comments in-line. Could you please have a look and let me know what you think?

@cachedout cachedout added pending-changes The pull request needs additional changes before it can be merged pending-discussion The issue or pull request needs more discussion before it can be closed or merged labels Nov 11, 2015
@sjorge
Copy link
Copy Markdown
Contributor Author

sjorge commented Nov 11, 2015

Thanks @cachedout, all comments make sense. Fixing them now.

I wasn't aware of salt.utils.fopen, is there a doc somewhere describing some of the commonly use utils? I also found the dict compare one by actually grepping the code for examples (my custom code was not python 2.6 compatible).

And it indeed looks like those parentheses were not necessary.

local:
----------
          ID: 7bcfc9c8-6e9a-11e5-8d57-73e262d7338e
    Function: smartos.image_absent
      Result: True
     Comment: image 7bcfc9c8-6e9a-11e5-8d57-73e262d7338e deleted
     Started: 16:06:59.321483
    Duration: 2491.202 ms
     Changes:
              ----------
              7bcfc9c8-6e9a-11e5-8d57-73e262d7338e:
                  None

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   2.491 s

Follow up commits incoming.

@cachedout
Copy link
Copy Markdown
Contributor

@sjorge I've meant to add the utils docs to the Contributor Guidelines but I haven't had a chance. Thanks for the reminder. I'll try to get that in today. Great idea!

@sjorge
Copy link
Copy Markdown
Contributor Author

sjorge commented Nov 11, 2015

That would be awesome :)

One more commit incoming, adding a bit more context where available for the log messages

@cachedout
Copy link
Copy Markdown
Contributor

Sounds great. Just let me know when this is ready to go. Thanks!

@sjorge
Copy link
Copy Markdown
Contributor Author

sjorge commented Nov 11, 2015

All done,

[WARNING ] smartos.vm_present::test.acheron.be::reprovision - image_uuid in state does not match current, reprovision not allowed

@cachedout
Copy link
Copy Markdown
Contributor

Great! After the tests finish running, I will get this in. Much appreciated, @sjorge. This will be a valuable addition to Salt.

cachedout pushed a commit that referenced this pull request Nov 11, 2015
SmartOS state module for managing vms and images
@cachedout cachedout merged commit 1de7d62 into saltstack:develop Nov 11, 2015
@sjorge sjorge deleted the state-smartos branch November 11, 2015 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-changes The pull request needs additional changes before it can be merged pending-discussion The issue or pull request needs more discussion before it can be closed or merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants