How to properly use runcmd and scripts-user in cloud-init?
Asked Answered
G

1

6

By looking at runcmd code (/usr/lib/python2.6/site-packages/cloudinit/config/cc_runcmd.py) I noticed there's no 'frequency' specified in comparison to other. Beside the only thing that scripts does is to save the scripts given as shells script under /var/lib/cloud/instance/scripts/runcmd .

So if I verbosely specify the modules, I MUST set the frequency.

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - timezone
 - [ runcmd, always ]

cloud_final_modules:
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - [ scripts-user, always ]
 - ssh-authkey-fingerprints

and the scripts created by this particular can be run by 'scripts-user', so I need to specify i.e.

- [ scripts-user, always ]

to make runcmd scripts working

Is it proper way to do so ? The documentation (at least current 0.7.7) lacks proper explanation of runcmd and user-scripts and how they can be utilized

I also don't understand difference between all the mode, once (first time the instance boot), instance(???), always ( that I understand), ????boot(that one exist? it doesn't seem to work...

EDIT:

ok, i have found in cloudinit/settings.py:

# Valid frequencies of handlers/modules
PER_INSTANCE = "once-per-instance"
PER_ALWAYS = "always"
PER_ONCE = "once"

and beside I found an explanation in this script:

[root@euca-10-254-97-216 ~]# cloud-init-per -h
Usage: cloud-init-per frequency name cmd [ arg1 [ arg2 [ ... ] ]
   run cmd with arguments provided.

   This utility can make it easier to use boothooks or bootcmd
   on a per "once" or "always" basis.

   If frequency is:
      * once: run only once (do not re-run for new instance-id)
      * instance: run only the first boot for a given instance-id
      * always: run every boot

But I don't catch the difference between 'once' and 'instance'. So if the same image (rather snapshot) with new instance id in the case 'once' it won't start ? in case 'instance' it will ?

Gulick answered 12/11, 2015 at 7:26 Comment(0)
K
3

I think the difference matters if you are creating an image that will be used to launch more instances.

Imagine you run the cloud-init scripts to set an instance. Later you make an image of that instance from which you can launch more instances.

When you start a new instance from the image:

  • the scripts with frequency "once" will not run again
  • the scripts with frequency "instance" will run on the first boot, and never again in that instance's lifetime
  • the scripts with frequency "always" will run during every boot
Karol answered 11/11, 2016 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.